Parser

Struct Parser 

Source
pub struct Parser<'a, 'ctx, 'int> { /* private fields */ }
Expand description

Recursive descent parser for natural language to first-order logic.

The parser transforms a token stream (from Lexer) into logical expressions (LogicExpr). It handles complex linguistic phenomena including:

  • Quantifier scope ambiguity
  • Pronoun resolution via DRS
  • Modal verb interpretation
  • Temporal and aspectual marking
  • VP ellipsis resolution

§Lifetimes

  • 'a: Arena lifetime for allocated AST nodes
  • 'ctx: WorldState lifetime for discourse tracking
  • 'int: Interner lifetime for symbol management

Implementations§

Source§

impl<'a, 'ctx, 'int> Parser<'a, 'ctx, 'int>

Source

pub fn new( tokens: Vec<Token>, world_state: &'ctx mut WorldState, interner: &'int mut Interner, ctx: AstContext<'a>, types: TypeRegistry, ) -> Self

Create a parser with WorldState for discourse-level parsing. WorldState is REQUIRED - there is no “single sentence mode”. A single sentence is just a discourse of length 1.

Source

pub fn set_discourse_event_var(&mut self, var: Symbol)

Source

pub fn drs_mut(&mut self) -> &mut Drs

Get mutable reference to the active DRS (from WorldState).

Source

pub fn drs_ref(&self) -> &Drs

Get immutable reference to the active DRS (from WorldState).

Source

pub fn swap_drs_with_world_state(&mut self)

Swap DRS between Parser and WorldState. Call at start of parsing to get the accumulated DRS from WorldState. Call at end of parsing to save the updated DRS back to WorldState.

Source

pub fn has_world_state(&self) -> bool

WorldState is always present (no “single sentence mode”)

Source

pub fn mode(&self) -> ParserMode

Source

pub fn is_known_type(&self, sym: Symbol) -> bool

Check if a symbol is a known type in the registry. Used to disambiguate “Stack of Integers” (generic type) vs “Owner of House” (possessive).

Source

pub fn is_generic_type(&self, sym: Symbol) -> bool

Check if a symbol is a known generic type (takes type parameters). Used to parse “Stack of Integers” as generic instantiation.

Source

pub fn process_block_headers(&mut self)

Source

pub fn get_event_var(&mut self) -> Symbol

Source

pub fn capture_event_template( &mut self, verb: Symbol, roles: &[(ThematicRole, Term<'a>)], modifiers: &[Symbol], )

Source

pub fn set_pp_attachment_mode(&mut self, attach_to_noun: bool)

Source

pub fn set_noun_priority_mode(&mut self, mode: bool)

Source

pub fn set_collective_mode(&mut self, mode: bool)

Source

pub fn set_event_reading_mode(&mut self, mode: bool)

Source

pub fn set_negative_scope_mode(&mut self, mode: NegativeScopeMode)

Source

pub fn set_modal_preference(&mut self, pref: ModalPreference)

Source

pub fn guard(&mut self) -> ParserGuard<'_, 'a, 'ctx, 'int>

Source

pub fn parse(&mut self) -> Result<&'a LogicExpr<'a>, ParseError>

Parses the token stream into a logical expression.

This is the main entry point for declarative/FOL parsing. It handles multi-sentence inputs by conjoining them with logical AND, and processes various sentence types including declaratives, questions, and imperatives.

§Returns

An arena-allocated LogicExpr representing the parsed input, or a ParseError with source location and Socratic explanation.

§Discourse State

The parser maintains discourse state across sentences, enabling anaphora resolution (“he”, “she”, “they” refer to prior entities) and temporal coherence (tense interpretation relative to reference time).

Source

pub fn parse_program(&mut self) -> Result<Vec<Stmt<'a>>, ParseError>

Parses a LOGOS program into a list of statements.

This is the main entry point for imperative/executable LOGOS code. It handles block structures (Definition, Policy, Procedure, Theorem), function definitions, type definitions, and executable statements.

§Returns

A vector of arena-allocated Stmt representing the program, or a ParseError with source location and Socratic explanation.

§Block Types
  • Definition: Type definitions (structs, enums, generics)
  • Policy: Security predicates and capability rules
  • Procedure: Executable code blocks
  • Theorem: Logical propositions with proof strategies

Trait Implementations§

Source§

impl<'a, 'ctx, 'int> ClauseParsing<'a, 'ctx, 'int> for Parser<'a, 'ctx, 'int>

Source§

fn parse_either_or(&mut self) -> Result<&'a LogicExpr<'a>, ParseError>

Parse “Either NP1 or NP2 is/are PRED” or “Either S1 or S2”

Handles coordination: “Either Alice or Bob is guilty” should become guilty(Alice) ∨ guilty(Bob), not Alice ∨ guilty(Bob)

Source§

fn parse_gapped_clause( &mut self, borrowed_verb: Symbol, ) -> Result<&'a LogicExpr<'a>, ParseError>

Phase 46: Generalized gapping with template-guided reconstruction. Handles NPs, PPs, temporal adverbs, and preserves roles from EventTemplate.

Source§

fn parse_disjunction(&mut self) -> Result<&'a LogicExpr<'a>, ParseError>

Parse disjunction (Or/Iff) - lowest precedence logical connectives. Calls parse_conjunction for operands to ensure And binds tighter.

Source§

fn parse_conjunction(&mut self) -> Result<&'a LogicExpr<'a>, ParseError>

Parse conjunction (And) - higher precedence than Or. Calls parse_atom for operands.

Source§

fn parse_sentence(&mut self) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a complete sentence, handling imperatives, ellipsis, and questions.
Source§

fn check_wh_word(&self) -> bool

Checks if current token is a wh-word (who, what, which, etc.).
Source§

fn parse_conditional(&mut self) -> Result<&'a LogicExpr<'a>, ParseError>

Parses “if P then Q” conditionals with DRS scope handling.
Source§

fn is_counterfactual_context(&self) -> bool

Returns true if parsing a counterfactual context.
Source§

fn parse_counterfactual_antecedent( &mut self, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses “if P were/had” counterfactual antecedent (subjunctive).
Source§

fn parse_counterfactual_consequent( &mut self, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses “Q would” counterfactual consequent.
Source§

fn extract_verb_from_expr(&self, expr: &LogicExpr<'a>) -> Option<Symbol>

Extracts the main verb from an expression.
Source§

fn is_complete_clause(&self, expr: &LogicExpr<'a>) -> bool

Returns true if expression is a complete clause.
Source§

fn parse_relative_clause( &mut self, gap_var: Symbol, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses “who/that/which” relative clauses attaching to noun phrases.
Source§

fn check_ellipsis_auxiliary(&self) -> bool

Checks for ellipsis auxiliary (did, does, can, etc.).
Source§

fn check_ellipsis_terminator(&self) -> bool

Checks for ellipsis terminator (too, also, as well).
Source§

fn try_parse_ellipsis( &mut self, ) -> Option<Result<&'a LogicExpr<'a>, ParseError>>

Attempts to parse VP ellipsis (“Mary did too”).
Source§

impl<'a, 'ctx, 'int> LogicVerbParsing<'a, 'ctx, 'int> for Parser<'a, 'ctx, 'int>

Source§

fn build_group_predicate( &mut self, subjects: &[Symbol], verb: Symbol, verb_time: Time, ) -> &'a LogicExpr<'a>

Build a group predicate for intransitive verbs

Source§

fn build_group_transitive( &mut self, subjects: &[Symbol], objects: &[Symbol], verb: Symbol, verb_time: Time, ) -> &'a LogicExpr<'a>

Build a transitive predicate with group subject and group object

Source§

fn parse_predicate_with_subject( &mut self, subject_symbol: Symbol, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a verb phrase given the subject as a constant symbol.
Source§

fn parse_predicate_with_subject_as_var( &mut self, subject_symbol: Symbol, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a verb phrase with subject as a bound variable.
Source§

fn try_parse_plural_subject( &mut self, first_subject: &NounPhrase<'a>, ) -> Result<Option<&'a LogicExpr<'a>>, ParseError>

Attempts to parse a plural subject (“John and Mary verb”). Returns Ok(Some(expr)) on success, Ok(None) if not plural, Err on semantic error.
Source§

fn parse_control_structure( &mut self, subject: &NounPhrase<'a>, verb: Symbol, verb_time: Time, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses control verb structures: “wants to VP”, “persuaded X to VP”.
Source§

fn is_control_verb(&self, verb: Symbol) -> bool

Checks if a verb is a control verb (want, try, persuade, etc.).
Source§

impl<'a, 'ctx, 'int> ModalParsing<'a, 'ctx, 'int> for Parser<'a, 'ctx, 'int>

Source§

fn parse_modal(&mut self) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a modal verb and its scope content.
Source§

fn parse_aspect_chain( &mut self, subject_symbol: Symbol, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses perfect/progressive aspect chain with a symbol subject.
Source§

fn parse_aspect_chain_with_term( &mut self, subject_term: Term<'a>, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses perfect/progressive aspect chain with a term subject.
Source§

fn token_to_vector(&self, token: &TokenType) -> ModalVector

Converts a modal token to its semantic vector (domain, force, flavor).
Source§

impl<'a, 'ctx, 'int> NounParsing<'a, 'ctx, 'int> for Parser<'a, 'ctx, 'int>

Source§

fn parse_noun_phrase( &mut self, greedy: bool, ) -> Result<NounPhrase<'a>, ParseError>

Parses a full noun phrase with optional greedy PP attachment.
Source§

fn parse_noun_phrase_for_relative( &mut self, ) -> Result<NounPhrase<'a>, ParseError>

Parses a noun phrase suitable for relative clause antecedent.
Source§

fn noun_phrase_to_term(&self, np: &NounPhrase<'a>) -> Term<'a>

Converts a parsed noun phrase to a first-order term.
Source§

fn check_possessive(&self) -> bool

Checks for possessive marker (’s).
Source§

fn check_of_preposition(&self) -> bool

Checks for “of” preposition (possessive or partitive).
Source§

fn check_proper_name_or_label(&self) -> bool

Checks for proper name or label (capitalized).
Source§

fn check_possessive_pronoun(&self) -> bool

Checks for possessive pronoun (his, her, its, their).
Source§

impl<'a, 'ctx, 'int> PragmaticsParsing<'a, 'ctx, 'int> for Parser<'a, 'ctx, 'int>

Source§

fn parse_focus(&mut self) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a focus particle construction: “only John runs”, “even Mary left”. Read more
Source§

fn parse_measure(&mut self) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a measure construction: “much water is cold”, “little food arrived”. Read more
Source§

fn parse_presupposition( &mut self, subject: &NounPhrase<'a>, presup_kind: PresupKind, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a presupposition-triggering verb: “stopped running”, “regrets leaving”. Read more
Source§

fn parse_predicate_for_subject( &mut self, subject: &NounPhrase<'a>, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a simple predicate for a given subject noun phrase. Read more
Source§

fn parse_scopal_adverb( &mut self, subject: &NounPhrase<'a>, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a scopal adverb construction: “always runs”, “never sleeps”. Read more
Source§

fn parse_superlative( &mut self, subject: &NounPhrase<'a>, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a superlative construction: “is the tallest student”. Read more
Source§

fn parse_comparative( &mut self, subject: &NounPhrase<'a>, _copula_time: Time, difference: Option<&'a Term<'a>>, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a comparative construction: “is taller than Mary”, “is greater than 0”. Read more
Source§

fn check_number(&self) -> bool

Checks if the current token is a numeric literal. Read more
Source§

fn parse_measure_phrase(&mut self) -> Result<&'a Term<'a>, ParseError>

Parses a measure phrase: “5 meters”, “100 kilograms”. Read more
Source§

impl<'a, 'ctx, 'int> QuantifierParsing<'a, 'ctx, 'int> for Parser<'a, 'ctx, 'int>

Source§

fn parse_quantified(&mut self) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a quantified expression from a quantifier determiner.
Source§

fn parse_restriction( &mut self, var_name: Symbol, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses the restrictor clause for a quantifier.
Source§

fn parse_verb_phrase_for_restriction( &mut self, var_name: Symbol, ) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a verb phrase as the nuclear scope of a quantifier.
Source§

fn combine_with_and( &self, exprs: Vec<&'a LogicExpr<'a>>, ) -> Result<&'a LogicExpr<'a>, ParseError>

Combines multiple expressions with conjunction.
Source§

fn wrap_with_definiteness_full( &mut self, np: &NounPhrase<'a>, predicate: &'a LogicExpr<'a>, ) -> Result<&'a LogicExpr<'a>, ParseError>

Source§

fn wrap_with_definiteness( &mut self, definiteness: Option<Definiteness>, noun: Symbol, predicate: &'a LogicExpr<'a>, ) -> Result<&'a LogicExpr<'a>, ParseError>

Source§

fn wrap_with_definiteness_and_adjectives( &mut self, definiteness: Option<Definiteness>, noun: Symbol, adjectives: &[Symbol], predicate: &'a LogicExpr<'a>, ) -> Result<&'a LogicExpr<'a>, ParseError>

Source§

fn wrap_with_definiteness_and_adjectives_and_pps( &mut self, definiteness: Option<Definiteness>, noun: Symbol, adjectives: &[Symbol], pps: &[&'a LogicExpr<'a>], predicate: &'a LogicExpr<'a>, ) -> Result<&'a LogicExpr<'a>, ParseError>

Source§

fn wrap_with_definiteness_for_object( &mut self, definiteness: Option<Definiteness>, noun: Symbol, predicate: &'a LogicExpr<'a>, ) -> Result<&'a LogicExpr<'a>, ParseError>

Source§

fn substitute_pp_placeholder( &mut self, pp: &'a LogicExpr<'a>, var: Symbol, ) -> &'a LogicExpr<'a>

Source§

fn substitute_constant_with_var( &self, expr: &'a LogicExpr<'a>, constant_name: Symbol, var_name: Symbol, ) -> Result<&'a LogicExpr<'a>, ParseError>

Source§

fn substitute_constant_with_var_sym( &self, expr: &'a LogicExpr<'a>, constant_name: Symbol, var_name: Symbol, ) -> Result<&'a LogicExpr<'a>, ParseError>

Source§

fn substitute_constant_with_sigma( &self, expr: &'a LogicExpr<'a>, constant_name: Symbol, sigma_term: Term<'a>, ) -> Result<&'a LogicExpr<'a>, ParseError>

Source§

fn find_main_verb_name(&self, expr: &LogicExpr<'a>) -> Option<Symbol>

Source§

fn transform_cardinal_to_group( &mut self, expr: &'a LogicExpr<'a>, ) -> Result<&'a LogicExpr<'a>, ParseError>

Source§

fn build_verb_neo_event( &mut self, verb: Symbol, subject_var: Symbol, object: Option<Term<'a>>, modifiers: Vec<Symbol>, ) -> &'a LogicExpr<'a>

Source§

impl<'a, 'ctx, 'int> QuestionParsing<'a, 'ctx, 'int> for Parser<'a, 'ctx, 'int>

Source§

fn parse_wh_question(&mut self) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a wh-question: “Who runs?”, “What does John love?”.
Source§

fn parse_yes_no_question(&mut self) -> Result<&'a LogicExpr<'a>, ParseError>

Parses a yes/no question: “Does John run?”, “Is Mary tall?”.
Source§

fn aux_token_to_modal_vector(&self, token: &TokenType) -> ModalVector

Converts an auxiliary token to its modal vector for questions.

Auto Trait Implementations§

§

impl<'a, 'ctx, 'int> Freeze for Parser<'a, 'ctx, 'int>

§

impl<'a, 'ctx, 'int> !RefUnwindSafe for Parser<'a, 'ctx, 'int>

§

impl<'a, 'ctx, 'int> !Send for Parser<'a, 'ctx, 'int>

§

impl<'a, 'ctx, 'int> !Sync for Parser<'a, 'ctx, 'int>

§

impl<'a, 'ctx, 'int> Unpin for Parser<'a, 'ctx, 'int>

§

impl<'a, 'ctx, 'int> !UnwindSafe for Parser<'a, 'ctx, 'int>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V