Decomplexify delimited!

This commit is contained in:
greg 2018-05-13 02:50:43 -07:00
parent 6b90e19eb1
commit f3e3843528

View File

@ -324,13 +324,13 @@ macro_rules! parse_method {
} }
macro_rules! delimited { macro_rules! delimited {
($self:expr, $start:pat, $start_str:expr, $parse_fn:ident, $( $delim:pat )|+, $end:pat, $end_str:expr, nonstrict) => { ($self:expr, $start:pat, $parse_fn:ident, $( $delim:pat )|+, $end:pat, nonstrict) => {
delimited!($self, $start, $start_str, $parse_fn, $( $delim )|*, $end, $end_str, false) delimited!($self, $start, $parse_fn, $( $delim )|*, $end, false)
}; };
($self:expr, $start:pat, $start_str:expr, $parse_fn:ident, $( $delim:pat )|+, $end:pat, $end_str:expr) => { ($self:expr, $start:pat, $parse_fn:ident, $( $delim:pat )|+, $end:pat) => {
delimited!($self, $start, $start_str, $parse_fn, $( $delim )|*, $end, $end_str, true) delimited!($self, $start, $parse_fn, $( $delim )|*, $end, true)
}; };
($self:expr, $start:pat, $start_str:expr, $parse_fn:ident, $( $delim:pat )|+, $end:pat, $end_str:expr, $strictness:expr) => { ($self:expr, $start:pat, $parse_fn:ident, $( $delim:pat )|+, $end:pat, $strictness:expr) => {
{ {
expect!($self, $start); expect!($self, $start);
let mut acc = vec![]; let mut acc = vec![];
@ -431,11 +431,11 @@ impl Parser {
let name = self.identifier()?; let name = self.identifier()?;
match self.peek() { match self.peek() {
LParen => { LParen => {
let tuple_members = delimited!(self, LParen, '(', type_name, Comma, RParen, ')'); let tuple_members = delimited!(self, LParen, type_name, Comma, RParen);
Ok(TupleStruct(name, tuple_members)) Ok(TupleStruct(name, tuple_members))
}, },
LCurlyBrace => { LCurlyBrace => {
let typed_identifier_list = delimited!(self, LCurlyBrace, '{', typed_identifier, Comma, RCurlyBrace, '}'); let typed_identifier_list = delimited!(self, LCurlyBrace, typed_identifier, Comma, RCurlyBrace);
Ok(Record(name, typed_identifier_list)) Ok(Record(name, typed_identifier_list))
}, },
_ => Ok(UnitStruct(name)) _ => Ok(UnitStruct(name))
@ -452,7 +452,7 @@ impl Parser {
parse_method!(func_declaration(&mut self) -> ParseResult<Declaration> { parse_method!(func_declaration(&mut self) -> ParseResult<Declaration> {
let signature = self.signature()?; let signature = self.signature()?;
if let LCurlyBrace = self.peek() { if let LCurlyBrace = self.peek() {
let statements = delimited!(self, LCurlyBrace, '{', statement, Newline | Semicolon, RCurlyBrace, '}', nonstrict); let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict);
Ok(Declaration::FuncDecl(signature, statements)) Ok(Declaration::FuncDecl(signature, statements))
} else { } else {
Ok(Declaration::FuncSig(signature)) Ok(Declaration::FuncSig(signature))
@ -462,7 +462,7 @@ impl Parser {
parse_method!(signature(&mut self) -> ParseResult<Signature> { parse_method!(signature(&mut self) -> ParseResult<Signature> {
expect!(self, Keyword(Func)); expect!(self, Keyword(Func));
let name = self.identifier()?; let name = self.identifier()?;
let params = delimited!(self, LParen, '(', formal_param, Comma, RParen, ')'); let params = delimited!(self, LParen, formal_param, Comma, RParen);
let type_anno = match self.peek() { let type_anno = match self.peek() {
Colon => Some(self.type_anno()?), Colon => Some(self.type_anno()?),
_ => None, _ => None,
@ -500,7 +500,7 @@ impl Parser {
}); });
parse_method!(signature_block(&mut self) -> ParseResult<Vec<Signature>> { parse_method!(signature_block(&mut self) -> ParseResult<Vec<Signature>> {
Ok(delimited!(self, LCurlyBrace, '{', signature, Newline | Semicolon, RCurlyBrace, '}', nonstrict)) Ok(delimited!(self, LCurlyBrace, signature, Newline | Semicolon, RCurlyBrace, nonstrict))
}); });
parse_method!(impl_declaration(&mut self) -> ParseResult<Declaration> { parse_method!(impl_declaration(&mut self) -> ParseResult<Declaration> {
@ -529,7 +529,7 @@ impl Parser {
}); });
parse_method!(decl_block(&mut self) -> ParseResult<Vec<Declaration>> { parse_method!(decl_block(&mut self) -> ParseResult<Vec<Declaration>> {
Ok(delimited!(self, LCurlyBrace, '{', func_declaration, Newline | Semicolon, RCurlyBrace, '}', nonstrict)) Ok(delimited!(self, LCurlyBrace, func_declaration, Newline | Semicolon, RCurlyBrace, nonstrict))
}); });
parse_method!(expression(&mut self) -> ParseResult<Expression> { parse_method!(expression(&mut self) -> ParseResult<Expression> {
@ -553,7 +553,7 @@ impl Parser {
parse_method!(type_name(&mut self) -> ParseResult<TypeName> { parse_method!(type_name(&mut self) -> ParseResult<TypeName> {
use self::TypeName::*; use self::TypeName::*;
Ok(match self.peek() { Ok(match self.peek() {
LParen => Tuple(delimited!(self, LParen, '(', type_name, Comma, RParen, ')')), LParen => Tuple(delimited!(self, LParen, type_name, Comma, RParen)),
_ => Singleton(self.type_singleton_name()?), _ => Singleton(self.type_singleton_name()?),
}) })
}); });
@ -562,7 +562,7 @@ impl Parser {
Ok(TypeSingletonName { Ok(TypeSingletonName {
name: self.identifier()?, name: self.identifier()?,
params: match self.peek() { params: match self.peek() {
LAngleBracket => delimited!(self, LAngleBracket, '<', type_name, Comma, RAngleBracket, '>'), LAngleBracket => delimited!(self, LAngleBracket, type_name, Comma, RAngleBracket),
_ => vec![], _ => vec![],
} }
}) })
@ -625,7 +625,7 @@ impl Parser {
parse_method!(call_expr(&mut self) -> ParseResult<Expression> { parse_method!(call_expr(&mut self) -> ParseResult<Expression> {
let index = self.index_expr()?; let index = self.index_expr()?;
Ok(if let LParen = self.peek() { Ok(if let LParen = self.peek() {
let arguments = delimited!(self, LParen, ')', expression, Comma, RParen, '('); let arguments = delimited!(self, LParen, expression, Comma, RParen);
Expression(ExpressionType::Call { f: bx!(index), arguments }, None) //TODO fix this none Expression(ExpressionType::Call { f: bx!(index), arguments }, None) //TODO fix this none
} else { } else {
index index
@ -635,7 +635,7 @@ impl Parser {
parse_method!(index_expr(&mut self) -> ParseResult<Expression> { parse_method!(index_expr(&mut self) -> ParseResult<Expression> {
let primary = self.primary()?; let primary = self.primary()?;
Ok(if let LSquareBracket = self.peek() { Ok(if let LSquareBracket = self.peek() {
let indexers = delimited!(self, LSquareBracket, '[', expression, Comma, RSquareBracket, ']'); let indexers = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket);
Expression(ExpressionType::Index { Expression(ExpressionType::Index {
indexee: bx!(Expression(primary.0, None)), indexee: bx!(Expression(primary.0, None)),
indexers, indexers,
@ -660,7 +660,7 @@ impl Parser {
}); });
parse_method!(list_expr(&mut self) -> ParseResult<Expression> { parse_method!(list_expr(&mut self) -> ParseResult<Expression> {
let exprs = delimited!(self, LSquareBracket, '[', expression, Comma, RSquareBracket, ']'); let exprs = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket);
Ok(Expression(ExpressionType::ListLiteral(exprs), None)) Ok(Expression(ExpressionType::ListLiteral(exprs), None))
}); });
@ -670,7 +670,7 @@ impl Parser {
parse_method!(lambda_expr(&mut self) -> ParseResult<Expression> { parse_method!(lambda_expr(&mut self) -> ParseResult<Expression> {
expect!(self, LCurlyBrace); expect!(self, LCurlyBrace);
let params = delimited!(self, Pipe, '|', formal_param, Comma, Pipe, '|'); let params = delimited!(self, Pipe, formal_param, Comma, Pipe);
let mut body = Vec::new(); let mut body = Vec::new();
loop { loop {
match self.peek() { match self.peek() {
@ -691,7 +691,7 @@ impl Parser {
let old_struct_value = self.restrictions.no_struct_literal; let old_struct_value = self.restrictions.no_struct_literal;
self.restrictions.no_struct_literal = false; self.restrictions.no_struct_literal = false;
let output = { let output = {
let mut inner = delimited!(self, LParen, '(', expression, Comma, RParen, ')'); let mut inner = delimited!(self, LParen, expression, Comma, RParen);
match inner.len() { match inner.len() {
0 => Ok(Expression(TupleLiteral(vec![]), None)), 0 => Ok(Expression(TupleLiteral(vec![]), None)),
1 => Ok(inner.pop().unwrap()), 1 => Ok(inner.pop().unwrap()),
@ -715,7 +715,7 @@ impl Parser {
}); });
parse_method!(record_block(&mut self) -> ParseResult<Vec<(Rc<String>, Expression)>> { parse_method!(record_block(&mut self) -> ParseResult<Vec<(Rc<String>, Expression)>> {
Ok(delimited!(self, LCurlyBrace, '{', record_entry, Comma, RCurlyBrace, '}')) Ok(delimited!(self, LCurlyBrace, record_entry, Comma, RCurlyBrace))
}); });
parse_method!(record_entry(&mut self) -> ParseResult<(Rc<String>, Expression)> { parse_method!(record_entry(&mut self) -> ParseResult<(Rc<String>, Expression)> {
@ -748,7 +748,7 @@ impl Parser {
}); });
parse_method!(block(&mut self) -> ParseResult<Block> { parse_method!(block(&mut self) -> ParseResult<Block> {
Ok(delimited!(self, LCurlyBrace, '{', statement, Newline | Semicolon, RCurlyBrace, '}', nonstrict)) Ok(delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict))
}); });
parse_method!(match_expr(&mut self) -> ParseResult<Expression> { parse_method!(match_expr(&mut self) -> ParseResult<Expression> {
@ -762,7 +762,7 @@ impl Parser {
}); });
parse_method!(match_body(&mut self) -> ParseResult<Vec<MatchArm>> { parse_method!(match_body(&mut self) -> ParseResult<Vec<MatchArm>> {
Ok(delimited!(self, LCurlyBrace, '{', match_arm, Comma, RCurlyBrace, '}')) Ok(delimited!(self, LCurlyBrace, match_arm, Comma, RCurlyBrace))
}); });
parse_method!(match_arm(&mut self) -> ParseResult<MatchArm> { parse_method!(match_arm(&mut self) -> ParseResult<MatchArm> {
@ -800,7 +800,7 @@ impl Parser {
parse_method!(for_expr(&mut self) -> ParseResult<Expression> { parse_method!(for_expr(&mut self) -> ParseResult<Expression> {
expect!(self, Keyword(Kw::For)); expect!(self, Keyword(Kw::For));
let enumerators = if let LCurlyBrace = self.peek() { let enumerators = if let LCurlyBrace = self.peek() {
delimited!(self, LCurlyBrace, '{', enumerator, Comma | Newline, RCurlyBrace, '}') delimited!(self, LCurlyBrace, enumerator, Comma | Newline, RCurlyBrace)
} else { } else {
let single_enum = { let single_enum = {
self.restrictions.no_struct_literal = true; self.restrictions.no_struct_literal = true;
@ -825,7 +825,7 @@ impl Parser {
use self::ForBody::*; use self::ForBody::*;
Ok(match self.peek() { Ok(match self.peek() {
LCurlyBrace => { LCurlyBrace => {
let statements = delimited!(self, LCurlyBrace, '{', statement, Newline | Semicolon, RCurlyBrace, '}', nonstrict); let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict);
StatementBlock(statements) StatementBlock(statements)
}, },
Keyword(Kw::Return) => { Keyword(Kw::Return) => {