Next: , Up: PEG Parsing   [Contents][Index]


6.17.1 PEG Syntax Reference

Normal PEG Syntax:

PEG Pattern: sequence a b

Parses a. If this succeeds, continues to parse b from the end of the text parsed as a. Succeeds if both a and b succeed.

"a b"

(and a b)

PEG Pattern: ordered choice a b

Parses a. If this fails, backtracks and parses b. Succeeds if either a or b succeeds.

"a/b"

(or a b)

PEG Pattern: zero or more a

Parses a as many times in a row as it can, starting each a at the end of the text parsed by the previous a. Always succeeds.

"a*"

(* a)

PEG Pattern: one or more a

Parses a as many times in a row as it can, starting each a at the end of the text parsed by the previous a. Succeeds if at least one a was parsed.

"a+"

(+ a)

PEG Pattern: optional a

Tries to parse a. Succeeds if a succeeds.

"a?"

(? a)

PEG Pattern: followed by a

Makes sure it is possible to parse a, but does not actually parse it. Succeeds if a would succeed.

"&a"

(followed-by a)

PEG Pattern: not followed by a

Makes sure it is impossible to parse a, but does not actually parse it. Succeeds if a would fail.

"!a"

(not-followed-by a)

PEG Pattern: string literal ``abc''

Parses the string "abc". Succeeds if that parsing succeeds.

"'abc'"

"abc"

PEG Pattern: any character

Parses any single character. Succeeds unless there is no more text to be parsed.

"."

peg-any

PEG Pattern: character class a b

Alternative syntax for “Ordered Choice a b” if a and b are characters.

"[ab]"

(or "a" "b")

PEG Pattern: range of characters a z

Parses any character falling between a and z.

"[a-z]"

(range #\a #\z)

Example:

"(a !b / c &d*) 'e'+"

Would be:

(and
 (or
  (and a (not-followed-by b))
  (and c (followed-by (* d))))
 (+ "e"))

Extended Syntax

There is some extra syntax for S-expressions.

PEG Pattern: ignore a

Ignore the text matching a

PEG Pattern: capture a

Capture the text matching a.

PEG Pattern: peg a

Embed the PEG pattern a using string syntax.

Example:

"!a / 'b'"

Is equivalent to

(or (peg "!a") "b")

and

(or (not-followed-by a) "b")

Next: , Up: PEG Parsing   [Contents][Index]