parser: abstract call argument parsing logic

This commit is contained in:
Ryan Chandler 2022-09-16 14:20:54 +01:00
parent 5dcf047d3a
commit 25f6e7ca45
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 47 additions and 151 deletions

View File

@ -1831,29 +1831,7 @@ impl Parser {
if self.current.kind == TokenKind::LeftParen {
self.lparen()?;
while self.current.kind != TokenKind::RightParen {
let mut name = None;
let mut unpack = false;
if matches!(self.current.kind, TokenKind::Identifier(_))
&& self.peek.kind == TokenKind::Colon
{
name = Some(self.ident_maybe_reserved()?);
self.next();
} else if self.current.kind == TokenKind::Ellipsis {
self.next();
unpack = true;
}
let value = self.expression(Precedence::Lowest)?;
args.push(Arg {
name,
unpack,
value,
});
self.optional_comma()?;
}
args = self.args_list()?;
self.rparen()?;
}
@ -1897,29 +1875,7 @@ impl Parser {
if self.current.kind == TokenKind::LeftParen {
self.lparen()?;
while self.current.kind != TokenKind::RightParen {
let mut name = None;
let mut unpack = false;
if matches!(self.current.kind, TokenKind::Identifier(_))
&& self.peek.kind == TokenKind::Colon
{
name = Some(self.ident_maybe_reserved()?);
self.next();
} else if self.current.kind == TokenKind::Ellipsis {
self.next();
unpack = true;
}
let value = self.expression(Precedence::Lowest)?;
args.push(Arg {
name,
unpack,
value,
});
self.optional_comma()?;
}
args = self.args_list()?;
self.rparen()?;
}
@ -2049,40 +2005,7 @@ impl Parser {
}
}
TokenKind::LeftParen => {
let mut args = Vec::new();
while !self.is_eof() && self.current.kind != TokenKind::RightParen {
let mut name = None;
let mut unpack = false;
if matches!(self.current.kind, TokenKind::Identifier(_))
&& self.peek.kind == TokenKind::Colon
{
name = Some(self.ident_maybe_reserved()?);
self.next();
} else if self.current.kind == TokenKind::Ellipsis {
self.next();
unpack = true;
}
if unpack && self.current.kind == TokenKind::RightParen {
args.push(Arg {
name: None,
unpack: false,
value: Expression::VariadicPlaceholder,
});
break;
}
let value = self.expression(Precedence::Lowest)?;
args.push(Arg {
name,
unpack,
value,
});
self.optional_comma()?;
}
let args = self.args_list()?;
self.rparen()?;
@ -2162,40 +2085,7 @@ impl Parser {
_ if self.current.kind == TokenKind::LeftParen || must_be_method_call => {
self.lparen()?;
let mut args = vec![];
while !self.is_eof() && self.current.kind != TokenKind::RightParen {
let mut name = None;
let mut unpack = false;
if matches!(self.current.kind, TokenKind::Identifier(_))
&& self.peek.kind == TokenKind::Colon
{
name = Some(self.ident_maybe_reserved()?);
self.next();
} else if self.current.kind == TokenKind::Ellipsis {
self.next();
unpack = true;
}
if unpack && self.current.kind == TokenKind::RightParen {
args.push(Arg {
name: None,
unpack: false,
value: Expression::VariadicPlaceholder,
});
break;
}
let value = self.expression(Precedence::Lowest)?;
args.push(Arg {
name,
unpack,
value,
});
self.optional_comma()?;
}
let args = self.args_list()?;
self.rparen()?;
@ -2237,40 +2127,7 @@ impl Parser {
if self.current.kind == TokenKind::LeftParen {
self.next();
let mut args = Vec::new();
while !self.is_eof() && self.current.kind != TokenKind::RightParen {
let mut name = None;
let mut unpack = false;
if matches!(self.current.kind, TokenKind::Identifier(_))
&& self.peek.kind == TokenKind::Colon
{
name = Some(self.ident_maybe_reserved()?);
self.next();
} else if self.current.kind == TokenKind::Ellipsis {
self.next();
unpack = true;
}
if unpack && self.current.kind == TokenKind::RightParen {
args.push(Arg {
name: None,
unpack: false,
value: Expression::VariadicPlaceholder,
});
break;
}
let value = self.expression(Precedence::Lowest)?;
args.push(Arg {
name,
value,
unpack,
});
self.optional_comma()?;
}
let args = self.args_list()?;
self.rparen()?;

View File

@ -1,10 +1,10 @@
use crate::{
ast::{ParamList, PropertyFlag},
ast::{ParamList, PropertyFlag, Arg},
Expression, Param, ParseError,
};
use trunk_lexer::TokenKind;
use super::{precedence::Precedence, Parser};
use super::{precedence::Precedence, Parser, ParseResult};
impl Parser {
pub(crate) fn param_list(&mut self) -> Result<ParamList, ParseError> {
@ -58,7 +58,6 @@ impl Parser {
default = Some(self.expression(Precedence::Lowest)?);
}
// TODO: Support variable types and default values.
params.push(Param {
name: Expression::Variable { name: var },
r#type: param_type,
@ -73,4 +72,44 @@ impl Parser {
Ok(params)
}
pub(crate) fn args_list(&mut self) -> ParseResult<Vec<Arg>> {
let mut args = Vec::new();
while !self.is_eof() && self.current.kind != TokenKind::RightParen {
let mut name = None;
let mut unpack = false;
if matches!(self.current.kind, TokenKind::Identifier(_))
&& self.peek.kind == TokenKind::Colon
{
name = Some(self.ident_maybe_reserved()?);
self.next();
} else if self.current.kind == TokenKind::Ellipsis {
self.next();
unpack = true;
}
if unpack && self.current.kind == TokenKind::RightParen {
args.push(Arg {
name: None,
unpack: false,
value: Expression::VariadicPlaceholder,
});
break;
}
let value = self.expression(Precedence::Lowest)?;
args.push(Arg {
name,
unpack,
value,
});
self.optional_comma()?;
}
Ok(args)
}
}