mirror of
https://github.com/danog/parser.git
synced 2025-01-23 05:21:22 +01:00
parser: abstract parameter list logic
This commit is contained in:
parent
e52dd5db95
commit
dc7604f7ad
@ -35,6 +35,8 @@ impl From<&str> for Identifier {
|
||||
}
|
||||
}
|
||||
|
||||
pub type ParamList = Vec<Param>;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Serialize)]
|
||||
pub struct Param {
|
||||
pub(crate) name: Expression,
|
||||
|
@ -22,6 +22,8 @@ macro_rules! expect {
|
||||
};
|
||||
}
|
||||
|
||||
mod params;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum FunctionKind {
|
||||
Named,
|
||||
@ -183,29 +185,7 @@ impl Parser {
|
||||
|
||||
expect!(self, TokenKind::LeftParen, "expected (");
|
||||
|
||||
let mut params = Vec::new();
|
||||
while ! self.is_eof() && self.current.kind != TokenKind::RightParen {
|
||||
let mut param_type = None;
|
||||
|
||||
// 1. If we don't see a variable, we should expect a type-string.
|
||||
if ! matches!(self.current.kind, TokenKind::Variable(_)) {
|
||||
// 1a. Try to parse the type.
|
||||
param_type = Some(self.type_string()?);
|
||||
}
|
||||
|
||||
// 2. Then expect a variable.
|
||||
let var = expect!(self, TokenKind::Variable(v), v, "expected variable");
|
||||
|
||||
// TODO: Support variable types and default values.
|
||||
params.push(Param {
|
||||
name: Expression::Variable(var),
|
||||
r#type: param_type,
|
||||
});
|
||||
|
||||
if let Token { kind: TokenKind::Comma, .. } = self.current {
|
||||
self.next();
|
||||
}
|
||||
}
|
||||
let params = self.param_list()?;
|
||||
|
||||
expect!(self, TokenKind::RightParen, "expected )");
|
||||
|
||||
@ -228,29 +208,7 @@ impl Parser {
|
||||
|
||||
expect!(self, TokenKind::LeftParen, "expected (");
|
||||
|
||||
let mut params = Vec::new();
|
||||
while ! self.is_eof() && self.current.kind != TokenKind::RightParen {
|
||||
let mut param_type = None;
|
||||
|
||||
// 1. If we don't see a variable, we should expect a type-string.
|
||||
if ! matches!(self.current.kind, TokenKind::Variable(_)) {
|
||||
// 1a. Try to parse the type.
|
||||
param_type = Some(self.type_string()?);
|
||||
}
|
||||
|
||||
// 2. Then expect a variable.
|
||||
let var = expect!(self, TokenKind::Variable(v), v, "expected variable");
|
||||
|
||||
// TODO: Support variable types and default values.
|
||||
params.push(Param {
|
||||
name: Expression::Variable(var),
|
||||
r#type: param_type,
|
||||
});
|
||||
|
||||
if let Token { kind: TokenKind::Comma, .. } = self.current {
|
||||
self.next();
|
||||
}
|
||||
}
|
||||
let params = self.param_list()?;
|
||||
|
||||
expect!(self, TokenKind::RightParen, "expected )");
|
||||
|
||||
@ -454,30 +412,7 @@ impl Parser {
|
||||
|
||||
expect!(self, TokenKind::LeftParen, "expected (");
|
||||
|
||||
let mut params = Vec::new();
|
||||
|
||||
while ! self.is_eof() && self.current.kind != TokenKind::RightParen {
|
||||
let mut param_type = None;
|
||||
|
||||
// 1. If we don't see a variable, we should expect a type-string.
|
||||
if ! matches!(self.current.kind, TokenKind::Variable(_)) {
|
||||
// 1a. Try to parse the type.
|
||||
param_type = Some(self.type_string()?);
|
||||
}
|
||||
|
||||
// 2. Then expect a variable.
|
||||
let var = expect!(self, TokenKind::Variable(v), v, "expected variable");
|
||||
|
||||
// TODO: Support variable types and default values.
|
||||
params.push(Param {
|
||||
name: Expression::Variable(var),
|
||||
r#type: param_type,
|
||||
});
|
||||
|
||||
if let Token { kind: TokenKind::Comma, .. } = self.current {
|
||||
self.next();
|
||||
}
|
||||
}
|
||||
let params = self.param_list()?;
|
||||
|
||||
expect!(self, TokenKind::RightParen, "expected )");
|
||||
|
||||
@ -640,29 +575,7 @@ impl Parser {
|
||||
|
||||
expect!(self, TokenKind::LeftParen, "expected (");
|
||||
|
||||
let mut params = Vec::new();
|
||||
while ! self.is_eof() && self.current.kind != TokenKind::RightParen {
|
||||
let mut param_type = None;
|
||||
|
||||
// 1. If we don't see a variable, we should expect a type-string.
|
||||
if ! matches!(self.current.kind, TokenKind::Variable(_)) {
|
||||
// 1a. Try to parse the type.
|
||||
param_type = Some(self.type_string()?);
|
||||
}
|
||||
|
||||
// 2. Then expect a variable.
|
||||
let var = expect!(self, TokenKind::Variable(v), v, "expected variable");
|
||||
|
||||
// TODO: Support variable types and default values.
|
||||
params.push(Param {
|
||||
name: Expression::Variable(var),
|
||||
r#type: param_type,
|
||||
});
|
||||
|
||||
if let Token { kind: TokenKind::Comma, .. } = self.current {
|
||||
self.next();
|
||||
}
|
||||
}
|
||||
let params = self.param_list()?;
|
||||
|
||||
expect!(self, TokenKind::RightParen, "expected )");
|
||||
|
||||
@ -791,30 +704,8 @@ impl Parser {
|
||||
|
||||
expect!(self, TokenKind::LeftParen, "expected (");
|
||||
|
||||
let mut params = Vec::new();
|
||||
while ! self.is_eof() && self.current.kind != TokenKind::RightParen {
|
||||
let mut param_type = None;
|
||||
|
||||
// 1. If we don't see a variable, we should expect a type-string.
|
||||
if ! matches!(self.current.kind, TokenKind::Variable(_)) {
|
||||
// 1a. Try to parse the type.
|
||||
param_type = Some(self.type_string()?);
|
||||
}
|
||||
|
||||
// 2. Then expect a variable.
|
||||
let var = expect!(self, TokenKind::Variable(v), v, "expected variable");
|
||||
|
||||
// TODO: Support variable types and default values.
|
||||
params.push(Param {
|
||||
name: Expression::Variable(var),
|
||||
r#type: param_type,
|
||||
});
|
||||
|
||||
if let Token { kind: TokenKind::Comma, .. } = self.current {
|
||||
self.next();
|
||||
}
|
||||
}
|
||||
|
||||
let params = self.param_list()?;
|
||||
|
||||
expect!(self, TokenKind::RightParen, "expected )");
|
||||
|
||||
let mut return_type = None;
|
||||
|
35
trunk_parser/src/parser/params.rs
Normal file
35
trunk_parser/src/parser/params.rs
Normal file
@ -0,0 +1,35 @@
|
||||
use crate::{ast::ParamList, ParseError, Param, Expression};
|
||||
use trunk_lexer::TokenKind;
|
||||
|
||||
use super::Parser;
|
||||
|
||||
impl Parser {
|
||||
pub(crate) fn param_list(&mut self) -> Result<ParamList, ParseError> {
|
||||
let mut params = ParamList::new();
|
||||
|
||||
while ! self.is_eof() && self.current.kind != TokenKind::RightParen {
|
||||
let mut param_type = None;
|
||||
|
||||
// 1. If we don't see a variable, we should expect a type-string.
|
||||
if ! matches!(self.current.kind, TokenKind::Variable(_)) {
|
||||
// 1a. Try to parse the type.
|
||||
param_type = Some(self.type_string()?);
|
||||
}
|
||||
|
||||
// 2. Then expect a variable.
|
||||
let var = expect!(self, TokenKind::Variable(v), v, "expected variable");
|
||||
|
||||
// TODO: Support variable types and default values.
|
||||
params.push(Param {
|
||||
name: Expression::Variable(var),
|
||||
r#type: param_type,
|
||||
});
|
||||
|
||||
if self.current.kind == TokenKind::Comma {
|
||||
self.next();
|
||||
}
|
||||
}
|
||||
|
||||
Ok(params)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user