Clean up rule format in grammar files

This commit is contained in:
Josh Holtrop 2022-06-05 16:28:35 -04:00
parent ba74d0a20a
commit df8088c3c6
2 changed files with 17 additions and 31 deletions

View File

@ -41,7 +41,7 @@ class Propane
elsif sliced = input.slice!(/\Adrop\s+(\S+)\n/) elsif sliced = input.slice!(/\Adrop\s+(\S+)\n/)
pattern = $1 pattern = $1
@drop_tokens << Token.new(pattern: pattern, line_number: line_number) @drop_tokens << Token.new(pattern: pattern, line_number: line_number)
elsif sliced = input.slice!(/\A(\S+)\s*:\s*\[(.*?)\] <<\n(.*?)^>>\n/m) elsif sliced = input.slice!(/\A(\S+)\s*->\s*(.*?)(?:;|<<\n(.*?)^>>\n)/m)
rule_name, components, code = $1, $2, $3 rule_name, components, code = $1, $2, $3
components = components.strip.split(/\s+/) components = components.strip.split(/\s+/)
@rules << Rule.new(rule_name, components, code, line_number) @rules << Rule.new(rule_name, components, code, line_number)

View File

@ -31,11 +31,10 @@ token int \\d+
token plus \\+ token plus \\+
token times \\* token times \\*
drop \\s+ drop \\s+
Start: [Foo] << Start -> Foo;
Foo -> int <<
>> >>
Foo: [int] << Foo -> plus <<
>>
Foo: [plus] <<
>> >>
EOF EOF
build_parser build_parser
@ -49,18 +48,12 @@ token plus \\+
token times \\* token times \\*
token zero 0 token zero 0
token one 1 token one 1
Start: [E] << Start -> E;
>> E -> E times B;
E: [E times B] << E -> E plus B;
>> E -> B;
E: [E plus B] << B -> zero;
>> B -> one;
E: [B] <<
>>
B: [zero] <<
>>
B: [one] <<
>>
EOF EOF
build_parser build_parser
end end
@ -69,14 +62,10 @@ EOF
write_grammar <<EOF write_grammar <<EOF
token a token a
token b token b
Start: [R1 a] << Start -> R1 a;
>> Start -> R2 b;
Start: [R2 b] << R1 -> a b;
>> R2 -> a b;
R1: [a b] <<
>>
R2: [a b] <<
>>
EOF EOF
build_parser build_parser
end end
@ -85,12 +74,9 @@ EOF
write_grammar <<EOF write_grammar <<EOF
token a token a
token b token b
Start: [a R1] << Start -> a R1;
>> Start -> b R1;
Start: [b R1] << R1 -> b;
>>
R1: [b] <<
>>
EOF EOF
build_parser build_parser
end end