mixinafPegger::Rules
afPegger::Rules
@
Js
(Advanced) A collection of utility methods that return PEG rules.
Use when programmatically creating PEG grammar.
- alphaChar
static Rule alphaChar(Bool not := false)
Matches any alphabetical character.
PEG notation:
[a-zA-Z]
- alphaNumChar
static Rule alphaNumChar(Bool not := false)
Matches any alphabetical or numerical character.
PEG notation:
[a-zA-Z0-9]
- anyChar
static Rule anyChar()
Matches any character.
PEG notation:
.
- atLeast
static Rule atLeast(Int n, Rule rule)
Processes the given rule repeatedly until it fails. Returns success if it succeeded at least
n
times.Example PEG notation:
???
- atMost
static Rule atMost(Int n, Rule rule)
Processes the given rule repeatedly, but at most
n
times. Returns success always.Example PEG notation:
???
- between
static Rule between(Range times, Rule rule)
Processes the given rule at most
n
times. Returns success if it succeeded at leastn
times.Example PEG notation:
???
- char
static Rule char(Int char, Bool not := false)
Matches the given character.
Example PEG notation:
[C]
- charIn
static Rule charIn(Range charRange, Bool not := false)
Matches any character in the given range.
Example PEG notation:
[C-N]
- charNot
Matches any character except the given one.
Example PEG notation:
[^C]
- charNotOf
static Rule charNotOf(Int[] chars)
Matches any character not in the given list.
Example PEG notation:
[^ChukNoris]
- charOf
static Rule charOf(Int[] chars, Bool not := false)
Matches any character in the given list.
Example PEG notation:
[ChukNoris]
- charRule
static Rule charRule(Str charClass)
Multi-purpose char class rule.
Example PEG notation:
[a-z_]i
- doAction
static Rule doAction(|Str? action)
Essentially a no-op rule as it always returns
true
- but processes the given action func when successful. Useful for inserting arbitrary actions in sequences:sequence { rule1, doAction { echo("Hello!") }, rule2, }
- eol
static Rule eol()
Matches end of line (or the end of the stream).
Example PEG notation:
\nl / \eos
- eos
static Rule eos()
Matches if the of the stream (EOS) is reached.
Example PEG notation:
\eos
- err
static Rule err(Str msg := "FAIL")
This rule throws an error if processed.
Example PEG notation:
\\fail(FAIL)
- firstOf
static Rule firstOf(Rule[] rules := Rule[,])
Processes the given rules in order until one succeeds. Returns success any succeeded.
firstOf([rule1, rule2, rule3])
Example PEG notation:
rule1 / rule2 / rule3 / ... / rule4
When defining a
firstOf()
rule you may also use it-block syntax:firstOf { rule1, rule2, rule3, }
- hexChar
static Rule hexChar(Bool not := false)
Matches any hexadecimal character.
PEG notation:
[a-fA-F0-9]
- nTimes
static Rule nTimes(Int times, Rule rule)
Processes the given rule
n
times. Returns success if the it always succeeded.Example PEG notation:
rule rule rule ... rule
- newLineChar
static Rule newLineChar(Bool not := false)
Matches the new line character. This assumes all new lines have been normalised to
\n
.PEG notation:
[\n]
- noop
static Rule noop(Str msg := "TODO")
No operation - a placeholder. The rule will always succeed if
pass
istrue
, and always fail ifpass
isfalse
.Example PEG notation:
\\noop(TODO)
- numChar
static Rule numChar(Bool not := false)
Matches any numerical character.
PEG notation:
[0-9]
- oneOrMore
static Rule oneOrMore(Rule rule)
Processes the given rule repeatedly until it fails. Returns success if it succeeded at least once.
Example PEG notation:
(rule)+
- onlyIf
Processes the given rule and return success if it succeeded. The rule is always rolled back so the characters may be subsequently re-read.
Example PEG notation:
&(rule)
- onlyIfNot
static Rule onlyIfNot(Rule rule)
Processes the given rule and return success if it failed. The rule is always rolled back so the characters may be subsequently re-read.
Example PEG notation:
!(rule)
- optional
static Rule optional(Rule rule)
Processes the given rule and returns success whether it passes or not.
Example PEG notation:
(rule)?
- sequence
static Rule sequence(Rule[] rules := Rule[,])
Processes the given rules in order until. Returns success if they all succeeded.
sequence([rule1, rule2, rule3])
Example PEG notation:
rule1 rule2 rule3 ... rule4
When defining a
sequence()
rule you may also use it-block syntax:sequence { rule1, rule2, rule3, }
- spaceChar
static Rule spaceChar(Bool not := false)
Matches any space character (excluding new line chars).
PEG notation:
[ \t]
- str
static Rule str(Str string, Bool ignoreCase := false)
Matches the given string.
Example PEG notation:
"ChuckNorris"
- strNot
static Rule strNot(Str string, Bool ignoreCase := false)
Matches one or more characters up to (but not including) the given string.
Example PEG notation:
(!"ChuckNorris" .)+
- whitespaceChar
static Rule whitespaceChar(Bool not := false)
Matches any whitespace character, including new lines.
PEG notation:
[ \t\n]
- wordChar
static Rule wordChar(Bool not := false)
Matches any word character, used as a Fantom identifier.
PEG notation:
[a-zA-Z0-9_]
- zeroOrMore
static Rule zeroOrMore(Rule rule)
Processes the given rule repeatedly until it fails.
Example PEG notation:
(rule)*