Skip to main content

GLLState

Struct GLLState 

Source
pub struct GLLState<'a> {
    pub input_pointer: usize,
    pub gss_pointer: NodeIndex<DefaultIx>,
    pub sppf_pointer: SPPFNodeIndex,
    pub sppf_root: SPPFNodeIndex,
    pub errors: Vec<GLLError<'a>>,
    /* private fields */
}
Expand description

The state object for the GLL parse process.

This object handles the bulk of the GLL parsing. It runs the code for the labels as needed, keeps track of the GSS and the SPPF, holds the common methods etc.

§Example

use wagon_gll::{GLLState, ParseResult, ROOT_UUID, Label, GLLBlockLabel, value::Value, LabelMap, RuleMap, RegexMap, ImplementationResult, GLLResult};
use wagon_ident::Ident;
use std::rc::Rc;
#[derive(Debug)]
struct Root;
impl<'a> Label<'a> for Root {
}
    let mut l_map: LabelMap = HashMap::new();
    let mut r_map: RuleMap = HashMap::new();
    let mut regex_map: RegexMap = HashMap::new();
    let root_label = Rc::new(Root{});
    let root_rule = Rc::new(vec![]);
    l_map.insert(ROOT_UUID, root_label);
    r_map.insert(ROOT_UUID, root_rule);
    let input = "".as_bytes();
    let mut state = GLLState::init(input, l_map, r_map, regex_map)?;
    state.main();

Fields§

§input_pointer: usize

A pointer to where in the input we currently are.

C_i in the original paper.

§gss_pointer: NodeIndex<DefaultIx>

A pointer to where in the GSS we currently are.

C_u in the original paper.

§sppf_pointer: SPPFNodeIndex

A pointer to where in the SPPF we currently are.

C_n in the original paper.

§sppf_root: SPPFNodeIndex

A simple pointer to $ for comparison purposes.

§errors: Vec<GLLError<'a>>

All the errors

Implementations§

Source§

impl<'a> GLLState<'a>

Source

pub fn init( input: &'a [u8], label_map: LabelMap<'a>, rule_map: RuleMap<'a>, regex_map: RegexMap<'a>, ) -> ImplementationResult<'a, Self>

Initialize the state.

Takes the input data as a byte-array. As well as a mapping of specific Label::uuid to the associated label and another mapping of a uuid to a specific rule.

§Errors

Returns GLLImplementationError::MissingRoot if no data was found in the label_map or rule_map for ROOT_UUID.

Source

pub fn create( &mut self, slot: &Rc<GrammarSlot<'a>>, args: AttributeMap<'a>, ) -> ImplementationResult<'a, NodeIndex<DefaultIx>>

Create a new GSS node.

This is the create method in the original paper. The arguments to that method are mapped as follows:

  • L => slot.
  • u => self.gss_pointer.
  • i => self.input_pointer.
  • w => self.sppf_pointer.

Differently from the paper, this method also takes a list of attributes that are passed along to the GSS.

§Errors

Returns a GLLParseError if something unexpected happens.

Source

pub fn get_node_p( &mut self, slot: Rc<GrammarSlot<'a>>, left: SPPFNodeIndex, right: SPPFNodeIndex, context_pointer: NodeIndex<DefaultIx>, gss_cycle: bool, ) -> ImplementationResult<'a, SPPFNodeIndex>

Find or create an SPPFNode::Packed.

This is get_node_p from the original paper. Differently from that paper, this also takes a context_pointer, which tells the intermediate node we are retrieving/creating where it can find it’s context.

§Errors

Returns an error either because something is inexplicably missing in one of the state datastructures, or because the weight evaluation failed.

Source

pub fn get_node_t( &mut self, terminal: &'a [u8], left: usize, right: usize, ) -> SPPFNodeIndex

Find or create an SPPFNode::Symbol.

get_node_t from the original paper.

Source

pub fn get_current_gss_node(&self) -> ImplementationResult<'a, &Rc<GSSNode<'a>>>

Get the GSSNode self.gss_pointer is currently pointing to.

§Errors

Returns GLLImplementationError::MissingGSSNode if for some inexplicable reason the node does not exist.

Source

pub fn get_current_sppf_node(&self) -> ImplementationResult<'a, &SPPFNode<'a>>

Get the SPPFNode self.sppf_pointer is currently pointing to.

§Errors

Returns GLLImplementationError::MissingSPPFNode if for some inexplicable reason the node does not exist.

Source

pub fn add( &mut self, slot: Rc<GrammarSlot<'a>>, g: NodeIndex<DefaultIx>, i: usize, s: SPPFNodeIndex, context_pointer: NodeIndex<DefaultIx>, )

Add a new slot to the self.visited and self.todo sets.

add from the original paper.

Source

pub fn pop( &mut self, ret_vals: &ReturnMap<'a>, attrs: AttributeMap<'a>, ) -> ImplementationResult<'a, ()>

Pop context back after a non-terminal was parsed.

pop from the original paper. The arguments to that method are mapped as follows:

  • u => self.gss_pointer
  • i => self.input_pointer
  • z => self.sppf_pointer

Additionally, this method takes a list of attributes that are returned after the non-terminal was parsed.

§Errors

Returns an error for the same reasons as GLLState::get_node_p.

Source

pub fn next(&mut self, bytes: Terminal<'a>) -> ParseResult<'a, ()>

Consume the following bytes from the input string.

If the bytes we just consumed are not the expected bytes, we return an error.

If no error is returned, we move self.input_pointer forward as much as needed.

§Errors

Returns either a GLLParseError::TooLong or GLLParseError::UnexpectedByte depending on the expected bytes and state of the input.

Source

pub fn has_next(&mut self, bytes: Terminal<'a>) -> bool

Check if the following bytes can be consumed, but do not consume them.

Source

pub fn next_regex( &mut self, pattern: &'a str, ) -> GLLResult<'a, Option<Terminal<'a>>>

Check if the given regex is accepting.

If it is, we move the pointer forwards and return the accepted bytes. If no bytes are accepted, we return None.

§Errors

Returns an error if the regex completely fails to build.

Source

pub fn has_regex(&self, pattern: &'a str) -> GLLResult<'a, bool>

Check if the following pattern can be matched, but do not consume the resulting bytes.

§Errors

Returns an error if the regex completely fails to build.

Source

pub fn regex_bytes( &self, pattern: &'a str, ) -> GLLResult<'a, Option<Terminal<'a>>>

Get the bytes matched by the pattern based on where the current input pointer is, but do not consume these bytes.

Similar to GLLState::next_regex but without consuming bytes.

§Errors

Returns an error if the regex completely fails to build.

Source

pub fn current_byte(&self) -> &[u8]

Get the current input byte for the state

Source

pub fn test_next(&mut self, label: &GLLBlockLabel<'a>) -> GLLResult<'a, bool>

Check if, given the current state, the Label’s first-follow set is accepting.

§Errors

Returns an error if something goes wrong during the first checking.

Source

pub fn get_rule( &self, ident: &'a str, ) -> ImplementationResult<'a, Rc<Vec<Ident>>>

Get a specific rule by its uuid.

§Errors

Returns a GLLImplementationError::UnknownRule if the rule does not exist.

Source

pub fn get_label(&self, ident: &Ident) -> GLLBlockLabel<'a>

Get a specific Label as identified by the given Ident.

Source

pub fn get_label_by_uuid( &self, label: &'a str, ) -> ImplementationResult<'a, GLLBlockLabel<'a>>

Get a specific Label by its uuid.

§Errors

Returns a GLLImplementationError::UnknownLabel if the label can not be found.

Source

pub fn get_regex_automaton( &self, regex: &'a str, ) -> ImplementationResult<'a, Rc<RegexTerminal<'a>>>

Get a specific RegexTerminal by its pattern.

This differs from Self::get_label_by_uuid in that it specifically returns a RegexTerminal, as opposed to some trait object.

§Errors

Returns a GLLImplementationError::UnknownLabel if the dfa can not be found.

Source

pub fn get_attribute( &self, i: AttributeKey, ) -> ImplementationResult<'a, &Value<'a>>

Get an attribute from the node pointed at by self.gss_pointer.

§Errors

Returns a GLLImplementationError::MissingAttribute if the ith attribute was never passed.

Source

pub fn restore_attribute( &self, i: AttributeKey, ) -> ImplementationResult<'a, &Value<'a>>

Get an attribute from the node pointed at by self.context_pointer.

§Errors

Returns a GLLImplementationError::MissingContext if the ith attribute is not in context.

Source

pub fn get_ret_val( &self, i: AttributeKey, ) -> ImplementationResult<'a, Option<&Value<'a>>>

Get an attribute from the return arguments at the node currently pointed to by self.sppf_pointer.

§Errors

Returns a GLLImplementationError::MissingSPPFNode if GLLState::sppf_pointer inexplicably points at a non-existant SPPF node.

Source

pub fn main(&mut self)

Run the parsing process.

Once this has finished running, we either completed parsing or ran into an error somewhere.

Source

pub fn print_sppf_dot( &mut self, crop: bool, math_mode: bool, ) -> ImplementationResult<'a, String>

Print current SPPF graph in graphviz format

§Errors

Returns a GLLImplementationError::Utf8Error if there is non-utf8 data anywhere in the SPPF.

Source

pub fn print_gss_dot(&self, math_mode: bool) -> ImplementationResult<'a, String>

Print current GSS graph in graphviz format

§Errors

Return a GLLImplementationError::Fatal if it is unable to find an SPPF node stored on an edge.

Source

pub fn accepts(&self) -> bool

Checks whether the current parser state has accepted the string

Source

pub fn final_accepts(&mut self) -> bool

Checks whether the parser is accepting. If it isn’t, and no errors were encountered, add an error.

This is the method you should use at the end to fully confirm whether the state is accepting.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for GLLState<'a>

§

impl<'a> !Send for GLLState<'a>

§

impl<'a> !Sync for GLLState<'a>

§

impl<'a> !UnwindSafe for GLLState<'a>

§

impl<'a> Freeze for GLLState<'a>

§

impl<'a> Unpin for GLLState<'a>

§

impl<'a> UnsafeUnpin for GLLState<'a>

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<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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.