fix(ast): SelfInc use wrong way to recognize
This commit is contained in:
+30
-55
@@ -1,14 +1,13 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ast::err::ParseError,
|
ast::{err::ParseError, types::{BinaryOp, Expr, ExprValue, IncDecOp, UnaryOp}},
|
||||||
ast::types::{BinaryOp, Expr, ExprValue, IncDecOp, UnaryOp},
|
|
||||||
diagnostic::span::Span,
|
diagnostic::span::Span,
|
||||||
lexer::types::TokenValue,
|
lexer::types::{Token, TokenValue},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{ParseProcessError, Parser};
|
use super::{ParseProcessError, Parser};
|
||||||
|
|
||||||
impl Parser {
|
impl Parser {
|
||||||
fn parse_primary_atom(&mut self) -> Result<Expr, ParseProcessError> {
|
fn parse_primary(&mut self) -> Result<Expr, ParseProcessError> {
|
||||||
let token = self.next().clone();
|
let token = self.next().clone();
|
||||||
match token.value {
|
match token.value {
|
||||||
TokenValue::Ident(name) => {
|
TokenValue::Ident(name) => {
|
||||||
@@ -47,13 +46,6 @@ impl Parser {
|
|||||||
span: Span::from_two(token.span, end_span),
|
span: Span::from_two(token.span, end_span),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
TokenValue::Eof => {
|
|
||||||
self.diagnostics.add_from_frontend_error(
|
|
||||||
ParseError::ExpectedBefore(TokenValue::Eof, "`)`"),
|
|
||||||
token.span,
|
|
||||||
);
|
|
||||||
return Err(ParseProcessError::ErrorInMatch);
|
|
||||||
}
|
|
||||||
_ => {
|
_ => {
|
||||||
let token = self.next();
|
let token = self.next();
|
||||||
self.diagnostics.add_from_frontend_error(
|
self.diagnostics.add_from_frontend_error(
|
||||||
@@ -85,11 +77,6 @@ impl Parser {
|
|||||||
..expr
|
..expr
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
TokenValue::Eof => {
|
|
||||||
self.diagnostics
|
|
||||||
.add_from_frontend_error(ParseError::ExpectedBefore(TokenValue::Eof, "`)`"), expr.span);
|
|
||||||
Err(ParseProcessError::ErrorInMatch)
|
|
||||||
}
|
|
||||||
_ => {
|
_ => {
|
||||||
let token = self.next();
|
let token = self.next();
|
||||||
self.diagnostics.add_from_frontend_error(
|
self.diagnostics.add_from_frontend_error(
|
||||||
@@ -109,8 +96,8 @@ impl Parser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn parse_primary(&mut self) -> Result<Expr, ParseProcessError> {
|
fn parse_postfix(&mut self) -> Result<Expr, ParseProcessError> {
|
||||||
let mut expr = self.parse_primary_atom()?;
|
let mut expr = self.parse_primary()?;
|
||||||
loop {
|
loop {
|
||||||
if self.peek().value == TokenValue::LBracket {
|
if self.peek().value == TokenValue::LBracket {
|
||||||
let start_span = expr.span;
|
let start_span = expr.span;
|
||||||
@@ -122,10 +109,6 @@ impl Parser {
|
|||||||
self.advance(1);
|
self.advance(1);
|
||||||
span
|
span
|
||||||
}
|
}
|
||||||
TokenValue::Eof => {
|
|
||||||
self.diagnostics.add_from_frontend_error(ParseError::ExpectedBefore(TokenValue::Eof, "`]`"), index.span);
|
|
||||||
return Err(ParseProcessError::ErrorInMatch);
|
|
||||||
}
|
|
||||||
_ => {
|
_ => {
|
||||||
let token = self.next();
|
let token = self.next();
|
||||||
self.diagnostics.add_from_frontend_error(
|
self.diagnostics.add_from_frontend_error(
|
||||||
@@ -144,12 +127,12 @@ impl Parser {
|
|||||||
};
|
};
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let op = match (&self.peek().value, &self.peek_n(1).value) {
|
if let TokenValue::PlusPlus | TokenValue::MinusMinus = self.peek().value {
|
||||||
(TokenValue::Plus, TokenValue::Plus) => Some(IncDecOp::Inc),
|
let op = if self.peek().value == TokenValue::PlusPlus {
|
||||||
(TokenValue::Minus, TokenValue::Minus) => Some(IncDecOp::Dec),
|
IncDecOp::Inc
|
||||||
_ => None,
|
} else {
|
||||||
};
|
IncDecOp::Dec
|
||||||
if let Some(op) = op {
|
};
|
||||||
let end_span = self.peek_n(1).span;
|
let end_span = self.peek_n(1).span;
|
||||||
self.advance(2);
|
self.advance(2);
|
||||||
expr = Expr {
|
expr = Expr {
|
||||||
@@ -170,19 +153,6 @@ impl Parser {
|
|||||||
let token = self.peek().clone();
|
let token = self.peek().clone();
|
||||||
match token.value {
|
match token.value {
|
||||||
TokenValue::Plus => {
|
TokenValue::Plus => {
|
||||||
if self.peek_n(1).value == TokenValue::Plus {
|
|
||||||
self.advance(2);
|
|
||||||
let expr = self.parse_unary()?;
|
|
||||||
let span = Span::from_two(token.span, expr.span);
|
|
||||||
return Ok(Expr {
|
|
||||||
value: ExprValue::IncDec {
|
|
||||||
op: IncDecOp::Inc,
|
|
||||||
operand: Box::new(expr),
|
|
||||||
is_prefix: true,
|
|
||||||
},
|
|
||||||
span,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
self.advance(1);
|
self.advance(1);
|
||||||
let expr = self.parse_unary()?;
|
let expr = self.parse_unary()?;
|
||||||
let span = Span::from_two(token.span, expr.span);
|
let span = Span::from_two(token.span, expr.span);
|
||||||
@@ -195,19 +165,6 @@ impl Parser {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
TokenValue::Minus => {
|
TokenValue::Minus => {
|
||||||
if self.peek_n(1).value == TokenValue::Minus {
|
|
||||||
self.advance(2);
|
|
||||||
let rhs = self.parse_unary()?;
|
|
||||||
let span = Span::from_two(token.span, rhs.span);
|
|
||||||
return Ok(Expr {
|
|
||||||
value: ExprValue::IncDec {
|
|
||||||
op: IncDecOp::Dec,
|
|
||||||
operand: Box::new(rhs),
|
|
||||||
is_prefix: true,
|
|
||||||
},
|
|
||||||
span,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
self.advance(1);
|
self.advance(1);
|
||||||
let rhs = self.parse_unary()?;
|
let rhs = self.parse_unary()?;
|
||||||
let span = Span::from_two(token.span, rhs.span);
|
let span = Span::from_two(token.span, rhs.span);
|
||||||
@@ -231,7 +188,25 @@ impl Parser {
|
|||||||
span,
|
span,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_ => self.parse_primary(),
|
TokenValue::PlusPlus | TokenValue::MinusMinus => {
|
||||||
|
let op = if token.value == TokenValue::PlusPlus {
|
||||||
|
IncDecOp::Inc
|
||||||
|
} else {
|
||||||
|
IncDecOp::Dec
|
||||||
|
};
|
||||||
|
self.advance(1);
|
||||||
|
let operand = self.parse_unary()?;
|
||||||
|
let span = Span::from_two(token.span, operand.span);
|
||||||
|
Ok(Expr {
|
||||||
|
value: ExprValue::IncDec {
|
||||||
|
op,
|
||||||
|
operand: Box::new(operand),
|
||||||
|
is_prefix: true,
|
||||||
|
},
|
||||||
|
span,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
_ => self.parse_postfix(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn parse_multiplicative(&mut self) -> Result<Expr, ParseProcessError> {
|
fn parse_multiplicative(&mut self) -> Result<Expr, ParseProcessError> {
|
||||||
|
|||||||
+18
-2
@@ -269,8 +269,24 @@ fn parse_puncuation(
|
|||||||
};
|
};
|
||||||
let c = str_iter.peek().ok_or(LexParseError::NotMatched)?;
|
let c = str_iter.peek().ok_or(LexParseError::NotMatched)?;
|
||||||
let token_value = match c {
|
let token_value = match c {
|
||||||
'+' => TokenValue::Plus,
|
'+' => {
|
||||||
'-' => TokenValue::Minus,
|
str_iter.advance(1);
|
||||||
|
if let Some('+') = str_iter.peek() {
|
||||||
|
TokenValue::PlusPlus
|
||||||
|
} else {
|
||||||
|
str_iter.back(1);
|
||||||
|
TokenValue::Plus
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'-' => {
|
||||||
|
str_iter.advance(1);
|
||||||
|
if let Some('-') = str_iter.peek() {
|
||||||
|
TokenValue::MinusMinus
|
||||||
|
} else {
|
||||||
|
str_iter.back(1);
|
||||||
|
TokenValue::Minus
|
||||||
|
}
|
||||||
|
},
|
||||||
'*' => TokenValue::Star,
|
'*' => TokenValue::Star,
|
||||||
'/' => TokenValue::Slash,
|
'/' => TokenValue::Slash,
|
||||||
'%' => TokenValue::Percent,
|
'%' => TokenValue::Percent,
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ pub enum TokenValue {
|
|||||||
TypeIdent(TypeIdent),
|
TypeIdent(TypeIdent),
|
||||||
|
|
||||||
Plus, Minus, Star, Slash, Percent,
|
Plus, Minus, Star, Slash, Percent,
|
||||||
|
PlusPlus, MinusMinus,
|
||||||
Equal, DoubleEqual, Not, NotEqual, Less, LessEqual, Greater, GreaterEqual, And, Or,
|
Equal, DoubleEqual, Not, NotEqual, Less, LessEqual, Greater, GreaterEqual, And, Or,
|
||||||
|
|
||||||
LParen, RParen,
|
LParen, RParen,
|
||||||
@@ -53,6 +54,8 @@ impl std::fmt::Display for TokenValue {
|
|||||||
TokenValue::TypeIdent(t) => write!(f, "type {}", t.as_ref()),
|
TokenValue::TypeIdent(t) => write!(f, "type {}", t.as_ref()),
|
||||||
TokenValue::Plus => write!(f, "`+`"),
|
TokenValue::Plus => write!(f, "`+`"),
|
||||||
TokenValue::Minus => write!(f, "`-`"),
|
TokenValue::Minus => write!(f, "`-`"),
|
||||||
|
TokenValue::PlusPlus => write!(f, "`++`"),
|
||||||
|
TokenValue::MinusMinus => write!(f, "`--`"),
|
||||||
TokenValue::Star => write!(f, "`*`"),
|
TokenValue::Star => write!(f, "`*`"),
|
||||||
TokenValue::Slash => write!(f, "`/`"),
|
TokenValue::Slash => write!(f, "`/`"),
|
||||||
TokenValue::Percent => write!(f, "`%`"),
|
TokenValue::Percent => write!(f, "`%`"),
|
||||||
|
|||||||
Reference in New Issue
Block a user