Replace Token struct with integer type

Replace _TOKEN_COUNT with INVALID_TOKEN_ID.
This commit is contained in:
Josh Holtrop 2023-07-09 22:22:50 -04:00
parent 6327bd1e96
commit 75f478204a
3 changed files with 21 additions and 47 deletions

View File

@ -26,9 +26,9 @@ class <%= @classname %>
/* An invalid ID value. */ /* An invalid ID value. */
private enum INVALID_ID = 0xFFFF_FFFFu; private enum INVALID_ID = 0xFFFF_FFFFu;
alias TokenID = uint; alias Token = <%= get_type_for(@grammar.invalid_token_id) %>;
enum : TokenID enum : Token
{ {
<% @grammar.tokens.each_with_index do |token, index| %> <% @grammar.tokens.each_with_index do |token, index| %>
TOKEN_<%= token.code_name %> = <%= index %>, TOKEN_<%= token.code_name %> = <%= index %>,
@ -36,37 +36,7 @@ class <%= @classname %>
<% raise "Token ID (#{token.id}) does not match index (#{index}) for token #{token.name}!" %> <% raise "Token ID (#{token.id}) does not match index (#{index}) for token #{token.name}!" %>
<% end %> <% end %>
<% end %> <% end %>
_TOKEN_COUNT = <%= @grammar.tokens.size %>, INVALID_TOKEN_ID = <%= @grammar.invalid_token_id %>,
}
struct Token
{
/* Number of tokens in this parser. */
enum count = <%= @grammar.tokens.size %>;
TokenID token;
alias token this;
@disable this();
this(TokenID token)
{
this.token = token;
}
static Token invalid()
{
return Token(count);
}
bool is_valid() const
{
return token < count;
}
bool is_invalid() const
{
return !is_valid();
}
} }
alias CodePoint = uint; alias CodePoint = uint;
@ -207,7 +177,7 @@ class <%= @classname %>
<% if state_table_entry[:token] %> <% if state_table_entry[:token] %>
Token(<%= state_table_entry[:token] %>u), Token(<%= state_table_entry[:token] %>u),
<% else %> <% else %>
Token.invalid(), INVALID_TOKEN_ID,
<% end %> <% end %>
<% if state_table_entry[:code_id] %> <% if state_table_entry[:code_id] %>
<%= state_table_entry[:code_id] %>u, <%= state_table_entry[:code_id] %>u,
@ -289,7 +259,7 @@ class <%= @classname %>
default: break; default: break;
} }
return Token.invalid(); return INVALID_TOKEN_ID;
} }
/** /**
@ -306,7 +276,7 @@ class <%= @classname %>
TokenInfo token_info; TokenInfo token_info;
token_info.row = m_input_row; token_info.row = m_input_row;
token_info.col = m_input_col; token_info.col = m_input_col;
token_info.token = _TOKEN_COUNT; token_info.token = INVALID_TOKEN_ID;
*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;
@ -322,9 +292,9 @@ class <%= @classname %>
* code did not explicitly return a token. So only override * code did not explicitly return a token. So only override
* the token to return if the user code does explicitly * the token to return if the user code does explicitly
* return a token. */ * return a token. */
if (user_code_token.is_valid()) if (user_code_token != INVALID_TOKEN_ID)
{ {
token_to_accept = user_code_token.token; token_to_accept = user_code_token;
} }
} }
@ -339,7 +309,7 @@ class <%= @classname %>
{ {
m_input_col += match_info.delta_col; m_input_col += match_info.delta_col;
} }
if (token_to_accept == _TOKEN_COUNT) if (token_to_accept == INVALID_TOKEN_ID)
{ {
return P_DROP; return P_DROP;
} }
@ -530,13 +500,13 @@ class <%= @classname %>
size_t parse() size_t parse()
{ {
Lexer.TokenInfo token_info; Lexer.TokenInfo token_info;
uint token = _TOKEN_COUNT; uint token = INVALID_TOKEN_ID;
StateValue[] statevalues = new StateValue[](1); StateValue[] statevalues = new StateValue[](1);
uint reduced_rule_set = INVALID_ID; uint reduced_rule_set = INVALID_ID;
ParserValue reduced_parser_value; ParserValue reduced_parser_value;
for (;;) for (;;)
{ {
if (token == _TOKEN_COUNT) if (token == INVALID_TOKEN_ID)
{ {
size_t lexer_result = m_lexer.lex_token(&token_info); size_t lexer_result = m_lexer.lex_token(&token_info);
if (lexer_result != P_TOKEN) if (lexer_result != P_TOKEN)
@ -567,7 +537,7 @@ class <%= @classname %>
if (reduced_rule_set == INVALID_ID) if (reduced_rule_set == INVALID_ID)
{ {
/* We shifted a token, mark it consumed. */ /* We shifted a token, mark it consumed. */
token = _TOKEN_COUNT; token = INVALID_TOKEN_ID;
statevalues[$-1].pvalue = token_info.pvalue; statevalues[$-1].pvalue = token_info.pvalue;
} }
else else
@ -593,7 +563,7 @@ class <%= @classname %>
/* Error, unexpected token. */ /* Error, unexpected token. */
write("Unexpected token "); write("Unexpected token ");
if (token < _TOKEN_COUNT) if (token != INVALID_TOKEN_ID)
{ {
writeln(token_names[token]); writeln(token_names[token]);
} }
@ -618,7 +588,7 @@ class <%= @classname %>
{ {
if (shifts[i].symbol == symbol) if (shifts[i].symbol == symbol)
{ {
// if (symbol < _TOKEN_COUNT) // if (symbol != INVALID_TOKEN_ID)
// { // {
// writeln("Shifting ", token_names[symbol]); // writeln("Shifting ", token_names[symbol]);
// } // }
@ -639,10 +609,10 @@ class <%= @classname %>
for (uint i = start; i < end; i++) for (uint i = start; i < end; i++)
{ {
if ((reduces[i].token == token) || if ((reduces[i].token == token) ||
(reduces[i].token == _TOKEN_COUNT)) (reduces[i].token == INVALID_TOKEN_ID))
{ {
// write("Reducing rule ", reduces[i].rule, ", rule set ", reduces[i].rule_set, " lookahead "); // write("Reducing rule ", reduces[i].rule, ", rule set ", reduces[i].rule_set, " lookahead ");
// if (token < _TOKEN_COUNT) // if (token != INVALID_TOKEN_ID)
// { // {
// writeln(token_names[token]); // writeln(token_names[token]);
// } // }

View File

@ -30,6 +30,10 @@ class Propane
@ptypes["default"] @ptypes["default"]
end end
def invalid_token_id
@tokens.size
end
private private
def parse_grammar! def parse_grammar!

View File

@ -62,7 +62,7 @@ class Propane
reduce_entries = reduce_entries =
case ra = item_set.reduce_actions case ra = item_set.reduce_actions
when Rule when Rule
[{token_id: @grammar.tokens.size, rule_id: ra.id, [{token_id: @grammar.invalid_token_id, rule_id: ra.id,
rule_set_id: ra.rule_set.id, n_states: ra.components.size}] rule_set_id: ra.rule_set.id, n_states: ra.components.size}]
when Hash when Hash
ra.map do |token, rule| ra.map do |token, rule|