Consolidate return codes

This commit is contained in:
Josh Holtrop 2023-07-08 09:09:08 -04:00
parent 0232b204c6
commit dec8879e05
13 changed files with 113 additions and 111 deletions

View File

@ -11,6 +11,18 @@ import std.stdio;
class <%= @classname %> class <%= @classname %>
{ {
/* Result codes. */
public enum : size_t
{
P_SUCCESS,
P_DECODE_ERROR,
P_UNEXPECTED_INPUT,
P_UNEXPECTED_TOKEN,
P_TOKEN,
P_DROP,
P_EOF,
}
alias TokenID = uint; alias TokenID = uint;
enum : TokenID enum : TokenID
@ -71,20 +83,21 @@ class <%= @classname %>
static class Decoder static class Decoder
{ {
enum Result /**
{ * Decode a UTF-8 code point.
SUCCESS, *
EOF, * Returns one of:
DECODE_ERROR, * - P_SUCCESS
} * - P_DECODE_ERROR
* - P_EOF
static Result decode_code_point(string input, */
static size_t decode_code_point(string input,
ref CodePoint out_code_point, ref CodePoint out_code_point,
ref ubyte out_code_point_length) ref ubyte out_code_point_length)
{ {
if (input.length == 0u) if (input.length == 0u)
{ {
return Result.EOF; return P_EOF;
} }
char c = input[0]; char c = input[0];
CodePoint code_point; CodePoint code_point;
@ -124,11 +137,11 @@ class <%= @classname %>
} }
else else
{ {
return Result.DECODE_ERROR; return P_DECODE_ERROR;
} }
if (input.length <= following_bytes) if (input.length <= following_bytes)
{ {
return Result.DECODE_ERROR; return P_DECODE_ERROR;
} }
code_point_length = cast(ubyte)(following_bytes + 1u); code_point_length = cast(ubyte)(following_bytes + 1u);
for (size_t i = 0u; i < following_bytes; i++) for (size_t i = 0u; i < following_bytes; i++)
@ -136,14 +149,14 @@ class <%= @classname %>
char b = input[i + 1u]; char b = input[i + 1u];
if ((b & 0xC0u) != 0x80u) if ((b & 0xC0u) != 0x80u)
{ {
return Result.DECODE_ERROR; return P_DECODE_ERROR;
} }
code_point = (code_point << 6u) | (b & 0x3Fu); code_point = (code_point << 6u) | (b & 0x3Fu);
} }
} }
out_code_point = code_point; out_code_point = code_point;
out_code_point_length = code_point_length; out_code_point_length = code_point_length;
return Result.SUCCESS; return P_SUCCESS;
} }
} }
@ -230,14 +243,6 @@ class <%= @classname %>
<% end %> <% end %>
]; ];
public enum : size_t
{
P_TOKEN,
P_UNEXPECTED_INPUT,
P_DECODE_ERROR,
P_DROP,
}
public static struct TokenInfo public static struct TokenInfo
{ {
size_t row; size_t row;
@ -259,6 +264,14 @@ class <%= @classname %>
m_mode = <%= @lexer.mode_id("default") %>; m_mode = <%= @lexer.mode_id("default") %>;
} }
/**
* Lex the next token in the input stream.
*
* Returns one of:
* - P_TOKEN
* - P_DECODE_ERROR
* - P_UNEXPECTED_INPUT
*/
size_t lex_token(TokenInfo * out_token_info) size_t lex_token(TokenInfo * out_token_info)
{ {
for (;;) for (;;)
@ -298,6 +311,15 @@ class <%= @classname %>
return Token.invalid(); return Token.invalid();
} }
/**
* Attempt to lex the next token in the input stream.
*
* Returns one of:
* - P_TOKEN
* - P_DECODE_ERROR
* - P_UNEXPECTED_INPUT
* - P_DROP
*/
private size_t attempt_lex_token(TokenInfo * out_token_info) private size_t attempt_lex_token(TokenInfo * out_token_info)
{ {
TokenInfo token_info; TokenInfo token_info;
@ -307,9 +329,10 @@ class <%= @classname %>
*out_token_info = token_info; // TODO: remove *out_token_info = token_info; // TODO: remove
MatchInfo match_info; MatchInfo match_info;
size_t unexpected_input_length; size_t unexpected_input_length;
switch (find_longest_match(match_info, unexpected_input_length)) size_t result = find_longest_match(match_info, unexpected_input_length);
switch (result)
{ {
case FindLongestMatchResult.FOUND_MATCH: case P_SUCCESS:
uint token_to_accept = match_info.accepting_state.token; uint token_to_accept = match_info.accepting_state.token;
if (match_info.accepting_state.code_id.is_valid()) if (match_info.accepting_state.code_id.is_valid())
{ {
@ -344,30 +367,16 @@ class <%= @classname %>
*out_token_info = token_info; *out_token_info = token_info;
return P_TOKEN; return P_TOKEN;
case FindLongestMatchResult.DECODE_ERROR: case P_EOF:
return P_DECODE_ERROR;
case FindLongestMatchResult.EOF:
token_info.token = TOKEN___EOF; token_info.token = TOKEN___EOF;
*out_token_info = token_info; *out_token_info = token_info;
return P_TOKEN; return P_TOKEN;
case FindLongestMatchResult.UNEXPECTED_INPUT:
return P_UNEXPECTED_INPUT;
default: default:
assert(false); return result;
} }
} }
enum FindLongestMatchResult
{
FOUND_MATCH,
DECODE_ERROR,
EOF,
UNEXPECTED_INPUT,
}
struct MatchInfo struct MatchInfo
{ {
size_t length; size_t length;
@ -376,7 +385,16 @@ class <%= @classname %>
const(State) * accepting_state; const(State) * accepting_state;
} }
private FindLongestMatchResult find_longest_match( /**
* Find the longest lexer pattern match at the current position.
*
* Returns one of:
* - P_SUCCESS
* - P_UNEXPECTED_INPUT
* - P_DECODE_ERROR
* - P_EOF
*/
private size_t find_longest_match(
ref MatchInfo out_match_info, ref MatchInfo out_match_info,
ref size_t out_unexpected_input_length) ref size_t out_unexpected_input_length)
{ {
@ -388,9 +406,10 @@ class <%= @classname %>
string input = m_input[(m_input_position + attempt_match.length)..(m_input.length)]; string input = m_input[(m_input_position + attempt_match.length)..(m_input.length)];
CodePoint code_point; CodePoint code_point;
ubyte code_point_length; ubyte code_point_length;
switch (Decoder.decode_code_point(input, code_point, code_point_length)) size_t result = Decoder.decode_code_point(input, code_point, code_point_length);
switch (result)
{ {
case Decoder.Result.SUCCESS: case P_SUCCESS:
auto transition_result = transition(current_state, code_point); auto transition_result = transition(current_state, code_point);
if (transition_result.found()) if (transition_result.found())
{ {
@ -414,41 +433,38 @@ class <%= @classname %>
else if (longest_match.length > 0) else if (longest_match.length > 0)
{ {
out_match_info = longest_match; out_match_info = longest_match;
return FindLongestMatchResult.FOUND_MATCH; return P_SUCCESS;
} }
else else
{ {
out_unexpected_input_length = attempt_match.length + code_point_length; out_unexpected_input_length = attempt_match.length + code_point_length;
return FindLongestMatchResult.UNEXPECTED_INPUT; return P_UNEXPECTED_INPUT;
} }
break; break;
case Decoder.Result.EOF: case P_EOF:
/* We hit EOF. */ /* We hit EOF. */
if (longest_match.length > 0) if (longest_match.length > 0)
{ {
/* We have a match, so use it. */ /* We have a match, so use it. */
out_match_info = longest_match; out_match_info = longest_match;
return FindLongestMatchResult.FOUND_MATCH; return P_SUCCESS;
} }
else if (attempt_match.length != 0) else if (attempt_match.length != 0)
{ {
/* There is a partial match - error! */ /* There is a partial match - error! */
out_unexpected_input_length = attempt_match.length; out_unexpected_input_length = attempt_match.length;
return FindLongestMatchResult.UNEXPECTED_INPUT; return P_UNEXPECTED_INPUT;
} }
else else
{ {
/* Valid EOF return. */ /* Valid EOF return. */
return FindLongestMatchResult.EOF; return P_EOF;
} }
break; break;
case Decoder.Result.DECODE_ERROR:
return FindLongestMatchResult.DECODE_ERROR;
default: default:
assert(false); return result;
} }
} }
} }
@ -564,14 +580,6 @@ class <%= @classname %>
m_lexer = new Lexer(input); m_lexer = new Lexer(input);
} }
public enum : size_t
{
P_SUCCESS,
P_DECODE_ERROR,
P_UNEXPECTED_INPUT,
P_UNEXPECTED_TOKEN,
}
size_t parse() size_t parse()
{ {
Lexer.TokenInfo token_info; Lexer.TokenInfo token_info;
@ -584,14 +592,9 @@ class <%= @classname %>
if (token == _TOKEN_COUNT) if (token == _TOKEN_COUNT)
{ {
size_t lexer_result = m_lexer.lex_token(&token_info); size_t lexer_result = m_lexer.lex_token(&token_info);
switch (lexer_result) if (lexer_result != P_TOKEN)
{ {
case Lexer.P_UNEXPECTED_INPUT: return lexer_result;
return P_UNEXPECTED_INPUT;
case Lexer.P_DECODE_ERROR:
return P_DECODE_ERROR;
default:
break;
} }
token = token_info.token; token = token_info.token;
} }

View File

@ -8,37 +8,36 @@ int main()
unittest unittest
{ {
alias Result = Testparser.Decoder.Result; size_t result;
Result result;
Testparser.CodePoint code_point; Testparser.CodePoint code_point;
ubyte code_point_length; ubyte code_point_length;
result = Testparser.Decoder.decode_code_point("5", code_point, code_point_length); result = Testparser.Decoder.decode_code_point("5", code_point, code_point_length);
assert(result == Result.SUCCESS); assert(result == Testparser.P_SUCCESS);
assert(code_point == '5'); assert(code_point == '5');
assert(code_point_length == 1u); assert(code_point_length == 1u);
result = Testparser.Decoder.decode_code_point("", code_point, code_point_length); result = Testparser.Decoder.decode_code_point("", code_point, code_point_length);
assert(result == Result.EOF); assert(result == Testparser.P_EOF);
result = Testparser.Decoder.decode_code_point("\xC2\xA9", code_point, code_point_length); result = Testparser.Decoder.decode_code_point("\xC2\xA9", code_point, code_point_length);
assert(result == Result.SUCCESS); assert(result == Testparser.P_SUCCESS);
assert(code_point == 0xA9u); assert(code_point == 0xA9u);
assert(code_point_length == 2u); assert(code_point_length == 2u);
result = Testparser.Decoder.decode_code_point("\xf0\x9f\xa7\xa1", code_point, code_point_length); result = Testparser.Decoder.decode_code_point("\xf0\x9f\xa7\xa1", code_point, code_point_length);
assert(result == Result.SUCCESS); assert(result == Testparser.P_SUCCESS);
assert(code_point == 0x1F9E1u); assert(code_point == 0x1F9E1u);
assert(code_point_length == 4u); assert(code_point_length == 4u);
result = Testparser.Decoder.decode_code_point("\xf0\x9f\x27", code_point, code_point_length); result = Testparser.Decoder.decode_code_point("\xf0\x9f\x27", code_point, code_point_length);
assert(result == Result.DECODE_ERROR); assert(result == Testparser.P_DECODE_ERROR);
result = Testparser.Decoder.decode_code_point("\xf0\x9f\xa7\xFF", code_point, code_point_length); result = Testparser.Decoder.decode_code_point("\xf0\x9f\xa7\xFF", code_point, code_point_length);
assert(result == Result.DECODE_ERROR); assert(result == Testparser.P_DECODE_ERROR);
result = Testparser.Decoder.decode_code_point("\xfe", code_point, code_point_length); result = Testparser.Decoder.decode_code_point("\xfe", code_point, code_point_length);
assert(result == Result.DECODE_ERROR); assert(result == Testparser.P_DECODE_ERROR);
} }
unittest unittest
@ -47,24 +46,24 @@ unittest
TokenInfo token_info; TokenInfo token_info;
string input = "5 + 4 * \n677 + 567"; string input = "5 + 4 * \n677 + 567";
Testparser.Lexer lexer = new Testparser.Lexer(input); Testparser.Lexer lexer = new Testparser.Lexer(input);
assert(lexer.lex_token(&token_info) == lexer.P_TOKEN); assert(lexer.lex_token(&token_info) == Testparser.P_TOKEN);
assert(token_info == TokenInfo(0, 0, 1, Testparser.TOKEN_int)); assert(token_info == TokenInfo(0, 0, 1, Testparser.TOKEN_int));
assert(lexer.lex_token(&token_info) == lexer.P_TOKEN); assert(lexer.lex_token(&token_info) == Testparser.P_TOKEN);
assert(token_info == TokenInfo(0, 2, 1, Testparser.TOKEN_plus)); assert(token_info == TokenInfo(0, 2, 1, Testparser.TOKEN_plus));
assert(lexer.lex_token(&token_info) == lexer.P_TOKEN); assert(lexer.lex_token(&token_info) == Testparser.P_TOKEN);
assert(token_info == TokenInfo(0, 4, 1, Testparser.TOKEN_int)); assert(token_info == TokenInfo(0, 4, 1, Testparser.TOKEN_int));
assert(lexer.lex_token(&token_info) == lexer.P_TOKEN); assert(lexer.lex_token(&token_info) == Testparser.P_TOKEN);
assert(token_info == TokenInfo(0, 6, 1, Testparser.TOKEN_times)); assert(token_info == TokenInfo(0, 6, 1, Testparser.TOKEN_times));
assert(lexer.lex_token(&token_info) == lexer.P_TOKEN); assert(lexer.lex_token(&token_info) == Testparser.P_TOKEN);
assert(token_info == TokenInfo(1, 0, 3, Testparser.TOKEN_int)); assert(token_info == TokenInfo(1, 0, 3, Testparser.TOKEN_int));
assert(lexer.lex_token(&token_info) == lexer.P_TOKEN); assert(lexer.lex_token(&token_info) == Testparser.P_TOKEN);
assert(token_info == TokenInfo(1, 4, 1, Testparser.TOKEN_plus)); assert(token_info == TokenInfo(1, 4, 1, Testparser.TOKEN_plus));
assert(lexer.lex_token(&token_info) == lexer.P_TOKEN); assert(lexer.lex_token(&token_info) == Testparser.P_TOKEN);
assert(token_info == TokenInfo(1, 6, 3, Testparser.TOKEN_int)); assert(token_info == TokenInfo(1, 6, 3, Testparser.TOKEN_int));
assert(lexer.lex_token(&token_info) == lexer.P_TOKEN); assert(lexer.lex_token(&token_info) == Testparser.P_TOKEN);
assert(token_info == TokenInfo(1, 9, 0, Testparser.TOKEN___EOF)); assert(token_info == TokenInfo(1, 9, 0, Testparser.TOKEN___EOF));
lexer = new Testparser.Lexer(""); lexer = new Testparser.Lexer("");
assert(lexer.lex_token(&token_info) == lexer.P_TOKEN); assert(lexer.lex_token(&token_info) == Testparser.P_TOKEN);
assert(token_info == TokenInfo(0, 0, 0, Testparser.TOKEN___EOF)); assert(token_info == TokenInfo(0, 0, 0, Testparser.TOKEN___EOF));
} }

View File

@ -10,9 +10,9 @@ unittest
{ {
string input = "aba"; string input = "aba";
auto parser = new Testparser.Parser(input); auto parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
input = "abb"; input = "abb";
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
} }

View File

@ -10,13 +10,13 @@ unittest
{ {
string input = "a"; string input = "a";
auto parser = new Testparser.Parser(input); auto parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_UNEXPECTED_TOKEN); assert(parser.parse() == Testparser.P_UNEXPECTED_TOKEN);
input = "a b"; input = "a b";
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
input = "bb"; input = "bb";
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
} }

View File

@ -10,6 +10,6 @@ unittest
{ {
string input = `identifier_123`; string input = `identifier_123`;
auto parser = new Testparser.Parser(input); auto parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
writeln("pass1"); writeln("pass1");
} }

View File

@ -10,11 +10,11 @@ unittest
{ {
string input = `abc "a string" def`; string input = `abc "a string" def`;
auto parser = new Testparser.Parser(input); auto parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
writeln("pass1"); writeln("pass1");
input = `abc "abc def" def`; input = `abc "abc def" def`;
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
writeln("pass2"); writeln("pass2");
} }

View File

@ -10,11 +10,11 @@ unittest
{ {
string input = `x`; string input = `x`;
auto parser = new Testparser.Parser(input); auto parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
assert(parser.result == 1u); assert(parser.result == 1u);
input = `fabulous`; input = `fabulous`;
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
assert(parser.result == 8u); assert(parser.result == 8u);
} }

View File

@ -10,5 +10,5 @@ unittest
{ {
string input = "ab"; string input = "ab";
auto parser = new Testparser.Parser(input); auto parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
} }

View File

@ -11,33 +11,33 @@ unittest
{ {
string input = ``; string input = ``;
auto parser = new Testparser.Parser(input); auto parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
input = `{}`; input = `{}`;
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
assert(cast(JSONObject)parser.result); assert(cast(JSONObject)parser.result);
input = `[]`; input = `[]`;
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
assert(cast(JSONArray)parser.result); assert(cast(JSONArray)parser.result);
input = `-45.6`; input = `-45.6`;
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
assert(cast(JSONNumber)parser.result); assert(cast(JSONNumber)parser.result);
assert((cast(JSONNumber)parser.result).value == -45.6); assert((cast(JSONNumber)parser.result).value == -45.6);
input = `2E-2`; input = `2E-2`;
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
assert(cast(JSONNumber)parser.result); assert(cast(JSONNumber)parser.result);
assert((cast(JSONNumber)parser.result).value == 0.02); assert((cast(JSONNumber)parser.result).value == 0.02);
input = `{"hi":true}`; input = `{"hi":true}`;
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
assert(cast(JSONObject)parser.result); assert(cast(JSONObject)parser.result);
JSONObject o = cast(JSONObject)parser.result; JSONObject o = cast(JSONObject)parser.result;
assert(o.value["hi"]); assert(o.value["hi"]);
@ -45,7 +45,7 @@ unittest
input = `{"ff": false, "nn": null}`; input = `{"ff": false, "nn": null}`;
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
assert(cast(JSONObject)parser.result); assert(cast(JSONObject)parser.result);
o = cast(JSONObject)parser.result; o = cast(JSONObject)parser.result;
assert(o.value["ff"]); assert(o.value["ff"]);

View File

@ -10,16 +10,16 @@ unittest
{ {
string input = "a"; string input = "a";
auto parser = new Testparser.Parser(input); auto parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
assert(parser.result == 1u); assert(parser.result == 1u);
input = ""; input = "";
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
assert(parser.result == 0u); assert(parser.result == 0u);
input = "aaaaaaaaaaaaaaaa"; input = "aaaaaaaaaaaaaaaa";
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
assert(parser.result == 16u); assert(parser.result == 16u);
} }

View File

@ -10,11 +10,11 @@ unittest
{ {
string input = "abcdef"; string input = "abcdef";
auto parser = new Testparser.Parser(input); auto parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
writeln("pass1"); writeln("pass1");
input = "defabcdef"; input = "defabcdef";
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
writeln("pass2"); writeln("pass2");
} }

View File

@ -10,5 +10,5 @@ unittest
{ {
string input = "defghidef"; string input = "defghidef";
auto parser = new Testparser.Parser(input); auto parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
} }

View File

@ -10,11 +10,11 @@ unittest
{ {
string input = "abcdef"; string input = "abcdef";
auto parser = new Testparser.Parser(input); auto parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
writeln("pass1"); writeln("pass1");
input = "abcabcdef"; input = "abcabcdef";
parser = new Testparser.Parser(input); parser = new Testparser.Parser(input);
assert(parser.parse() == Testparser.Parser.P_SUCCESS); assert(parser.parse() == Testparser.P_SUCCESS);
writeln("pass2"); writeln("pass2");
} }