pub struct Context<P, Data, Start, StateIndex, const MAX_REDUCE_RULES: usize>where
P: Parser<Term = <Data as SemanticValue>::Term, NonTerm = <Data as SemanticValue>::NonTerm, StateIndex = StateIndex>,
Data: SemanticValue,
Start: StartExtractor<Data>,{ /* private fields */ }Expand description
A struct that maintains the current state and the values associated with each symbol. This handles the divergence and merging of the parser.
Implementations§
Source§impl<P, Data, Start, StateIndex, const MAX_REDUCE_RULES: usize> Context<P, Data, Start, StateIndex, MAX_REDUCE_RULES>where
P: Parser<Term = <Data as SemanticValue>::Term, NonTerm = <Data as SemanticValue>::NonTerm, StateIndex = StateIndex>,
Data: SemanticValue,
Start: StartExtractor<Data>,
StateIndex: Index,
impl<P, Data, Start, StateIndex, const MAX_REDUCE_RULES: usize> Context<P, Data, Start, StateIndex, MAX_REDUCE_RULES>where
P: Parser<Term = <Data as SemanticValue>::Term, NonTerm = <Data as SemanticValue>::NonTerm, StateIndex = StateIndex>,
Data: SemanticValue,
Start: StartExtractor<Data>,
StateIndex: Index,
Sourcepub fn new(
userdata: <Data as SemanticValue>::UserData,
) -> Context<P, Data, Start, StateIndex, MAX_REDUCE_RULES>
pub fn new( userdata: <Data as SemanticValue>::UserData, ) -> Context<P, Data, Start, StateIndex, MAX_REDUCE_RULES>
Create a new context.
current_branches is initialized with a root node.
pub fn with_default_userdata() -> Context<P, Data, Start, StateIndex, MAX_REDUCE_RULES>
Sourcepub fn userdata(&self) -> &<Data as SemanticValue>::UserData
pub fn userdata(&self) -> &<Data as SemanticValue>::UserData
Borrow the user data for the first active path.
In GLR mode, each forked branch owns an independently cloned user data value.
Sourcepub fn userdata_all(
&self,
) -> impl Iterator<Item = &<Data as SemanticValue>::UserData>
pub fn userdata_all( &self, ) -> impl Iterator<Item = &<Data as SemanticValue>::UserData>
Borrow the user data for every active path.
In GLR mode, each forked branch owns an independently cloned user data value.
Sourcepub fn userdata_mut(&mut self) -> &mut <Data as SemanticValue>::UserData
pub fn userdata_mut(&mut self) -> &mut <Data as SemanticValue>::UserData
Mutably borrow the user data for the first active path.
In GLR mode, each forked branch owns an independently cloned user data value.
Sourcepub fn userdata_all_mut(
&mut self,
) -> impl Iterator<Item = &mut <Data as SemanticValue>::UserData>
pub fn userdata_all_mut( &mut self, ) -> impl Iterator<Item = &mut <Data as SemanticValue>::UserData>
Mutably borrow the user data for every active path.
In GLR mode, each forked branch owns an independently cloned user data value.
Sourcepub fn debug_check(&self)
pub fn debug_check(&self)
for debugging; checks for memory leak, not freed nodes, etc.
Sourcepub fn states(&self) -> impl Iterator<Item = usize>
pub fn states(&self) -> impl Iterator<Item = usize>
Get current states in every diverged paths.
Sourcepub fn state_stacks(&self) -> impl Iterator<Item = impl Iterator<Item = usize>>
pub fn state_stacks(&self) -> impl Iterator<Item = impl Iterator<Item = usize>>
Get iterators of state stacks in all diverged paths.
Sourcepub fn accept(
&mut self,
) -> Result<(<Start as StartExtractor<Data>>::StartType, <Data as SemanticValue>::UserData), ParseError<<Data as SemanticValue>::Term, <Data as SemanticValue>::Location, <Data as SemanticValue>::ReduceActionError, <Data as SemanticValue>::UserData>>
pub fn accept( &mut self, ) -> Result<(<Start as StartExtractor<Data>>::StartType, <Data as SemanticValue>::UserData), ParseError<<Data as SemanticValue>::Term, <Data as SemanticValue>::Location, <Data as SemanticValue>::ReduceActionError, <Data as SemanticValue>::UserData>>
End this context and return the first successful start symbol and user data pair.
ParseError from EOF leaves branches that only saw NoAction reusable. Successful
acceptance moves accepted branch values out and consumes the context. If every branch is
consumed by reduce-action failure or failed recovery, later feeds or accepts return a
consumed-context error.
Sourcepub fn accept_all(
&mut self,
) -> Result<impl Iterator<Item = (<Start as StartExtractor<Data>>::StartType, <Data as SemanticValue>::UserData)>, ParseError<<Data as SemanticValue>::Term, <Data as SemanticValue>::Location, <Data as SemanticValue>::ReduceActionError, <Data as SemanticValue>::UserData>>
pub fn accept_all( &mut self, ) -> Result<impl Iterator<Item = (<Start as StartExtractor<Data>>::StartType, <Data as SemanticValue>::UserData)>, ParseError<<Data as SemanticValue>::Term, <Data as SemanticValue>::Location, <Data as SemanticValue>::ReduceActionError, <Data as SemanticValue>::UserData>>
End this context and return iterator of the start value and user data from each successful path.
EOF is handled with the same branch lifecycle as a normal feed: branches that only see
NoAction remain reusable, branches that fail during reduce-action replay are consumed, and
successful acceptance moves all accepted branch values out and consumes the context.
Sourcepub fn expected_token(
&self,
) -> (BTreeSet<<P as Parser>::TermClass>, BTreeSet<<P as Parser>::NonTerm>)
pub fn expected_token( &self, ) -> (BTreeSet<<P as Parser>::TermClass>, BTreeSet<<P as Parser>::NonTerm>)
Get next expected (terminals, non-terminals) for current context.
Sourcepub fn expected_token_str<'a>(
&self,
) -> (impl Iterator<Item = &'static str> + use<P, Data, Start, StateIndex, MAX_REDUCE_RULES>, impl Iterator<Item = &'static str> + use<P, Data, Start, StateIndex, MAX_REDUCE_RULES>)
pub fn expected_token_str<'a>( &self, ) -> (impl Iterator<Item = &'static str> + use<P, Data, Start, StateIndex, MAX_REDUCE_RULES>, impl Iterator<Item = &'static str> + use<P, Data, Start, StateIndex, MAX_REDUCE_RULES>)
Same as expected_token(), but returns as printable type.
Sourcepub fn feed(
&mut self,
term: <Data as SemanticValue>::Term,
) -> Result<FeedSuccess<<Data as SemanticValue>::Term, <Data as SemanticValue>::Location, <Data as SemanticValue>::ReduceActionError, <Data as SemanticValue>::UserData>, ParseError<<Data as SemanticValue>::Term, <Data as SemanticValue>::Location, <Data as SemanticValue>::ReduceActionError, <Data as SemanticValue>::UserData>>
pub fn feed( &mut self, term: <Data as SemanticValue>::Term, ) -> Result<FeedSuccess<<Data as SemanticValue>::Term, <Data as SemanticValue>::Location, <Data as SemanticValue>::ReduceActionError, <Data as SemanticValue>::UserData>, ParseError<<Data as SemanticValue>::Term, <Data as SemanticValue>::Location, <Data as SemanticValue>::ReduceActionError, <Data as SemanticValue>::UserData>>
Feed one terminal to parser, and update stacks.
This will use Default::default() for location.
Sourcepub fn feed_location(
&mut self,
term: <P as Parser>::Term,
location: <Data as SemanticValue>::Location,
) -> Result<FeedSuccess<<Data as SemanticValue>::Term, <Data as SemanticValue>::Location, <Data as SemanticValue>::ReduceActionError, <Data as SemanticValue>::UserData>, ParseError<<Data as SemanticValue>::Term, <Data as SemanticValue>::Location, <Data as SemanticValue>::ReduceActionError, <Data as SemanticValue>::UserData>>
pub fn feed_location( &mut self, term: <P as Parser>::Term, location: <Data as SemanticValue>::Location, ) -> Result<FeedSuccess<<Data as SemanticValue>::Term, <Data as SemanticValue>::Location, <Data as SemanticValue>::ReduceActionError, <Data as SemanticValue>::UserData>, ParseError<<Data as SemanticValue>::Term, <Data as SemanticValue>::Location, <Data as SemanticValue>::ReduceActionError, <Data as SemanticValue>::UserData>>
Feed one terminal with location to parser, and update state stack.
Each active GLR branch is first checked with the same CFG simulation used by can_feed.
Branches rejected by that simulation are NoAction branches and are the only branches
eligible for panic-mode recovery. If at least one branch can grammatically shift the
terminal, recovery is not entered; failed sibling branches are returned in
FeedSuccess::errors.
Branch lifecycle for one lookahead:
- A branch that only reaches
NoActionhas not mutated its stack or user data. If no branch succeeds, it is restored intocurrent_branchesand the context remains reusable. - A branch that fails in a reduce action is consumed; its user data moves into
ParseErrorBranch::ReduceAction. - If any branch succeeds, only successful branches remain active. Failed sibling branches
are reported in
FeedSuccess::errors. - If no branch succeeds and no reusable
NoActionbranch remains, the whole context is consumed and later feeds or accepts return a consumed-context error.
Sourcepub fn can_feed(&self, term: &<P as Parser>::Term) -> bool
pub fn can_feed(&self, term: &<P as Parser>::Term) -> bool
Check if term can be feeded to current state.
This does not simulate for reduce action error, or panic mode.
So this function will return false even if term can be shifted as error token,
and will return true if Err variant is returned by reduce_action.
Sourcepub fn can_accept(&self) -> bool
pub fn can_accept(&self) -> bool
Check if current context can be terminated and get the start value.