Skip to content

Commit

Permalink
Support :rept expressions in PEG
Browse files Browse the repository at this point in the history
  • Loading branch information
stouset authored and gkellogg committed Oct 24, 2020
1 parent 80a11d1 commit d82029c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/ebnf/peg/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Rule
# * `opt`: returns the value matched, or `nil` if unmatched.
# * `plus`: returns an array of the values matched for the specified production, or `:unmatched`, if none are matched. For Terminals, these are concatenated into a single string.
# * `range`: returns a string composed of the values matched, or `:unmatched`, if less than `min` are matched.
# * `rept`: returns an array of the values matched for the speficied production, or `:unmatched`, if none are matched. For Terminals, these are concatenated into a single string.
# * `seq`: returns an array composed of single-entry hashes for each matched production indexed by the production name, or `:unmatched` if any production fails to match. For Terminals, returns a string created by concatenating these values. Via option in a `production` or definition, the result can be a single hash with values for each matched production; note that this is not always possible due to the possibility of repeated productions within the sequence.
# * `star`: returns an array of the values matched for the specified production. For Terminals, these are concatenated into a single string.
#
Expand Down Expand Up @@ -142,6 +143,14 @@ def parse(input)
parser.update_furthest_failure(input.pos, input.lineno, expr[1])
:unmatched
end
when :rept
# Result is an array of all expressions while they match,
# an empty array of none match
rept = rept(input, expr[1], expr[2], expr[3])

# # Update furthest failure for strings and terminals
parser.update_furthest_failure(input.pos, input.lineno, expr[3]) if terminal?
rept.is_a?(Array) && terminal? ? rept.join("") : rept
when :seq
# Evaluate each expression into an array of hashes where each hash contains a key from the associated production and the value is the parsed value of that production. Returns :unmatched if the input does not match the production. Value ordering is ensured by native Hash ordering.
seq = expr[1..-1].each_with_object([]) do |prod, accumulator|
Expand Down
30 changes: 30 additions & 0 deletions spec/peg/rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@
input: " A A A ",
expect: %w(A A A)
},
"(rept 1 2 A) with ' A A A '" => {
rule: [:rept, 1, 2, "A"],
input: " A A A ",
expect: %w(A A)
},
"(rept 1 4 A) with ' A A A '" => {
rule: [:rept, 1, 4, "A"],
input: " A A A ",
expect: %w(A A A)
},
"(rept 4 10 A) with ' A A A '" => {
rule: [:rept, 4, 10, "A"],
input: " A A A ",
expect: :unmatched
},
"(seq 'A' 'B')" => {
rule: [:seq, "A", "B"],
input: "A B",
Expand Down Expand Up @@ -213,6 +228,21 @@
input: " A A A ",
expect: %w(A A A)
},
"(rept 1 2 A) with ' A A A '" => {
rule: [:rept, 1, 2, "A"],
input: " A A A ",
expect: %w(A A)
},
"(rept 1 4 A) with ' A A A '" => {
rule: [:rept, 1, 4, "A"],
input: " A A A ",
expect: %w(A A A)
},
"(rept 4 10 A) with ' A A A '" => {
rule: [:rept, 4, 10, "A"],
input: " A A A ",
expect: :unmatched
},
"(seq 'A' 'B')" => {
rule: [:seq, "A", "B"],
input: "A B",
Expand Down

0 comments on commit d82029c

Please sign in to comment.