2017-08-29 05:08:09 -07:00
|
|
|
use std::rc::Rc;
|
2018-02-23 01:58:06 -08:00
|
|
|
use std::iter::Peekable;
|
2017-09-09 01:25:11 -07:00
|
|
|
use std::vec::IntoIter;
|
2017-08-29 04:27:07 -07:00
|
|
|
|
2018-03-23 18:43:43 -07:00
|
|
|
use tokenizing::*;
|
|
|
|
use tokenizing::Kw::*;
|
|
|
|
use tokenizing::TokenType::*;
|
2017-09-06 09:42:29 -07:00
|
|
|
|
2018-06-04 19:25:40 -07:00
|
|
|
use ast::*;
|
|
|
|
|
2018-03-23 18:43:43 -07:00
|
|
|
use builtin::{BinOp, PrefixOp};
|
2018-02-23 04:10:00 -08:00
|
|
|
|
2018-07-11 02:32:29 -07:00
|
|
|
#[derive(Debug)]
|
2017-09-11 02:07:17 -07:00
|
|
|
pub struct ParseError {
|
|
|
|
pub msg: String,
|
2018-07-11 02:32:29 -07:00
|
|
|
pub token: Option<Token>
|
2017-09-11 02:07:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ParseError {
|
|
|
|
fn new<T>(msg: &str) -> ParseResult<T> {
|
2018-07-15 15:01:50 -07:00
|
|
|
Err(ParseError {
|
2018-07-11 02:32:29 -07:00
|
|
|
msg: msg.to_string(),
|
|
|
|
token: None
|
|
|
|
})
|
2017-09-11 02:07:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type ParseResult<T> = Result<T, ParseError>;
|
|
|
|
|
2017-09-15 16:35:45 -07:00
|
|
|
#[derive(Debug)]
|
2017-09-17 05:06:58 -07:00
|
|
|
pub struct ParseRecord {
|
|
|
|
production_name: String,
|
|
|
|
next_token: String,
|
2018-02-10 15:10:06 -08:00
|
|
|
level: u32,
|
2017-09-17 05:06:58 -07:00
|
|
|
}
|
2017-09-15 04:23:39 -07:00
|
|
|
|
2018-10-20 14:27:00 -07:00
|
|
|
pub struct Parser {
|
2018-10-21 16:33:21 -07:00
|
|
|
token_handler: TokenHandler,
|
2017-09-15 04:23:39 -07:00
|
|
|
parse_record: Vec<ParseRecord>,
|
2018-02-11 22:10:21 -08:00
|
|
|
parse_level: u32,
|
|
|
|
restrictions: ParserRestrictions,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ParserRestrictions {
|
|
|
|
no_struct_literal: bool
|
2017-09-09 01:25:11 -07:00
|
|
|
}
|
|
|
|
|
2018-10-21 16:33:21 -07:00
|
|
|
struct TokenHandler {
|
|
|
|
tokens: Peekable<IntoIter<Token>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenHandler {
|
|
|
|
fn new(tokens: Vec<Token>) -> TokenHandler {
|
|
|
|
let tokens = tokens.into_iter().peekable();
|
|
|
|
TokenHandler { tokens }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn peek(&mut self) -> TokenType {
|
|
|
|
self.tokens.peek().map(|ref t| { t.token_type.clone() }).unwrap_or(TokenType::EOF)
|
|
|
|
}
|
|
|
|
fn peek_with_token_offset(&mut self) -> Token {
|
|
|
|
self.tokens.peek().map(|t: &Token| { t.clone()}).unwrap_or(Token { token_type: TokenType::EOF, offset: (0,0)})
|
|
|
|
}
|
|
|
|
fn next(&mut self) -> TokenType {
|
|
|
|
self.tokens.next().map(|ref t| { t.token_type.clone() }).unwrap_or(TokenType::EOF)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-09 01:25:11 -07:00
|
|
|
impl Parser {
|
2018-10-20 15:41:09 -07:00
|
|
|
pub fn new(initial_input: Vec<Token>) -> Parser {
|
2018-02-11 22:10:21 -08:00
|
|
|
Parser {
|
2018-10-21 16:33:21 -07:00
|
|
|
token_handler: TokenHandler::new(initial_input),
|
2018-02-11 22:10:21 -08:00
|
|
|
parse_record: vec![],
|
|
|
|
parse_level: 0,
|
|
|
|
restrictions: ParserRestrictions { no_struct_literal: false }
|
|
|
|
}
|
2017-09-09 01:25:11 -07:00
|
|
|
}
|
|
|
|
|
2017-09-11 02:07:17 -07:00
|
|
|
fn peek(&mut self) -> TokenType {
|
2018-10-21 16:33:21 -07:00
|
|
|
self.token_handler.peek()
|
2017-09-09 01:27:15 -07:00
|
|
|
}
|
2018-03-02 00:42:52 -08:00
|
|
|
fn peek_with_token_offset(&mut self) -> Token {
|
2018-10-21 16:33:21 -07:00
|
|
|
self.token_handler.peek_with_token_offset()
|
2018-03-02 00:42:52 -08:00
|
|
|
}
|
2017-09-11 02:07:17 -07:00
|
|
|
fn next(&mut self) -> TokenType {
|
2018-10-21 16:33:21 -07:00
|
|
|
self.token_handler.next()
|
2017-09-09 01:25:11 -07:00
|
|
|
}
|
2018-10-20 14:27:00 -07:00
|
|
|
|
2018-10-20 15:41:09 -07:00
|
|
|
pub fn parse(&mut self) -> ParseResult<AST> {
|
|
|
|
self.program()
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
pub fn parse_with_new_tokens(&mut self, new_tokens: Vec<Token>) -> ParseResult<AST> {
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2018-10-20 14:27:00 -07:00
|
|
|
pub fn format_parse_trace(self) -> Vec<String> {
|
|
|
|
self.parse_record.into_iter().map(|r| {
|
|
|
|
let mut indent = String::new();
|
|
|
|
for _ in 0..r.level {
|
|
|
|
indent.push(' ');
|
|
|
|
}
|
|
|
|
format!("{}Production `{}`, token: {}", indent, r.production_name, r.next_token)
|
|
|
|
}).collect()
|
|
|
|
}
|
2017-09-08 16:42:42 -07:00
|
|
|
}
|
|
|
|
|
2018-05-13 02:42:43 -07:00
|
|
|
macro_rules! print_token_pattern {
|
|
|
|
($tokenpattern:pat) => { stringify!($tokenpattern) }
|
|
|
|
}
|
|
|
|
|
2017-09-11 02:07:17 -07:00
|
|
|
macro_rules! expect {
|
2018-05-13 02:42:43 -07:00
|
|
|
($self:expr, $token_type:pat) => { expect!($self, $token_type if true) };
|
|
|
|
($self:expr, $token_type:pat if $cond:expr) => {
|
2017-09-13 22:40:05 -07:00
|
|
|
match $self.peek() {
|
|
|
|
$token_type if $cond => $self.next(),
|
2017-09-30 14:41:37 -07:00
|
|
|
tok => {
|
2018-05-13 02:42:43 -07:00
|
|
|
let msg = format!("Expected {}, got {:?}", print_token_pattern!($token_type), tok);
|
2018-07-11 02:32:29 -07:00
|
|
|
return ParseError::new(&msg);
|
2017-09-30 14:41:37 -07:00
|
|
|
}
|
2017-09-13 22:40:05 -07:00
|
|
|
}
|
2017-09-11 02:07:17 -07:00
|
|
|
}
|
2017-09-09 00:31:15 -07:00
|
|
|
}
|
|
|
|
|
2017-09-28 23:55:10 -07:00
|
|
|
macro_rules! delimited {
|
2018-05-13 02:50:43 -07:00
|
|
|
($self:expr, $start:pat, $parse_fn:ident, $( $delim:pat )|+, $end:pat, nonstrict) => {
|
|
|
|
delimited!($self, $start, $parse_fn, $( $delim )|*, $end, false)
|
2017-09-30 13:04:56 -07:00
|
|
|
};
|
2018-05-13 02:50:43 -07:00
|
|
|
($self:expr, $start:pat, $parse_fn:ident, $( $delim:pat )|+, $end:pat) => {
|
|
|
|
delimited!($self, $start, $parse_fn, $( $delim )|*, $end, true)
|
2017-09-30 13:04:56 -07:00
|
|
|
};
|
2018-05-13 02:50:43 -07:00
|
|
|
($self:expr, $start:pat, $parse_fn:ident, $( $delim:pat )|+, $end:pat, $strictness:expr) => {
|
2017-09-28 23:55:10 -07:00
|
|
|
{
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!($self, $start);
|
2017-09-29 14:10:49 -07:00
|
|
|
let mut acc = vec![];
|
|
|
|
loop {
|
2017-09-30 13:46:50 -07:00
|
|
|
let peek = $self.peek();
|
|
|
|
match peek {
|
|
|
|
$end | EOF => break,
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
if !$strictness {
|
|
|
|
match peek {
|
|
|
|
$( $delim )|* => { $self.next(); continue },
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
2017-09-29 14:10:49 -07:00
|
|
|
acc.push($self.$parse_fn()?);
|
|
|
|
match $self.peek() {
|
2017-09-30 13:04:56 -07:00
|
|
|
$( $delim )|* => { $self.next(); continue },
|
2018-03-02 22:40:12 -08:00
|
|
|
_ if $strictness => break,
|
|
|
|
_ => continue,
|
2017-09-29 14:10:49 -07:00
|
|
|
};
|
|
|
|
}
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!($self, $end);
|
2017-09-29 14:10:49 -07:00
|
|
|
acc
|
2017-09-28 23:55:10 -07:00
|
|
|
}
|
2017-09-30 13:04:56 -07:00
|
|
|
};
|
2017-09-28 23:55:10 -07:00
|
|
|
}
|
|
|
|
|
2018-07-14 00:47:16 -07:00
|
|
|
/* Schala EBNF Grammar */
|
|
|
|
/* Terminal productions are in 'single quotes' or UPPERCASE if they are a class
|
|
|
|
* or not representable in ASCII
|
|
|
|
|
|
|
|
/* Top-level Structure */
|
|
|
|
|
|
|
|
program := (statement delimiter)* EOF
|
|
|
|
delimiter := NEWLINE | ';'
|
|
|
|
statement := expression | declaration
|
|
|
|
block := '{' (statement delimiter)* '}'
|
|
|
|
|
|
|
|
declaration := type_declaration | func_declaration | binding_declaration | impl_declaration
|
|
|
|
|
|
|
|
/* Declarations - Types */
|
|
|
|
|
|
|
|
type_declaration := 'type' type_declaration_body
|
|
|
|
type_declaration_body := 'alias' type_alias | 'mut'? type_singleton_name '=' type_body
|
|
|
|
type_alias := IDENTIFIER '=' type_name
|
|
|
|
type_body := variant_specifier ('|' variant_specifier)*
|
|
|
|
variant_specifier := IDENTIFIER | IDENTIFIER '{' typed_identifier_list '}' | IDENTIFIER '(' type_name* ')'
|
|
|
|
typed_identifier_list := typed_identifier*
|
|
|
|
typed_identifier := IDENTIFIER type_anno
|
|
|
|
|
|
|
|
/* Declaration - Functions */
|
|
|
|
|
|
|
|
func_declaration := func_signature func_body
|
|
|
|
func_body := ε | '{' (statement delimiter)* '}'
|
2018-08-24 16:49:59 -07:00
|
|
|
func_signature := 'fn' func_name formal_param_list func_body
|
|
|
|
func_name := IDENTIFIER | operator
|
2018-07-14 00:47:16 -07:00
|
|
|
formal_param_list := '(' (formal_param ',')* ')'
|
|
|
|
formal_param := IDENTIFIER type_anno+
|
|
|
|
|
|
|
|
/* Declaration - Variable bindings */
|
|
|
|
binding_declaration := 'let' 'mut'? IDENTIFIER '=' expresion
|
|
|
|
|
|
|
|
/* Declaration - Interface */
|
|
|
|
|
|
|
|
interface_declaration := 'interface' interface_name signature_block
|
|
|
|
impl_declaration := 'impl' IDENTIFIER decl_block | 'impl' interface_name 'for' IDENTIFIER decl_block
|
|
|
|
decl_block := '{' (func_declaration)* '}'
|
|
|
|
signature_block := '{' (func_signature)* '}'
|
|
|
|
interface_name := IDENTIFIER
|
|
|
|
|
|
|
|
/* Type annotations */
|
|
|
|
|
|
|
|
type_anno := (':' type_name)+
|
|
|
|
type_name := type_singleton_name | '(' type_names ')'
|
|
|
|
type_names := ε | type_name (, type_name)*
|
|
|
|
type_singleton_name = IDENTIFIER (type_params)*
|
|
|
|
type_params := '<' type_name (, type_name)* '>'
|
|
|
|
|
|
|
|
|
|
|
|
/* Expressions */
|
|
|
|
|
|
|
|
expression := precedence_expr type_anno+
|
|
|
|
precedence_expr := prefix_expr
|
|
|
|
prefix_expr := prefix_op call_expr
|
|
|
|
prefix_op := '+' | '-' | '!' | '~'
|
|
|
|
call_expr := index_expr ( '(' expr_list ')' )*
|
|
|
|
expr_list := expression (',' expression)* | ε
|
|
|
|
index_expr := primary ( '[' (expression (',' (expression)* | ε) ']' )*
|
|
|
|
primary := literal | paren_expr | if_expr | for_expr | while_expr | identifier_expr | curly_brace_expr | list_expr
|
|
|
|
|
|
|
|
/* Primary Expressions */
|
|
|
|
|
|
|
|
curly_brace_expr := lambda_expr | anonymous_struct //TODO
|
|
|
|
list_expr := '[' (expression, ',')* ']'
|
|
|
|
lambda_expr := '{' '|' (formal_param ',')* '|' (type_anno)* (statement delimiter)* '}'
|
|
|
|
paren_expr := LParen paren_inner RParen
|
|
|
|
paren_inner := (expression ',')*
|
|
|
|
identifier_expr := named_struct | IDENTIFIER
|
|
|
|
|
|
|
|
/* Expression - Literals */
|
|
|
|
|
|
|
|
literal := 'true' | 'false' | number_literal | STR_LITERAL
|
|
|
|
named_struct := IDENTIFIER record_block
|
|
|
|
record_block := '{' (record_entry, ',')* | '}' //TODO support anonymus structs, update syntax
|
|
|
|
record_entry := IDENTIFIER ':' expression
|
|
|
|
anonymous_struct := TODO
|
|
|
|
|
|
|
|
// a float_literal can still be assigned to an int in type-checking
|
|
|
|
number_literal := int_literal | float_literal
|
|
|
|
int_literal = ('0x' | '0b') digits
|
|
|
|
float_literal := digits ('.' digits)
|
|
|
|
digits := (DIGIT_GROUP underscore)+
|
|
|
|
|
|
|
|
/* Pattern syntax */
|
|
|
|
pattern := '(' (pattern, ',')* ')' | simple_pattern
|
|
|
|
simple_pattern := pattern_literal | record_pattern | tuple_struct_pattern
|
2018-08-21 19:57:45 -07:00
|
|
|
pattern_literal := 'true' | 'false' | signed_number_literal | STR_LITERAL | IDENTIFIER
|
|
|
|
signed_number_literal := '-'? number_literal
|
2018-07-14 00:47:16 -07:00
|
|
|
record_pattern := IDENTIFIER '{' (record_pattern_entry, ',')* '}'
|
|
|
|
record_pattern_entry := IDENTIFIER | IDENTIFIER ':' Pattern
|
|
|
|
tuple_struct_pattern := IDENTIFIER '(' (pattern, ',')* ')'
|
|
|
|
|
2018-08-15 09:34:00 -07:00
|
|
|
expr_or_block := '{' (statement delimiter)* '}' | expr
|
|
|
|
|
2018-07-14 00:47:16 -07:00
|
|
|
/* Expression - If */
|
|
|
|
if_expr := 'if' discriminator ('then' condititional | 'is' simple_pattern_match | guard_block)
|
2018-08-19 18:44:54 -07:00
|
|
|
discriminator := precedence_expr (operator)+
|
2018-08-19 15:58:31 -07:00
|
|
|
conditional := expr_or_block else_clause
|
2018-07-14 00:47:16 -07:00
|
|
|
simple_pattern_match := pattern 'then' conditional
|
2018-08-19 15:58:31 -07:00
|
|
|
else_clause := ε | 'else' expr_or_block
|
2018-07-14 00:47:16 -07:00
|
|
|
guard_block := '{' (guard_arm, ',')* '}'
|
2018-08-19 15:58:31 -07:00
|
|
|
guard_arm := guard '->' expr_or_block
|
2018-07-14 00:47:16 -07:00
|
|
|
guard := 'is' pattern | (operator)+ precedence_expr
|
|
|
|
|
|
|
|
/* Expression - While */
|
|
|
|
while_expr := 'while' while_cond '{' (statement delimiter)* '}'
|
|
|
|
while_cond := ε | expression | expression 'is' pattern //TODO maybe is-expresions should be primary
|
|
|
|
|
|
|
|
//TODO this implies there must be at least one enumerator, which the parser doesn't support right
|
|
|
|
//this second, and maybe should fail later anyway
|
|
|
|
/* Expression - For */
|
|
|
|
for_expr := 'for' (enumerator | '{' enumerators '}') for_expr_body
|
|
|
|
for_expr_body := 'return' expression | '{' (statement delimiter)* '}
|
|
|
|
enumerators := enumerator (',' enumerators)*
|
|
|
|
enumerator := identifier '<-' expression | identifier '=' expression //TODO add guards, etc.
|
|
|
|
*/
|
|
|
|
|
2017-09-11 02:07:17 -07:00
|
|
|
impl Parser {
|
2018-10-20 14:27:00 -07:00
|
|
|
//TODO make this a proper public interface
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
2018-10-20 15:41:09 -07:00
|
|
|
fn program(&mut self) -> ParseResult<AST> {
|
2017-09-15 04:23:39 -07:00
|
|
|
let mut statements = Vec::new();
|
|
|
|
loop {
|
|
|
|
match self.peek() {
|
|
|
|
EOF => break,
|
|
|
|
Newline | Semicolon => {
|
|
|
|
self.next();
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
_ => statements.push(self.statement()?),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(AST(statements))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-11 02:07:17 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn statement(&mut self) -> ParseResult<Statement> {
|
2017-09-11 02:07:17 -07:00
|
|
|
//TODO handle error recovery here
|
|
|
|
match self.peek() {
|
|
|
|
Keyword(Type) => self.type_declaration().map(|decl| { Statement::Declaration(decl) }),
|
|
|
|
Keyword(Func)=> self.func_declaration().map(|func| { Statement::Declaration(func) }),
|
2018-07-11 16:44:15 -07:00
|
|
|
Keyword(Let) => self.binding_declaration().map(|decl| Statement::Declaration(decl)),
|
2018-04-24 16:30:17 -07:00
|
|
|
Keyword(Interface) => self.interface_declaration().map(|decl| Statement::Declaration(decl)),
|
2017-10-03 03:49:07 -07:00
|
|
|
Keyword(Impl) => self.impl_declaration().map(|decl| Statement::Declaration(decl)),
|
2017-09-28 00:06:08 -07:00
|
|
|
_ => self.expression().map(|expr| { Statement::ExpressionStatement(expr) } ),
|
2017-09-11 02:07:17 -07:00
|
|
|
}
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-11 02:07:17 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn type_declaration(&mut self) -> ParseResult<Declaration> {
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Keyword(Type));
|
2017-10-04 22:02:31 -07:00
|
|
|
self.type_declaration_body()
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-10-04 22:02:31 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn type_declaration_body(&mut self) -> ParseResult<Declaration> {
|
2017-10-04 22:02:31 -07:00
|
|
|
if let Keyword(Alias) = self.peek() {
|
|
|
|
self.type_alias()
|
|
|
|
} else {
|
2018-07-12 02:07:52 -07:00
|
|
|
let mutable = if let Keyword(Mut) = self.peek() {
|
2018-07-11 20:40:55 -07:00
|
|
|
self.next();
|
2018-07-12 02:07:52 -07:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2018-02-12 00:51:53 -08:00
|
|
|
let name = self.type_singleton_name()?;
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Operator(ref c) if **c == "=");
|
2017-10-04 22:02:31 -07:00
|
|
|
let body = self.type_body()?;
|
2018-07-12 02:07:52 -07:00
|
|
|
Ok(Declaration::TypeDecl { name, body, mutable})
|
2017-10-04 22:02:31 -07:00
|
|
|
}
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-10-04 22:02:31 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn type_alias(&mut self) -> ParseResult<Declaration> {
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Keyword(Alias));
|
2017-09-13 22:40:05 -07:00
|
|
|
let alias = self.identifier()?;
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Operator(ref c) if **c == "=");
|
2017-09-13 22:40:05 -07:00
|
|
|
let original = self.identifier()?;
|
|
|
|
Ok(Declaration::TypeAlias(alias, original))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-13 22:40:05 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn type_body(&mut self) -> ParseResult<TypeBody> {
|
2017-10-08 18:47:57 -07:00
|
|
|
let mut variants = Vec::new();
|
|
|
|
variants.push(self.variant_specifier()?);
|
|
|
|
loop {
|
|
|
|
if let Pipe = self.peek() {
|
|
|
|
self.next();
|
|
|
|
variants.push(self.variant_specifier()?);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(TypeBody(variants))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-10-08 18:47:57 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn variant_specifier(&mut self) -> ParseResult<Variant> {
|
2017-10-08 19:02:52 -07:00
|
|
|
use self::Variant::*;
|
|
|
|
|
|
|
|
let name = self.identifier()?;
|
|
|
|
match self.peek() {
|
|
|
|
LParen => {
|
2018-05-13 02:50:43 -07:00
|
|
|
let tuple_members = delimited!(self, LParen, type_name, Comma, RParen);
|
2017-10-08 19:02:52 -07:00
|
|
|
Ok(TupleStruct(name, tuple_members))
|
|
|
|
},
|
|
|
|
LCurlyBrace => {
|
2018-05-13 02:50:43 -07:00
|
|
|
let typed_identifier_list = delimited!(self, LCurlyBrace, typed_identifier, Comma, RCurlyBrace);
|
2017-10-08 19:02:52 -07:00
|
|
|
Ok(Record(name, typed_identifier_list))
|
|
|
|
},
|
2017-10-08 19:15:08 -07:00
|
|
|
_ => Ok(UnitStruct(name))
|
2017-10-08 19:02:52 -07:00
|
|
|
}
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-10-08 19:02:52 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn typed_identifier(&mut self) -> ParseResult<(Rc<String>, TypeIdentifier)> {
|
2017-10-08 19:02:52 -07:00
|
|
|
let identifier = self.identifier()?;
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Colon);
|
2017-10-08 19:02:52 -07:00
|
|
|
let type_name = self.type_name()?;
|
|
|
|
Ok((identifier, type_name))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-11 02:07:17 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn func_declaration(&mut self) -> ParseResult<Declaration> {
|
2018-08-24 16:49:59 -07:00
|
|
|
let signature = self.func_signature()?;
|
2017-10-08 21:21:02 -07:00
|
|
|
if let LCurlyBrace = self.peek() {
|
2018-05-13 02:50:43 -07:00
|
|
|
let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict);
|
2017-10-08 21:21:02 -07:00
|
|
|
Ok(Declaration::FuncDecl(signature, statements))
|
|
|
|
} else {
|
|
|
|
Ok(Declaration::FuncSig(signature))
|
|
|
|
}
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-10-08 21:21:02 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn func_signature(&mut self) -> ParseResult<Signature> {
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Keyword(Func));
|
2018-08-24 16:49:59 -07:00
|
|
|
let (name, operator) = match self.peek() {
|
|
|
|
Operator(s) => {
|
|
|
|
let name = s.clone();
|
|
|
|
self.next();
|
|
|
|
(name, true)
|
|
|
|
},
|
|
|
|
_ => (self.identifier()?, false)
|
|
|
|
};
|
2018-05-13 02:50:43 -07:00
|
|
|
let params = delimited!(self, LParen, formal_param, Comma, RParen);
|
2017-10-08 21:21:02 -07:00
|
|
|
let type_anno = match self.peek() {
|
|
|
|
Colon => Some(self.type_anno()?),
|
|
|
|
_ => None,
|
|
|
|
};
|
2018-08-24 16:49:59 -07:00
|
|
|
Ok(Signature { name, operator, params, type_anno })
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-11 20:37:19 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn formal_param(&mut self) -> ParseResult<FormalParam> {
|
2017-10-08 20:55:05 -07:00
|
|
|
let name = self.identifier()?;
|
|
|
|
let ty = match self.peek() {
|
|
|
|
Colon => Some(self.type_anno()?),
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
Ok((name, ty))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-07-11 16:44:15 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn binding_declaration(&mut self) -> ParseResult<Declaration> {
|
2018-07-11 16:44:15 -07:00
|
|
|
expect!(self, Keyword(Kw::Let));
|
|
|
|
let constant = match self.peek() {
|
|
|
|
Keyword(Kw::Mut) => {
|
|
|
|
self.next();
|
|
|
|
false
|
|
|
|
}
|
|
|
|
_ => true
|
2017-09-17 06:04:24 -07:00
|
|
|
};
|
|
|
|
let name = self.identifier()?;
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Operator(ref o) if **o == "=");
|
2017-09-17 06:04:24 -07:00
|
|
|
let expr = self.expression()?;
|
|
|
|
|
|
|
|
Ok(Declaration::Binding { name, constant, expr })
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-17 06:04:24 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn interface_declaration(&mut self) -> ParseResult<Declaration> {
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Keyword(Interface));
|
2018-02-21 22:06:56 -08:00
|
|
|
let name = self.identifier()?;
|
|
|
|
let signatures = self.signature_block()?;
|
2018-04-24 16:30:17 -07:00
|
|
|
Ok(Declaration::Interface { name, signatures })
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-02-21 22:06:56 -08:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn signature_block(&mut self) -> ParseResult<Vec<Signature>> {
|
2018-08-24 16:49:59 -07:00
|
|
|
Ok(delimited!(self, LCurlyBrace, func_signature, Newline | Semicolon, RCurlyBrace, nonstrict))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-10-03 03:49:07 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn impl_declaration(&mut self) -> ParseResult<Declaration> {
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Keyword(Impl));
|
2017-10-08 19:30:52 -07:00
|
|
|
let first = self.type_name()?;
|
2017-10-03 03:49:07 -07:00
|
|
|
let second = if let Keyword(For) = self.peek() {
|
|
|
|
self.next();
|
2017-10-08 19:30:52 -07:00
|
|
|
Some(self.type_name()?)
|
2017-10-03 03:49:07 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2017-10-08 14:24:02 -07:00
|
|
|
|
|
|
|
let block = self.decl_block()?;
|
|
|
|
|
|
|
|
let result = match (first, second) {
|
2017-10-08 19:30:52 -07:00
|
|
|
(first, Some(second)) => {
|
|
|
|
match first {
|
2018-10-18 13:27:09 -07:00
|
|
|
TypeIdentifier::Singleton(TypeSingletonName { ref name, ref params }) if params.len() == 0 =>
|
2018-04-24 16:30:17 -07:00
|
|
|
Declaration::Impl { type_name: second, interface_name: Some(name.clone()), block },
|
|
|
|
_ => return ParseError::new(&format!("Invalid name for an interface")),
|
2017-10-08 19:30:52 -07:00
|
|
|
}
|
|
|
|
},
|
2018-04-24 16:30:17 -07:00
|
|
|
(first, None) => Declaration::Impl { type_name: first, interface_name: None, block }
|
2017-10-08 14:24:02 -07:00
|
|
|
};
|
|
|
|
Ok(result)
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-10-08 14:24:02 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn decl_block(&mut self) -> ParseResult<Vec<Declaration>> {
|
2018-05-13 02:50:43 -07:00
|
|
|
Ok(delimited!(self, LCurlyBrace, func_declaration, Newline | Semicolon, RCurlyBrace, nonstrict))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-10-03 03:49:07 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn expression(&mut self) -> ParseResult<Expression> {
|
2018-02-23 04:10:00 -08:00
|
|
|
let mut expr_body = self.precedence_expr(BinOp::min_precedence())?;
|
2017-09-26 22:10:13 -07:00
|
|
|
let type_anno = match self.peek() {
|
2017-09-28 23:55:10 -07:00
|
|
|
Colon => Some(self.type_anno()?),
|
2017-09-26 22:10:13 -07:00
|
|
|
_ => None
|
|
|
|
};
|
2017-10-01 00:50:13 -07:00
|
|
|
if let Some(_) = expr_body.1 {
|
2017-09-30 23:30:02 -07:00
|
|
|
return ParseError::new("Bad parse state");
|
2017-09-27 22:27:50 -07:00
|
|
|
}
|
|
|
|
expr_body.1 = type_anno;
|
|
|
|
Ok(expr_body)
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-26 22:10:13 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn type_anno(&mut self) -> ParseResult<TypeIdentifier> {
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Colon);
|
2017-09-29 14:10:49 -07:00
|
|
|
self.type_name()
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-29 14:10:49 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn type_name(&mut self) -> ParseResult<TypeIdentifier> {
|
2018-10-18 13:27:09 -07:00
|
|
|
use self::TypeIdentifier::*;
|
2017-09-28 23:55:10 -07:00
|
|
|
Ok(match self.peek() {
|
2018-05-13 02:50:43 -07:00
|
|
|
LParen => Tuple(delimited!(self, LParen, type_name, Comma, RParen)),
|
2018-02-12 00:51:53 -08:00
|
|
|
_ => Singleton(self.type_singleton_name()?),
|
|
|
|
})
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-02-12 00:51:53 -08:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn type_singleton_name(&mut self) -> ParseResult<TypeSingletonName> {
|
2018-02-12 00:51:53 -08:00
|
|
|
Ok(TypeSingletonName {
|
|
|
|
name: self.identifier()?,
|
|
|
|
params: match self.peek() {
|
2018-05-13 02:50:43 -07:00
|
|
|
LAngleBracket => delimited!(self, LAngleBracket, type_name, Comma, RAngleBracket),
|
2018-02-12 00:51:53 -08:00
|
|
|
_ => vec![],
|
|
|
|
}
|
2017-09-28 23:55:10 -07:00
|
|
|
})
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-12 05:16:37 -07:00
|
|
|
|
|
|
|
// this implements Pratt parsing, see http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/
|
|
|
|
fn precedence_expr(&mut self, precedence: i32) -> ParseResult<Expression> {
|
2017-09-17 05:06:58 -07:00
|
|
|
let record = ParseRecord {
|
|
|
|
production_name: "precedence_expr".to_string(),
|
2018-03-02 22:11:25 -08:00
|
|
|
next_token: format!("{}", self.peek_with_token_offset().to_string_with_metadata()),
|
2018-02-10 15:10:06 -08:00
|
|
|
level: self.parse_level,
|
2017-09-17 05:06:58 -07:00
|
|
|
};
|
2018-02-10 17:45:00 -08:00
|
|
|
self.parse_level += 1;
|
2017-09-17 05:06:58 -07:00
|
|
|
self.parse_record.push(record);
|
|
|
|
|
2017-09-17 17:44:06 -07:00
|
|
|
let mut lhs = self.prefix_expr()?;
|
2017-09-12 05:16:37 -07:00
|
|
|
loop {
|
2018-08-19 20:33:50 -07:00
|
|
|
let new_precedence = match BinOp::get_precedence_from_token(&self.peek()) {
|
|
|
|
Some(p) => p,
|
|
|
|
None => break,
|
2017-09-12 05:16:37 -07:00
|
|
|
};
|
2017-09-17 05:12:20 -07:00
|
|
|
|
2017-09-12 05:16:37 -07:00
|
|
|
if precedence >= new_precedence {
|
|
|
|
break;
|
|
|
|
}
|
2018-08-19 20:33:50 -07:00
|
|
|
let operation = match BinOp::from_sigil_token(&self.next()) {
|
|
|
|
Some(sigil) => sigil,
|
|
|
|
None => unreachable!()
|
2017-09-12 05:16:37 -07:00
|
|
|
};
|
|
|
|
let rhs = self.precedence_expr(new_precedence)?;
|
2018-02-23 03:04:19 -08:00
|
|
|
lhs = Expression(ExpressionType::BinExp(operation, bx!(lhs), bx!(rhs)), None);
|
2017-09-12 05:16:37 -07:00
|
|
|
}
|
2018-02-10 17:45:00 -08:00
|
|
|
self.parse_level -= 1;
|
2017-09-12 02:30:27 -07:00
|
|
|
Ok(lhs)
|
2017-09-11 02:07:17 -07:00
|
|
|
}
|
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn prefix_expr(&mut self) -> ParseResult<Expression> {
|
2017-09-17 17:44:06 -07:00
|
|
|
match self.peek() {
|
2018-02-23 04:10:00 -08:00
|
|
|
Operator(ref op) if PrefixOp::is_prefix(&*op) => {
|
|
|
|
let sigil = match self.next() {
|
2017-09-17 17:44:06 -07:00
|
|
|
Operator(op) => op,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let expr = self.primary()?;
|
2017-09-27 22:27:50 -07:00
|
|
|
Ok(Expression(
|
2018-02-24 17:50:57 -08:00
|
|
|
ExpressionType::PrefixExp(PrefixOp::from_sigil(sigil.as_str()), bx!(expr)),
|
2017-09-27 22:27:50 -07:00
|
|
|
None))
|
2017-09-17 17:44:06 -07:00
|
|
|
},
|
2018-03-06 00:38:33 -08:00
|
|
|
_ => self.call_expr()
|
2017-09-17 17:44:06 -07:00
|
|
|
}
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-17 17:44:06 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn call_expr(&mut self) -> ParseResult<Expression> {
|
2018-03-06 02:51:45 -08:00
|
|
|
let index = self.index_expr()?;
|
2018-03-06 00:38:33 -08:00
|
|
|
Ok(if let LParen = self.peek() {
|
2018-05-13 02:50:43 -07:00
|
|
|
let arguments = delimited!(self, LParen, expression, Comma, RParen);
|
2018-03-06 02:51:45 -08:00
|
|
|
Expression(ExpressionType::Call { f: bx!(index), arguments }, None) //TODO fix this none
|
|
|
|
} else {
|
|
|
|
index
|
|
|
|
})
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-03-06 02:51:45 -08:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn index_expr(&mut self) -> ParseResult<Expression> {
|
2018-03-06 02:51:45 -08:00
|
|
|
let primary = self.primary()?;
|
|
|
|
Ok(if let LSquareBracket = self.peek() {
|
2018-05-13 02:50:43 -07:00
|
|
|
let indexers = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket);
|
2018-03-06 02:51:45 -08:00
|
|
|
Expression(ExpressionType::Index {
|
|
|
|
indexee: bx!(Expression(primary.0, None)),
|
|
|
|
indexers,
|
|
|
|
}, None)
|
2018-03-06 00:38:33 -08:00
|
|
|
} else {
|
|
|
|
primary
|
|
|
|
})
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-03-06 00:38:33 -08:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn primary(&mut self) -> ParseResult<Expression> {
|
2017-09-13 03:46:16 -07:00
|
|
|
match self.peek() {
|
2018-03-04 02:11:22 -08:00
|
|
|
LCurlyBrace => self.curly_brace_expr(),
|
2017-09-13 03:46:16 -07:00
|
|
|
LParen => self.paren_expr(),
|
2018-03-08 12:01:24 -08:00
|
|
|
LSquareBracket => self.list_expr(),
|
2017-09-18 23:22:28 -07:00
|
|
|
Keyword(Kw::If) => self.if_expr(),
|
2017-10-08 22:02:58 -07:00
|
|
|
Keyword(Kw::For) => self.for_expr(),
|
2018-05-12 23:49:02 -07:00
|
|
|
Keyword(Kw::While) => self.while_expr(),
|
2017-09-13 20:10:06 -07:00
|
|
|
Identifier(_) => self.identifier_expr(),
|
2017-09-13 03:46:16 -07:00
|
|
|
_ => self.literal(),
|
|
|
|
}
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-13 03:46:16 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn list_expr(&mut self) -> ParseResult<Expression> {
|
2018-05-13 02:50:43 -07:00
|
|
|
let exprs = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket);
|
2018-03-08 12:01:24 -08:00
|
|
|
Ok(Expression(ExpressionType::ListLiteral(exprs), None))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-03-08 12:01:24 -08:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn curly_brace_expr(&mut self) -> ParseResult<Expression> {
|
2018-03-04 02:11:22 -08:00
|
|
|
self.lambda_expr()
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-03-04 02:11:22 -08:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn lambda_expr(&mut self) -> ParseResult<Expression> {
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, LCurlyBrace);
|
2018-05-13 02:50:43 -07:00
|
|
|
let params = delimited!(self, Pipe, formal_param, Comma, Pipe);
|
2018-03-04 02:11:22 -08:00
|
|
|
let mut body = Vec::new();
|
|
|
|
loop {
|
|
|
|
match self.peek() {
|
|
|
|
EOF | RCurlyBrace => break,
|
|
|
|
Newline | Semicolon => {
|
|
|
|
self.next();
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
_ => body.push(self.statement()?),
|
|
|
|
}
|
|
|
|
}
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, RCurlyBrace);
|
2018-03-04 02:11:22 -08:00
|
|
|
Ok(Expression(ExpressionType::Lambda { params, body }, None)) //TODO need to handle types somehow
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-03-04 02:11:22 -08:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn paren_expr(&mut self) -> ParseResult<Expression> {
|
2017-10-06 20:28:07 -07:00
|
|
|
use self::ExpressionType::*;
|
2018-02-11 22:10:21 -08:00
|
|
|
let old_struct_value = self.restrictions.no_struct_literal;
|
|
|
|
self.restrictions.no_struct_literal = false;
|
2018-05-13 01:43:29 -07:00
|
|
|
let output = {
|
2018-05-13 02:50:43 -07:00
|
|
|
let mut inner = delimited!(self, LParen, expression, Comma, RParen);
|
2018-02-11 22:10:21 -08:00
|
|
|
match inner.len() {
|
|
|
|
0 => Ok(Expression(TupleLiteral(vec![]), None)),
|
|
|
|
1 => Ok(inner.pop().unwrap()),
|
|
|
|
_ => Ok(Expression(TupleLiteral(inner), None)),
|
|
|
|
}
|
2018-05-13 01:43:29 -07:00
|
|
|
};
|
2018-02-11 22:10:21 -08:00
|
|
|
self.restrictions.no_struct_literal = old_struct_value;
|
|
|
|
output
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-11 15:42:49 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn identifier_expr(&mut self) -> ParseResult<Expression> {
|
2017-09-27 22:27:50 -07:00
|
|
|
use self::ExpressionType::*;
|
2017-09-13 20:10:06 -07:00
|
|
|
let identifier = self.identifier()?;
|
2017-10-08 22:52:05 -07:00
|
|
|
Ok(match self.peek() {
|
2018-02-11 22:10:21 -08:00
|
|
|
LCurlyBrace if !self.restrictions.no_struct_literal => {
|
2017-10-14 13:54:17 -07:00
|
|
|
let fields = self.record_block()?;
|
2018-02-26 18:12:37 -08:00
|
|
|
Expression(NamedStruct { name: identifier, fields }, None)
|
2017-10-13 18:56:02 -07:00
|
|
|
},
|
2018-02-26 18:12:37 -08:00
|
|
|
_ => Expression(Value(identifier), None)
|
2017-10-08 22:52:05 -07:00
|
|
|
})
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-13 20:10:06 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn record_block(&mut self) -> ParseResult<Vec<(Rc<String>, Expression)>> {
|
2018-05-13 02:50:43 -07:00
|
|
|
Ok(delimited!(self, LCurlyBrace, record_entry, Comma, RCurlyBrace))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-10-14 13:54:17 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn record_entry(&mut self) -> ParseResult<(Rc<String>, Expression)> {
|
2017-10-14 13:54:17 -07:00
|
|
|
let field_name = self.identifier()?;
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Colon);
|
2017-10-14 13:54:17 -07:00
|
|
|
let value = self.expression()?;
|
|
|
|
Ok((field_name, value))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-10-14 13:54:17 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn if_expr(&mut self) -> ParseResult<Expression> {
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Keyword(Kw::If));
|
2018-06-19 02:05:25 -07:00
|
|
|
let discriminator = Box::new({
|
2018-02-11 22:10:21 -08:00
|
|
|
self.restrictions.no_struct_literal = true;
|
2018-06-19 02:05:25 -07:00
|
|
|
let x = self.discriminator();
|
2018-02-11 22:10:21 -08:00
|
|
|
self.restrictions.no_struct_literal = false;
|
2018-05-12 20:51:30 -07:00
|
|
|
x?
|
2018-06-19 02:05:25 -07:00
|
|
|
});
|
|
|
|
|
2018-06-20 02:07:11 -07:00
|
|
|
let body = Box::new(match self.peek() {
|
|
|
|
Keyword(Kw::Then) => self.conditional()?,
|
|
|
|
Keyword(Kw::Is) => self.simple_pattern_match()? ,
|
|
|
|
_ => self.guard_block()?
|
2018-06-19 02:05:25 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(Expression(ExpressionType::IfExpression { discriminator, body }, None))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-06-19 02:05:25 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn discriminator(&mut self) -> ParseResult<Discriminator> {
|
2018-08-19 18:44:54 -07:00
|
|
|
let lhs = self.prefix_expr()?;
|
2018-08-19 21:25:07 -07:00
|
|
|
let ref next = self.peek();
|
|
|
|
Ok(if let Some(op) = BinOp::from_sigil_token(next) {
|
|
|
|
Discriminator::BinOp(lhs, op)
|
|
|
|
} else {
|
|
|
|
Discriminator::Simple(lhs)
|
2018-08-19 18:44:54 -07:00
|
|
|
})
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-06-19 02:05:25 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn conditional(&mut self) -> ParseResult<IfExpressionBody> {
|
2018-06-19 02:05:25 -07:00
|
|
|
expect!(self, Keyword(Kw::Then));
|
2018-08-19 15:58:31 -07:00
|
|
|
let then_clause = self.expr_or_block()?;
|
2017-09-18 23:22:28 -07:00
|
|
|
let else_clause = self.else_clause()?;
|
2018-06-19 02:05:25 -07:00
|
|
|
Ok(IfExpressionBody::SimpleConditional(then_clause, else_clause))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-06-19 02:05:25 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn simple_pattern_match(&mut self) -> ParseResult<IfExpressionBody> {
|
2018-06-20 02:07:11 -07:00
|
|
|
expect!(self, Keyword(Kw::Is));
|
|
|
|
let pat = self.pattern()?;
|
|
|
|
expect!(self, Keyword(Kw::Then));
|
2018-08-19 15:58:31 -07:00
|
|
|
let then_clause = self.expr_or_block()?;
|
2018-06-20 02:07:11 -07:00
|
|
|
let else_clause = self.else_clause()?;
|
|
|
|
Ok(IfExpressionBody::SimplePatternMatch(pat, then_clause, else_clause))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-06-20 02:07:11 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn else_clause(&mut self) -> ParseResult<Option<Block>> {
|
2017-09-18 23:22:28 -07:00
|
|
|
Ok(if let Keyword(Kw::Else) = self.peek() {
|
|
|
|
self.next();
|
2018-08-19 15:58:31 -07:00
|
|
|
Some(self.expr_or_block()?)
|
2017-09-18 23:22:28 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
})
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-18 23:22:28 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn guard_block(&mut self) -> ParseResult<IfExpressionBody> {
|
2018-10-17 13:44:29 -07:00
|
|
|
//TODO - delimited! isn't sophisticated enough to do thisa
|
|
|
|
//let guards = delimited!(self, LCurlyBrace, guard_arm, Comma, RCurlyBrace);
|
|
|
|
expect!(self, LCurlyBrace);
|
2018-11-05 13:07:08 -08:00
|
|
|
|
2018-10-17 13:44:29 -07:00
|
|
|
let mut guards = vec![];
|
|
|
|
loop {
|
|
|
|
match self.peek() {
|
2018-11-05 13:07:08 -08:00
|
|
|
RCurlyBrace | EOF => break,
|
|
|
|
Semicolon | Newline => { self.next(); continue},
|
|
|
|
_ => {
|
|
|
|
let guard_arm = self.guard_arm()?;
|
|
|
|
guards.push(guard_arm);
|
|
|
|
loop {
|
|
|
|
match self.peek() {
|
|
|
|
Semicolon | Newline => { self.next(); continue; },
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let RCurlyBrace = self.peek() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
expect!(self, Comma);
|
|
|
|
}
|
2018-10-17 13:44:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
expect!(self, RCurlyBrace);
|
2018-07-13 20:18:57 -07:00
|
|
|
Ok(IfExpressionBody::GuardList(guards))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-07-13 20:18:57 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn guard_arm(&mut self) -> ParseResult<GuardArm> {
|
2018-07-13 21:50:38 -07:00
|
|
|
let guard = self.guard()?;
|
|
|
|
expect!(self, Operator(ref c) if **c == "->");
|
2018-08-19 15:58:31 -07:00
|
|
|
let body = self.expr_or_block()?;
|
2018-07-13 21:50:38 -07:00
|
|
|
Ok(GuardArm { guard, body })
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-07-13 21:50:38 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn guard(&mut self) -> ParseResult<Guard> {
|
2018-07-13 22:12:30 -07:00
|
|
|
Ok(match self.peek() {
|
|
|
|
Keyword(Kw::Is) => {
|
|
|
|
self.next();
|
|
|
|
let pat = self.pattern()?;
|
|
|
|
Guard::Pat(pat)
|
|
|
|
},
|
2018-08-22 16:41:31 -07:00
|
|
|
ref tok if BinOp::from_sigil_token(tok).is_some() => {
|
|
|
|
let op = BinOp::from_sigil_token(&self.next()).unwrap();
|
2018-10-19 11:02:10 -07:00
|
|
|
let precedence = op.get_precedence();
|
|
|
|
let Expression(expr, _) = self.precedence_expr(precedence)?;
|
2018-08-22 16:41:31 -07:00
|
|
|
Guard::HalfExpr(HalfExpr { op: Some(op), expr })
|
|
|
|
},
|
2018-10-20 00:55:37 -07:00
|
|
|
_ => {
|
2018-10-19 11:02:10 -07:00
|
|
|
//TODO - I think there's a better way to do this involving the precedence of ->
|
|
|
|
let Expression(expr, _) = self.prefix_expr()?;
|
2018-08-22 16:41:31 -07:00
|
|
|
Guard::HalfExpr(HalfExpr { op: None, expr })
|
|
|
|
}
|
2018-07-13 22:12:30 -07:00
|
|
|
})
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-07-13 20:18:57 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn pattern(&mut self) -> ParseResult<Pattern> {
|
2018-07-01 01:26:19 -07:00
|
|
|
if let LParen = self.peek() {
|
|
|
|
let tuple_pattern_variants = delimited!(self, LParen, pattern, Comma, RParen);
|
|
|
|
Ok(Pattern::TuplePattern(tuple_pattern_variants))
|
|
|
|
} else {
|
|
|
|
self.simple_pattern()
|
|
|
|
}
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-07-01 01:26:19 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn simple_pattern(&mut self) -> ParseResult<Pattern> {
|
2018-07-01 01:26:19 -07:00
|
|
|
Ok(match self.peek() {
|
|
|
|
Identifier(_) => {
|
|
|
|
let id = self.identifier()?;
|
|
|
|
match self.peek() {
|
2018-07-13 03:19:02 -07:00
|
|
|
LCurlyBrace => {
|
|
|
|
let members = delimited!(self, LCurlyBrace, record_pattern_entry, Comma, RCurlyBrace);
|
|
|
|
Pattern::Record(id, members)
|
|
|
|
},
|
2018-07-01 01:26:19 -07:00
|
|
|
LParen => {
|
|
|
|
let members = delimited!(self, LParen, pattern, Comma, RParen);
|
|
|
|
Pattern::TupleStruct(id, members)
|
|
|
|
},
|
|
|
|
_ => Pattern::Literal(PatternLiteral::VarPattern(id))
|
|
|
|
}
|
|
|
|
},
|
2018-08-21 20:02:10 -07:00
|
|
|
Keyword(Kw::True) => {
|
|
|
|
self.next();
|
|
|
|
Pattern::Literal(PatternLiteral::BoolPattern(true))
|
|
|
|
},
|
|
|
|
Keyword(Kw::False) => {
|
|
|
|
self.next();
|
|
|
|
Pattern::Literal(PatternLiteral::BoolPattern(false))
|
|
|
|
},
|
|
|
|
StrLiteral(s) => {
|
|
|
|
self.next();
|
|
|
|
Pattern::Literal(PatternLiteral::StringPattern(s))
|
|
|
|
},
|
2018-08-21 19:57:45 -07:00
|
|
|
DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.signed_number_literal()?,
|
|
|
|
Operator(ref op) if **op == "-" => self.signed_number_literal()?,
|
2018-07-10 03:43:14 -07:00
|
|
|
Underscore => {
|
|
|
|
self.next();
|
|
|
|
Pattern::Ignored
|
|
|
|
},
|
2018-07-01 01:26:19 -07:00
|
|
|
other => return ParseError::new(&format!("{:?} is not a valid Pattern", other))
|
2018-06-24 02:15:08 -07:00
|
|
|
})
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-06-20 02:07:11 -07:00
|
|
|
|
2018-10-19 02:36:23 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn signed_number_literal(&mut self) -> ParseResult<Pattern> {
|
2018-08-21 19:57:45 -07:00
|
|
|
let neg = match self.peek() {
|
|
|
|
Operator(ref op) if **op == "-" => {
|
|
|
|
self.next();
|
|
|
|
true
|
|
|
|
},
|
|
|
|
_ => false
|
|
|
|
};
|
|
|
|
let Expression(expr_type, _) = self.number_literal()?;
|
|
|
|
Ok(Pattern::Literal(PatternLiteral::NumPattern { neg, num: expr_type }))
|
2018-10-19 02:36:23 -07:00
|
|
|
}
|
2018-08-21 19:57:45 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn record_pattern_entry(&mut self) -> ParseResult<(Rc<String>, Pattern)> {
|
2018-07-13 21:21:27 -07:00
|
|
|
let name = self.identifier()?;
|
|
|
|
Ok(match self.peek() {
|
|
|
|
Colon => {
|
|
|
|
expect!(self, Colon);
|
|
|
|
let pat = self.pattern()?;
|
|
|
|
(name, pat)
|
|
|
|
},
|
|
|
|
_ => (name.clone(), Pattern::Literal(PatternLiteral::StringPattern(name.clone())))
|
|
|
|
})
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-07-13 03:19:02 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn block(&mut self) -> ParseResult<Block> {
|
2018-05-13 02:50:43 -07:00
|
|
|
Ok(delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-18 23:22:28 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn expr_or_block(&mut self) -> ParseResult<Block> {
|
2018-08-19 15:58:31 -07:00
|
|
|
match self.peek() {
|
|
|
|
LCurlyBrace => self.block(),
|
|
|
|
_ => {
|
|
|
|
let expr = self.expression()?;
|
|
|
|
Ok(vec![Statement::ExpressionStatement(expr)])
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-08-19 15:58:31 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn while_expr(&mut self) -> ParseResult<Expression> {
|
2018-05-12 23:49:02 -07:00
|
|
|
use self::ExpressionType::*;
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Keyword(Kw::While));
|
2018-05-12 23:49:02 -07:00
|
|
|
let condition = {
|
|
|
|
self.restrictions.no_struct_literal = true;
|
|
|
|
let x = self.while_cond();
|
|
|
|
self.restrictions.no_struct_literal = false;
|
|
|
|
x?.map(|expr| bx!(expr))
|
|
|
|
};
|
|
|
|
let body = self.block()?;
|
|
|
|
Ok(Expression(WhileExpression {condition, body}, None))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-05-12 23:49:02 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn while_cond(&mut self) -> ParseResult<Option<Expression>> {
|
2018-05-12 23:49:02 -07:00
|
|
|
Ok(match self.peek() {
|
|
|
|
LCurlyBrace => None,
|
|
|
|
_ => Some(self.expression()?),
|
|
|
|
})
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-05-12 23:49:02 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn for_expr(&mut self) -> ParseResult<Expression> {
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Keyword(Kw::For));
|
2018-05-13 01:40:14 -07:00
|
|
|
let enumerators = if let LCurlyBrace = self.peek() {
|
2018-05-13 02:50:43 -07:00
|
|
|
delimited!(self, LCurlyBrace, enumerator, Comma | Newline, RCurlyBrace)
|
2018-05-13 01:40:14 -07:00
|
|
|
} else {
|
|
|
|
let single_enum = {
|
|
|
|
self.restrictions.no_struct_literal = true;
|
|
|
|
let s = self.enumerator();
|
|
|
|
self.restrictions.no_struct_literal = false;
|
|
|
|
s?
|
|
|
|
};
|
|
|
|
vec![single_enum]
|
|
|
|
};
|
|
|
|
let body = Box::new(self.for_expr_body()?);
|
|
|
|
Ok(Expression(ExpressionType::ForExpression { enumerators, body }, None))
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-05-13 01:40:14 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn enumerator(&mut self) -> ParseResult<Enumerator> {
|
2018-05-13 01:40:14 -07:00
|
|
|
let id = self.identifier()?;
|
2018-05-13 02:42:43 -07:00
|
|
|
expect!(self, Operator(ref c) if **c == "<-");
|
2018-05-13 01:40:14 -07:00
|
|
|
let generator = self.expression()?;
|
|
|
|
Ok(Enumerator { id, generator })
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-05-13 01:40:14 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn for_expr_body(&mut self) -> ParseResult<ForBody> {
|
2018-05-13 01:40:14 -07:00
|
|
|
use self::ForBody::*;
|
|
|
|
Ok(match self.peek() {
|
|
|
|
LCurlyBrace => {
|
2018-05-13 02:50:43 -07:00
|
|
|
let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict);
|
2018-05-13 01:40:14 -07:00
|
|
|
StatementBlock(statements)
|
|
|
|
},
|
|
|
|
Keyword(Kw::Return) => {
|
|
|
|
self.next();
|
|
|
|
MonadicReturn(self.expression()?)
|
|
|
|
},
|
|
|
|
_ => return ParseError::new("for expressions must end in a block or 'return'"),
|
|
|
|
})
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-10-08 22:02:58 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn identifier(&mut self) -> ParseResult<Rc<String>> {
|
2017-09-11 15:42:49 -07:00
|
|
|
match self.next() {
|
|
|
|
Identifier(s) => Ok(s),
|
|
|
|
p => ParseError::new(&format!("Expected an identifier, got {:?}", p)),
|
|
|
|
}
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-11 02:07:17 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn literal(&mut self) -> ParseResult<Expression> {
|
2017-09-27 22:27:50 -07:00
|
|
|
use self::ExpressionType::*;
|
2017-09-11 02:07:17 -07:00
|
|
|
match self.peek() {
|
2018-01-08 06:12:45 -08:00
|
|
|
DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.number_literal(),
|
2017-09-27 22:27:50 -07:00
|
|
|
Keyword(Kw::True) => {
|
|
|
|
self.next();
|
|
|
|
Ok(Expression(BoolLiteral(true), None))
|
|
|
|
},
|
|
|
|
Keyword(Kw::False) => {
|
|
|
|
self.next();
|
|
|
|
Ok(Expression(BoolLiteral(false), None))
|
|
|
|
},
|
2017-09-16 15:57:48 -07:00
|
|
|
StrLiteral(s) => {
|
|
|
|
self.next();
|
2017-09-27 22:27:50 -07:00
|
|
|
Ok(Expression(StringLiteral(s), None))
|
2017-09-16 15:57:48 -07:00
|
|
|
}
|
|
|
|
e => ParseError::new(&format!("Expected a literal expression, got {:?}", e)),
|
2017-09-11 02:07:17 -07:00
|
|
|
}
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-16 14:34:37 -07:00
|
|
|
|
2018-10-19 02:36:23 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn number_literal(&mut self) -> ParseResult<Expression> {
|
2017-09-11 02:07:17 -07:00
|
|
|
match self.peek() {
|
2018-01-08 06:12:45 -08:00
|
|
|
HexLiteral(_) | BinNumberSigil => self.int_literal(),
|
2017-09-11 02:07:17 -07:00
|
|
|
_ => self.float_literal(),
|
|
|
|
}
|
2018-10-19 02:36:23 -07:00
|
|
|
}
|
2017-09-11 02:07:17 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn int_literal(&mut self) -> ParseResult<Expression> {
|
2017-09-27 22:27:50 -07:00
|
|
|
use self::ExpressionType::*;
|
2017-09-11 02:07:17 -07:00
|
|
|
match self.next() {
|
|
|
|
BinNumberSigil => {
|
2017-09-11 23:27:15 -07:00
|
|
|
let digits = self.digits()?;
|
|
|
|
let n = parse_binary(digits)?;
|
2018-05-12 01:44:03 -07:00
|
|
|
Ok(Expression(NatLiteral(n), None))
|
2017-09-11 02:07:17 -07:00
|
|
|
},
|
2018-01-08 06:12:45 -08:00
|
|
|
HexLiteral(text) => {
|
2018-02-11 16:35:38 -08:00
|
|
|
let digits: String = text.chars().filter(|c| c.is_digit(16)).collect();
|
|
|
|
let n = parse_hex(digits)?;
|
2018-05-12 01:44:03 -07:00
|
|
|
Ok(Expression(NatLiteral(n), None))
|
2017-09-11 02:07:17 -07:00
|
|
|
},
|
|
|
|
_ => return ParseError::new("Expected '0x' or '0b'"),
|
|
|
|
}
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-11 02:07:17 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn float_literal(&mut self) -> ParseResult<Expression> {
|
2017-09-27 22:27:50 -07:00
|
|
|
use self::ExpressionType::*;
|
2017-09-11 02:38:27 -07:00
|
|
|
let mut digits = self.digits()?;
|
|
|
|
if let TokenType::Period = self.peek() {
|
|
|
|
self.next();
|
|
|
|
digits.push_str(".");
|
|
|
|
digits.push_str(&self.digits()?);
|
|
|
|
match digits.parse::<f64>() {
|
2017-09-27 22:27:50 -07:00
|
|
|
Ok(f) => Ok(Expression(FloatLiteral(f), None)),
|
2017-09-16 17:44:06 -07:00
|
|
|
Err(e) => ParseError::new(&format!("Float failed to parse with error: {}", e)),
|
2017-09-11 02:38:27 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match digits.parse::<u64>() {
|
2018-05-12 01:44:03 -07:00
|
|
|
Ok(d) => Ok(Expression(NatLiteral(d), None)),
|
2017-09-16 17:44:06 -07:00
|
|
|
Err(e) => ParseError::new(&format!("Integer failed to parse with error: {}", e)),
|
2017-09-11 02:38:27 -07:00
|
|
|
}
|
2017-09-11 02:07:17 -07:00
|
|
|
}
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-11 02:07:17 -07:00
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn digits(&mut self) -> ParseResult<String> {
|
2017-09-11 02:07:17 -07:00
|
|
|
let mut ds = String::new();
|
|
|
|
loop {
|
2017-09-11 02:38:27 -07:00
|
|
|
match self.peek() {
|
|
|
|
Underscore => { self.next(); continue; },
|
|
|
|
DigitGroup(ref s) => { self.next(); ds.push_str(s)},
|
2017-09-11 02:07:48 -07:00
|
|
|
_ => break,
|
2017-09-11 02:07:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(ds)
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-11 02:07:17 -07:00
|
|
|
}
|
2017-09-09 00:31:15 -07:00
|
|
|
|
2017-09-11 23:27:15 -07:00
|
|
|
fn parse_binary(digits: String) -> ParseResult<u64> {
|
|
|
|
let mut result: u64 = 0;
|
|
|
|
let mut multiplier = 1;
|
|
|
|
for d in digits.chars().rev() {
|
|
|
|
match d {
|
|
|
|
'1' => result += multiplier,
|
|
|
|
'0' => (),
|
|
|
|
_ => return ParseError::new("Encountered a character not '1' or '0 while parsing a binary literal"),
|
|
|
|
}
|
2018-02-21 03:52:16 -08:00
|
|
|
multiplier = match multiplier.checked_mul(2) {
|
|
|
|
Some(m) => m,
|
|
|
|
None => return ParseError::new("This binary expression will overflow")
|
|
|
|
}
|
2017-09-11 23:27:15 -07:00
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2018-01-08 05:57:36 -08:00
|
|
|
fn parse_hex(digits: String) -> ParseResult<u64> {
|
|
|
|
let mut result: u64 = 0;
|
|
|
|
let mut multiplier: u64 = 1;
|
|
|
|
for d in digits.chars().rev() {
|
|
|
|
match d.to_digit(16) {
|
|
|
|
Some(n) => result += n as u64 * multiplier,
|
|
|
|
None => return ParseError::new("Encountered a non-hex digit in a hex literal"),
|
|
|
|
}
|
2018-02-21 03:52:16 -08:00
|
|
|
multiplier = match multiplier.checked_mul(16) {
|
|
|
|
Some(m) => m,
|
|
|
|
None => return ParseError::new("This hex expression will overflow")
|
|
|
|
}
|
2018-01-08 05:57:36 -08:00
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2017-09-11 03:21:07 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod parse_tests {
|
2017-09-28 00:06:08 -07:00
|
|
|
use ::std::rc::Rc;
|
2018-10-20 14:27:00 -07:00
|
|
|
use super::tokenize;
|
|
|
|
use super::ParseResult;
|
2018-06-19 02:22:19 -07:00
|
|
|
use builtin::{PrefixOp, BinOp};
|
2018-08-14 23:09:11 -07:00
|
|
|
use ast::{AST, Expression, Statement, IfExpressionBody, Discriminator, Pattern, PatternLiteral, TypeBody, Enumerator, ForBody};
|
2017-09-11 03:21:07 -07:00
|
|
|
use super::Statement::*;
|
2017-09-13 22:40:05 -07:00
|
|
|
use super::Declaration::*;
|
2017-10-08 21:21:02 -07:00
|
|
|
use super::Signature;
|
2018-10-18 13:27:09 -07:00
|
|
|
use super::TypeIdentifier::*;
|
2018-02-12 00:51:53 -08:00
|
|
|
use super::TypeSingletonName;
|
2017-09-28 00:06:08 -07:00
|
|
|
use super::ExpressionType::*;
|
2017-10-08 19:15:08 -07:00
|
|
|
use super::Variant::*;
|
2018-05-13 01:40:14 -07:00
|
|
|
use super::ForBody::*;
|
2017-09-12 00:48:37 -07:00
|
|
|
|
2018-10-20 14:27:00 -07:00
|
|
|
fn parse(tokens: Vec<::tokenizing::Token>) -> ParseResult<AST> {
|
|
|
|
let mut parser = super::Parser::new(tokens);
|
2018-10-20 15:41:09 -07:00
|
|
|
parser.parse()
|
2018-10-20 14:27:00 -07:00
|
|
|
}
|
|
|
|
|
2017-09-13 22:40:05 -07:00
|
|
|
macro_rules! rc {
|
2017-09-13 22:47:25 -07:00
|
|
|
($string:tt) => { Rc::new(stringify!($string).to_string()) }
|
2017-09-13 22:40:05 -07:00
|
|
|
}
|
2017-09-12 00:48:37 -07:00
|
|
|
macro_rules! parse_test {
|
2018-10-20 14:27:00 -07:00
|
|
|
($string:expr, $correct:expr) => { assert_eq!(parse(tokenize($string)).unwrap(), $correct) }
|
2017-09-12 00:48:37 -07:00
|
|
|
}
|
2017-09-30 13:04:56 -07:00
|
|
|
macro_rules! parse_error {
|
2018-10-20 14:27:00 -07:00
|
|
|
($string:expr) => { assert!(parse(tokenize($string)).is_err()) }
|
2017-09-30 13:04:56 -07:00
|
|
|
}
|
2017-10-10 21:02:32 -07:00
|
|
|
macro_rules! val {
|
2018-02-26 18:12:37 -08:00
|
|
|
($var:expr) => { Value(Rc::new($var.to_string())) }
|
2017-09-13 20:49:17 -07:00
|
|
|
}
|
2017-09-28 00:06:08 -07:00
|
|
|
macro_rules! exprstatement {
|
2017-09-29 19:10:37 -07:00
|
|
|
($expr_type:expr) => { Statement::ExpressionStatement(Expression($expr_type, None)) };
|
|
|
|
($expr_type:expr, $type_anno:expr) => { Statement::ExpressionStatement(Expression($expr_type, Some($type_anno))) };
|
2017-09-28 00:06:08 -07:00
|
|
|
}
|
2017-09-29 19:10:37 -07:00
|
|
|
macro_rules! ty {
|
2018-02-12 00:51:53 -08:00
|
|
|
($name:expr) => { Singleton(tys!($name)) }
|
|
|
|
}
|
|
|
|
macro_rules! tys {
|
|
|
|
($name:expr) => { TypeSingletonName { name: Rc::new($name.to_string()), params: vec![] } };
|
2017-09-29 19:10:37 -07:00
|
|
|
}
|
2017-09-12 00:48:37 -07:00
|
|
|
|
2018-03-06 01:06:36 -08:00
|
|
|
|
|
|
|
/* new style of test macros */
|
|
|
|
|
|
|
|
macro_rules! single_expr {
|
|
|
|
($exprtype:expr) => { AST(vec![Statement::ExpressionStatement(Expression($exprtype, None))]) };
|
|
|
|
($exprtype:expr, $type:expr) => { AST(vec![Statement::ExpressionStatement(Expression($exprtype, $type))]) }
|
|
|
|
}
|
2018-03-06 01:31:31 -08:00
|
|
|
macro_rules! ex {
|
|
|
|
($expr_type:expr) => { Expression($expr_type, None) }
|
|
|
|
}
|
2018-03-06 01:06:36 -08:00
|
|
|
macro_rules! binexp {
|
|
|
|
($op:expr, $lhs:expr, $rhs:expr) => { BinExp(BinOp::from_sigil($op), bx!(Expression($lhs, None)), bx!(Expression($rhs, None))) }
|
|
|
|
}
|
|
|
|
macro_rules! prefexp {
|
|
|
|
($op:expr, $lhs:expr) => { PrefixExp(PrefixOp::from_sigil($op), bx!(Expression($lhs, None))) }
|
|
|
|
}
|
2018-03-06 01:31:31 -08:00
|
|
|
macro_rules! exst {
|
|
|
|
($expr_type:expr) => { Statement::ExpressionStatement(Expression($expr_type, None)) };
|
|
|
|
($expr_type:expr, $type_anno:expr) => { Statement::ExpressionStatement(Expression($expr_type, Some($type_anno))) };
|
|
|
|
($op:expr, $lhs:expr, $rhs:expr) => { Statement::ExpressionStatement(ex!(binexp!($op, $lhs, $rhs))) };
|
|
|
|
}
|
2018-03-06 01:06:36 -08:00
|
|
|
|
2017-09-11 03:21:07 -07:00
|
|
|
#[test]
|
2017-09-13 03:46:16 -07:00
|
|
|
fn parsing_number_literals_and_binexps() {
|
2018-03-06 01:06:36 -08:00
|
|
|
parse_test! { ".2", single_expr!(FloatLiteral(0.2)) };
|
|
|
|
parse_test! { "8.1", single_expr!(FloatLiteral(8.1)) };
|
2018-02-11 16:35:38 -08:00
|
|
|
|
2018-05-12 01:44:03 -07:00
|
|
|
parse_test! { "0b010", single_expr!(NatLiteral(2)) };
|
|
|
|
parse_test! { "0b0_1_0_", single_expr!(NatLiteral(2)) }
|
2018-02-11 16:35:38 -08:00
|
|
|
|
2018-05-12 01:44:03 -07:00
|
|
|
parse_test! {"0xff", single_expr!(NatLiteral(255)) };
|
|
|
|
parse_test! {"0xf_f_", single_expr!(NatLiteral(255)) };
|
2018-02-11 16:35:38 -08:00
|
|
|
|
2018-05-12 01:44:03 -07:00
|
|
|
parse_test!("0xf_f_+1", AST(vec![exprstatement!(binexp!("+", NatLiteral(255), NatLiteral(1)))]));
|
2018-02-11 16:35:38 -08:00
|
|
|
|
2018-03-06 01:06:36 -08:00
|
|
|
parse_test! {"3; 4; 4.3", AST(
|
2018-05-12 01:44:03 -07:00
|
|
|
vec![exprstatement!(NatLiteral(3)), exprstatement!(NatLiteral(4)),
|
2018-03-06 01:06:36 -08:00
|
|
|
exprstatement!(FloatLiteral(4.3))])
|
|
|
|
};
|
2017-09-12 05:16:37 -07:00
|
|
|
|
|
|
|
parse_test!("1 + 2 * 3", AST(vec!
|
|
|
|
[
|
2018-05-12 01:44:03 -07:00
|
|
|
exprstatement!(binexp!("+", NatLiteral(1), binexp!("*", NatLiteral(2), NatLiteral(3))))
|
2017-09-12 05:16:37 -07:00
|
|
|
]));
|
|
|
|
|
|
|
|
parse_test!("1 * 2 + 3", AST(vec!
|
|
|
|
[
|
2018-05-12 01:44:03 -07:00
|
|
|
exprstatement!(binexp!("+", binexp!("*", NatLiteral(1), NatLiteral(2)), NatLiteral(3)))
|
2017-09-12 05:16:37 -07:00
|
|
|
]));
|
2017-09-12 15:52:23 -07:00
|
|
|
|
2018-05-12 01:44:03 -07:00
|
|
|
parse_test!("1 && 2", AST(vec![exprstatement!(binexp!("&&", NatLiteral(1), NatLiteral(2)))]));
|
2017-09-12 15:52:23 -07:00
|
|
|
|
2017-09-28 00:06:08 -07:00
|
|
|
parse_test!("1 + 2 * 3 + 4", AST(vec![exprstatement!(
|
2017-09-28 00:45:36 -07:00
|
|
|
binexp!("+",
|
2018-05-12 01:44:03 -07:00
|
|
|
binexp!("+", NatLiteral(1), binexp!("*", NatLiteral(2), NatLiteral(3))),
|
|
|
|
NatLiteral(4)))]));
|
2017-09-12 15:52:23 -07:00
|
|
|
|
2017-09-13 03:46:16 -07:00
|
|
|
parse_test!("(1 + 2) * 3", AST(vec!
|
2018-05-12 01:44:03 -07:00
|
|
|
[exprstatement!(binexp!("*", binexp!("+", NatLiteral(1), NatLiteral(2)), NatLiteral(3)))]));
|
2017-09-17 04:26:44 -07:00
|
|
|
|
2017-09-28 00:06:08 -07:00
|
|
|
parse_test!(".1 + .2", AST(vec![exprstatement!(binexp!("+", FloatLiteral(0.1), FloatLiteral(0.2)))]));
|
2018-05-12 01:44:03 -07:00
|
|
|
parse_test!("1 / 2", AST(vec![exprstatement!(binexp!("/", NatLiteral(1), NatLiteral(2)))]));
|
2017-09-11 03:21:07 -07:00
|
|
|
}
|
2017-09-13 03:46:16 -07:00
|
|
|
|
2017-10-06 20:28:07 -07:00
|
|
|
#[test]
|
|
|
|
fn parsing_tuples() {
|
|
|
|
parse_test!("()", AST(vec![exprstatement!(TupleLiteral(vec![]))]));
|
|
|
|
parse_test!("(\"hella\", 34)", AST(vec![exprstatement!(
|
|
|
|
TupleLiteral(
|
2018-05-12 01:44:03 -07:00
|
|
|
vec![ex!(StringLiteral(rc!(hella))), ex!(NatLiteral(34))]
|
2017-10-06 20:28:07 -07:00
|
|
|
)
|
|
|
|
)]));
|
|
|
|
parse_test!("((1+2), \"slough\")", AST(vec![exprstatement!(TupleLiteral(vec![
|
2018-05-12 01:44:03 -07:00
|
|
|
ex!(binexp!("+", NatLiteral(1), NatLiteral(2))),
|
2017-10-06 20:28:07 -07:00
|
|
|
ex!(StringLiteral(rc!(slough))),
|
|
|
|
]))]))
|
|
|
|
}
|
|
|
|
|
2017-09-13 20:49:17 -07:00
|
|
|
#[test]
|
|
|
|
fn parsing_identifiers() {
|
2017-10-10 21:02:32 -07:00
|
|
|
parse_test!("a", AST(vec![exprstatement!(val!("a"))]));
|
|
|
|
parse_test!("a + b", AST(vec![exprstatement!(binexp!("+", val!("a"), val!("b")))]));
|
2017-09-14 00:11:52 -07:00
|
|
|
//parse_test!("a[b]", AST(vec![Expression(
|
|
|
|
//parse_test!("a[]", <- TODO THIS NEEDS TO FAIL
|
|
|
|
//parse_test!(damn()[a] ,<- TODO needs to succeed
|
2018-02-23 03:04:19 -08:00
|
|
|
parse_test!("a[b,c]", AST(vec![exprstatement!(Index { indexee: bx!(ex!(val!("a"))), indexers: vec![ex!(val!("b")), ex!(val!("c"))]} )]));
|
2017-10-13 18:56:02 -07:00
|
|
|
|
|
|
|
parse_test!("None", AST(vec![exprstatement!(val!("None"))]));
|
2017-10-14 13:54:17 -07:00
|
|
|
parse_test!("Pandas { a: x + y }", AST(vec![
|
2018-02-26 18:12:37 -08:00
|
|
|
exprstatement!(NamedStruct { name: rc!(Pandas), fields: vec![(rc!(a), ex!(binexp!("+", val!("x"), val!("y"))))]})
|
|
|
|
]));
|
2017-09-13 20:49:17 -07:00
|
|
|
}
|
2017-09-13 22:40:05 -07:00
|
|
|
|
2017-09-17 02:38:11 -07:00
|
|
|
#[test]
|
2017-09-17 06:04:24 -07:00
|
|
|
fn parsing_complicated_operators() {
|
2017-10-10 21:02:32 -07:00
|
|
|
parse_test!("a <- b", AST(vec![exprstatement!(binexp!("<-", val!("a"), val!("b")))]));
|
|
|
|
parse_test!("a || b", AST(vec![exprstatement!(binexp!("||", val!("a"), val!("b")))]));
|
|
|
|
parse_test!("a<>b", AST(vec![exprstatement!(binexp!("<>", val!("a"), val!("b")))]));
|
2017-09-28 00:06:08 -07:00
|
|
|
parse_test!("a.b.c.d", AST(vec![exprstatement!(binexp!(".",
|
2017-09-17 17:57:21 -07:00
|
|
|
binexp!(".",
|
2017-10-10 21:02:32 -07:00
|
|
|
binexp!(".", val!("a"), val!("b")),
|
|
|
|
val!("c")),
|
|
|
|
val!("d")))]));
|
2018-05-12 01:44:03 -07:00
|
|
|
parse_test!("-3", AST(vec![exprstatement!(prefexp!("-", NatLiteral(3)))]));
|
2017-09-28 00:06:08 -07:00
|
|
|
parse_test!("-0.2", AST(vec![exprstatement!(prefexp!("-", FloatLiteral(0.2)))]));
|
2018-05-12 01:44:03 -07:00
|
|
|
parse_test!("!3", AST(vec![exprstatement!(prefexp!("!", NatLiteral(3)))]));
|
2017-10-10 21:02:32 -07:00
|
|
|
parse_test!("a <- -b", AST(vec![exprstatement!(binexp!("<-", val!("a"), prefexp!("-", val!("b"))))]));
|
|
|
|
parse_test!("a <--b", AST(vec![exprstatement!(binexp!("<--", val!("a"), val!("b")))]));
|
2017-09-17 02:38:11 -07:00
|
|
|
}
|
2017-09-14 00:11:52 -07:00
|
|
|
|
2017-09-13 23:37:49 -07:00
|
|
|
#[test]
|
|
|
|
fn parsing_functions() {
|
2018-08-24 16:49:59 -07:00
|
|
|
parse_test!("fn oi()", AST(vec![Declaration(FuncSig(Signature { name: rc!(oi), operator: false, params: vec![], type_anno: None }))]));
|
2018-02-23 03:04:19 -08:00
|
|
|
parse_test!("oi()", AST(vec![exprstatement!(Call { f: bx!(ex!(val!("oi"))), arguments: vec![] })]));
|
2017-09-28 00:06:08 -07:00
|
|
|
parse_test!("oi(a, 2 + 2)", AST(vec![exprstatement!(Call
|
2018-02-23 03:04:19 -08:00
|
|
|
{ f: bx!(ex!(val!("oi"))),
|
2018-05-12 01:44:03 -07:00
|
|
|
arguments: vec![ex!(val!("a")), ex!(binexp!("+", NatLiteral(2), NatLiteral(2)))]
|
2017-09-15 03:49:47 -07:00
|
|
|
})]));
|
2017-09-30 13:04:56 -07:00
|
|
|
parse_error!("a(b,,c)");
|
2017-10-08 20:55:05 -07:00
|
|
|
|
2017-10-08 21:25:51 -07:00
|
|
|
parse_test!("fn a(b, c: Int): Int", AST(vec![Declaration(
|
2018-08-24 16:49:59 -07:00
|
|
|
FuncSig(Signature { name: rc!(a), operator: false, params: vec![
|
2017-10-08 20:55:05 -07:00
|
|
|
(rc!(b), None), (rc!(c), Some(ty!("Int")))
|
2017-10-08 21:25:51 -07:00
|
|
|
], type_anno: Some(ty!("Int")) }))]));
|
|
|
|
|
|
|
|
|
|
|
|
parse_test!("fn a(x) { x() }", AST(vec![Declaration(
|
2018-08-24 16:49:59 -07:00
|
|
|
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None },
|
2018-02-23 03:04:19 -08:00
|
|
|
vec![exprstatement!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })]))]));
|
2018-03-02 00:42:52 -08:00
|
|
|
parse_test!("fn a(x) {\n x() }", AST(vec![Declaration(
|
2018-08-24 16:49:59 -07:00
|
|
|
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None },
|
2018-03-02 00:42:52 -08:00
|
|
|
vec![exprstatement!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })]))]));
|
2018-03-02 22:40:12 -08:00
|
|
|
|
|
|
|
let multiline = r#"
|
|
|
|
fn a(x) {
|
|
|
|
x()
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
parse_test!(multiline, AST(vec![Declaration(
|
2018-08-24 16:49:59 -07:00
|
|
|
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None },
|
2018-03-02 22:40:12 -08:00
|
|
|
vec![exprstatement!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })]))]));
|
|
|
|
let multiline2 = r#"
|
|
|
|
fn a(x) {
|
|
|
|
|
|
|
|
x()
|
|
|
|
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
parse_test!(multiline2, AST(vec![Declaration(
|
2018-08-24 16:49:59 -07:00
|
|
|
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None },
|
2018-03-02 22:40:12 -08:00
|
|
|
vec![exprstatement!(Call { f: bx!(ex!(val!("x"))), arguments: vec![] })]))]));
|
|
|
|
|
2017-09-13 23:37:49 -07:00
|
|
|
}
|
|
|
|
|
2017-09-17 04:31:27 -07:00
|
|
|
#[test]
|
2017-09-17 06:04:24 -07:00
|
|
|
fn parsing_bools() {
|
2017-09-28 00:06:08 -07:00
|
|
|
parse_test!("false", AST(vec![exprstatement!(BoolLiteral(false))]));
|
|
|
|
parse_test!("true", AST(vec![exprstatement!(BoolLiteral(true))]));
|
2017-09-17 04:31:27 -07:00
|
|
|
}
|
|
|
|
|
2017-09-16 15:57:48 -07:00
|
|
|
#[test]
|
|
|
|
fn parsing_strings() {
|
2017-09-28 00:06:08 -07:00
|
|
|
parse_test!(r#""hello""#, AST(vec![exprstatement!(StringLiteral(rc!(hello)))]));
|
2017-09-16 15:57:48 -07:00
|
|
|
}
|
|
|
|
|
2017-09-13 22:40:05 -07:00
|
|
|
#[test]
|
|
|
|
fn parsing_types() {
|
2018-07-12 02:07:52 -07:00
|
|
|
parse_test!("type Yolo = Yolo", AST(vec![Declaration(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![UnitStruct(rc!(Yolo))]), mutable: false} )]));
|
|
|
|
parse_test!("type mut Yolo = Yolo", AST(vec![Declaration(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![UnitStruct(rc!(Yolo))]), mutable: true} )]));
|
2017-10-04 22:02:31 -07:00
|
|
|
parse_test!("type alias Sex = Drugs", AST(vec![Declaration(TypeAlias(rc!(Sex), rc!(Drugs)))]));
|
2018-02-12 01:45:36 -08:00
|
|
|
parse_test!("type Sanchez = Miguel | Alejandro(Int, Option<a>) | Esperanza { a: Int, b: String }",
|
2018-07-12 02:07:52 -07:00
|
|
|
AST(vec![Declaration(TypeDecl{
|
|
|
|
name: tys!("Sanchez"),
|
|
|
|
body: TypeBody(vec![
|
2017-10-08 19:15:08 -07:00
|
|
|
UnitStruct(rc!(Miguel)),
|
|
|
|
TupleStruct(rc!(Alejandro), vec![
|
2018-02-12 00:51:53 -08:00
|
|
|
Singleton(TypeSingletonName { name: rc!(Int), params: vec![] }),
|
|
|
|
Singleton(TypeSingletonName { name: rc!(Option), params: vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })] }),
|
2017-10-08 19:15:08 -07:00
|
|
|
]),
|
2018-02-12 01:45:36 -08:00
|
|
|
Record(rc!(Esperanza), vec![
|
2018-07-12 02:07:52 -07:00
|
|
|
(rc!(a), Singleton(TypeSingletonName { name: rc!(Int), params: vec![] })),
|
|
|
|
(rc!(b), Singleton(TypeSingletonName { name: rc!(String), params: vec![] })),
|
|
|
|
])
|
|
|
|
]),
|
|
|
|
mutable: false
|
|
|
|
})]));
|
2018-02-12 01:45:36 -08:00
|
|
|
|
|
|
|
parse_test!("type Jorge<a> = Diego | Kike(a)", AST(vec![
|
2018-07-12 02:07:52 -07:00
|
|
|
Declaration(TypeDecl{
|
|
|
|
name: TypeSingletonName { name: rc!(Jorge), params: vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })] },
|
|
|
|
body: TypeBody(vec![UnitStruct(rc!(Diego)), TupleStruct(rc!(Kike), vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })])]),
|
|
|
|
mutable: false
|
|
|
|
}
|
2018-02-12 01:45:36 -08:00
|
|
|
)]));
|
2017-09-13 22:40:05 -07:00
|
|
|
}
|
2017-09-17 06:04:24 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parsing_bindings() {
|
2018-07-11 16:44:15 -07:00
|
|
|
parse_test!("let mut a = 10", AST(vec![Declaration(Binding { name: rc!(a), constant: false, expr: ex!(NatLiteral(10)) } )]));
|
|
|
|
parse_test!("let a = 2 + 2", AST(vec![Declaration(Binding { name: rc!(a), constant: true, expr: ex!(binexp!("+", NatLiteral(2), NatLiteral(2))) }) ]));
|
2017-09-17 06:04:24 -07:00
|
|
|
}
|
2017-09-18 23:22:28 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parsing_block_expressions() {
|
2018-06-19 02:22:19 -07:00
|
|
|
parse_test! {
|
|
|
|
"if a() then { b(); c() }", AST(vec![exprstatement!(
|
|
|
|
IfExpression {
|
|
|
|
discriminator: bx! {
|
|
|
|
Discriminator::Simple(ex!(Call { f: bx!(ex!(val!("a"))), arguments: vec![]}))
|
|
|
|
},
|
|
|
|
body: bx! {
|
|
|
|
IfExpressionBody::SimpleConditional(
|
|
|
|
vec![exprstatement!(Call { f: bx!(ex!(val!("b"))), arguments: vec![]}), exprstatement!(Call { f: bx!(ex!(val!("c"))), arguments: vec![] })],
|
|
|
|
None
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)])
|
|
|
|
};
|
|
|
|
|
2018-06-19 02:35:01 -07:00
|
|
|
parse_test! {
|
|
|
|
"if a() then { b(); c() } else { q }", AST(vec![exprstatement!(
|
|
|
|
IfExpression {
|
|
|
|
discriminator: bx! {
|
|
|
|
Discriminator::Simple(ex!(Call { f: bx!(ex!(val!("a"))), arguments: vec![]}))
|
|
|
|
},
|
|
|
|
body: bx! {
|
|
|
|
IfExpressionBody::SimpleConditional(
|
|
|
|
vec![exprstatement!(Call { f: bx!(ex!(val!("b"))), arguments: vec![]}), exprstatement!(Call { f: bx!(ex!(val!("c"))), arguments: vec![] })],
|
|
|
|
Some(
|
|
|
|
vec![exprstatement!(val!("q"))],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)])
|
|
|
|
};
|
|
|
|
|
2018-06-19 02:05:25 -07:00
|
|
|
/*
|
|
|
|
parse_test!("if a() then { b(); c() }", AST(vec![exprstatement!(
|
2018-02-23 03:04:19 -08:00
|
|
|
IfExpression(bx!(ex!(Call { f: bx!(ex!(val!("a"))), arguments: vec![]})),
|
|
|
|
vec![exprstatement!(Call { f: bx!(ex!(val!("b"))), arguments: vec![]}), exprstatement!(Call { f: bx!(ex!(val!("c"))), arguments: vec![] })],
|
2017-09-28 00:06:08 -07:00
|
|
|
None)
|
|
|
|
)]));
|
2017-09-18 23:22:28 -07:00
|
|
|
parse_test!(r#"
|
2018-06-19 02:05:25 -07:00
|
|
|
if true then {
|
2017-09-18 23:22:28 -07:00
|
|
|
const a = 10
|
|
|
|
b
|
|
|
|
} else {
|
|
|
|
c
|
2017-09-28 00:06:08 -07:00
|
|
|
}"#,
|
2018-02-23 03:04:19 -08:00
|
|
|
AST(vec![exprstatement!(IfExpression(bx!(ex!(BoolLiteral(true))),
|
2018-05-12 01:44:03 -07:00
|
|
|
vec![Declaration(Binding { name: rc!(a), constant: true, expr: ex!(NatLiteral(10)) }),
|
2018-02-26 18:12:37 -08:00
|
|
|
exprstatement!(val!(rc!(b)))],
|
|
|
|
Some(vec![exprstatement!(val!(rc!(c)))])))])
|
2017-09-18 23:22:28 -07:00
|
|
|
);
|
2018-02-11 22:10:21 -08:00
|
|
|
|
|
|
|
parse_test!("if a { b } else { c }", AST(vec![exprstatement!(
|
2018-02-23 03:04:19 -08:00
|
|
|
IfExpression(bx!(ex!(val!("a"))),
|
2018-02-11 22:10:21 -08:00
|
|
|
vec![exprstatement!(val!("b"))],
|
|
|
|
Some(vec![exprstatement!(val!("c"))])))]));
|
|
|
|
|
|
|
|
parse_test!("if (A {a: 1}) { b } else { c }", AST(vec![exprstatement!(
|
2018-05-12 01:44:03 -07:00
|
|
|
IfExpression(bx!(ex!(NamedStruct { name: rc!(A), fields: vec![(rc!(a), ex!(NatLiteral(1)))]})),
|
2018-02-11 22:10:21 -08:00
|
|
|
vec![exprstatement!(val!("b"))],
|
|
|
|
Some(vec![exprstatement!(val!("c"))])))]));
|
|
|
|
|
|
|
|
parse_error!("if A {a: 1} { b } else { c }");
|
2018-06-19 02:05:25 -07:00
|
|
|
*/
|
2017-09-18 23:22:28 -07:00
|
|
|
}
|
2018-02-21 22:06:56 -08:00
|
|
|
#[test]
|
2018-04-24 16:30:17 -07:00
|
|
|
fn parsing_interfaces() {
|
|
|
|
parse_test!("interface Unglueable { fn unglue(a: Glue); fn mar(): Glue }", AST(vec![
|
|
|
|
Declaration(Interface {
|
2018-02-22 03:26:32 -08:00
|
|
|
name: rc!(Unglueable),
|
2018-02-21 22:06:56 -08:00
|
|
|
signatures: vec![
|
2018-08-24 16:49:59 -07:00
|
|
|
Signature { name: rc!(unglue), operator: false, params: vec![(rc!(a), Some(Singleton(TypeSingletonName { name: rc!(Glue), params: vec![] })))], type_anno: None },
|
|
|
|
Signature { name: rc!(mar), operator: false, params: vec![], type_anno: Some(Singleton(TypeSingletonName { name: rc!(Glue), params: vec![] })) },
|
2018-02-21 22:06:56 -08:00
|
|
|
]
|
|
|
|
})
|
|
|
|
]));
|
|
|
|
}
|
2017-09-28 00:45:36 -07:00
|
|
|
|
2017-10-08 14:24:02 -07:00
|
|
|
#[test]
|
|
|
|
fn parsing_impls() {
|
|
|
|
parse_test!("impl Heh { fn yolo(); fn swagg(); }", AST(vec![
|
|
|
|
Declaration(Impl {
|
2017-10-08 19:39:41 -07:00
|
|
|
type_name: ty!("Heh"),
|
2018-04-24 16:30:17 -07:00
|
|
|
interface_name: None,
|
2017-10-08 14:24:02 -07:00
|
|
|
block: vec![
|
2018-08-24 16:49:59 -07:00
|
|
|
FuncSig(Signature { name: rc!(yolo), operator: false, params: vec![], type_anno: None }),
|
|
|
|
FuncSig(Signature { name: rc!(swagg), operator: false, params: vec![], type_anno: None })
|
2017-10-08 14:24:02 -07:00
|
|
|
] })]));
|
|
|
|
|
|
|
|
parse_test!("impl Mondai for Lollerino { fn yolo(); fn swagg(); }", AST(vec![
|
|
|
|
Declaration(Impl {
|
2017-10-08 19:39:41 -07:00
|
|
|
type_name: ty!("Lollerino"),
|
2018-04-24 16:30:17 -07:00
|
|
|
interface_name: Some(rc!(Mondai)),
|
2017-10-08 14:24:02 -07:00
|
|
|
block: vec![
|
2018-08-24 16:49:59 -07:00
|
|
|
FuncSig(Signature { name: rc!(yolo), operator: false, params: vec![], type_anno: None}),
|
|
|
|
FuncSig(Signature { name: rc!(swagg), operator: false, params: vec![], type_anno: None })
|
2017-10-08 14:24:02 -07:00
|
|
|
] })]));
|
2017-10-08 19:39:41 -07:00
|
|
|
parse_test!("impl Option<WTFMate> { fn oi() }", AST(vec![
|
|
|
|
Declaration(Impl {
|
2018-02-12 00:51:53 -08:00
|
|
|
type_name: Singleton(TypeSingletonName { name: rc!(Option), params: vec![ty!("WTFMate")]}),
|
2018-04-24 16:30:17 -07:00
|
|
|
interface_name: None,
|
2017-10-08 19:39:41 -07:00
|
|
|
block: vec![
|
2018-08-24 16:49:59 -07:00
|
|
|
FuncSig(Signature { name: rc!(oi), operator: false, params: vec![], type_anno: None }),
|
2017-10-08 19:39:41 -07:00
|
|
|
]
|
|
|
|
})]));
|
2017-10-08 14:24:02 -07:00
|
|
|
}
|
|
|
|
|
2017-09-28 00:45:36 -07:00
|
|
|
#[test]
|
|
|
|
fn parsing_type_annotations() {
|
2018-07-11 16:44:15 -07:00
|
|
|
parse_test!("let a = b : Int", AST(vec![
|
2017-09-28 00:45:36 -07:00
|
|
|
Declaration(Binding { name: rc!(a), constant: true, expr:
|
2017-10-10 21:02:32 -07:00
|
|
|
Expression(val!("b"), Some(ty!("Int"))) })]));
|
2017-09-28 00:45:36 -07:00
|
|
|
|
2017-09-29 19:10:37 -07:00
|
|
|
parse_test!("a : Int", AST(vec![
|
2017-10-10 21:02:32 -07:00
|
|
|
exprstatement!(val!("a"), ty!("Int"))
|
2017-09-29 19:10:37 -07:00
|
|
|
]));
|
|
|
|
|
|
|
|
parse_test!("a : Option<Int>", AST(vec![
|
2018-02-12 00:51:53 -08:00
|
|
|
exprstatement!(val!("a"), Singleton(TypeSingletonName { name: rc!(Option), params: vec![ty!("Int")] }))
|
2017-09-29 19:10:37 -07:00
|
|
|
]));
|
2017-09-28 00:45:36 -07:00
|
|
|
|
2017-09-29 19:10:37 -07:00
|
|
|
parse_test!("a : KoreanBBQSpecifier<Kimchi, Option<Bulgogi> >", AST(vec![
|
2018-02-12 00:51:53 -08:00
|
|
|
exprstatement!(val!("a"), Singleton(TypeSingletonName { name: rc!(KoreanBBQSpecifier), params: vec![
|
|
|
|
ty!("Kimchi"), Singleton(TypeSingletonName { name: rc!(Option), params: vec![ty!("Bulgogi")] })
|
|
|
|
] }))
|
2017-09-29 19:10:37 -07:00
|
|
|
]));
|
2017-09-30 01:14:49 -07:00
|
|
|
|
|
|
|
parse_test!("a : (Int, Yolo<a>)", AST(vec![
|
2017-10-10 21:02:32 -07:00
|
|
|
exprstatement!(val!("a"), Tuple(
|
2018-02-12 00:51:53 -08:00
|
|
|
vec![ty!("Int"), Singleton(TypeSingletonName {
|
2017-09-30 01:14:49 -07:00
|
|
|
name: rc!(Yolo), params: vec![ty!("a")]
|
2018-02-12 00:51:53 -08:00
|
|
|
})]))]));
|
2017-09-28 00:45:36 -07:00
|
|
|
}
|
2018-03-04 02:11:22 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parsing_lambdas() {
|
2018-03-06 01:31:31 -08:00
|
|
|
parse_test! { "{|x| x + 1}", single_expr!(
|
2018-05-12 01:44:03 -07:00
|
|
|
Lambda { params: vec![(rc!(x), None)], body: vec![exst!("+", val!("x"), NatLiteral(1))] }
|
2018-03-06 01:31:31 -08:00
|
|
|
) }
|
2018-03-04 02:11:22 -08:00
|
|
|
|
|
|
|
parse_test!("{ |x: Int, y| a;b;c;}", AST(vec![
|
|
|
|
exprstatement!(Lambda {
|
|
|
|
params: vec![(rc!(x), Some(ty!("Int"))), (rc!(y), None)],
|
2018-03-06 01:31:31 -08:00
|
|
|
body: vec![exst!(val!("a")), exst!(val!("b")), exst!(val!("c"))]
|
2018-03-04 02:11:22 -08:00
|
|
|
})
|
|
|
|
]));
|
2018-03-06 00:56:19 -08:00
|
|
|
|
|
|
|
parse_test!("{|x| y}(1)", AST(vec![
|
|
|
|
exprstatement!(Call { f: bx!(ex!(
|
|
|
|
Lambda { params: vec![(rc!(x), None)], body: vec![exprstatement!(val!("y"))] })),
|
2018-05-12 01:44:03 -07:00
|
|
|
arguments: vec![ex!(NatLiteral(1))] })]));
|
2018-03-04 02:11:22 -08:00
|
|
|
}
|
2018-03-08 12:01:24 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list_literals() {
|
|
|
|
parse_test! {
|
|
|
|
"[1,2]", AST(vec![
|
2018-05-12 01:44:03 -07:00
|
|
|
exprstatement!(ListLiteral(vec![ex!(NatLiteral(1)), ex!(NatLiteral(2))]))])
|
2018-03-08 12:01:24 -08:00
|
|
|
};
|
|
|
|
}
|
2018-05-12 23:49:02 -07:00
|
|
|
|
|
|
|
#[test]
|
2018-05-13 01:40:14 -07:00
|
|
|
fn while_expr() {
|
|
|
|
parse_test! {
|
|
|
|
"while { }", AST(vec![
|
|
|
|
exprstatement!(WhileExpression { condition: None, body: vec![] })])
|
|
|
|
}
|
2018-05-12 23:49:02 -07:00
|
|
|
|
2018-05-13 01:40:14 -07:00
|
|
|
parse_test! {
|
|
|
|
"while a == b { }", AST(vec![
|
|
|
|
exprstatement!(WhileExpression { condition: Some(bx![ex![binexp!("==", val!("a"), val!("b"))]]), body: vec![] })])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn for_expr() {
|
|
|
|
parse_test! {
|
|
|
|
"for { a <- maybeValue } return 1", AST(vec![
|
|
|
|
exprstatement!(ForExpression {
|
|
|
|
enumerators: vec![Enumerator { id: rc!(a), generator: ex!(val!("maybeValue")) }],
|
|
|
|
body: bx!(MonadicReturn(ex!(NatLiteral(1))))
|
|
|
|
})])
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_test! {
|
|
|
|
"for n <- someRange { f(n); }", AST(vec![
|
|
|
|
exprstatement!(ForExpression { enumerators: vec![Enumerator { id: rc!(n), generator: ex!(val!("someRange"))}],
|
|
|
|
body: bx!(ForBody::StatementBlock(vec![exprstatement!(Call { f: bx![ex!(val!("f"))], arguments: vec![ex!(val!("n"))] })]))
|
|
|
|
})])
|
|
|
|
}
|
|
|
|
}
|
2018-07-13 03:19:02 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn patterns() {
|
|
|
|
parse_test! {
|
|
|
|
"if x is Some(a) then { 4 } else { 9 }", AST(vec![
|
|
|
|
exprstatement!(
|
2018-08-19 15:58:31 -07:00
|
|
|
IfExpression {
|
|
|
|
discriminator: bx!(Discriminator::Simple(ex!(Value(rc!(x))))),
|
|
|
|
body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(rc!(Some), vec![Pattern::Literal(PatternLiteral::VarPattern(rc!(a)))]), vec![exprstatement!(NatLiteral(4))], Some(vec![exprstatement!(NatLiteral(9))]))) }
|
|
|
|
)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_test! {
|
|
|
|
"if x is Some(a) then 4 else 9", AST(vec![
|
|
|
|
exprstatement!(
|
2018-07-13 03:19:02 -07:00
|
|
|
IfExpression {
|
2018-07-13 20:04:52 -07:00
|
|
|
discriminator: bx!(Discriminator::Simple(ex!(Value(rc!(x))))),
|
|
|
|
body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(rc!(Some), vec![Pattern::Literal(PatternLiteral::VarPattern(rc!(a)))]), vec![exprstatement!(NatLiteral(4))], Some(vec![exprstatement!(NatLiteral(9))]))) }
|
2018-07-13 03:19:02 -07:00
|
|
|
)
|
|
|
|
])
|
|
|
|
}
|
2018-07-13 21:21:27 -07:00
|
|
|
|
|
|
|
parse_test! {
|
|
|
|
"if x is Something { a, b: x } then { 4 } else { 9 }", AST(vec![
|
|
|
|
exprstatement!(
|
|
|
|
IfExpression {
|
|
|
|
discriminator: bx!(Discriminator::Simple(ex!(Value(rc!(x))))),
|
|
|
|
body: bx!(IfExpressionBody::SimplePatternMatch(
|
|
|
|
Pattern::Record(rc!(Something), vec![
|
|
|
|
(rc!(a),Pattern::Literal(PatternLiteral::StringPattern(rc!(a)))),
|
|
|
|
(rc!(b),Pattern::Literal(PatternLiteral::VarPattern(rc!(x))))
|
|
|
|
]),
|
|
|
|
vec![exprstatement!(NatLiteral(4))], Some(vec![exprstatement!(NatLiteral(9))])))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
])
|
|
|
|
}
|
2018-08-21 19:57:45 -07:00
|
|
|
|
2018-08-21 20:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pattern_literals() {
|
2018-08-21 19:57:45 -07:00
|
|
|
parse_test! {
|
|
|
|
"if x is -1 then 1 else 2", AST(vec![
|
|
|
|
exprstatement!(
|
|
|
|
IfExpression {
|
|
|
|
discriminator: bx!(Discriminator::Simple(ex!(Value(rc!(x))))),
|
|
|
|
body: bx!(IfExpressionBody::SimplePatternMatch(
|
|
|
|
Pattern::Literal(PatternLiteral::NumPattern { neg: true, num: NatLiteral(1) }),
|
|
|
|
vec![exprstatement!(NatLiteral(1))],
|
|
|
|
Some(vec![exprstatement!(NatLiteral(2))]),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_test! {
|
|
|
|
"if x is 1 then 1 else 2", AST(vec![
|
|
|
|
exprstatement!(
|
|
|
|
IfExpression {
|
|
|
|
discriminator: bx!(Discriminator::Simple(ex!(Value(rc!(x))))),
|
|
|
|
body: bx!(IfExpressionBody::SimplePatternMatch(
|
|
|
|
Pattern::Literal(PatternLiteral::NumPattern { neg: false, num: NatLiteral(1) }),
|
|
|
|
vec![exprstatement!(NatLiteral(1))],
|
|
|
|
Some(vec![exprstatement!(NatLiteral(2))]),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2018-08-21 20:02:10 -07:00
|
|
|
parse_test! {
|
|
|
|
"if x is true then 1 else 2", AST(vec![
|
|
|
|
exprstatement!(
|
|
|
|
IfExpression {
|
|
|
|
discriminator: bx!(Discriminator::Simple(ex!(Value(rc!(x))))),
|
|
|
|
body: bx!(IfExpressionBody::SimplePatternMatch(
|
|
|
|
Pattern::Literal(PatternLiteral::BoolPattern(true)),
|
|
|
|
vec![exprstatement!(NatLiteral(1))],
|
|
|
|
Some(vec![exprstatement!(NatLiteral(2))]),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
])
|
|
|
|
}
|
2018-08-21 19:57:45 -07:00
|
|
|
|
2018-08-21 20:02:10 -07:00
|
|
|
parse_test! {
|
|
|
|
"if x is \"gnosticism\" then 1 else 2", AST(vec![
|
|
|
|
exprstatement!(
|
|
|
|
IfExpression {
|
|
|
|
discriminator: bx!(Discriminator::Simple(ex!(Value(rc!(x))))),
|
|
|
|
body: bx!(IfExpressionBody::SimplePatternMatch(
|
|
|
|
Pattern::Literal(PatternLiteral::StringPattern(rc!(gnosticism))),
|
|
|
|
vec![exprstatement!(NatLiteral(1))],
|
|
|
|
Some(vec![exprstatement!(NatLiteral(2))]),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
])
|
|
|
|
}
|
2018-07-13 03:19:02 -07:00
|
|
|
}
|
2017-09-11 03:21:07 -07:00
|
|
|
}
|