Skip to content

PEX Definitions ​

Parsing expressions can be defined using the following syntax:

(and e1 e2…) ​

A sequence of PEXs that must all be matched. The and form is optional and implicit.

(or e1 e2…) ​

Prioritized choices, meaning that, as in Elisp, the choices are tried in order, and the first successful match is used. Note that this is distinct from context-free grammars, in which selection between multiple matches is indeterminate.

(any) ​

Matches any single character, as the regexp “.”.

string ​

A literal string.

(char c) ​

A single character c, as an Elisp character literal.

(* e) ​

Zero or more instances of expression e, as the regexp ‘*’. Matching is always “greedy”.

(+ e) ​

One or more instances of expression e, as the regexp ‘+’. Matching is always “greedy”.

(opt e) ​

Zero or one instance of expression e, as the regexp ‘?’.

symbol ​

A symbol representing a previously-defined PEG rule.

(range ch1 ch2) ​

The character range between ch1 and ch2, as the regexp ‘[ch1-ch2]’.

[ch1-ch2 "+*" ?x] ​

A character set, which can include ranges, character literals, or strings of characters.

[ascii cntrl] ​

A list of named character classes.

(syntax-class name) ​

A single syntax class.

(funcall e args…) ​

Call PEX e (previously defined with define-peg-rule) with arguments args.

(null) ​

The empty string.

The following expressions are used as anchors or tests – they do not move point, but return a boolean value which can be used to constrain matches as a way of controlling the parsing process (see Writing PEG Rules).

(bob) ​

Beginning of buffer.

(eob) ​

End of buffer.

(bol) ​

Beginning of line.

(eol) ​

End of line.

(bow) ​

Beginning of word.

(eow) ​

End of word.

(bos) ​

Beginning of symbol.

(eos) ​

End of symbol.

(if e) ​

Returns non-nil if parsing PEX e from point succeeds (point is not moved).

(not e) ​

Returns non-nil if parsing PEX e from point fails (point is not moved).

(guard exp) ​

Treats the value of the Lisp expression exp as a boolean.

Character-class matching can refer to the classes named in peg-char-classes, equivalent to character classes in regular expressions (see Character Classes)