Skip to content

Help with parser #25

Open
Open
@rndev-io

Description

@rndev-io

Hi, cool and useful library, thanks!

I need help to write parser for next grammar (it's some subset of CMake grammar)

FIRST(abc efg)

S_E_C_O_N_D()

third(
    abc
    efg
)

FOURTH(
    abc.efg
)

Where FIRST, S_E_C_O_N_D, third, FOURTH some macros, and abc, efg, abc.efg macros arguments (zero or more). I write parser, but I don't understand how to separate macros and arguments tokenization.

class CMake : Grammar<Any?>() {
    val LINE_COMMENT by token("#.*", ignore = true)
    val NL by token("[\r\n]+", ignore = true)
    val WS by token("\\s+", ignore = true)
    val O_PAREN by token("\\(")
    val C_PAREN by token("\\)")
    val IDENT by token("[A-Za-z_][A-Za-z0-9_]*")
    val ARG by token("[^\\s()#]+")
    val macros by IDENT * -O_PAREN * optional(parser(this::arguments)) * -C_PAREN
    val arguments by separated(parser(this::ARG), WS, true)
    override val rootParser by oneOrMore(macros)
}

This parser throw exception Could not parse input: MismatchedToken(expected=C_PAREN (\)), found=IDENT for "abc" at 6 (1:7)), but abc is ARG, not IDENT.

Thanks for help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions