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)?;
|
self.eval_stmts(&stmts)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -693,11 +693,9 @@ mod tests {
|
||||||
// expressions, because writing out abstract syntax trees by hand
|
// expressions, because writing out abstract syntax trees by hand
|
||||||
// takes forever and is error-prone.
|
// takes forever and is error-prone.
|
||||||
fn eval(env: &mut ExecEnv, src: &str) -> Result<Value, Error> {
|
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
|
// We can assume there won't be any syntax errors in the
|
||||||
// interpreter tests.
|
// interpreter tests.
|
||||||
let ast = parser.parse().unwrap();
|
let ast = crate::parser::parse(src).unwrap();
|
||||||
env.eval_stmts(&ast).map(|()| Value::Nul)
|
env.eval_stmts(&ast).map(|()| Value::Nul)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,14 +10,14 @@ use logos::{Lexer, Logos};
|
||||||
/// Parser structure which holds lexer and metadata
|
/// Parser structure which holds lexer and metadata
|
||||||
///
|
///
|
||||||
/// Make one using [`Parser::new`] function
|
/// Make one using [`Parser::new`] function
|
||||||
pub struct Parser<'source> {
|
struct Parser<'source> {
|
||||||
lexer: Lexer<'source, Token>,
|
lexer: Lexer<'source, Token>,
|
||||||
tdark: bool,
|
tdark: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'source> Parser<'source> {
|
impl<'source> Parser<'source> {
|
||||||
/// Create a new parser from source code
|
/// Create a new parser from source code
|
||||||
pub fn new(source: &'source str) -> Self {
|
fn new(source: &'source str) -> Self {
|
||||||
Self {
|
Self {
|
||||||
lexer: Token::lexer(source),
|
lexer: Token::lexer(source),
|
||||||
tdark: false,
|
tdark: false,
|
||||||
|
@ -27,7 +27,7 @@ impl<'source> Parser<'source> {
|
||||||
/// Start parsing tokens
|
/// Start parsing tokens
|
||||||
///
|
///
|
||||||
/// Loops trough lexer, parses statements, returns AST
|
/// 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![];
|
let mut ast = vec![];
|
||||||
while let Some(token) = self.lexer.next() {
|
while let Some(token) = self.lexer.next() {
|
||||||
match token {
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
mod repl;
|
mod repl;
|
||||||
|
|
||||||
use ablescript::interpret::ExecEnv;
|
use ablescript::interpret::ExecEnv;
|
||||||
use ablescript::parser::Parser;
|
use ablescript::parser::parse;
|
||||||
use clap::{Arg, Command};
|
use clap::{Arg, Command};
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
|
@ -42,8 +42,7 @@ fn main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Parse & evaluate
|
// Parse & evaluate
|
||||||
let mut parser = Parser::new(&source);
|
if let Err(e) = parse(&source).and_then(|ast| {
|
||||||
if let Err(e) = parser.parse().and_then(|ast| {
|
|
||||||
if ast_print {
|
if ast_print {
|
||||||
println!("{:#?}", ast);
|
println!("{:#?}", ast);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use ablescript::interpret::ExecEnv;
|
use ablescript::interpret::ExecEnv;
|
||||||
use ablescript::parser::Parser;
|
use ablescript::parser::parse;
|
||||||
use rustyline::Editor;
|
use rustyline::Editor;
|
||||||
|
|
||||||
pub fn repl(ast_print: bool) {
|
pub fn repl(ast_print: bool) {
|
||||||
|
@ -21,7 +21,7 @@ pub fn repl(ast_print: bool) {
|
||||||
None => readline.to_owned(),
|
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 {
|
if ast_print {
|
||||||
println!("{:#?}", &ast);
|
println!("{:#?}", &ast);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue