mirror of
https://github.com/rm-dr/daisy
synced 2025-07-05 01:59:30 -07:00
Rewrote parser
This commit is contained in:
@ -1,15 +1,97 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
|
||||
mod tokenize;
|
||||
mod treeify;
|
||||
mod groupify;
|
||||
mod find_subs;
|
||||
|
||||
|
||||
use crate::parser::tokenize::p_tokenize;
|
||||
use crate::parser::groupify::p_groupify;
|
||||
use crate::parser::treeify::p_treeify;
|
||||
use crate::parser::find_subs::p_find_subs;
|
||||
use crate::parser::tokenize::tokenize;
|
||||
use crate::parser::groupify::groupify;
|
||||
use crate::parser::treeify::treeify;
|
||||
use crate::parser::find_subs::find_subs;
|
||||
|
||||
use crate::tokens::LineLocation;
|
||||
use crate::tokens::Token;
|
||||
use crate::tokens::Operator;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
enum PreToken {
|
||||
PreNumber(LineLocation, String),
|
||||
PreWord(LineLocation, String),
|
||||
PreOperator(LineLocation, String),
|
||||
|
||||
PreGroupStart(LineLocation),
|
||||
PreGroupEnd(LineLocation),
|
||||
PreGroup(LineLocation, VecDeque<PreToken>),
|
||||
|
||||
Container(Token)
|
||||
}
|
||||
|
||||
impl PreToken {
|
||||
#[inline(always)]
|
||||
pub fn get_line_location(&self) -> &LineLocation {
|
||||
match self {
|
||||
PreToken::PreNumber(l, _)
|
||||
| PreToken::PreWord(l, _)
|
||||
| PreToken::PreOperator(l, _)
|
||||
| PreToken::PreGroupStart(l)
|
||||
| PreToken::PreGroupEnd(l)
|
||||
| PreToken::PreGroup(l, _)
|
||||
=> l,
|
||||
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn get_mut_line_location(&mut self) -> &mut LineLocation {
|
||||
match self {
|
||||
PreToken::PreNumber(l, _)
|
||||
| PreToken::PreWord(l, _)
|
||||
| PreToken::PreOperator(l, _)
|
||||
| PreToken::PreGroupStart(l)
|
||||
| PreToken::PreGroupEnd(l)
|
||||
| PreToken::PreGroup(l, _)
|
||||
=> l,
|
||||
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn to_token(self) -> Result<Token, (LineLocation, ParserError)>{
|
||||
match self {
|
||||
PreToken::PreNumber(l, s) => {
|
||||
let n = match s.parse() {
|
||||
Ok(n) => n,
|
||||
Err(_) => return Err((l, ParserError::BadNumber))
|
||||
};
|
||||
return Ok(Token::Number(l, n));
|
||||
},
|
||||
PreToken::PreWord(l, s) => {
|
||||
return Ok(match &s[..] {
|
||||
// Mathematical constants
|
||||
"π"|"pi" => { Token::Constant(l, 3.141592653, String::from("pi")) },
|
||||
"e" => { Token::Constant(l, 2.71828, String::from("e")) },
|
||||
"phi"|"φ" => { Token::Constant(l, 1.61803, String::from("phi")) },
|
||||
_ => { return Err((l, ParserError::Undefined(s))); }
|
||||
});
|
||||
}
|
||||
PreToken::Container(v) => { return Ok(v); }
|
||||
|
||||
PreToken::PreOperator(_,_)
|
||||
| PreToken::PreGroupStart(_)
|
||||
| PreToken::PreGroupEnd(_)
|
||||
| PreToken::PreGroup(_, _)
|
||||
=> panic!()
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
use crate::tokens;
|
||||
|
||||
/// Types of parser errors.
|
||||
/// If we cannot parse a string, one of these is returned.
|
||||
@ -53,16 +135,16 @@ impl ParserError {
|
||||
pub fn parse(
|
||||
s: &String
|
||||
) -> Result<
|
||||
tokens::Token,
|
||||
(tokens::LineLocation, ParserError)
|
||||
Token,
|
||||
(LineLocation, ParserError)
|
||||
> {
|
||||
|
||||
let tokens = p_tokenize(s);
|
||||
let (_, tokens) = p_find_subs(tokens);
|
||||
let mut g = p_groupify(tokens)?;
|
||||
g = p_treeify(g)?;
|
||||
let tokens = tokenize(s);
|
||||
let (_, tokens) = find_subs(tokens);
|
||||
let g = groupify(tokens)?;
|
||||
let t = treeify(g)?;
|
||||
|
||||
return Ok(g);
|
||||
return Ok(t);
|
||||
}
|
||||
|
||||
|
||||
@ -70,8 +152,8 @@ pub fn substitute(s: &String) -> String{
|
||||
if s == "" { return s.clone() }
|
||||
let mut new_s = s.clone();
|
||||
|
||||
let tokens = p_tokenize(s);
|
||||
let (subs, _) = p_find_subs(tokens);
|
||||
let tokens = tokenize(s);
|
||||
let (subs, _) = find_subs(tokens);
|
||||
|
||||
for r in subs.iter() {
|
||||
new_s.replace_range(
|
||||
|
Reference in New Issue
Block a user