Skip to main content

Parser

Struct Parser 

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

Parser state: a cursor over a token stream.

Implementations§

Source§

impl Parser

Source

pub fn new(tokens: Vec<Token>) -> Self

Create a new parser from a token stream, starting at position 0.

Source

pub fn from_tokens_at(tokens: Vec<Token>, pos: usize) -> Self

Create a parser starting at a specific position in the token stream.

Use this to resume parsing after handing the token stream to another parser (e.g., a downstream DSL parser that calls fabula’s parser for pattern sections).

Source

pub fn pos(&self) -> usize

Current cursor position in the token stream.

Source

pub fn into_inner(self) -> (Vec<Token>, usize)

Consume the parser, returning the token stream and cursor position.

Use this to recover the tokens after parsing a section, so a downstream DSL can continue parsing from where fabula left off.

Source

pub fn parse_document(&mut self) -> Result<Document, ParseError>

Parse a complete document (patterns, graphs, and compose directives).

Source

pub fn parse_pattern_only(&mut self) -> Result<PatternAst, ParseError>

Parse a single pattern declaration, then assert EOF.

Source

pub fn parse_graph_only(&mut self) -> Result<GraphAst, ParseError>

Parse a single graph declaration, then assert EOF.

Source

pub fn parse_pattern(&mut self) -> Result<PatternAst, ParseError>

Parse a full pattern name { ... } declaration.

Source

pub fn parse_pattern_body(&mut self) -> Result<PatternBody, ParseError>

Parse the body of a pattern — stages, negations, and temporal constraints — without the pattern name { } wrapper.

Stops when it sees } or EOF but does not consume the closing brace. The caller owns the block structure and is responsible for consuming the delimiter.

This is the primary composability entry point for downstream DSLs that embed fabula pattern syntax in their own blocks:

// salience-dsl example
parser.expect_ident()?;             // "precondition"
parser.expect(TokenKind::LBrace)?;  // {
let body = parser.parse_pattern_body()?;
parser.expect(TokenKind::RBrace)?;  // }
let pattern = compile_pattern_body_with("name", &body, &mapper)?;
Source

pub fn parse_stage(&mut self) -> Result<StageAst, ParseError>

Parse a stage anchor { clauses... } block.

Source

pub fn parse_negation(&mut self) -> Result<NegationAst, ParseError>

Parse an unless [between|after] ... { clauses } negation block.

Source

pub fn parse_temporal(&mut self) -> Result<TemporalAst, ParseError>

Parse a temporal left relation right [gap range] constraint.

Source

pub fn parse_compose(&mut self) -> Result<ComposeAst, ParseError>

Parse a compose name = ... directive.

Source

pub fn parse_clause(&mut self) -> Result<ClauseAst, ParseError>

Parse a single clause: [!] [?]source.label = | -> | < | > | <= | >= target.

Source

pub fn parse_graph(&mut self) -> Result<GraphAst, ParseError>

Parse a graph { ... } declaration.

Source

pub fn peek(&self) -> &Token

Peek at the current token without advancing.

Source

pub fn advance(&mut self) -> &Token

Advance the cursor and return the consumed token.

Source

pub fn at_eof(&self) -> bool

Check if the cursor is at the end of the token stream.

Source

pub fn check(&self, kind: TokenKind) -> bool

Check if the current token matches the given kind (by discriminant).

Source

pub fn expect(&mut self, expected: TokenKind) -> Result<&Token, ParseError>

Expect the current token to be of the given kind, advance, and return it.

Source

pub fn expect_ident(&mut self) -> Result<String, ParseError>

Expect and consume an identifier token. Some keywords are allowed as identifiers in certain positions (between, after, compose, sharing).

Source

pub fn expect_ident_or_string(&mut self) -> Result<String, ParseError>

Expect and consume an identifier or string literal token.

Source

pub fn expect_number(&mut self) -> Result<f64, ParseError>

Expect and consume a number literal token, with optional leading -.

Source

pub fn error(&self, msg: &str) -> ParseError

Create a parse error at the current token position.

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<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, 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.