Return a lexer error on unexpected input - close #3

This commit is contained in:
Josh Holtrop 2023-07-08 10:36:58 -04:00
parent ecef933255
commit 83a4037740
2 changed files with 42 additions and 0 deletions

View File

@ -81,6 +81,29 @@ EOF
expect(results.status).to eq 0
end
it "detects a lexer error when an unknown character is seen" do
write_grammar <<EOF
ptype int;
token int /\\d+/ <<
int v;
foreach (c; match)
{
v *= 10;
v += (c - '0');
}
$$ = v;
>>
Start -> int <<
$$ = $1;
>>
EOF
build_parser
compile("spec/test_lexer_unknown_character.d")
results = run
expect(results.stderr).to eq ""
expect(results.status).to eq 0
end
it "generates a parser" do
write_grammar <<EOF
token plus /\\+/;

View File

@ -0,0 +1,19 @@
import testparser;
import std.stdio;
int main()
{
return 0;
}
unittest
{
string input = `x`;
auto parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.P_UNEXPECTED_INPUT);
input = `123`;
parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.P_SUCCESS);
assert(parser.result == 123u);
}