Skip to content

Commit

Permalink
Merge pull request trueagi-io#513 from luketpeterson/main
Browse files Browse the repository at this point in the history
Adding `Parser` trait
  • Loading branch information
vsbogd authored Nov 22, 2023
2 parents 21cbf3a + 75a8b47 commit eb8c994
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion c/src/metta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ impl runner_state_t {
pub extern "C" fn runner_state_new_with_parser(metta: *const metta_t, parser: sexpr_parser_t) -> runner_state_t {
let metta = unsafe{ &*metta }.borrow();
let parser = parser.into_inner();
let state = RunnerState::new_with_parser(metta, parser);
let state = RunnerState::new_with_parser(metta, Box::new(parser));
state.into()
}

Expand Down
14 changes: 7 additions & 7 deletions lib/src/metta/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::common::shared::Shared;

use super::*;
use super::space::*;
use super::text::{Tokenizer, SExprParser};
use super::text::{Tokenizer, Parser, SExprParser};
use super::types::validate_atom;

use std::rc::Rc;
Expand Down Expand Up @@ -60,7 +60,7 @@ enum MettaRunnerMode {
pub struct RunnerState<'m, 'i> {
mode: MettaRunnerMode,
metta: &'m Metta,
parser: Option<SExprParser<'i>>,
parser: Option<Box<dyn Parser + 'i>>,
atoms: Option<&'i [Atom]>,
interpreter_state: Option<InterpreterState<'m, DynSpace>>,
results: Vec<Vec<Atom>>,
Expand Down Expand Up @@ -245,8 +245,8 @@ impl Metta {
self.0.settings.borrow().get(key.into()).map(|a| a.to_string())
}

pub fn run(&self, parser: SExprParser) -> Result<Vec<Vec<Atom>>, String> {
let state = RunnerState::new_with_parser(self, parser);
pub fn run(&self, parser: impl Parser) -> Result<Vec<Vec<Atom>>, String> {
let state = RunnerState::new_with_parser(self, Box::new(parser));
state.run_to_completion()
}

Expand Down Expand Up @@ -297,8 +297,8 @@ impl<'m, 'i> RunnerState<'m, 'i> {
results: vec![],
}
}
/// Returns a new RunnerState, for running code from the [SExprParser] with the specified [Metta] runner
pub fn new_with_parser(metta: &'m Metta, parser: SExprParser<'i>) -> Self {
/// Returns a new RunnerState, for running code from the [Parser] with the specified [Metta] runner
pub fn new_with_parser(metta: &'m Metta, parser: Box<dyn Parser + 'i>) -> Self {
let mut state = Self::new(metta);
state.parser = Some(parser);
state
Expand Down Expand Up @@ -345,7 +345,7 @@ impl<'m, 'i> RunnerState<'m, 'i> {

// Get the next atom, and start a new intperpreter
let next_atom = if let Some(parser) = self.parser.as_mut() {
match parser.parse(&self.metta.0.tokenizer.borrow()) {
match parser.next_atom(&self.metta.0.tokenizer.borrow()) {
Ok(atom) => atom,
Err(err) => {
self.mode = MettaRunnerMode::TERMINATE;
Expand Down
16 changes: 16 additions & 0 deletions lib/src/metta/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ impl SyntaxNode {
}
}

/// Implemented on a type that yields atoms to be interpreted as MeTTa code. Typically
/// by parsing source text
pub trait Parser {
fn next_atom(&mut self, tokenizer: &Tokenizer) -> Result<Option<Atom>, String>;
}

impl Parser for SExprParser<'_> {
fn next_atom(&mut self, tokenizer: &Tokenizer) -> Result<Option<Atom>, String> {
self.parse(tokenizer)
}
}

/// Provides a parser for MeTTa code written in S-Expression Syntax
///
/// NOTE: The SExprParser type is short-lived, and can be created cheaply to evaluate a specific block
/// of MeTTa source code.
#[derive(Clone)]
pub struct SExprParser<'a> {
text: &'a str,
Expand Down

0 comments on commit eb8c994

Please sign in to comment.