Skip to content

CLI Reference

text
pegtool [options] [GRAMMAR_FILE]

Without a file, pegtool reads the grammar from stdin. Without -o, it writes generated source to stdout.

Output and targets

OptionDescription
-o FILEWrite output to the specified file
-t TARGET, -target TARGETSelect go, hx, ts, cs, c, or rust; defaults to go
-xParse and validate the grammar without generating a parser
-receiver-name NAMEChange the default name c of the current-match object in actions
-nolintAdd lint-suppression comments to generated Go output

Target aliases:

Canonical valueAliases
hxhaxe
tstypescript
cscsharp, c#
cc99

Generation performance and runtime form

OptionDescription
-optimize-parserGenerate a fixed production runtime; effects differ by target
-optimize-ref-expr-by-indexReplace name lookups with rule-array indexes, potentially producing large diffs
-cacheCache only the generator's parsing of .peg; does not affect the generated parser
-debugPrint debugging output while the generator parses .peg

Do not confuse -cache with memoization in the generated parser. See Choosing Performance Options for the complete selection criteria.

Entry points and multiple grammars

OptionDescription
-alternate-entrypoints A,BVerify that the listed rules exist; currently does not change the generated API or output
-grammar-onlyGenerate only the grammar object and actions without duplicating the runtime
-grammar-name NAMESet the grammar variable name; defaults to g
-run-func-prefix PREFIXPrefix generated action callbacks

The explicitly supported and tested multi-grammar workflow currently uses one Go package. See Multiple Grammars and Entry Points for a complete example.

Haxe-specific option

OptionDescription
-haxe-use-hxunicodeUse hxUnicode for Unicode classes, reducing embedded source at the cost of a dependency

Using this option with a non-Haxe target is an error. A grammar without Unicode classes does not gain an external dependency from the option.

Help

powershell
pegtool -h
pegtool -help

Common commands

powershell
# Validate a grammar.
pegtool -x grammar.peg

# Production Go parser with stable diffs.
pegtool -t go -optimize-parser -o parser.go grammar.peg

# Haxe parser with optional memoization.
pegtool -t hx -o Parser.hx grammar.peg

# Strict TypeScript parser.
pegtool -t ts -o parser.ts grammar.peg

Released under the BSD 3-Clause License