Fix index/call parsing

This commit is contained in:
Greg Shuflin 2021-10-30 19:46:40 -07:00
parent 46fe03b43f
commit 1c6545fb74
3 changed files with 63 additions and 36 deletions

View File

@ -69,15 +69,21 @@
//! ```text //! ```text
//! expression := precedence_expr type_anno? //! expression := precedence_expr type_anno?
//! precedence_expr := prefix_expr //! precedence_expr := prefix_expr
//! prefix_expr := prefix_op call_expr //! prefix_expr := prefix_op extended_expr
//! prefix_op := "+" | "-" | "!" | "~" //!
//! call_expr := index_expr ( "(" invocation_list ")" )* | ε //! prefix_op := "+" | "-" | "!" | "~" | ε
//! invocation_list := invocation_argument ("," invocation_argument)* | ε //!
//! invocation_argument := expression | IDENTIFIER "=" expression | "_" //! extended_expr := primary (ε | index | call | access)*
//! index_expr := primary ( "[" (expression ("," (expression)* | ε) "]" )* //! index := "[" (expression ("," expression)*) "]"
//! call := "(" invocation_list ")"
//! access := "." identifier
//! primary := literal | paren_expr | if_expr | for_expr | while_expr | identifier_expr | lambda_expr | anonymous_struct | list_expr //! primary := literal | paren_expr | if_expr | for_expr | while_expr | identifier_expr | lambda_expr | anonymous_struct | list_expr
//! expr_or_block := "{" (statement delimiter)* "}" | expr //! expr_or_block := "{" (statement delimiter)* "}" | expr
//!
//! invocation_list := invocation_argument ("," invocation_argument)* | ε
//! invocation_argument := expression | IDENTIFIER "=" expression | "_"
//! ``` //! ```
//! //TODO fix expressions, add accessor_expr as a new thing
//! //!
//! ### Primary expressions //! ### Primary expressions
//! //!
@ -688,20 +694,39 @@ impl Parser {
ExpressionKind::PrefixExp(prefix_op, Box::new(expr)) ExpressionKind::PrefixExp(prefix_op, Box::new(expr))
)) ))
}, },
_ => self.call_expr() _ => self.extended_expr()
} }
} }
#[recursive_descent_method] #[recursive_descent_method]
fn call_expr(&mut self) -> ParseResult<Expression> { fn extended_expr(&mut self) -> ParseResult<Expression> {
let mut expr = self.index_expr()?; let mut expression = self.primary()?;
//TODO look at this loop {
while let LParen = self.token_handler.peek_kind() { //TODO need a next non whitespace
let arguments = delimited!(self, LParen, invocation_argument, Comma, RParen); let next = self.token_handler.peek_kind();
expr = Expression::new(self.id_store.fresh(), ExpressionKind::Call { f: Box::new(expr), arguments }); //TODO no type anno is incorrect match next {
Period => unimplemented!(),
LSquareBracket => {
let indexers = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket);
if indexers.is_empty() {
return ParseError::new_with_token("Empty index expressions are not allowed", self.token_handler.peek());
} }
expression = Expression::new(self.id_store.fresh(), ExpressionKind::Index {
Ok(expr) indexee: Box::new(expression),
indexers,
});
},
LParen => {
let arguments = delimited!(self, LParen, invocation_argument, Comma, RParen);
expression = Expression::new(self.id_store.fresh(), ExpressionKind::Call {
f: Box::new(expression),
arguments,
});
},
_ => break,
}
}
Ok(expression)
} }
#[recursive_descent_method] #[recursive_descent_method]

View File

@ -120,7 +120,6 @@ macro_rules! assert_fail_expr {
assert_eq!(err.msg, $failure); assert_eq!(err.msg, $failure);
}; };
} }
#[test] #[test]
fn basic_literals() { fn basic_literals() {
use ExpressionKind::*; use ExpressionKind::*;
@ -202,14 +201,14 @@ fn operators() {
assert_expr!("a <> 1", binop("<>", expr(Value(qn!(a))), expr(NatLiteral(1)))); assert_expr!("a <> 1", binop("<>", expr(Value(qn!(a))), expr(NatLiteral(1))));
} }
//TODO have a test for dot accessors #[test]
/* fn accessors() {
assert_expr!("a.b.c.d", exst!(binexp!(".", /*
binexp!(".", assert_expr!("a.b");
binexp!(".", val!("a"), val!("b")), assert_expr!("a.b.c.d()");
val!("c")), assert_expr!("a.b().c.d()");
val!("d")))); */
*/ }
#[test] #[test]
fn tuples() { fn tuples() {
@ -273,17 +272,19 @@ fn index() {
indexers: vec![expr(Value(qn!(b))), expr(Value(qn!(c)))] indexers: vec![expr(Value(qn!(b))), expr(Value(qn!(c)))]
}) })
); );
//TODO this is a parser bug assert_expr!(
/* "perspicacity()[a]",
assert_expr!("perspicacity()[a]", expr(Index{ expr(Index {
indexee: bx(expr(Call { indexee: bx(expr(Call { f: bx(expr(Value(qn!(perspicacity)))), arguments: vec![] })),
f: bx(expr(Value(qn!(perspicacity)))),
arguments: vec![]
})),
indexers: vec![expr(Value(qn!(a)))] indexers: vec![expr(Value(qn!(a)))]
})); })
*/ );
//TODO parse_test("a()[b]()[d]")
let a = expr(Call { f: bx(expr(Value(qn!(a)))), arguments: vec![] });
let b = expr(Index { indexee: bx(a), indexers: vec![expr(Value(qn!(b)))] });
let c = expr(Call { f: bx(b), arguments: vec![] });
let d = expr(Index { indexee: bx(c), indexers: vec![expr(Value(qn!(d)))] });
assert_expr!("a()[b]()[d]", d);
assert_fail_expr!("a[]", "Empty index expressions are not allowed"); assert_fail_expr!("a[]", "Empty index expressions are not allowed");
} }

View File

@ -1,5 +1,6 @@
#![cfg(test)] #![cfg(test)]
use test_case::test_case; use test_case::test_case;
use pretty_assertions::assert_eq;
use crate::{ use crate::{
symbol_table::SymbolTable, symbol_table::SymbolTable,