This commit is contained in:
2026-04-14 04:14:46 +02:00
parent a914892fc1
commit 6c46c1f0bb
4 changed files with 50 additions and 26 deletions
+33 -14
View File
@@ -169,11 +169,20 @@ impl Statement {
impl Expression {
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Self {
match pair.as_rule() {
Rule::expr | Rule::term => {
let inner = pair.into_inner().next().unwrap();
Expression::from_pair(inner)
Rule::expr => {
let mut inner = pair.into_inner();
let exp = Expression::from_pair(inner.next().unwrap());
if inner.len() > 0 {
Expression::Postfix {
initial: Box::new(exp),
postfixes: inner.map(|p| Postfix::from_pair(p)).collect(),
}
} else {
exp
}
}
Rule::var_path => Expression::Identifier(pair.as_str().to_string()),
Rule::primary => Expression::from_pair(pair.into_inner().next().unwrap()),
Rule::identifier => Expression::Identifier(pair.as_str().to_string()),
Rule::integer => {
let value = pair.as_str().parse::<i64>().unwrap();
@@ -192,16 +201,6 @@ impl Expression {
Expression::StringLiteral(inner_str.to_string())
}
Rule::func_call => {
let mut inner = pair.into_inner();
let callee = Expression::from_pair(inner.next().unwrap());
let args = inner.map(Expression::from_pair).collect();
Expression::FunctionCall {
callee: Box::new(callee),
args,
}
}
_ => unimplemented!(
"Expression parsing not implemented yet {:?}",
pair.as_rule()
@@ -209,3 +208,23 @@ impl Expression {
}
}
}
impl Postfix {
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Self {
match pair.as_rule() {
Rule::postfix => {
let inner = pair.into_inner();
if let Some(field) = inner.peek() {
if field.as_rule() == Rule::identifier {
Postfix::FieldAccess(field.as_str().to_string())
} else {
Postfix::FunctionCall(inner.map(|p| Expression::from_pair(p)).collect())
}
} else {
Postfix::FunctionCall(vec![])
}
}
_ => unimplemented!("Postfix parsing not implemented yet {:?}", pair.as_rule()),
}
}
}