Basic Syntax
rule() - Define Rules
rule(:letter) { match['a-zA-Z'] }
rule(:digit) { match['0-9'] }
Meaning: Define reusable parser rules
match[] - Character Classes
match['a-z']
match['a-zA-Z0-9']
match['\u0300-\u036F']
Meaning: Same as regex [...]
str() - String Matching
str('-')
str('--')
str(' - ')
Meaning: Exact string match
Combinations
>> - Sequence
rule(:word) { letter >> letter }
Meaning: Ordered concatenation (AND)
| - Alternative
rule(:token) do
double_hyphen_word | # try first
hyphenated_word # try later
end
Important: PEG takes first match
.repeat - Repetition
match['a-z'].repeat
match['a-z'].repeat(1)
AST Construction
.as(:symbol) - Naming
rule(:word) {
letter.repeat(1).as(:word)
}
{ word: "hello" }
Meaning: Name for AST identification
root() - Start Rule
rule(:sentence) {
token >> space?
}
root(:sentence)
Meaning: Which rule to start parsing from