Clone wiki

stx:goodies/petitparser / BasicUsage

Using PetitCompiler

For the impatient...

To get an instance of compiled parser, simply send a #compile method to any instance of PetitParser:

| petitParser compiledParser |
petitParser := MyParser new.
"compilation might be costly do it only when you change your parser"
compiledParser := parser compile.
compiledParser parse: myInput.

The #compile method returns an instance of a compiled parser, ready to parse input. Use #parse: to parse input, just like with standard PetitParser parsers.

NOTE: Parser compilation may take some times (up to several seconds for complex grammars). Therefore you may want to cache the compiled parser somewhere for later reuse.

TODO: Suggest some solutuin here.

Compiling a PetitParser

The principal way to compile a parser is to use PPParser>>compileWithOptions:, passing a compilation options. Options could be either an instance of PPCCompilationOptions or an array specification, like #( debug: false ) where debug: is the option name and false is the value.

Example: following with compile SomeParser into class named SomeParserCompiled

SomeParser compileWithOptions: #( parserName: #SomeParserCompiled )

For list of available options and their descriptions, see class PPCCompilationOptions, protocol options

Updated