forked from asklyphe-public/asklyphe
improve number lexing (temporarily breaking function lexing)
This commit is contained in:
parent
adf97d4b20
commit
cbf35e9746
1 changed files with 19 additions and 15 deletions
|
@ -156,7 +156,8 @@ enum ParseErr {
|
|||
struct Lexer<'a> {
|
||||
data: &'a str,
|
||||
data_ptr: &'a str,
|
||||
idx: usize,
|
||||
// idx: usize,
|
||||
next_by: usize,
|
||||
next_tok: Result<Token, ParseErr>,
|
||||
}
|
||||
|
||||
|
@ -164,20 +165,18 @@ struct Lexer<'a> {
|
|||
impl Lexer<'_> {
|
||||
|
||||
fn new(data: &str) -> Lexer<'_> {
|
||||
let mut n: Lexer = Lexer {data, data_ptr: data, idx: 0, next_tok: Err(ParseErr::Eof)};
|
||||
let mut n: Lexer = Lexer {data, data_ptr: data, next_by: 0, next_tok: Err(ParseErr::Eof)};
|
||||
n.next();
|
||||
debug!("New finished!");
|
||||
n
|
||||
}
|
||||
|
||||
fn _next(&mut self) -> Result<Token, ParseErr> {
|
||||
match self.data.chars().nth(self.idx) {
|
||||
self.data_ptr = &self.data_ptr[self.next_by..];
|
||||
match self.data_ptr.chars().nth(0) {
|
||||
Some(val) => {
|
||||
debug!("lexing char '{}' at idx {}", val, self.idx);
|
||||
// debug!("current char '{}'", self.data.chars().nth(0).unwrap());
|
||||
self.idx += 1;
|
||||
// TODO: make more efficient
|
||||
self.data_ptr = &self.data[self.idx..];
|
||||
debug!("lexing char '{}' at idx {}", val, self.data.chars().count() - self.data_ptr.chars().count());
|
||||
self.next_by = 1;
|
||||
match val {
|
||||
'+' => Ok(Token::Op(Op::Add)),
|
||||
'-' => Ok(Token::Op(Op::Subtract)),
|
||||
|
@ -187,18 +186,20 @@ impl Lexer<'_> {
|
|||
'(' => Ok(Token::Op(Op::LParen)),
|
||||
')' => Ok(Token::Op(Op::RParen)),
|
||||
_ if val.is_whitespace() => self._next(),
|
||||
// TODO: maybe parse '-' as part of number so I can do '1 + -1' and similar
|
||||
_ if val.is_digit(10) => {
|
||||
let start = self.idx - 1;
|
||||
let mut len: usize = 0;
|
||||
self.data_ptr.chars().take_while(|c| c.is_digit(10) || *c == '.').for_each(|_| len += 1);
|
||||
self.next_by += len - 1;
|
||||
|
||||
self.data_ptr.chars().take_while(|c| c.is_digit(10)).for_each(|_| self.idx += 1);//.next().unwrap_or(' ').is_digit(10) {self.idx += 1;}
|
||||
|
||||
match self.data[start..self.idx].parse() {
|
||||
match self.data_ptr[..len].parse() {
|
||||
Ok(val) => Ok(Token::Atom(Atom::Number(val))),
|
||||
Err(e) => Err(ParseErr::Invalid),
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
/* match (&self.data[self.idx - 1..]) {
|
||||
s if s.starts_with(""
|
||||
}
|
||||
let mut l: usize;
|
||||
l = matches(&self.data[self.idx - 1..], "sin");
|
||||
if l != 0 {
|
||||
|
@ -235,13 +236,16 @@ impl Lexer<'_> {
|
|||
self.idx += l;
|
||||
return Ok(Token::Op(Op::Func(Func::SquareRoot)));
|
||||
}
|
||||
|
||||
*/
|
||||
debug!("got invalid char '{}'", val);
|
||||
Err(ParseErr::Invalid)
|
||||
}
|
||||
}
|
||||
}
|
||||
None => Err(ParseErr::Eof),
|
||||
None => {
|
||||
self.next_by = 0;
|
||||
Err(ParseErr::Eof)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue