forked from AbleScript/ablescript
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.
This commit is contained in:
parent
98b72b14e2
commit
5aada3c09e
|
@ -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<Value, Error> {
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Block, Error> {
|
||||
fn parse(&mut self) -> Result<Block, Error> {
|
||||
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<Block, Error> {
|
||||
Parser::new(source).parse()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue