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);
|
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;
|
Lexer.TokenInfo token_info;
|
||||||
uint token = _TOKEN_COUNT;
|
uint token = _TOKEN_COUNT;
|
||||||
@ -576,6 +584,15 @@ 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)
|
||||||
|
{
|
||||||
|
case Lexer.P_UNEXPECTED_INPUT:
|
||||||
|
return P_UNEXPECTED_INPUT;
|
||||||
|
case Lexer.P_DECODE_ERROR:
|
||||||
|
return P_DECODE_ERROR;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
token = token_info.token;
|
token = token_info.token;
|
||||||
}
|
}
|
||||||
uint shift_state = 0xFFFFFFFFu;
|
uint shift_state = 0xFFFFFFFFu;
|
||||||
@ -590,7 +607,7 @@ class <%= @classname %>
|
|||||||
{
|
{
|
||||||
/* Successful parse. */
|
/* Successful parse. */
|
||||||
parse_result = statevalues[$-1].pvalue;
|
parse_result = statevalues[$-1].pvalue;
|
||||||
return true;
|
return P_SUCCESS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (shift_state != 0xFFFFFFFFu)
|
if (shift_state != 0xFFFFFFFFu)
|
||||||
@ -634,7 +651,7 @@ class <%= @classname %>
|
|||||||
{
|
{
|
||||||
writeln("{other}");
|
writeln("{other}");
|
||||||
}
|
}
|
||||||
return false;
|
return P_UNEXPECTED_TOKEN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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() == true);
|
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||||
|
|
||||||
input = "abb";
|
input = "abb";
|
||||||
parser = new Testparser.Parser(input);
|
parser = new Testparser.Parser(input);
|
||||||
assert(parser.parse() == true);
|
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||||
}
|
}
|
||||||
|
@ -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() == false);
|
assert(parser.parse() == Testparser.Parser.P_UNEXPECTED_TOKEN);
|
||||||
|
|
||||||
input = "a b";
|
input = "a b";
|
||||||
parser = new Testparser.Parser(input);
|
parser = new Testparser.Parser(input);
|
||||||
assert(parser.parse() == true);
|
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||||
|
|
||||||
input = "bb";
|
input = "bb";
|
||||||
parser = new Testparser.Parser(input);
|
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`;
|
string input = `identifier_123`;
|
||||||
auto parser = new Testparser.Parser(input);
|
auto parser = new Testparser.Parser(input);
|
||||||
assert(parser.parse() == true);
|
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||||
writeln("pass1");
|
writeln("pass1");
|
||||||
}
|
}
|
||||||
|
@ -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() == true);
|
assert(parser.parse() == Testparser.Parser.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() == true);
|
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||||
writeln("pass2");
|
writeln("pass2");
|
||||||
}
|
}
|
||||||
|
@ -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() == true);
|
assert(parser.parse() == Testparser.Parser.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() == true);
|
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||||
assert(parser.result == 8u);
|
assert(parser.result == 8u);
|
||||||
}
|
}
|
||||||
|
@ -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() == true);
|
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||||
}
|
}
|
||||||
|
@ -11,33 +11,33 @@ unittest
|
|||||||
{
|
{
|
||||||
string input = ``;
|
string input = ``;
|
||||||
auto parser = new Testparser.Parser(input);
|
auto parser = new Testparser.Parser(input);
|
||||||
assert(parser.parse());
|
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||||
|
|
||||||
input = `{}`;
|
input = `{}`;
|
||||||
parser = new Testparser.Parser(input);
|
parser = new Testparser.Parser(input);
|
||||||
assert(parser.parse());
|
assert(parser.parse() == Testparser.Parser.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());
|
assert(parser.parse() == Testparser.Parser.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());
|
assert(parser.parse() == Testparser.Parser.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());
|
assert(parser.parse() == Testparser.Parser.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());
|
assert(parser.parse() == Testparser.Parser.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());
|
assert(parser.parse() == Testparser.Parser.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"]);
|
||||||
|
@ -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() == true);
|
assert(parser.parse() == Testparser.Parser.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() == true);
|
assert(parser.parse() == Testparser.Parser.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() == true);
|
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||||
assert(parser.result == 16u);
|
assert(parser.result == 16u);
|
||||||
}
|
}
|
||||||
|
@ -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() == true);
|
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||||
writeln("pass1");
|
writeln("pass1");
|
||||||
|
|
||||||
input = "defabcdef";
|
input = "defabcdef";
|
||||||
parser = new Testparser.Parser(input);
|
parser = new Testparser.Parser(input);
|
||||||
assert(parser.parse() == true);
|
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||||
writeln("pass2");
|
writeln("pass2");
|
||||||
}
|
}
|
||||||
|
@ -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() == true);
|
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||||
}
|
}
|
||||||
|
@ -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() == true);
|
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||||
writeln("pass1");
|
writeln("pass1");
|
||||||
|
|
||||||
input = "abcabcdef";
|
input = "abcabcdef";
|
||||||
parser = new Testparser.Parser(input);
|
parser = new Testparser.Parser(input);
|
||||||
assert(parser.parse() == true);
|
assert(parser.parse() == Testparser.Parser.P_SUCCESS);
|
||||||
writeln("pass2");
|
writeln("pass2");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user