SymbolTable

Struct SymbolTable 

Source
pub struct SymbolTable { /* private fields */ }
Expand description

Stores the names of the terminal and nonterminal symbols when building a parser.

Terminals are defined in the lexicon and don’t change. They have two parts to their name:

  • the identifier in the lexicon
  • the value, which is the source string they represent (optional)

For example:

Plus : '+';
...
ID    : [a-zA-Z][a-zA-Z_0-9]*;

If Arrow’s token ID is 0 and ID’s is 24,

t[0] = ("Plus".to_string(), Some("+".to_string()));
t[24] = ("ID".to_string(), None);

They’re added to the symbol table with add_terminal().

Nonterminals are defined in the grammar, and possibly completed by new ones when the rules are adapted to the target parser. For example, recursive rules are transformed for LL(1) parsers, which usually adds extra rules.

expr: expr Plus term | term;

If expr is 0 and term is 1,

nt[0] = "expr".to_string();
nt[1] = "term".to_string();

They’re added with add_nonterminal.

The new rules are called “children” of the transformed rules, and can be added with add_child_nonterminal(). The name is the parent’s name followed by “_”. For example, adding a child to nonterminal 0 creates

nt[2] = "expr_1".to_string()

Implementations§

Source§

impl SymbolTable

Source

pub fn new() -> Self

Source

pub fn to_fixed_sym_table(self) -> FixedSymTable

Source

pub fn add_terminal<T: Into<String>>( &mut self, name: T, value_maybe: Option<T>, ) -> TokenId

Source

pub fn extend_terminals<I: IntoIterator<Item = (T, Option<T>)>, T: Into<String>>( &mut self, terminals: I, )

Source

pub fn get_terminals(&self) -> impl Iterator<Item = &(String, Option<String>)>

Source

pub fn get_num_t(&self) -> usize

Source

pub fn set_t_value(&mut self, token: TokenId, name_maybe: Option<String>)

Source

pub fn downsize_num_t(&mut self, num_t: usize)

Source

pub fn add_nonterminal<T: Into<String>>(&mut self, name: T) -> VarId

Source

pub fn add_child_nonterminal(&mut self, var: VarId) -> VarId

Source

pub fn extend_nonterminals<I: IntoIterator<Item = T>, T: Into<String>>( &mut self, nonterminals: I, )

Source

pub fn get_nonterminals(&self) -> impl Iterator<Item = &String>

Source

pub fn get_num_nt(&self) -> usize

Source

pub fn remove_nonterminal(&mut self, v: VarId)

Source

pub fn set_nt_name(&mut self, var: VarId, name: String)

Source

pub fn remove_nt_name(&mut self, var: VarId) -> String

Removes the name assigned to NT var and returns it. Internally, the name of the NT is replaced by another unique string. The NT is expected to be removed later.

Source

pub fn gen_source_code_t( &self, indent: usize, has_enum: bool, has_array: bool, ) -> String

Trait Implementations§

Source§

impl Clone for SymbolTable

Source§

fn clone(&self) -> SymbolTable

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SymbolTable

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SymbolTable

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl SymInfoTable for SymbolTable

Source§

fn is_token_data(&self, token: TokenId) -> bool

Does Symbol::T(token) hold lexer string data? Read more
Source§

fn is_symbol_t_data(&self, symbol: &Symbol) -> bool

Is symbol a terminal holding lexer string data? Read more
Source§

fn is_symbol_t_fixed(&self, symbol: &Symbol) -> bool

Source§

fn get_t_str(&self, token: TokenId) -> String

Source§

fn get_t_name(&self, token: TokenId) -> String

Source§

fn get_nt_name(&self, var: VarId) -> String

Source§

fn get_name(&self, symbol: &Symbol) -> String

Gets the symbol’s name: the nonterminal identifier, the terminal identifier, or “ε”, “$”, …
Source§

fn get_str(&self, symbol: &Symbol) -> String

Gets the symbol’s representation string: the nonterminal identifier, the terminal string value (if it exists), or “ε”, “$”, …
Source§

fn get_name_quote(&self, symbol: &Symbol) -> String

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<S> BuildFrom<S> for S

Source§

fn build_from(source: S) -> S

Converts to this type from the input type.
Source§

impl<S, T> BuildInto<T> for S
where T: BuildFrom<S>,

Source§

fn build_into(self) -> T

Calls T::from(self) to convert a [S] into a [T].

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<S, T> TryBuildInto<T> for S
where T: TryBuildFrom<S>,

Source§

type Error = <T as TryBuildFrom<S>>::Error

The type returned in the event of a conversion error.
Source§

fn try_build_into(self) -> Result<T, <T as TryBuildFrom<S>>::Error>

Performs the conversion.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.