Switch to new API - close #8

The new API is more C-like and will allow consistency across all future
supported language targets.
This commit is contained in:
Josh Holtrop 2023-07-12 15:46:13 -04:00
parent e0e5e87338
commit 7a1b4064c1
15 changed files with 611 additions and 481 deletions

File diff suppressed because it is too large Load Diff

View File

@ -203,7 +203,7 @@ class Propane
unless mode_id
raise Error.new("Lexer mode '#{mode_name}' not found")
end
"m_mode = #{mode_id}u"
"context.mode = #{mode_id}u"
end
end
code

View File

@ -12,31 +12,31 @@ unittest
CodePoint code_point;
ubyte code_point_length;
result = Decoder.decode_code_point("5", &code_point, &code_point_length);
result = p_decode_code_point("5", &code_point, &code_point_length);
assert(result == P_SUCCESS);
assert(code_point == '5');
assert(code_point_length == 1u);
result = Decoder.decode_code_point("", &code_point, &code_point_length);
result = p_decode_code_point("", &code_point, &code_point_length);
assert(result == P_EOF);
result = Decoder.decode_code_point("\xC2\xA9", &code_point, &code_point_length);
result = p_decode_code_point("\xC2\xA9", &code_point, &code_point_length);
assert(result == P_SUCCESS);
assert(code_point == 0xA9u);
assert(code_point_length == 2u);
result = Decoder.decode_code_point("\xf0\x9f\xa7\xa1", &code_point, &code_point_length);
result = p_decode_code_point("\xf0\x9f\xa7\xa1", &code_point, &code_point_length);
assert(result == P_SUCCESS);
assert(code_point == 0x1F9E1u);
assert(code_point_length == 4u);
result = Decoder.decode_code_point("\xf0\x9f\x27", &code_point, &code_point_length);
result = p_decode_code_point("\xf0\x9f\x27", &code_point, &code_point_length);
assert(result == P_DECODE_ERROR);
result = Decoder.decode_code_point("\xf0\x9f\xa7\xFF", &code_point, &code_point_length);
result = p_decode_code_point("\xf0\x9f\xa7\xFF", &code_point, &code_point_length);
assert(result == P_DECODE_ERROR);
result = Decoder.decode_code_point("\xfe", &code_point, &code_point_length);
result = p_decode_code_point("\xfe", &code_point, &code_point_length);
assert(result == P_DECODE_ERROR);
}
@ -44,25 +44,26 @@ unittest
{
TokenInfo token_info;
string input = "5 + 4 * \n677 + 567";
Lexer lexer = new Lexer(input);
assert(lexer.lex_token(&token_info) == P_SUCCESS);
p_context_t context;
p_context_init(&context, input);
assert(p_lex(&context, &token_info) == P_SUCCESS);
assert(token_info == TokenInfo(Position(0, 0), 1, TOKEN_int));
assert(lexer.lex_token(&token_info) == P_SUCCESS);
assert(p_lex(&context, &token_info) == P_SUCCESS);
assert(token_info == TokenInfo(Position(0, 2), 1, TOKEN_plus));
assert(lexer.lex_token(&token_info) == P_SUCCESS);
assert(p_lex(&context, &token_info) == P_SUCCESS);
assert(token_info == TokenInfo(Position(0, 4), 1, TOKEN_int));
assert(lexer.lex_token(&token_info) == P_SUCCESS);
assert(p_lex(&context, &token_info) == P_SUCCESS);
assert(token_info == TokenInfo(Position(0, 6), 1, TOKEN_times));
assert(lexer.lex_token(&token_info) == P_SUCCESS);
assert(p_lex(&context, &token_info) == P_SUCCESS);
assert(token_info == TokenInfo(Position(1, 0), 3, TOKEN_int));
assert(lexer.lex_token(&token_info) == P_SUCCESS);
assert(p_lex(&context, &token_info) == P_SUCCESS);
assert(token_info == TokenInfo(Position(1, 4), 1, TOKEN_plus));
assert(lexer.lex_token(&token_info) == P_SUCCESS);
assert(p_lex(&context, &token_info) == P_SUCCESS);
assert(token_info == TokenInfo(Position(1, 6), 3, TOKEN_int));
assert(lexer.lex_token(&token_info) == P_SUCCESS);
assert(p_lex(&context, &token_info) == P_SUCCESS);
assert(token_info == TokenInfo(Position(1, 9), 0, TOKEN___EOF));
lexer = new Lexer("");
assert(lexer.lex_token(&token_info) == P_SUCCESS);
p_context_init(&context, "");
assert(p_lex(&context, &token_info) == P_SUCCESS);
assert(token_info == TokenInfo(Position(0, 0), 0, TOKEN___EOF));
}

View File

@ -9,10 +9,11 @@ int main()
unittest
{
string input = "aba";
auto parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
p_context_t context;
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
input = "abb";
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
}

View File

@ -9,14 +9,15 @@ int main()
unittest
{
string input = "a";
auto parser = new Parser(input);
assert(parser.parse() == P_UNEXPECTED_TOKEN);
p_context_t context;
p_context_init(&context, input);
assert(p_parse(&context) == P_UNEXPECTED_TOKEN);
input = "a b";
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
input = "bb";
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
}

View File

@ -9,7 +9,8 @@ int main()
unittest
{
string input = `identifier_123`;
auto parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
p_context_t context;
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
writeln("pass1");
}

View File

@ -9,12 +9,13 @@ int main()
unittest
{
string input = `abc "a string" def`;
auto parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
p_context_t context;
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
writeln("pass1");
input = `abc "abc def" def`;
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
writeln("pass2");
}

View File

@ -9,12 +9,13 @@ int main()
unittest
{
string input = `x`;
auto parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
assert(parser.result == 1u);
p_context_t context;
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
assert(p_result(&context) == 1u);
input = `fabulous`;
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
assert(parser.result == 8u);
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
assert(p_result(&context) == 8u);
}

View File

@ -9,11 +9,12 @@ int main()
unittest
{
string input = `x`;
auto parser = new Parser(input);
assert(parser.parse() == P_UNEXPECTED_INPUT);
p_context_t context;
p_context_init(&context, input);
assert(p_parse(&context) == P_UNEXPECTED_INPUT);
input = `123`;
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
assert(parser.result == 123u);
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
assert(p_result(&context) == 123u);
}

View File

@ -9,6 +9,7 @@ int main()
unittest
{
string input = "ab";
auto parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
p_context_t context;
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
}

View File

@ -10,44 +10,45 @@ int main()
unittest
{
string input = ``;
auto parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
p_context_t context;
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
input = `{}`;
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
assert(cast(JSONObject)parser.result);
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
assert(cast(JSONObject)p_result(&context));
input = `[]`;
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
assert(cast(JSONArray)parser.result);
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
assert(cast(JSONArray)p_result(&context));
input = `-45.6`;
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
assert(cast(JSONNumber)parser.result);
assert((cast(JSONNumber)parser.result).value == -45.6);
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
assert(cast(JSONNumber)p_result(&context));
assert((cast(JSONNumber)p_result(&context)).value == -45.6);
input = `2E-2`;
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
assert(cast(JSONNumber)parser.result);
assert((cast(JSONNumber)parser.result).value == 0.02);
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
assert(cast(JSONNumber)p_result(&context));
assert((cast(JSONNumber)p_result(&context)).value == 0.02);
input = `{"hi":true}`;
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
assert(cast(JSONObject)parser.result);
JSONObject o = cast(JSONObject)parser.result;
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
assert(cast(JSONObject)p_result(&context));
JSONObject o = cast(JSONObject)p_result(&context);
assert(o.value["hi"]);
assert(cast(JSONTrue)o.value["hi"]);
input = `{"ff": false, "nn": null}`;
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
assert(cast(JSONObject)parser.result);
o = cast(JSONObject)parser.result;
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
assert(cast(JSONObject)p_result(&context));
o = cast(JSONObject)p_result(&context);
assert(o.value["ff"]);
assert(cast(JSONFalse)o.value["ff"]);
assert(o.value["nn"]);

View File

@ -9,17 +9,18 @@ int main()
unittest
{
string input = "a";
auto parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
assert(parser.result == 1u);
p_context_t context;
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
assert(p_result(&context) == 1u);
input = "";
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
assert(parser.result == 0u);
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
assert(p_result(&context) == 0u);
input = "aaaaaaaaaaaaaaaa";
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
assert(parser.result == 16u);
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
assert(p_result(&context) == 16u);
}

View File

@ -9,12 +9,13 @@ int main()
unittest
{
string input = "abcdef";
auto parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
p_context_t context;
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
writeln("pass1");
input = "defabcdef";
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
writeln("pass2");
}

View File

@ -9,6 +9,7 @@ int main()
unittest
{
string input = "defghidef";
auto parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
p_context_t context;
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
}

View File

@ -9,12 +9,13 @@ int main()
unittest
{
string input = "abcdef";
auto parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
p_context_t context;
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
writeln("pass1");
input = "abcabcdef";
parser = new Parser(input);
assert(parser.parse() == P_SUCCESS);
p_context_init(&context, input);
assert(p_parse(&context) == P_SUCCESS);
writeln("pass2");
}