propane/spec/propane_spec.rb
Josh Holtrop 30f4cfcc99 Write parser log file
Fix bug of skipping rule set IDs.
Remove unneeded out_sets from ItemSet class.
2022-06-26 11:06:55 -04:00

89 lines
1.5 KiB
Ruby

require "fileutils"
describe Propane do
def write_grammar(grammar)
File.write("spec/run/testparser.propane", grammar)
end
def build_parser
result = system(*%w[./propane.sh spec/run/testparser.propane spec/run/testparser.d --log spec/run/testparser.log])
expect(result).to be_truthy
end
def compile(test_file)
result = system(*%w[gdc -funittest -o spec/run/testparser spec/run/testparser.d], test_file)
expect(result).to be_truthy
end
def run
result = system("spec/run/testparser")
expect(result).to be_truthy
end
before(:each) do
FileUtils.rm_rf("spec/run")
FileUtils.mkdir_p("spec/run")
end
it "generates a D lexer" do
write_grammar <<EOF
token int \\d+
token plus \\+
token times \\*
drop \\s+
Start -> Foo;
Foo -> int <<
>>
Foo -> plus <<
>>
EOF
build_parser
compile("spec/test_d_lexer.d")
run
end
it "generates a parser" do
write_grammar <<EOF
token plus \\+
token times \\*
token zero 0
token one 1
Start -> E;
E -> E times B;
E -> E plus B;
E -> B;
B -> zero;
B -> one;
EOF
build_parser
end
it "distinguishes between multiple identical rules with lookahead symbol" do
write_grammar <<EOF
token a
token b
Start -> R1 a;
Start -> R2 b;
R1 -> a b;
R2 -> a b;
EOF
build_parser
compile("spec/test_d_lexer3.d")
run
end
it "handles reducing a rule that could be arrived at from multiple states" do
write_grammar <<EOF
token a
token b
drop \\s+
Start -> a R1;
Start -> b R1;
R1 -> b;
EOF
build_parser
compile("spec/test_d_lexer2.d")
run
end
end