tiny.smg.tree.runtime.language
Defined in tree.runtime.
API (17)
Actions
Public operations.
Language.aliasesLanguage.entryLanguage.externalValidLanguage.fieldIdLanguage.fieldMapLanguage.hasActionsLanguage.initLanguage.isReservedLanguage.lookupLanguage.modeLanguage.nextStateLanguage.publicSymbolLanguage.symbolMetadataLanguage.symbolName
Types and contracts
Public types and contracts.
Source
Source: tools/smg/src/tree/runtime/language.zig
zig
const std = @import("std");const runtime = @import("root.zig");const abi = runtime.abi;pub const Mode = struct { lex_state: u16, external_lex_state: u16, reserved_word_set_id: u16,};pub const Entry = struct { actions: []const abi.Action, reusable: bool,};pub const Language = struct { raw: *const abi.Language, pub fn init(raw: *const abi.Language) !Language { if (raw.abi_version < 14 or raw.abi_version > 15) return error.UnsupportedGrammarAbi; if (raw.lex_fn == null or raw.parse_actions == null) return error.InvalidGrammar; return .{ .raw = raw }; } pub fn symbolName(self: Language, symbol: abi.Symbol) []const u8 { if (symbol == abi.error_symbol) return "ERROR"; if (symbol >= self.raw.symbol_count + self.raw.alias_count) return ""; return std.mem.span(self.raw.symbol_names.?[symbol]); } pub fn symbolMetadata(self: Language, symbol: abi.Symbol) abi.SymbolMetadata { if (symbol == abi.error_symbol) return .{ .visible = true, .named = true, .supertype = false }; return self.raw.symbol_metadata.?[symbol]; } pub fn publicSymbol(self: Language, symbol: abi.Symbol) abi.Symbol { if (symbol == abi.error_symbol) return symbol; return self.raw.public_symbol_map.?[symbol]; } pub fn fieldId(self: Language, name: []const u8) abi.Field { var id: abi.Field = 1; while (id <= self.raw.field_count) : (id += 1) { const pointer = self.raw.field_names.?[id]; if (@intFromPtr(pointer) == 0) continue; const candidate = std.mem.span(@as([*:0]const u8, @ptrCast(pointer))); switch (std.mem.order(u8, name, candidate)) { .eq => return id, .lt => return 0, .gt => {}, } } return 0; } pub fn mode(self: Language, state: abi.State) Mode { if (self.raw.abi_version < 15) { const modes: [*]const abi.LexMode14 = @ptrCast(@alignCast(self.raw.lex_modes.?)); const value = modes[state]; return .{ .lex_state = value.lex_state, .external_lex_state = value.external_lex_state, .reserved_word_set_id = 0 }; } const modes: [*]const abi.LexMode15 = @ptrCast(@alignCast(self.raw.lex_modes.?)); const value = modes[state]; return .{ .lex_state = value.lex_state, .external_lex_state = value.external_lex_state, .reserved_word_set_id = value.reserved_word_set_id }; } pub fn lookup(self: Language, state: abi.State, symbol: abi.Symbol) u16 { if (state < self.raw.large_state_count) { return self.raw.parse_table.?[@as(usize, state) * self.raw.symbol_count + symbol]; } var index: usize = self.raw.small_parse_table_map.?[state - self.raw.large_state_count]; const table = self.raw.small_parse_table.?; const group_count = table[index]; index += 1; for (0..group_count) |_| { const value = table[index]; const symbol_count = table[index + 1]; index += 2; for (0..symbol_count) |_| { if (table[index] == symbol) return value; index += 1; } } return 0; } pub fn entry(self: Language, state: abi.State, symbol: abi.Symbol) Entry { if (symbol == abi.error_symbol) return .{ .actions = &.{}, .reusable = false }; const index = self.lookup(state, symbol); const values = self.raw.parse_actions.?; const header = values[index].entry; const actions: [*]const abi.Action = @ptrCast(&values[index + 1]); return .{ .actions = actions[0..header.count], .reusable = header.reusable }; } pub fn hasActions(self: Language, state: abi.State, symbol: abi.Symbol) bool { return self.lookup(state, symbol) != 0; } pub fn nextState(self: Language, state: abi.State, symbol: abi.Symbol) abi.State { if (symbol < self.raw.token_count) { const actions = self.entry(state, symbol).actions; if (actions.len == 0) return 0; const action = actions[actions.len - 1]; if (action.type != @backingInt(abi.ActionType.shift)) return 0; return if (action.shift.extra) state else action.shift.state; } return self.lookup(state, symbol); } pub fn aliases(self: Language, production: u16) []const abi.Symbol { if (production == 0 or self.raw.max_alias_sequence_length == 0) return &.{}; const start = @as(usize, production) * self.raw.max_alias_sequence_length; return self.raw.alias_sequences.?[start .. start + self.raw.max_alias_sequence_length]; } pub fn fieldMap(self: Language, production: u16) []const abi.FieldMapEntry { if (self.raw.field_count == 0) return &.{}; const slice = self.raw.field_map_slices.?[production]; return self.raw.field_map_entries.?[slice.index .. slice.index + slice.length]; } pub fn isReserved(self: Language, lexer_mode: Mode, symbol: abi.Symbol) bool { if (lexer_mode.reserved_word_set_id == 0 or self.raw.reserved_words == null) return false; const start = @as(usize, lexer_mode.reserved_word_set_id) * self.raw.max_reserved_word_set_size; for (self.raw.reserved_words.?[start .. start + self.raw.max_reserved_word_set_size]) |word| { if (word == symbol) return true; if (word == 0) return false; } return false; } pub fn externalValid(self: Language, state: u16) []const bool { if (state == 0 or self.raw.external_token_count == 0) return &.{}; const start = @as(usize, state) * self.raw.external_token_count; return self.raw.external_scanner.states.?[start .. start + self.raw.external_token_count]; }};Source: tools/smg/src/tree/runtime/root.zig:2
zig
pub const language = @import("language.zig");Complete caller list for tree.runtime.language.Language.init
8 direct callers.
tiny.smg.tree.runtime.parser.parse[function] attools/smg/src/tree/runtime/parser.zig:50tools.smg.src.tree.runtime.parser.test_focused_active_version_compaction_ranks_precedence_before_stable_identities[function] — test; no exact target attools/smg/src/tree/runtime/parser.zig:472in nearest public ownertiny.smg.tree.runtime.parsertools.smg.src.tree.runtime.parser.test_focused_an_allowed_boundary_merge_frees_the_next_temporary_version[function] — test; no exact target attools/smg/src/tree/runtime/parser.zig:623in nearest public ownertiny.smg.tree.runtime.parsertools.smg.src.tree.runtime.parser.test_focused_equal-precedence_active_version_compaction_retains_the_first_six_identities[function] — test; no exact target attools/smg/src/tree/runtime/parser.zig:428in nearest public ownertiny.smg.tree.runtime.parsertools.smg.src.tree.runtime.parser.test_focused_temporary_overflow_cannot_merge_a_dropped_late_candidate[function] — test; no exact target attools/smg/src/tree/runtime/parser.zig:565in nearest public ownertiny.smg.tree.runtime.parsertools.smg.src.tree.runtime.parser.test_focused_temporary_version_overflow_is_global_and_retains_FIFO_identities[function] — test; no exact target attools/smg/src/tree/runtime/parser.zig:516in nearest public ownertiny.smg.tree.runtime.parsertools.smg.src.tree.runtime.subtree.test_tree_owns_immutable_source_spans[function] — test; no exact target attools/smg/src/tree/runtime/subtree.zig:174in nearest public ownertiny.smg.tree.runtime.subtreetools.smg.src.tree.validation.test_pinned_grammar_terminal_shifts_end_action_entries[function] — test; no exact target attools/smg/src/tree/validation.zig:299in nearest public ownertools.smg.src.tree.validation
Audit
| Definitions | 18 |
|---|---|
| Public names | 18 |
| Members | 6 |
| Version | 26.7.0 |
| Revision | daab053ee433 |