FnCall parsing.
This commit is contained in:
@@ -44,6 +44,7 @@ pub struct Let {
|
|||||||
pub struct Assign {
|
pub struct Assign {
|
||||||
pub name: Ident,
|
pub name: Ident,
|
||||||
pub value: Expr,
|
pub value: Expr,
|
||||||
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
@@ -51,12 +52,14 @@ pub struct FnDef {
|
|||||||
pub name: Ident,
|
pub name: Ident,
|
||||||
pub params: Vec<FnParam>,
|
pub params: Vec<FnParam>,
|
||||||
pub body: Vec<Stmt>,
|
pub body: Vec<Stmt>,
|
||||||
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct FnParam {
|
pub struct FnParam {
|
||||||
pub name: Ident,
|
pub name: Ident,
|
||||||
pub ty: Type,
|
pub ty: Type,
|
||||||
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
@@ -100,6 +103,7 @@ pub struct LitString {
|
|||||||
pub struct FnCall {
|
pub struct FnCall {
|
||||||
pub name: Ident,
|
pub name: Ident,
|
||||||
pub args: Vec<Expr>,
|
pub args: Vec<Expr>,
|
||||||
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
|
|||||||
+74
-2
@@ -1,7 +1,7 @@
|
|||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{Expr, Ident, Let, LitNumber, LitString, Stmt},
|
ast::{Expr, FnCall, Ident, Let, LitNumber, LitString, Stmt},
|
||||||
lexer::Token,
|
lexer::Token,
|
||||||
span::{Span, Spanned},
|
span::{Span, Spanned},
|
||||||
};
|
};
|
||||||
@@ -51,6 +51,14 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn next_if(&mut self, expected: Token) -> Option<Spanned<Token>> {
|
||||||
|
if self.peek() == Some(&expected) {
|
||||||
|
self.next()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn slice(&self, span: Span) -> Result<&'a str, ParseError> {
|
fn slice(&self, span: Span) -> Result<&'a str, ParseError> {
|
||||||
self.input
|
self.input
|
||||||
.get(span.start()..span.end())
|
.get(span.start()..span.end())
|
||||||
@@ -136,7 +144,24 @@ where
|
|||||||
name: raw.to_string(),
|
name: raw.to_string(),
|
||||||
span: *token.span(),
|
span: *token.span(),
|
||||||
};
|
};
|
||||||
Ok(Expr::Variable(ident))
|
match self.peek() {
|
||||||
|
Some(Token::ParenL) => {
|
||||||
|
self.next().unwrap();
|
||||||
|
let mut args = Vec::new();
|
||||||
|
while self.peek() != Some(&Token::ParenR) {
|
||||||
|
args.push(self.parse_expr()?);
|
||||||
|
self.next_if(Token::Comma);
|
||||||
|
}
|
||||||
|
let rparen = self.next_expected(Token::ParenR)?;
|
||||||
|
let fncall = FnCall {
|
||||||
|
name: ident,
|
||||||
|
args,
|
||||||
|
span: token.span_to(&rparen),
|
||||||
|
};
|
||||||
|
Ok(Expr::FnCall(fncall))
|
||||||
|
}
|
||||||
|
_ => Ok(Expr::Variable(ident)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Some(_) => Err(ParseError {
|
Some(_) => Err(ParseError {
|
||||||
span: self.current_span,
|
span: self.current_span,
|
||||||
@@ -291,4 +316,51 @@ mod tests {
|
|||||||
);
|
);
|
||||||
pretty_assertions::assert_eq!(parser.next(), None);
|
pretty_assertions::assert_eq!(parser.next(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn expr_fncall() {
|
||||||
|
let input = "f(x)";
|
||||||
|
let lexer = Lexer::new(input);
|
||||||
|
let mut parser = Parser::new(input, lexer);
|
||||||
|
pretty_assertions::assert_eq!(
|
||||||
|
parser.parse_expr(),
|
||||||
|
Ok(Expr::FnCall(FnCall {
|
||||||
|
name: Ident {
|
||||||
|
name: "f".to_string(),
|
||||||
|
span: Span::new(0, 1),
|
||||||
|
},
|
||||||
|
args: vec![Expr::Variable(Ident {
|
||||||
|
name: "x".to_string(),
|
||||||
|
span: Span::new(2, 3),
|
||||||
|
})],
|
||||||
|
span: Span::new(0, 4),
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
pretty_assertions::assert_eq!(parser.next(), None);
|
||||||
|
|
||||||
|
let input = "f(x, y)";
|
||||||
|
let lexer = Lexer::new(input);
|
||||||
|
let mut parser = Parser::new(input, lexer);
|
||||||
|
pretty_assertions::assert_eq!(
|
||||||
|
parser.parse_expr(),
|
||||||
|
Ok(Expr::FnCall(FnCall {
|
||||||
|
name: Ident {
|
||||||
|
name: "f".to_string(),
|
||||||
|
span: Span::new(0, 1),
|
||||||
|
},
|
||||||
|
args: vec![
|
||||||
|
Expr::Variable(Ident {
|
||||||
|
name: "x".to_string(),
|
||||||
|
span: Span::new(2, 3),
|
||||||
|
}),
|
||||||
|
Expr::Variable(Ident {
|
||||||
|
name: "y".to_string(),
|
||||||
|
span: Span::new(5, 6),
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
span: Span::new(0, 7),
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
pretty_assertions::assert_eq!(parser.next(), None);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user