Merge pull request #98 from ryangjchandler/refactor/args

This commit is contained in:
Ryan Chandler 2022-09-16 14:23:20 +01:00 committed by GitHub
commit 7677435f6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 153 deletions

View File

@ -1,8 +1,9 @@
name: Tests name: Tests
on: on:
- push push:
- pull_request pull_request:
types: [opened, reopened]
jobs: jobs:
tests: tests:

View File

@ -1831,29 +1831,7 @@ impl Parser {
if self.current.kind == TokenKind::LeftParen { if self.current.kind == TokenKind::LeftParen {
self.lparen()?; self.lparen()?;
while self.current.kind != TokenKind::RightParen { args = self.args_list()?;
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()?;
}
self.rparen()?; self.rparen()?;
} }
@ -1897,29 +1875,7 @@ impl Parser {
if self.current.kind == TokenKind::LeftParen { if self.current.kind == TokenKind::LeftParen {
self.lparen()?; self.lparen()?;
while self.current.kind != TokenKind::RightParen { args = self.args_list()?;
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()?;
}
self.rparen()?; self.rparen()?;
} }
@ -2049,40 +2005,7 @@ impl Parser {
} }
} }
TokenKind::LeftParen => { TokenKind::LeftParen => {
let mut args = Vec::new(); let args = self.args_list()?;
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()?;
}
self.rparen()?; self.rparen()?;
@ -2162,40 +2085,7 @@ impl Parser {
_ if self.current.kind == TokenKind::LeftParen || must_be_method_call => { _ if self.current.kind == TokenKind::LeftParen || must_be_method_call => {
self.lparen()?; self.lparen()?;
let mut args = vec![]; let args = self.args_list()?;
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()?;
}
self.rparen()?; self.rparen()?;
@ -2237,40 +2127,7 @@ impl Parser {
if self.current.kind == TokenKind::LeftParen { if self.current.kind == TokenKind::LeftParen {
self.next(); self.next();
let mut args = Vec::new(); let args = self.args_list()?;
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()?;
}
self.rparen()?; self.rparen()?;

View File

@ -1,10 +1,10 @@
use crate::{ use crate::{
ast::{ParamList, PropertyFlag}, ast::{Arg, ParamList, PropertyFlag},
Expression, Param, ParseError, Expression, Param, ParseError,
}; };
use trunk_lexer::TokenKind; use trunk_lexer::TokenKind;
use super::{precedence::Precedence, Parser}; use super::{precedence::Precedence, ParseResult, Parser};
impl Parser { impl Parser {
pub(crate) fn param_list(&mut self) -> Result<ParamList, ParseError> { pub(crate) fn param_list(&mut self) -> Result<ParamList, ParseError> {
@ -58,7 +58,6 @@ impl Parser {
default = Some(self.expression(Precedence::Lowest)?); default = Some(self.expression(Precedence::Lowest)?);
} }
// TODO: Support variable types and default values.
params.push(Param { params.push(Param {
name: Expression::Variable { name: var }, name: Expression::Variable { name: var },
r#type: param_type, r#type: param_type,
@ -73,4 +72,44 @@ impl Parser {
Ok(params) 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)
}
} }