Add forward slashes around patterns and parse more robustly

This commit is contained in:
josh committed 2022-09-28 23:05:01 -04:00
1 parent 1547528ecf
commit 04367db0ac
4 files changed
+86 -44

No files matched your search

+4 -4
View File
@@ -10,7 +10,7 @@ class Foobar;
token while;
token id
[a-zA-Z_][a-zA-Z_0-9]*;
/[a-zA-Z_][a-zA-Z_0-9]*/;
token token_with_code <<
Code for the token
@@ -18,7 +18,7 @@ Code for the token
tokenid token_with_no_pattern;
drop \\s+;
drop /\\s+/;
A -> B <<
a = 42;
@@ -46,13 +46,13 @@ EOF
o = grammar.tokens.find {|token| token.name == "id"}
expect(o).to_not be_nil
expect(o.line_number).to eq 8
expect(o.line_number).to eq 9
expect(o.id).to eq 1
o = grammar.patterns.find {|pattern| pattern.token == o}
expect(o).to_not be_nil
expect(o.pattern).to eq "[a-zA-Z_][a-zA-Z_0-9]*"
expect(o.line_number).to eq 8
expect(o.line_number).to eq 9
expect(o.code_id).to be_nil
expect(o.code).to be_nil
+6 -6
View File
@@ -82,15 +82,15 @@ EOF
expect(run(<<EOF, "foobar")).to eq expected
token foo;
token bar;
token identifier [a-z]+;
token identifier /[a-z]+/;
EOF
expected = [
["plusplus", "++"],
["plus", "+"],
]
expect(run(<<EOF, "+++")).to eq expected
token plus \\+;
token plusplus \\+\\+;
token plus /\\+/;
token plusplus /\\+\\+/;
EOF
end
@@ -103,7 +103,7 @@ EOF
expect(run(<<EOF, "foo \tbar")).to eq expected
token foo;
token bar;
token WS \\s+;
token WS /\\s+/;
EOF
end
@@ -116,7 +116,7 @@ EOF
expect(run(<<EOF, "foo \tbar")).to eq expected
token foo;
token bar;
drop \\s+;
drop /\\s+/;
EOF
end
@@ -125,7 +125,7 @@ EOF
["semicolon", ";"],
]
expect(run(<<EOF, ";")).to eq expected
token semicolon \;;
token semicolon /;/;
EOF
end
end
+10 -10
View File
@@ -27,10 +27,10 @@ describe Propane do
it "generates a D lexer" do
write_grammar <<EOF
token int \\d+;
token plus \\+;
token times \\*;
drop \\s+;
token int /\\d+/;
token plus /\\+/;
token times /\\*/;
drop /\\s+/;
Start -> Foo;
Foo -> int <<
>>
@@ -44,10 +44,10 @@ EOF
it "generates a parser" do
write_grammar <<EOF
token plus \\+;
token times \\*;
token zero 0;
token one 1;
token plus /\\+/;
token times /\\*/;
token zero /0/;
token one /1/;
Start -> E;
E -> E times B;
E -> E plus B;
@@ -60,7 +60,7 @@ EOF
it "generates an SLR parser" do
write_grammar <<EOF
token one 1;
token one /1/;
Start -> E;
E -> one E;
E -> one;
@@ -86,7 +86,7 @@ EOF
write_grammar <<EOF
token a;
token b;
drop \\s+;
drop /\\s+/;
Start -> a R1;
Start -> b R1;
R1 -> b;