classafPegger::Peg
sys::Obj afPegger::Peg
@
Js
Parsing Expression Grammar (PEG)
- contains
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
static Void debugOn(Bool debug := true)
Turns debug messaging on and off.
- each
Searches the string and calls the given function for each PEG Match found.
Also see search(), and eachWhile().
- eachWhile
- find
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
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
new makeRule(Str str, Rule rule)
Creates a PEG class ready to match the given string and rule.
- match
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
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
Bool matches()
Performs a PEG match against the entire string. Returns
true
if it matches.See contains() to search for a match instead.
- parseGrammar
static Grammar parseGrammar(Str grammar)
Parses a list of PEG grammar definitions. For example:
parseGrammar("a <- [abc] / [xyz] / b b <- \space+ [^abc]")
- parseRule
- pegGrammar
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
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
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
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}
.- search
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
Match[] searchAll(Int? offset := null)
Returns all matches from the given offset.