classafPegger::Peg

sys::Obj
  afPegger::Peg

@Js

Parsing Expression Grammar (PEG)

contains

Source

Bool contains()

Returns true if the string contains a PEG match. Convenience for:

fantom: syntax
search != null

See matches() to check for a match against the entire string.

debugOn

Source

static Void debugOn(Bool debug := true)

Turns debug messaging on and off.

each

Source

Void each(|Match fn)

Searches the string and calls the given function for each PEG Match found.

Also see search(), and eachWhile().

eachWhile

Source

Obj? eachWhile(|Match->Obj? fn)

Searches the string and calls the given function for each PEG Match found, until a non-null result is returned. Returns null if there are no matches.

Also see search(), and each().

find

Source

Str? find(Str? label := null, Int? offset := null)

Returns the matched text of the next PEG match, optionally returning the contents of the given label.

peg := Peg("a12 + b23 / c69 * z666", "foo:[a-z] [0-9]+")
peg.find         // --> "a12"
peg.find         // --> "b23"
peg.find("foo")  // --> "c"
peg.find("foo")  // --> "z"

See matched() to match against the entire string.

makePattern

Source

new makePattern(Str str, Str rulePattern)

Parses the given PEG pattern in to a rule. Convenience for:

Peg(str, Peg.parseRule(pattern))

See Rule.parseRule

makeRule

Source

new makeRule(Str str, Rule rule)

Creates a PEG class ready to match the given string and rule.

match

Source

Match? match(Int? offset := null)

Performs a PEG match against the entire string (from the given offset) - use for matching grammars.

See search() to search for a match anywhere in the string.

matched

Source

Str? matched()

Performs a PEG match against the entire string. Returns the matched string.

See find() to search for a match anywhere in the string.

matches

Source

Bool matches()

Performs a PEG match against the entire string. Returns true if it matches.

See contains() to search for a match instead.

parseGrammar

Source

static Grammar parseGrammar(Str grammar)

Parses a list of PEG grammar definitions. For example:

parseGrammar("a <- [abc] / [xyz] / b
              b <- \space+ [^abc]")

See Grammar.parseGrammar

parseRule

Source

static Rule parseRule(Str pattern)

Parses a pattern in to simple rule. For example:

parseRule("[abc] / [xyz]")

See Rule.parseRule

pegGrammar

Source

static Grammar pegGrammar()

Returns the grammar PEG used to parse PEG grammar.

It's not particularly useful, but it may be interesting to some.

replace

Source

Str replace(Str:Str replacements, Int? offset := null)

Searches for the next PEG match and replaces a portion of it.

replacements is a map of label names to replacement values.

Use the label root to replace the entire Match.

Use standard ${interpolation} in values to reference other matched labels. Interpolation may be escaped with a backslash - \${not interpolated}.

str := "(34) -> {12}"
pat := "foo:[0-9]+ ' -> ' bar:[0-9]+"
peg := Peg(str, pat)
val := peg.replace(["foo":"\${bar}", "bar":"\${foo}"])
    // --> (12) -> {34}
replaceAll

Source

Str replaceAll(Str:Str replacements, Int? offset := null)

Replaces all occurrences of a PEG match.

replacements is a map of label names to replacement values.

Use the label root to replace the entire Match.

Use standard ${interpolation} in values to reference other matched labels. Interpolation may be escaped with a backslash - \${this is not interpolated}.

str := "I like abba!"
pat := "foo:'a' / bar:'b'"
peg := Peg(str, pat)
val := peg.replaceAll(["foo":"b", "bar":"a"])
    // --> I like baab!
replaceAllFn

Source

Str replaceAllFn(|Match->Str:Str fn)

Calls the given fn on each match, that returns the replacements to be performed.

replacements is a map of label names to replacement values.

Use the label root to replace the entire Match.

Use standard ${interpolation} in values to reference other matched labels. Interpolation may be escaped with a backslash - \${this is not interpolated}.

Source

Match? search(Int? offset := null)

Searches for the next PEG match (optionally with the given label) and returns the matched string (if any).

Returns null if there are no more matches.

searchAll

Source

Match[] searchAll(Int? offset := null)

Returns all matches from the given offset.