From e432f62b055d0211c47db2d839692a67d554b943 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 13 Mar 2023 21:56:18 -0400 Subject: [PATCH] Add UserCodeID struct to represent user code IDs --- assets/parser.d.erb | 47 +++++++++++++++++++++++++++++++++++++------- lib/propane/lexer.rb | 2 -- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/assets/parser.d.erb b/assets/parser.d.erb index 9db579d..637ac45 100644 --- a/assets/parser.d.erb +++ b/assets/parser.d.erb @@ -45,12 +45,12 @@ class <%= @classname %> return Token(count); } - bool is_valid() + bool is_valid() const { return token < count; } - bool is_invalid() + bool is_invalid() const { return !is_valid(); } @@ -190,12 +190,41 @@ class <%= @classname %> uint destination; } + private struct UserCodeID + { + enum count = <%= (@grammar.patterns.map(&:code_id).compact.max || -1) + 1%>; + uint user_code_id; + alias user_code_id this; + + @disable this(); + + this(uint user_code_id) + { + this.user_code_id = user_code_id; + } + + static UserCodeID invalid() + { + return UserCodeID(count); + } + + bool is_valid() const + { + return user_code_id < count; + } + + bool is_invalid() const + { + return !is_valid(); + } + } + private struct State { uint transition_table_index; uint n_transitions; uint token; - uint code_id; + UserCodeID code_id; } private struct Mode @@ -215,7 +244,11 @@ class <%= @classname %> State(<%= state_table_entry[:transition_table_index] %>u, <%= state_table_entry[:n_transitions] %>u, <%= state_table_entry[:token] %>u, - <%= state_table_entry[:code_id] %>u), +<% if state_table_entry[:code_id] %> + UserCodeID(<%= state_table_entry[:code_id] %>u)), +<% else %> + UserCodeID.invalid()), +<% end %> <% end %> ]; @@ -284,7 +317,7 @@ class <%= @classname %> * @return Token ID to accept, or _TOKEN_COUNT if the user code does * not explicitly return a token. */ - private uint user_code(uint code_id, string match, Result * result) + private uint user_code(UserCodeID code_id, string match, Result * result) { switch (code_id) { @@ -316,7 +349,7 @@ class <%= @classname %> if (match_info.accepting_state != null) { uint token_to_accept = match_info.accepting_state.token; - if (match_info.accepting_state.code_id != 0xFFFF_FFFFu) + if (match_info.accepting_state.code_id.is_valid()) { uint user_code_token = user_code(match_info.accepting_state.code_id, m_input[m_input_position..(m_input_position + match_info.length)], &result); /* A return of _TOKEN_COUNT from user_code() means @@ -386,7 +419,7 @@ class <%= @classname %> } current_state = dest; if ((states[current_state].token != _TOKEN_COUNT) || - (states[current_state].code_id != 0xFFFF_FFFFu)) + (states[current_state].code_id.is_valid())) { attempt_match_info.accepting_state = &states[current_state]; *match_info = attempt_match_info; diff --git a/lib/propane/lexer.rb b/lib/propane/lexer.rb index 96c2a6f..472d0f8 100644 --- a/lib/propane/lexer.rb +++ b/lib/propane/lexer.rb @@ -37,8 +37,6 @@ class Propane code_id = if state.accepts && state.accepts.code_id state.accepts.code_id - else - 0xFFFF_FFFF end state_table << { transition_table_index: transition_table.size,