Search code examples
javaparsingparser-combinatorsjparsec

JParsec - Scanner.string() equivalent for tokens


I have a parser for language that looks like this:

[nodeType OPTIONAL STUFF]

There are a few node typed, such as "ask" and "set", so currently ask and set are defined as keywords. This causes problems in other parts of the parser.

Is there a way to define a token parser that accepts only tokens who contain specific text?


Solution

  • Yes. You can have a look at the Terminals class and this is also explained in the Tutorial.

    Actually, jparsec combines the two passes of parsing that have been traditionally separated in lex and yacc: The lexical analysis phase (e.g. Terminals) parses a String and returns a stream of Tokens, the syntactic analysis (other Parser instances) parses stream of Tokens and interpret them, usually building an AST or embedding semantic directly.