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: usizeA 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: SPPFNodeIndexA pointer to where in the SPPF we currently are.
C_n in the original paper.
sppf_root: SPPFNodeIndexA simple pointer to $ for comparison purposes.
errors: Vec<GLLError<'a>>All the errors
Implementations§
Source§impl<'a> GLLState<'a>
impl<'a> GLLState<'a>
Sourcepub fn init(
input: &'a [u8],
label_map: LabelMap<'a>,
rule_map: RuleMap<'a>,
regex_map: RegexMap<'a>,
) -> ImplementationResult<'a, Self>
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.
Sourcepub fn create(
&mut self,
slot: &Rc<GrammarSlot<'a>>,
args: AttributeMap<'a>,
) -> ImplementationResult<'a, NodeIndex<DefaultIx>>
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.
Sourcepub fn get_node_p(
&mut self,
slot: Rc<GrammarSlot<'a>>,
left: SPPFNodeIndex,
right: SPPFNodeIndex,
context_pointer: NodeIndex<DefaultIx>,
gss_cycle: bool,
) -> ImplementationResult<'a, SPPFNodeIndex>
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.
Sourcepub fn get_node_t(
&mut self,
terminal: &'a [u8],
left: usize,
right: usize,
) -> SPPFNodeIndex
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.
Sourcepub fn get_current_gss_node(&self) -> ImplementationResult<'a, &Rc<GSSNode<'a>>>
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.
Sourcepub fn get_current_sppf_node(&self) -> ImplementationResult<'a, &SPPFNode<'a>>
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.
Sourcepub fn add(
&mut self,
slot: Rc<GrammarSlot<'a>>,
g: NodeIndex<DefaultIx>,
i: usize,
s: SPPFNodeIndex,
context_pointer: NodeIndex<DefaultIx>,
)
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.
Sourcepub fn pop(
&mut self,
ret_vals: &ReturnMap<'a>,
attrs: AttributeMap<'a>,
) -> ImplementationResult<'a, ()>
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_pointeri=>self.input_pointerz=>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.
Sourcepub fn next(&mut self, bytes: Terminal<'a>) -> ParseResult<'a, ()>
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.
Sourcepub fn has_next(&mut self, bytes: Terminal<'a>) -> bool
pub fn has_next(&mut self, bytes: Terminal<'a>) -> bool
Check if the following bytes can be consumed, but do not consume them.
Sourcepub fn has_regex(&self, pattern: &'a str) -> GLLResult<'a, bool>
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.
Sourcepub fn regex_bytes(
&self,
pattern: &'a str,
) -> GLLResult<'a, Option<Terminal<'a>>>
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.
Sourcepub fn current_byte(&self) -> &[u8] ⓘ
pub fn current_byte(&self) -> &[u8] ⓘ
Get the current input byte for the state
Sourcepub fn test_next(&mut self, label: &GLLBlockLabel<'a>) -> GLLResult<'a, bool>
pub fn test_next(&mut self, label: &GLLBlockLabel<'a>) -> GLLResult<'a, bool>
Sourcepub fn get_rule(
&self,
ident: &'a str,
) -> ImplementationResult<'a, Rc<Vec<Ident>>>
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.
Sourcepub fn get_label(&self, ident: &Ident) -> GLLBlockLabel<'a>
pub fn get_label(&self, ident: &Ident) -> GLLBlockLabel<'a>
Sourcepub fn get_label_by_uuid(
&self,
label: &'a str,
) -> ImplementationResult<'a, GLLBlockLabel<'a>>
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.
Sourcepub fn get_regex_automaton(
&self,
regex: &'a str,
) -> ImplementationResult<'a, Rc<RegexTerminal<'a>>>
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.
Sourcepub fn get_attribute(
&self,
i: AttributeKey,
) -> ImplementationResult<'a, &Value<'a>>
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.
Sourcepub fn restore_attribute(
&self,
i: AttributeKey,
) -> ImplementationResult<'a, &Value<'a>>
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.
Sourcepub fn get_ret_val(
&self,
i: AttributeKey,
) -> ImplementationResult<'a, Option<&Value<'a>>>
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.
Sourcepub fn main(&mut self)
pub fn main(&mut self)
Run the parsing process.
Once this has finished running, we either completed parsing or ran into an error somewhere.
Sourcepub fn print_sppf_dot(
&mut self,
crop: bool,
math_mode: bool,
) -> ImplementationResult<'a, String>
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.
Sourcepub fn print_gss_dot(&self, math_mode: bool) -> ImplementationResult<'a, String>
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.
Sourcepub fn final_accepts(&mut self) -> bool
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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