From 5aada3c09e738eb594ed283f98400e833a181df7 Mon Sep 17 00:00:00 2001 From: Erin Date: Mon, 18 Apr 2022 22:09:41 +0200 Subject: [PATCH] Made Parser private and made `parse` function Parser had only consturctor and parse functions public. Likely, there would be just a creation of parser followed by .parse(), and now this action is performed in `parse` function. --- ablescript/src/interpret.rs | 6 ++---- ablescript/src/parser.rs | 11 ++++++++--- ablescript_cli/src/main.rs | 5 ++--- ablescript_cli/src/repl.rs | 4 ++-- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/ablescript/src/interpret.rs b/ablescript/src/interpret.rs index 5fe34c3..b15967d 100644 --- a/ablescript/src/interpret.rs +++ b/ablescript/src/interpret.rs @@ -453,7 +453,7 @@ impl ExecEnv { }); } - let stmts = crate::parser::Parser::new(&code).parse()?; + let stmts = crate::parser::parse(&code)?; self.eval_stmts(&stmts)?; } } @@ -693,11 +693,9 @@ mod tests { // expressions, because writing out abstract syntax trees by hand // takes forever and is error-prone. fn eval(env: &mut ExecEnv, src: &str) -> Result { - let mut parser = crate::parser::Parser::new(src); - // We can assume there won't be any syntax errors in the // interpreter tests. - let ast = parser.parse().unwrap(); + let ast = crate::parser::parse(src).unwrap(); env.eval_stmts(&ast).map(|()| Value::Nul) } diff --git a/ablescript/src/parser.rs b/ablescript/src/parser.rs index 6df7ed5..57cdab2 100644 --- a/ablescript/src/parser.rs +++ b/ablescript/src/parser.rs @@ -10,14 +10,14 @@ use logos::{Lexer, Logos}; /// Parser structure which holds lexer and metadata /// /// Make one using [`Parser::new`] function -pub struct Parser<'source> { +struct Parser<'source> { lexer: Lexer<'source, Token>, tdark: bool, } impl<'source> Parser<'source> { /// Create a new parser from source code - pub fn new(source: &'source str) -> Self { + fn new(source: &'source str) -> Self { Self { lexer: Token::lexer(source), tdark: false, @@ -27,7 +27,7 @@ impl<'source> Parser<'source> { /// Start parsing tokens /// /// Loops trough lexer, parses statements, returns AST - pub fn parse(&mut self) -> Result { + fn parse(&mut self) -> Result { let mut ast = vec![]; while let Some(token) = self.lexer.next() { match token { @@ -605,6 +605,11 @@ impl<'source> Parser<'source> { } } +/// Parse AbleScript into AST +pub fn parse(source: &str) -> Result { + Parser::new(source).parse() +} + #[cfg(test)] mod tests { use super::*; diff --git a/ablescript_cli/src/main.rs b/ablescript_cli/src/main.rs index af9c71a..9d0b276 100644 --- a/ablescript_cli/src/main.rs +++ b/ablescript_cli/src/main.rs @@ -3,7 +3,7 @@ mod repl; use ablescript::interpret::ExecEnv; -use ablescript::parser::Parser; +use ablescript::parser::parse; use clap::{Arg, Command}; use std::process::exit; @@ -42,8 +42,7 @@ fn main() { }; // Parse & evaluate - let mut parser = Parser::new(&source); - if let Err(e) = parser.parse().and_then(|ast| { + if let Err(e) = parse(&source).and_then(|ast| { if ast_print { println!("{:#?}", ast); } diff --git a/ablescript_cli/src/repl.rs b/ablescript_cli/src/repl.rs index 75bd3ef..0ea239f 100644 --- a/ablescript_cli/src/repl.rs +++ b/ablescript_cli/src/repl.rs @@ -1,5 +1,5 @@ use ablescript::interpret::ExecEnv; -use ablescript::parser::Parser; +use ablescript::parser::parse; use rustyline::Editor; pub fn repl(ast_print: bool) { @@ -21,7 +21,7 @@ pub fn repl(ast_print: bool) { None => readline.to_owned(), }; - partial = match Parser::new(&partial_data).parse().and_then(|ast| { + partial = match parse(&partial_data).and_then(|ast| { if ast_print { println!("{:#?}", &ast); }