PEGO

The pego Command

go install github.com/ornew/pego/cmd/pego@latest

Overview

Command pego is a command-line tool for working with PEGO grammars.

Usage:

pego parse -g grammar.pego [-s main] [-i input] [-f json|sexpr] [-stream] [-unit u] [-backend b]
pego fmt [-w] [-l] [grammar.pego ...]
pego convert [-to pego|json] [-o output] grammar.pego|grammar.json|grammar.pegoc
pego gen -g grammar.pego -pkg name [-s main] [-o parser.go] [-types] [-recognize] [-nodoc]
pego gen -lang ts -g grammar.pego [-s main] [-o parser.ts] [-recognize]
pego compile -g grammar.pego [-s main] [-no-ast] -o grammar.pegoc
pego trace -g grammar.pego [-s main] [-i input] [-max-depth n] [-rule name] [-failures] [-f text|json]
pego profile -g grammar.pego [-s main] [-i input] [-sort column] [-n rows] [-f text|json]
pego explain -g grammar.pego [-s main] [-i input] [-n calls]
pego lint -g grammar.pego [-s main] [-f text|json] [-disable checks] [-strict] [-list]
pego sample -g grammar.pego [-s main] [-n 10] [-seed N] [-max-depth D] [-max-repeat R] [-max-len L] [-budget B]
            [-coverage] [-invalid] [-f lines|json]
pego lsp

Usage

pego <command> [flags] [arguments]

A <grammar> is PEGO source (.pego), a grammar in JSON (.json), or a grammar compiled with pego compile (.pegoc).

Run "pego <command> -h" for the flags of a command.

This reference is generated from the output of pego and pego <command> -h.

Commands

pego parse

pego parse -g <grammar> [-s <rule>] [-i <input>] [-f json|sexpr] [-stream] [-check]
     [-unit codepoints|bytes] [-backend closure|bytecode|bytecode-iterative]

Parse the input with a grammar and print the syntax tree. Without -i, the input is read from standard input.

FlagDefaultDescription
-backend stringexecution backend: closure, bytecode, or bytecode-iterative (default: chosen for the grammar)
-checkonly check that the input matches the grammar, without building a tree, and print ok if it does
-f string"json"output format: json or sexpr
-g stringgrammar file (.pego, .json, or .pegoc)
-i stringinput string (default: standard input)
-s stringstart rule name (default: the one saved in a .pegoc, otherwise main)
-streamprint each #stream element of the start rule on its own line as soon as it matches
-unit string"codepoints"unit of positions: codepoints or bytes

pego fmt

pego fmt [-w] [-l] [<file.pego> ...]

Format PEGO source files, keeping comments. Without files, standard input is formatted. -w rewrites the files in place; -l lists the files whose formatting differs.

FlagDefaultDescription
-llist the files whose formatting differs
-wwrite the result to the file instead of standard output

pego convert

pego convert [-to pego|json] [-o <file>] <grammar>

Convert a grammar between PEGO source and JSON. -to defaults to the other format of the input and is required for compiled grammars.

FlagDefaultDescription
-o stringoutput file (default: standard output)
-to stringoutput format, pego or json (default: the other format of the input)

pego gen

pego gen -g <grammar> -pkg <package> [-s <rule>] [-o <file>] [-types] [-recognize]
     [-nodoc]
pego gen -lang ts -g <grammar> [-s <rule>] [-o <file>] [-recognize]

Generate a Go parser (or, with -lang ts, a TypeScript module) from a grammar. Without -o, the code is written to standard output. With -types, Go types for the grammar's types and ParseAST, which returns the result as values of those types, are generated too (Go only); with -recognize, Recognize (recognize in TypeScript), which checks input without building a tree. -nodoc leaves out the package comment of the Go code, for a package documented in another file.

FlagDefaultDescription
-g stringgrammar file (.pego, .json, or .pegoc)
-lang string"go"language of the generated code: go or ts (TypeScript)
-nodocleave out the package comment (Go)
-o stringoutput file (default: standard output)
-pkg stringpackage name of the generated code (Go)
-recognizealso generate Recognize, which checks input without building a tree
-s stringstart rule of the generated Parse function (default: the one saved in a .pegoc, otherwise main)
-typesalso generate Go types for the grammar's types and ParseAST (Go)

pego compile

pego compile -g <grammar> [-s <rule>] [-no-ast] -o <file.pegoc>

Compile a grammar and save it. The result can be used as <grammar> by the other commands. With -no-ast, the grammar AST is omitted; the result then runs only on the bytecode backends and cannot be converted back to a grammar.

FlagDefaultDescription
-g stringgrammar file (.pego, .json, or .pegoc)
-no-astomit the grammar AST (the result runs only on the bytecode backends)
-o stringoutput file (.pegoc)
-s stringstart rule saved as the default (default: the one saved in a .pegoc, otherwise main)

pego trace

pego trace -g <grammar> [-s <rule>] [-i <input>] [-max-depth <n>] [-rule <name>]
     [-failures] [-f text|json] [-unit u] [-backend b]

Parse the input and print every rule call as an indented call tree with positions, results and memo hits (-f json: one event per line).

FlagDefaultDescription
-backend stringexecution backend: closure, bytecode, or bytecode-iterative (default: chosen for the grammar)
-f string"text"output format: text (an indented call tree) or json (one event per line)
-failuresshow what was expected where each failed call failed
-g stringgrammar file (.pego, .json, or .pegoc)
-i stringinput string (default: standard input)
-max-depth intshow calls nested at most this deep, counting from the top call shown (1), which is the start rule or a call of a -rule rule; 0 shows all
-rule valueshow only the calls of this rule and the calls nested in them (repeatable, or comma-separated)
-s stringstart rule name (default: the one saved in a .pegoc, otherwise main)
-unit string"codepoints"unit of positions: codepoints or bytes

pego profile

pego profile -g <grammar> [-s <rule>] [-i <input>] [-sort <column>] [-n <rows>]
     [-f text|json] [-unit u] [-backend b]

Parse the input and print the cost of each rule, with hints on where the grammar does more work than it needs to.

FlagDefaultDescription
-backend stringexecution backend: closure, bytecode, or bytecode-iterative (default: chosen for the grammar)
-f string"text"output format: text or json
-g stringgrammar file (.pego, .json, or .pegoc)
-i stringinput string (default: standard input)
-n int30number of rules to show (0 shows all)
-s stringstart rule name (default: the one saved in a .pegoc, otherwise main)
-sort string"self"column to sort by: self, time, calls, evals, memo, failed, repeats, consumed, wasted, or name
-unit string"codepoints"unit of positions: codepoints or bytes

pego explain

pego explain -g <grammar> [-s <rule>] [-i <input>] [-n <calls>] [-unit u] [-backend b]

Parse the input and, for a syntax error, print the rule calls that recorded what it says was expected, each with the calls it was nested in.

FlagDefaultDescription
-backend stringexecution backend: closure, bytecode, or bytecode-iterative (default: chosen for the grammar)
-g stringgrammar file (.pego, .json, or .pegoc)
-i stringinput string (default: standard input)
-n int10most calls to show per error (0 shows all)
-s stringstart rule name (default: the one saved in a .pegoc, otherwise main)
-unit string"codepoints"unit of positions: codepoints or bytes

pego lint

pego lint -g <grammar> [-s <rule>] [-f text|json] [-disable <checks>] [-strict]

[-list] Report likely mistakes in a grammar: alternatives and expressions that can never match or have no effect, unused captures and rules, and shapes that slow down incremental parsing. Fails if an error is found (with -strict, also a warning). -list lists the checks.

FlagDefaultDescription
-disable valuecomma-separated checks not to run (the flag can be repeated)
-f string"text"output format: text or json
-g stringgrammar file (.pego, .json, or .pegoc with the AST)
-listlist the checks and exit
-s stringstart rule (default: the one saved in a .pegoc, otherwise main)
-strictfail on warnings too, not only on errors

pego sample

pego sample -g <grammar> [-s <rule>] [-n <count>] [-seed <n>] [-max-depth <d>]
     [-max-repeat <r>] [-max-len <bytes>] [-budget <steps>] [-coverage] [-invalid]
     [-f lines|json]

Generate distinct inputs that the grammar accepts, for tests and fuzz corpora, each printed as a quoted string on its own line. -coverage prefers rules and alternatives not exercised yet and reports what was missed; -invalid generates near-miss inputs that the grammar rejects.

FlagDefaultDescription
-budget int20000steps one attempt may take before it gives up
-coverageprefer rules and alternatives not exercised yet, and report the coverage
-f string"lines"output format: lines (one quoted input per line) or json
-g stringgrammar file (.pego, .json, or .pegoc with the AST)
-invalidgenerate near-miss inputs that the grammar rejects instead
-max-depth int5recursion depth beyond which the generator finishes the input the shortest way
-max-len int512soft limit on the length of an input in bytes
-max-repeat int3iterations beyond the minimum that a repetition aims for, at most
-n int10number of distinct inputs to generate
-s stringstart rule (default: the one saved in a .pegoc, otherwise main)
-seed uintseed of the random decisions; the same seed gives the same inputs

pego lsp

pego lsp

Run a Language Server Protocol server for .pego files on standard input and output, for editors: diagnostics, formatting, go to definition, references, hover, rename and completion.

FlagDefaultDescription
-stdiotruecommunicate over standard input and output (the only transport)