pegjs/docs/guides/using-the-parser.md
Futago-za Ryuu 2ac387e1c9 Move documentation
The README.md file in the root of the repository had become too large, and as a result the file became hard to maintain.

This commit extracts all the documentation and moves it to separate but managable files within the docs directory, a new folder also located in the root of the repository.
2018-02-16 02:39:13 +00:00

1 KiB

Using the Parser

parser { DefaultTracer?, SyntaxError, parse(input[, options]) }

Using the generated parser is simple; just call its parse method and pass an input string as a parameter. The method will return a parse result (the exact value depends on the grammar used to generate the parser) or throw an exception if the input is invalid. The exception will contain location, expected, found, and message properties with more details about the error.

parser.parse("abba"); // returns ["a", "b", "b", "a"]

parser.parse("abcd"); // throws an exception

You can tweak parser behavior by passing a second parameter with an options object to the parse method. The following options are supported:

  • startRule — name of the rule to start parsing from (depends on the rules the grammar supports as starting rules)
  • tracer — tracer to use (only if the parser was generated with "trace": true option passed to the compiler)

Parsers can also support their own custom options on the options object passed to them.