Return integer result code from Parser.parse()
This commit is contained in:
parent
c88338698a
commit
0232b204c6
@ -564,7 +564,15 @@ class <%= @classname %>
|
||||
m_lexer = new Lexer(input);
|
||||
}
|
||||
|
||||
bool parse()
|
||||
public enum : size_t
|
||||
{
|
||||
P_SUCCESS,
|
||||
P_DECODE_ERROR,
|
||||
P_UNEXPECTED_INPUT,
|
||||
P_UNEXPECTED_TOKEN,
|
||||
}
|
||||
|
||||
size_t parse()
|
||||
{
|
||||
Lexer.TokenInfo token_info;
|
||||
uint token = _TOKEN_COUNT;
|
||||
@ -576,6 +584,15 @@ class <%= @classname %>
|
||||
if (token == _TOKEN_COUNT)
|
||||
{
|
||||
size_t lexer_result = m_lexer.lex_token(&token_info);
|
||||
switch (lexer_result)
|
||||
{
|
||||
case Lexer.P_UNEXPECTED_INPUT:
|
||||
return P_UNEXPECTED_INPUT;
|
||||
case Lexer.P_DECODE_ERROR:
|
||||
return P_DECODE_ERROR;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
token = token_info.token;
|
||||
}
|
||||
uint shift_state = 0xFFFFFFFFu;
|
||||
@ -590,7 +607,7 @@ class <%= @classname %>
|
||||
{
|
||||
/* Successful parse. */
|
||||
parse_result = statevalues[$-1].pvalue;
|
||||
return true;
|
||||
return P_SUCCESS;
|
||||
}
|
||||
}
|
||||
if (shift_state != 0xFFFFFFFFu)
|
||||
@ -634,7 +651,7 @@ class <%= @classname %>
|
||||
{
|
||||
writeln("{other}");
|
||||
}
|
||||
return false;
|
||||
return P_UNEXPECTED_TOKEN;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,9 +10,9 @@ unittest
|
||||
{
|
||||
string input = "aba";
|
||||
auto parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
|
||||
input = "abb";
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
}
|
||||
|
@ -10,13 +10,13 @@ unittest
|
||||
{
|
||||
string input = "a";
|
||||
auto parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == false);
|
||||
assert(parser.parse() == Testparser.Parser.P_UNEXPECTED_TOKEN);
|
||||
|
||||
input = "a b";
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
|
||||
input = "bb";
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
}
|
||||
|
@ -10,6 +10,6 @@ unittest
|
||||
{
|
||||
string input = `identifier_123`;
|
||||
auto parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
writeln("pass1");
|
||||
}
|
||||
|
@ -10,11 +10,11 @@ unittest
|
||||
{
|
||||
string input = `abc "a string" def`;
|
||||
auto parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
writeln("pass1");
|
||||
|
||||
input = `abc "abc def" def`;
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
writeln("pass2");
|
||||
}
|
||||
|
@ -10,11 +10,11 @@ unittest
|
||||
{
|
||||
string input = `x`;
|
||||
auto parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
assert(parser.result == 1u);
|
||||
|
||||
input = `fabulous`;
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
assert(parser.result == 8u);
|
||||
}
|
||||
|
@ -10,5 +10,5 @@ unittest
|
||||
{
|
||||
string input = "ab";
|
||||
auto parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
}
|
||||
|
@ -11,33 +11,33 @@ unittest
|
||||
{
|
||||
string input = ``;
|
||||
auto parser = new Testparser.Parser(input);
|
||||
assert(parser.parse());
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
|
||||
input = `{}`;
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse());
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
assert(cast(JSONObject)parser.result);
|
||||
|
||||
input = `[]`;
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse());
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
assert(cast(JSONArray)parser.result);
|
||||
|
||||
input = `-45.6`;
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse());
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
assert(cast(JSONNumber)parser.result);
|
||||
assert((cast(JSONNumber)parser.result).value == -45.6);
|
||||
|
||||
input = `2E-2`;
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse());
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
assert(cast(JSONNumber)parser.result);
|
||||
assert((cast(JSONNumber)parser.result).value == 0.02);
|
||||
|
||||
input = `{"hi":true}`;
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse());
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
assert(cast(JSONObject)parser.result);
|
||||
JSONObject o = cast(JSONObject)parser.result;
|
||||
assert(o.value["hi"]);
|
||||
@ -45,7 +45,7 @@ unittest
|
||||
|
||||
input = `{"ff": false, "nn": null}`;
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse());
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
assert(cast(JSONObject)parser.result);
|
||||
o = cast(JSONObject)parser.result;
|
||||
assert(o.value["ff"]);
|
||||
|
@ -10,16 +10,16 @@ unittest
|
||||
{
|
||||
string input = "a";
|
||||
auto parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
assert(parser.result == 1u);
|
||||
|
||||
input = "";
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
assert(parser.result == 0u);
|
||||
|
||||
input = "aaaaaaaaaaaaaaaa";
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
assert(parser.result == 16u);
|
||||
}
|
||||
|
@ -10,11 +10,11 @@ unittest
|
||||
{
|
||||
string input = "abcdef";
|
||||
auto parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
writeln("pass1");
|
||||
|
||||
input = "defabcdef";
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
writeln("pass2");
|
||||
}
|
||||
|
@ -10,5 +10,5 @@ unittest
|
||||
{
|
||||
string input = "defghidef";
|
||||
auto parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
}
|
||||
|
@ -10,11 +10,11 @@ unittest
|
||||
{
|
||||
string input = "abcdef";
|
||||
auto parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
writeln("pass1");
|
||||
|
||||
input = "abcabcdef";
|
||||
parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||
writeln("pass2");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user