4c688ce8b2
need to fix
1810 lines
60 KiB
Rust
1810 lines
60 KiB
Rust
//! # Parsing
|
|
//! This module is where the recursive-descent parsing methods live.
|
|
//!
|
|
//!
|
|
//! # Schala EBNF Grammar
|
|
//! This document is the authoritative grammar of Schala, represented in something approximating
|
|
//! Extended Backus-Naur form. Terminal productions are in "double quotes", or UPPERCASE
|
|
//! if they represent a class of tokens rather than an specific string, or are otherwise
|
|
//! unreprsentable 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
|
|
//! ```
|
|
//! ### Functions
|
|
//!
|
|
//! ```
|
|
//! func_declaration := func_signature func_body
|
|
//! func_body := ε | nonempty_func_body
|
|
//! nonempty_func_body := "{" (statement delimiter)* "}"
|
|
//! func_signature := "fn" func_name formal_param_list type_anno+
|
|
//! func_name := IDENTIFIER | operator
|
|
//! formal_param_list := "(" (formal_param ",")* ")"
|
|
//! formal_param := IDENTIFIER type_anno+
|
|
//! ```
|
|
//!
|
|
//! ### Variable bindings
|
|
//! ```binding_declaration := "let" "mut"? IDENTIFIER "=" expresion```
|
|
//!
|
|
//! ### Interfaces
|
|
//!
|
|
//! ```
|
|
//! interface_declaration := "interface" type_singleton_name signature_block
|
|
//! impl_declaration := "impl" type_singleton_name decl_block | "impl" type_singleton_name "for" type_name decl_block
|
|
//! decl_block := "{" (func_declaration)* "}"
|
|
//! signature_block := "{" (func_signature)* "}"
|
|
//! ```
|
|
//!
|
|
//! ### 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 ( "(" invocation_list ")" )* | ε
|
|
//! invocation_list := invocation_argument ("," invocation_argument)* | ε
|
|
//! invocation_argument := expression | IDENTIFIER "=" expression | "_"
|
|
//! index_expr := primary ( "[" (expression ("," (expression)* | ε) "]" )*
|
|
//! primary := literal | paren_expr | if_expr | for_expr | while_expr | identifier_expr | lambda_expr | anonymous_struct | list_expr
|
|
//! expr_or_block := "{" (statement delimiter)* "}" | expr
|
|
//! ```
|
|
//!
|
|
//! ### Primary expressions
|
|
//!
|
|
//! ```
|
|
//! list_expr := "[" (expression, ",")* "]"
|
|
//! lambda_expr := "\\" lambda_param_list type_anno+ nonempty_func_body
|
|
//! lambda_param_list := formal_param_list | formal_param
|
|
//! paren_expr := "(" paren_inner ")"
|
|
//! paren_inner := (expression ",")*
|
|
//! identifier_expr := named_struct | IDENTIFIER
|
|
//! ```
|
|
//!
|
|
//! ## 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
|
|
//! number_literal := int_literal | float_literal
|
|
//! int_literal = ("0x" | "0b") digits
|
|
//! float_literal := digits ("." digits)
|
|
//! digits := (digit_group underscore*)+
|
|
//! digit_group := DIGIT+
|
|
//! ```
|
|
//!
|
|
//! ### Patterns
|
|
//! ```
|
|
//! pattern := "(" (pattern, ",")* ")" | simple_pattern
|
|
//! simple_pattern := pattern_literal | record_pattern | tuple_struct_pattern
|
|
//! pattern_literal := "true" | "false" | signed_number_literal | STR_LITERAL | IDENTIFIER
|
|
//! signed_number_literal := "-"? number_literal
|
|
//! record_pattern := IDENTIFIER "{" (record_pattern_entry, ",")* "}"
|
|
//! record_pattern_entry := IDENTIFIER | IDENTIFIER ":" Pattern
|
|
//! tuple_struct_pattern := IDENTIFIER "(" (pattern, ",")* ")"
|
|
//! ```
|
|
//!
|
|
//! ### If-expressions
|
|
//! ```
|
|
//! if_expr := "if" discriminator ("then" condititional | "is" simple_pattern_match | guard_block)
|
|
//! discriminator := precedence_expr (operator)+
|
|
//! conditional := expr_or_block else_clause
|
|
//! simple_pattern_match := pattern "then" conditional
|
|
//! else_clause := ε | "else" expr_or_block
|
|
//! guard_block := "{" (guard_arm, ",")* "}"
|
|
//! guard_arm := guard "->" expr_or_block
|
|
//! guard := "is" pattern | (operator)+ precedence_expr
|
|
//! ```
|
|
//!
|
|
//! ### While expressions
|
|
//! ```
|
|
//! 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
|
|
//! ### For-expressions
|
|
//! ```
|
|
//! 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.
|
|
//! ```
|
|
//!
|
|
|
|
use std::rc::Rc;
|
|
use std::iter::Peekable;
|
|
use std::vec::IntoIter;
|
|
|
|
use crate::tokenizing::*;
|
|
use crate::tokenizing::Kw::*;
|
|
use crate::tokenizing::TokenKind::*;
|
|
|
|
use crate::ast::*;
|
|
|
|
use crate::builtin::{BinOp, PrefixOp};
|
|
|
|
/// Represents a parsing error
|
|
#[derive(Debug)]
|
|
pub struct ParseError {
|
|
pub msg: String,
|
|
pub token: Token
|
|
}
|
|
|
|
impl ParseError {
|
|
fn new_with_token<T, M>(msg: M, token: Token) -> ParseResult<T> where M: Into<String> {
|
|
Err(ParseError { msg: msg.into(), token })
|
|
}
|
|
}
|
|
|
|
/// Represents either a successful parsing result or a ParseError
|
|
pub type ParseResult<T> = Result<T, ParseError>;
|
|
|
|
#[derive(Debug)]
|
|
pub struct ParseRecord {
|
|
production_name: String,
|
|
next_token: String,
|
|
level: u32,
|
|
}
|
|
|
|
/// Main data structure for doing parsing.
|
|
pub struct Parser {
|
|
token_handler: TokenHandler,
|
|
parse_record: Vec<ParseRecord>,
|
|
parse_level: u32,
|
|
restrictions: ParserRestrictions,
|
|
}
|
|
|
|
struct ParserRestrictions {
|
|
no_struct_literal: bool
|
|
}
|
|
|
|
struct TokenHandler {
|
|
tokens: Peekable<IntoIter<Token>>,
|
|
end_of_file: (usize, usize),
|
|
}
|
|
|
|
impl TokenHandler {
|
|
fn new(tokens: Vec<Token>) -> TokenHandler {
|
|
let end_of_file = match tokens.last() {
|
|
None => (0, 0),
|
|
Some(t) => (t.line_num, t.char_num)
|
|
};
|
|
let tokens = tokens.into_iter().peekable();
|
|
TokenHandler { tokens, end_of_file }
|
|
}
|
|
|
|
fn peek_kind(&mut self) -> TokenKind {
|
|
self.tokens.peek().map(|ref t| { t.kind.clone() }).unwrap_or(TokenKind::EOF)
|
|
}
|
|
fn peek(&mut self) -> Token {
|
|
self.tokens.peek().map(|t: &Token| { t.clone()}).unwrap_or(Token { kind: TokenKind::EOF, line_num: self.end_of_file.0, char_num: self.end_of_file.1})
|
|
}
|
|
fn next(&mut self) -> Token {
|
|
self.tokens.next().unwrap_or(Token { kind: TokenKind::EOF, line_num: self.end_of_file.0, char_num: self.end_of_file.1})
|
|
}
|
|
}
|
|
|
|
impl Parser {
|
|
/// Create a new parser initialized with some tokens.
|
|
pub fn new(initial_input: Vec<Token>) -> Parser {
|
|
Parser {
|
|
token_handler: TokenHandler::new(initial_input),
|
|
parse_record: vec![],
|
|
parse_level: 0,
|
|
restrictions: ParserRestrictions { no_struct_literal: false }
|
|
}
|
|
}
|
|
|
|
/// Parse all loaded tokens up to this point.
|
|
pub fn parse(&mut self) -> ParseResult<AST> {
|
|
self.program()
|
|
}
|
|
|
|
/*
|
|
pub fn parse_with_new_tokens(&mut self, new_tokens: Vec<Token>) -> ParseResult<AST> {
|
|
|
|
}
|
|
*/
|
|
|
|
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()
|
|
}
|
|
}
|
|
|
|
macro_rules! print_token_pattern {
|
|
($tokenpattern:pat) => { stringify!($tokenpattern) }
|
|
}
|
|
|
|
macro_rules! expect {
|
|
($self:expr, $token_kind:pat) => { expect!($self, $token_kind if true) };
|
|
($self:expr, $expected_kind:pat if $cond:expr) => {
|
|
{
|
|
let tok = $self.token_handler.peek();
|
|
match tok.get_kind() {
|
|
$expected_kind if $cond => $self.token_handler.next(),
|
|
actual_kind => {
|
|
let msg = format!("Expected {}, got {:?}", print_token_pattern!($expected_kind), actual_kind);
|
|
return ParseError::new_with_token(msg, tok);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
macro_rules! delimited {
|
|
($self:expr, $start:pat, $parse_fn:ident, $( $delim:pat )|+, $end:pat, nonstrict) => {
|
|
delimited!($self, $start, $parse_fn, $( $delim )|*, $end, false)
|
|
};
|
|
($self:expr, $start:pat, $parse_fn:ident, $( $delim:pat )|+, $end:pat) => {
|
|
delimited!($self, $start, $parse_fn, $( $delim )|*, $end, true)
|
|
};
|
|
($self:expr, $start:pat, $parse_fn:ident, $( $delim:pat )|+, $end:pat, $strictness:expr) => {
|
|
{
|
|
expect!($self, $start);
|
|
let mut acc = vec![];
|
|
loop {
|
|
let peek = $self.token_handler.peek();
|
|
match peek.get_kind() {
|
|
$end | EOF => break,
|
|
_ => (),
|
|
}
|
|
if !$strictness {
|
|
match peek.get_kind() {
|
|
$( $delim )|* => { $self.token_handler.next(); continue },
|
|
_ => ()
|
|
}
|
|
}
|
|
acc.push($self.$parse_fn()?);
|
|
match $self.token_handler.peek().get_kind() {
|
|
$( $delim )|* => { $self.token_handler.next(); continue },
|
|
_ if $strictness => break,
|
|
_ => continue,
|
|
};
|
|
}
|
|
expect!($self, $end);
|
|
acc
|
|
}
|
|
};
|
|
}
|
|
|
|
|
|
impl Parser {
|
|
/// `program := (statement delimiter)* EOF`
|
|
/// `delimiter := NEWLINE | ';'`
|
|
#[recursive_descent_method]
|
|
fn program(&mut self) -> ParseResult<AST> {
|
|
let mut statements = Vec::new();
|
|
loop {
|
|
match self.token_handler.peek().get_kind() {
|
|
EOF => break,
|
|
Newline | Semicolon => {
|
|
self.token_handler.next();
|
|
continue;
|
|
},
|
|
_ => statements.push(
|
|
Meta::new(self.statement()?)
|
|
),
|
|
}
|
|
}
|
|
Ok(AST(statements))
|
|
}
|
|
|
|
/// `statement := expression | declaration`
|
|
#[recursive_descent_method]
|
|
fn statement(&mut self) -> ParseResult<Statement> {
|
|
//TODO handle error recovery here
|
|
match self.token_handler.peek().get_kind() {
|
|
Keyword(Type) => self.type_declaration().map(|decl| { Statement::Declaration(decl) }),
|
|
Keyword(Func)=> self.func_declaration().map(|func| { Statement::Declaration(func) }),
|
|
Keyword(Let) => self.binding_declaration().map(|decl| Statement::Declaration(decl)),
|
|
Keyword(Interface) => self.interface_declaration().map(|decl| Statement::Declaration(decl)),
|
|
Keyword(Impl) => self.impl_declaration().map(|decl| Statement::Declaration(decl)),
|
|
_ => self.expression().map(|expr| { Statement::ExpressionStatement(expr.into()) } ),
|
|
}
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn type_declaration(&mut self) -> ParseResult<Declaration> {
|
|
expect!(self, Keyword(Type));
|
|
self.type_declaration_body()
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn type_declaration_body(&mut self) -> ParseResult<Declaration> {
|
|
if let Keyword(Alias) = self.token_handler.peek_kind() {
|
|
self.type_alias()
|
|
} else {
|
|
let mutable = if let Keyword(Mut) = self.token_handler.peek_kind() {
|
|
self.token_handler.next();
|
|
true
|
|
} else {
|
|
false
|
|
};
|
|
let name = self.type_singleton_name()?;
|
|
expect!(self, Operator(ref c) if **c == "=");
|
|
let body = self.type_body()?;
|
|
Ok(Declaration::TypeDecl { name, body, mutable})
|
|
}
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn type_alias(&mut self) -> ParseResult<Declaration> {
|
|
expect!(self, Keyword(Alias));
|
|
let alias = self.identifier()?;
|
|
expect!(self, Operator(ref c) if **c == "=");
|
|
let original = self.identifier()?;
|
|
Ok(Declaration::TypeAlias(alias, original))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn type_body(&mut self) -> ParseResult<TypeBody> {
|
|
let mut variants = Vec::new();
|
|
variants.push(self.variant_specifier()?);
|
|
loop {
|
|
if let Pipe = self.token_handler.peek_kind() {
|
|
self.token_handler.next();
|
|
variants.push(self.variant_specifier()?);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
Ok(TypeBody(variants))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn variant_specifier(&mut self) -> ParseResult<Variant> {
|
|
use self::Variant::*;
|
|
|
|
let name = self.identifier()?;
|
|
match self.token_handler.peek_kind() {
|
|
LParen => {
|
|
let tuple_members = delimited!(self, LParen, type_name, Comma, RParen);
|
|
Ok(TupleStruct(name, tuple_members))
|
|
},
|
|
LCurlyBrace => {
|
|
let typed_identifier_list = delimited!(self, LCurlyBrace, typed_identifier, Comma, RCurlyBrace);
|
|
Ok(Record {name, members: typed_identifier_list })
|
|
},
|
|
_ => Ok(UnitStruct(name))
|
|
}
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn typed_identifier(&mut self) -> ParseResult<(Rc<String>, TypeIdentifier)> {
|
|
let identifier = self.identifier()?;
|
|
expect!(self, Colon);
|
|
let type_name = self.type_name()?;
|
|
Ok((identifier, type_name))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn func_declaration(&mut self) -> ParseResult<Declaration> {
|
|
let signature = self.func_signature()?;
|
|
if let LCurlyBrace = self.token_handler.peek_kind() {
|
|
let statements = self.nonempty_func_body()?;
|
|
Ok(Declaration::FuncDecl(signature, statements))
|
|
} else {
|
|
Ok(Declaration::FuncSig(signature))
|
|
}
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn func_signature(&mut self) -> ParseResult<Signature> {
|
|
expect!(self, Keyword(Func));
|
|
let (name, operator) = match self.token_handler.peek_kind() {
|
|
Operator(s) => {
|
|
let name = s.clone();
|
|
self.token_handler.next();
|
|
(name, true)
|
|
},
|
|
_ => (self.identifier()?, false)
|
|
};
|
|
let params = self.formal_param_list()?;
|
|
let type_anno = match self.token_handler.peek_kind() {
|
|
Colon => Some(self.type_anno()?),
|
|
_ => None,
|
|
};
|
|
Ok(Signature { name, operator, params, type_anno })
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn nonempty_func_body(&mut self) -> ParseResult<Vec<Meta<Statement>>> {
|
|
let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict);
|
|
Ok(statements.into_iter().map(|s| Meta::new(s)).collect())
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn formal_param_list(&mut self) -> ParseResult<Vec<FormalParam>> {
|
|
Ok(delimited!(self, LParen, formal_param, Comma, RParen))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn formal_param(&mut self) -> ParseResult<FormalParam> {
|
|
let name = self.identifier()?;
|
|
let ty = match self.token_handler.peek_kind() {
|
|
Colon => Some(self.type_anno()?),
|
|
_ => None
|
|
};
|
|
Ok((name, ty))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn binding_declaration(&mut self) -> ParseResult<Declaration> {
|
|
expect!(self, Keyword(Kw::Let));
|
|
let constant = match self.token_handler.peek_kind() {
|
|
Keyword(Kw::Mut) => {
|
|
self.token_handler.next();
|
|
false
|
|
}
|
|
_ => true
|
|
};
|
|
let name = self.identifier()?;
|
|
let type_anno = if let Colon = self.token_handler.peek_kind() {
|
|
Some(self.type_anno()?)
|
|
} else {
|
|
None
|
|
};
|
|
|
|
expect!(self, Operator(ref o) if **o == "=");
|
|
let expr = self.expression()?.into();
|
|
|
|
Ok(Declaration::Binding { name, constant, type_anno, expr })
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn interface_declaration(&mut self) -> ParseResult<Declaration> {
|
|
expect!(self, Keyword(Interface));
|
|
let name = self.identifier()?;
|
|
let signatures = self.signature_block()?;
|
|
Ok(Declaration::Interface { name, signatures })
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn signature_block(&mut self) -> ParseResult<Vec<Signature>> {
|
|
Ok(delimited!(self, LCurlyBrace, func_signature, Newline | Semicolon, RCurlyBrace, nonstrict))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn impl_declaration(&mut self) -> ParseResult<Declaration> {
|
|
expect!(self, Keyword(Impl));
|
|
let first = self.type_singleton_name()?;
|
|
let second = if let Keyword(For) = self.token_handler.peek_kind() {
|
|
self.token_handler.next();
|
|
Some(self.type_name()?)
|
|
} else {
|
|
None
|
|
};
|
|
|
|
let block = self.decl_block()?;
|
|
Ok(match (first, second) {
|
|
(interface_name, Some(type_name)) =>
|
|
Declaration::Impl { type_name, interface_name: Some(interface_name), block },
|
|
(type_singleton_name, None) =>
|
|
Declaration::Impl { type_name: TypeIdentifier::Singleton(type_singleton_name), interface_name: None, block }
|
|
})
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn decl_block(&mut self) -> ParseResult<Vec<Declaration>> {
|
|
Ok(delimited!(self, LCurlyBrace, func_declaration, Newline | Semicolon, RCurlyBrace, nonstrict))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn expression(&mut self) -> ParseResult<Expression> {
|
|
let mut expr_body = self.precedence_expr(BinOp::min_precedence())?;
|
|
let type_anno = match self.token_handler.peek_kind() {
|
|
Colon => Some(self.type_anno()?),
|
|
_ => None
|
|
};
|
|
if let Some(_) = expr_body.1 {
|
|
return ParseError::new_with_token("Bad parse state encountered", self.token_handler.peek());
|
|
}
|
|
expr_body.1 = type_anno;
|
|
Ok(expr_body)
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn type_anno(&mut self) -> ParseResult<TypeIdentifier> {
|
|
expect!(self, Colon);
|
|
self.type_name()
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn type_name(&mut self) -> ParseResult<TypeIdentifier> {
|
|
use self::TypeIdentifier::*;
|
|
Ok(match self.token_handler.peek_kind() {
|
|
LParen => Tuple(delimited!(self, LParen, type_name, Comma, RParen)),
|
|
_ => Singleton(self.type_singleton_name()?),
|
|
})
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn type_singleton_name(&mut self) -> ParseResult<TypeSingletonName> {
|
|
Ok(TypeSingletonName {
|
|
name: self.identifier()?,
|
|
params: match self.token_handler.peek_kind() {
|
|
LAngleBracket => delimited!(self, LAngleBracket, type_name, Comma, RAngleBracket),
|
|
_ => vec![],
|
|
}
|
|
})
|
|
}
|
|
|
|
// 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> {
|
|
let record = ParseRecord {
|
|
production_name: "precedence_expr".to_string(),
|
|
next_token: format!("{}", self.token_handler.peek().to_string_with_metadata()),
|
|
level: self.parse_level,
|
|
};
|
|
self.parse_level += 1;
|
|
self.parse_record.push(record);
|
|
|
|
let mut lhs = self.prefix_expr()?;
|
|
loop {
|
|
let new_precedence = match BinOp::get_precedence_from_token(&self.token_handler.peek_kind()) {
|
|
Some(p) => p,
|
|
None => break,
|
|
};
|
|
|
|
if precedence >= new_precedence {
|
|
break;
|
|
}
|
|
let next_tok = self.token_handler.next();
|
|
let operation = match BinOp::from_sigil_token(&next_tok.kind) {
|
|
Some(sigil) => sigil,
|
|
None => unreachable!()
|
|
};
|
|
let rhs = self.precedence_expr(new_precedence)?;
|
|
lhs = Expression(ExpressionKind::BinExp(operation, bx!(lhs.into()), bx!(rhs.into())), None);
|
|
}
|
|
self.parse_level -= 1;
|
|
Ok(lhs)
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn prefix_expr(&mut self) -> ParseResult<Expression> {
|
|
match self.token_handler.peek_kind() {
|
|
Operator(ref op) if PrefixOp::is_prefix(&*op) => {
|
|
let sigil = match self.token_handler.next().kind {
|
|
Operator(op) => op,
|
|
_ => unreachable!(),
|
|
};
|
|
let expr = self.primary()?;
|
|
Ok(Expression(
|
|
ExpressionKind::PrefixExp(PrefixOp::from_sigil(sigil.as_str()), bx!(expr.into())),
|
|
None
|
|
))
|
|
},
|
|
_ => self.call_expr()
|
|
}
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn call_expr(&mut self) -> ParseResult<Expression> {
|
|
let mut expr = self.index_expr()?;
|
|
while let LParen = self.token_handler.peek_kind() {
|
|
let arguments = delimited!(self, LParen, invocation_argument, Comma, RParen);
|
|
let arguments = arguments.into_iter().map(|s| Meta::new(s)).collect();
|
|
expr = Expression(ExpressionKind::Call { f: bx!(expr.into()), arguments }, None); //TODO none is incorrect
|
|
}
|
|
|
|
Ok(expr)
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn invocation_argument(&mut self) -> ParseResult<InvocationArgument> {
|
|
Ok(match self.token_handler.peek_kind() {
|
|
Underscore => {
|
|
self.token_handler.next();
|
|
InvocationArgument::Ignored
|
|
},
|
|
Identifier(s) => {
|
|
self.token_handler.next();
|
|
match self.token_handler.peek_kind() {
|
|
Operator(ref op) if **op == "=" => {
|
|
self.token_handler.next();
|
|
let expr = self.expression()?;
|
|
InvocationArgument::Keyword { name: s.clone(), expr }
|
|
},
|
|
_ => {
|
|
let expr = self.expression()?;
|
|
InvocationArgument::Positional(expr)
|
|
}
|
|
}
|
|
},
|
|
_ => InvocationArgument::Positional(self.expression()?)
|
|
})
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn index_expr(&mut self) -> ParseResult<Expression> {
|
|
let primary = self.primary()?;
|
|
Ok(if let LSquareBracket = self.token_handler.peek_kind() {
|
|
let indexers = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket)
|
|
.into_iter().map(|ex| ex.into()).collect();
|
|
Expression(ExpressionKind::Index {
|
|
indexee: bx!(Expression(primary.0, None).into()),
|
|
indexers,
|
|
}, None)
|
|
} else {
|
|
primary
|
|
})
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn primary(&mut self) -> ParseResult<Expression> {
|
|
match self.token_handler.peek_kind() {
|
|
LCurlyBrace => self.curly_brace_expr(),
|
|
Backslash => self.lambda_expr(),
|
|
LParen => self.paren_expr(),
|
|
LSquareBracket => self.list_expr(),
|
|
Keyword(Kw::If) => self.if_expr(),
|
|
Keyword(Kw::For) => self.for_expr(),
|
|
Keyword(Kw::While) => self.while_expr(),
|
|
Identifier(_) => self.identifier_expr(),
|
|
_ => self.literal(),
|
|
}
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn list_expr(&mut self) -> ParseResult<Expression> {
|
|
let exprs = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket)
|
|
.into_iter().map(|ex| ex.into()).collect();
|
|
Ok(Expression(ExpressionKind::ListLiteral(exprs), None))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn curly_brace_expr(&mut self) -> ParseResult<Expression> {
|
|
ParseError::new_with_token("Not implemented", self.token_handler.peek())
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn lambda_expr(&mut self) -> ParseResult<Expression> {
|
|
expect!(self, Backslash);
|
|
let params = self.lambda_param_list()?;
|
|
let type_anno = match self.token_handler.peek_kind() {
|
|
Colon => Some(self.type_anno()?),
|
|
_ => None,
|
|
};
|
|
let body = self.nonempty_func_body()?;
|
|
Ok(Expression(ExpressionKind::Lambda { params, type_anno, body }, None)) //TODO need to handle types somehow
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn lambda_param_list(&mut self) -> ParseResult<Vec<FormalParam>> {
|
|
if let LParen = self.token_handler.peek_kind() {
|
|
self.formal_param_list()
|
|
} else {
|
|
let single_param = self.formal_param()?;
|
|
Ok(vec![single_param])
|
|
}
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn paren_expr(&mut self) -> ParseResult<Expression> {
|
|
use self::ExpressionKind::*;
|
|
let old_struct_value = self.restrictions.no_struct_literal;
|
|
self.restrictions.no_struct_literal = false;
|
|
let output = {
|
|
let mut inner = delimited!(self, LParen, expression, Comma, RParen);
|
|
match inner.len() {
|
|
0 => Ok(Expression(TupleLiteral(vec![]), None)),
|
|
1 => Ok(inner.pop().unwrap()),
|
|
_ => {
|
|
let inner: Vec<Meta<Expression>> = inner.into_iter().map(|ex| ex.into()).collect();
|
|
Ok(Expression(TupleLiteral(inner), None))
|
|
}
|
|
}
|
|
};
|
|
self.restrictions.no_struct_literal = old_struct_value;
|
|
output
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn identifier_expr(&mut self) -> ParseResult<Expression> {
|
|
use self::ExpressionKind::*;
|
|
let identifier = self.identifier()?;
|
|
Ok(match self.token_handler.peek_kind() {
|
|
LCurlyBrace if !self.restrictions.no_struct_literal => {
|
|
let fields = self.record_block()?;
|
|
Expression(NamedStruct { name: identifier, fields }, None)
|
|
},
|
|
_ => Expression(Value(identifier), None)
|
|
})
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn record_block(&mut self) -> ParseResult<Vec<(Rc<String>, Meta<Expression>)>> {
|
|
Ok(
|
|
delimited!(self, LCurlyBrace, record_entry, Comma, RCurlyBrace)
|
|
.into_iter().map(|(s, ex)| (s, ex.into())).collect()
|
|
)
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn record_entry(&mut self) -> ParseResult<(Rc<String>, Expression)> {
|
|
let field_name = self.identifier()?;
|
|
expect!(self, Colon);
|
|
let value = self.expression()?;
|
|
Ok((field_name, value))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn if_expr(&mut self) -> ParseResult<Expression> {
|
|
expect!(self, Keyword(Kw::If));
|
|
let discriminator = Box::new({
|
|
self.restrictions.no_struct_literal = true;
|
|
let x = self.discriminator();
|
|
self.restrictions.no_struct_literal = false;
|
|
x?
|
|
});
|
|
|
|
let body = Box::new(match self.token_handler.peek_kind() {
|
|
Keyword(Kw::Then) => self.conditional()?,
|
|
Keyword(Kw::Is) => self.simple_pattern_match()? ,
|
|
_ => self.guard_block()?
|
|
});
|
|
|
|
Ok(Expression(ExpressionKind::IfExpression { discriminator, body }, None))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn discriminator(&mut self) -> ParseResult<Discriminator> {
|
|
let lhs = self.prefix_expr()?;
|
|
let ref next = self.token_handler.peek_kind();
|
|
Ok(if let Some(op) = BinOp::from_sigil_token(next) {
|
|
Discriminator::BinOp(lhs, op)
|
|
} else {
|
|
Discriminator::Simple(lhs)
|
|
})
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn conditional(&mut self) -> ParseResult<IfExpressionBody> {
|
|
expect!(self, Keyword(Kw::Then));
|
|
let then_clause = self.expr_or_block()?;
|
|
let else_clause = self.else_clause()?;
|
|
Ok(IfExpressionBody::SimpleConditional(then_clause, else_clause))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn simple_pattern_match(&mut self) -> ParseResult<IfExpressionBody> {
|
|
expect!(self, Keyword(Kw::Is));
|
|
let pat = self.pattern()?;
|
|
expect!(self, Keyword(Kw::Then));
|
|
let then_clause = self.expr_or_block()?;
|
|
let else_clause = self.else_clause()?;
|
|
Ok(IfExpressionBody::SimplePatternMatch(pat, then_clause, else_clause))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn else_clause(&mut self) -> ParseResult<Option<Block>> {
|
|
Ok(if let Keyword(Kw::Else) = self.token_handler.peek_kind() {
|
|
self.token_handler.next();
|
|
Some(self.expr_or_block()?)
|
|
} else {
|
|
None
|
|
})
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn guard_block(&mut self) -> ParseResult<IfExpressionBody> {
|
|
//TODO - delimited! isn't sophisticated enough to do thisa
|
|
//let guards = delimited!(self, LCurlyBrace, guard_arm, Comma, RCurlyBrace);
|
|
expect!(self, LCurlyBrace);
|
|
|
|
let mut guards = vec![];
|
|
loop {
|
|
match self.token_handler.peek_kind() {
|
|
RCurlyBrace | EOF => break,
|
|
Semicolon | Newline => { self.token_handler.next(); continue},
|
|
_ => {
|
|
let guard_arm = self.guard_arm()?;
|
|
guards.push(guard_arm);
|
|
loop {
|
|
match self.token_handler.peek_kind() {
|
|
Semicolon | Newline => { self.token_handler.next(); continue; },
|
|
_ => break,
|
|
}
|
|
}
|
|
if let RCurlyBrace = self.token_handler.peek_kind() {
|
|
break;
|
|
}
|
|
expect!(self, Comma);
|
|
}
|
|
}
|
|
}
|
|
expect!(self, RCurlyBrace);
|
|
Ok(IfExpressionBody::GuardList(guards))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn guard_arm(&mut self) -> ParseResult<GuardArm> {
|
|
let guard = self.guard()?;
|
|
expect!(self, Operator(ref c) if **c == "->");
|
|
let body = self.expr_or_block()?;
|
|
Ok(GuardArm { guard, body })
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn guard(&mut self) -> ParseResult<Guard> {
|
|
Ok(match self.token_handler.peek_kind() {
|
|
Keyword(Kw::Is) => {
|
|
self.token_handler.next();
|
|
let pat = self.pattern()?;
|
|
Guard::Pat(pat)
|
|
},
|
|
ref tok if BinOp::from_sigil_token(tok).is_some() => {
|
|
let op = BinOp::from_sigil_token(&self.token_handler.next().kind).unwrap();
|
|
let precedence = op.get_precedence();
|
|
let Expression(expr, _) = self.precedence_expr(precedence)?;
|
|
Guard::HalfExpr(HalfExpr { op: Some(op), expr })
|
|
},
|
|
_ => {
|
|
//TODO - I think there's a better way to do this involving the precedence of ->
|
|
let Expression(expr, _) = self.prefix_expr()?;
|
|
Guard::HalfExpr(HalfExpr { op: None, expr })
|
|
}
|
|
})
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn pattern(&mut self) -> ParseResult<Pattern> {
|
|
if let LParen = self.token_handler.peek_kind() {
|
|
let tuple_pattern_variants = delimited!(self, LParen, pattern, Comma, RParen);
|
|
Ok(Pattern::TuplePattern(tuple_pattern_variants))
|
|
} else {
|
|
self.simple_pattern()
|
|
}
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn simple_pattern(&mut self) -> ParseResult<Pattern> {
|
|
Ok({
|
|
let tok = self.token_handler.peek();
|
|
match tok.get_kind() {
|
|
Identifier(_) => {
|
|
let id = self.identifier()?;
|
|
match self.token_handler.peek_kind() {
|
|
LCurlyBrace => {
|
|
let members = delimited!(self, LCurlyBrace, record_pattern_entry, Comma, RCurlyBrace);
|
|
Pattern::Record(id, members)
|
|
},
|
|
LParen => {
|
|
let members = delimited!(self, LParen, pattern, Comma, RParen);
|
|
Pattern::TupleStruct(id, members)
|
|
},
|
|
_ => Pattern::Literal(PatternLiteral::VarPattern(id))
|
|
}
|
|
},
|
|
Keyword(Kw::True) => {
|
|
self.token_handler.next();
|
|
Pattern::Literal(PatternLiteral::BoolPattern(true))
|
|
},
|
|
Keyword(Kw::False) => {
|
|
self.token_handler.next();
|
|
Pattern::Literal(PatternLiteral::BoolPattern(false))
|
|
},
|
|
StrLiteral(s) => {
|
|
self.token_handler.next();
|
|
Pattern::Literal(PatternLiteral::StringPattern(s))
|
|
},
|
|
DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.signed_number_literal()?,
|
|
Operator(ref op) if **op == "-" => self.signed_number_literal()?,
|
|
Underscore => {
|
|
self.token_handler.next();
|
|
Pattern::Ignored
|
|
},
|
|
other => return ParseError::new_with_token(format!("{:?} is not a valid Pattern", other), tok)
|
|
}
|
|
})
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn signed_number_literal(&mut self) -> ParseResult<Pattern> {
|
|
let neg = match self.token_handler.peek_kind() {
|
|
Operator(ref op) if **op == "-" => {
|
|
self.token_handler.next();
|
|
true
|
|
},
|
|
_ => false
|
|
};
|
|
let Expression(expr_type, _) = self.number_literal()?;
|
|
Ok(Pattern::Literal(PatternLiteral::NumPattern { neg, num: expr_type }))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn record_pattern_entry(&mut self) -> ParseResult<(Rc<String>, Pattern)> {
|
|
let name = self.identifier()?;
|
|
Ok(match self.token_handler.peek_kind() {
|
|
Colon => {
|
|
expect!(self, Colon);
|
|
let pat = self.pattern()?;
|
|
(name, pat)
|
|
},
|
|
_ => (name.clone(), Pattern::Literal(PatternLiteral::StringPattern(name.clone())))
|
|
})
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn block(&mut self) -> ParseResult<Block> {
|
|
let block = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict);
|
|
Ok(block.into_iter().map(|s| { Meta::new(s) }).collect())
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn expr_or_block(&mut self) -> ParseResult<Block> {
|
|
match self.token_handler.peek_kind() {
|
|
LCurlyBrace => self.block(),
|
|
_ => {
|
|
let expr = self.expression()?;
|
|
Ok(vec![Meta::new(Statement::ExpressionStatement(expr.into()))])
|
|
}
|
|
}
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn while_expr(&mut self) -> ParseResult<Expression> {
|
|
use self::ExpressionKind::*;
|
|
expect!(self, Keyword(Kw::While));
|
|
let condition = {
|
|
self.restrictions.no_struct_literal = true;
|
|
let x = self.while_cond();
|
|
self.restrictions.no_struct_literal = false;
|
|
x?.map(|expr| bx!(expr.into()))
|
|
};
|
|
let body = self.block()?;
|
|
Ok(Expression(WhileExpression {condition, body}, None))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn while_cond(&mut self) -> ParseResult<Option<Expression>> {
|
|
Ok(match self.token_handler.peek_kind() {
|
|
LCurlyBrace => None,
|
|
_ => Some(self.expression()?),
|
|
})
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn for_expr(&mut self) -> ParseResult<Expression> {
|
|
expect!(self, Keyword(Kw::For));
|
|
let enumerators = if let LCurlyBrace = self.token_handler.peek_kind() {
|
|
delimited!(self, LCurlyBrace, enumerator, Comma | Newline, RCurlyBrace)
|
|
} 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(ExpressionKind::ForExpression { enumerators, body }, None))
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn enumerator(&mut self) -> ParseResult<Enumerator> {
|
|
let id = self.identifier()?;
|
|
expect!(self, Operator(ref c) if **c == "<-");
|
|
let generator = self.expression()?.into();
|
|
Ok(Enumerator { id, generator })
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn for_expr_body(&mut self) -> ParseResult<ForBody> {
|
|
use self::ForBody::*;
|
|
let tok = self.token_handler.peek();
|
|
Ok(match tok.get_kind() {
|
|
LCurlyBrace => {
|
|
let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict);
|
|
StatementBlock(statements.into_iter().map(|s| Meta::new(s)).collect())
|
|
},
|
|
Keyword(Kw::Return) => {
|
|
self.token_handler.next();
|
|
MonadicReturn(self.expression()?.into())
|
|
},
|
|
_ => return ParseError::new_with_token("for expressions must end in a block or 'return'", tok),
|
|
})
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn identifier(&mut self) -> ParseResult<Rc<String>> {
|
|
let tok = self.token_handler.next();
|
|
match tok.get_kind() {
|
|
Identifier(s) => Ok(s),
|
|
p => ParseError::new_with_token(format!("Expected an identifier, got {:?}", p), tok),
|
|
}
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn literal(&mut self) -> ParseResult<Expression> {
|
|
use self::ExpressionKind::*;
|
|
|
|
let tok = self.token_handler.peek();
|
|
match tok.get_kind() {
|
|
DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.number_literal(),
|
|
Keyword(Kw::True) => {
|
|
self.token_handler.next();
|
|
Ok(Expression(BoolLiteral(true), None))
|
|
},
|
|
Keyword(Kw::False) => {
|
|
self.token_handler.next();
|
|
Ok(Expression(BoolLiteral(false), None))
|
|
},
|
|
StrLiteral(s) => {
|
|
self.token_handler.next();
|
|
Ok(Expression(StringLiteral(s.clone()), None))
|
|
}
|
|
e => ParseError::new_with_token(format!("Expected a literal expression, got {:?}", e), tok),
|
|
}
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn number_literal(&mut self) -> ParseResult<Expression> {
|
|
match self.token_handler.peek_kind() {
|
|
HexLiteral(_) | BinNumberSigil => self.int_literal(),
|
|
_ => self.float_literal(),
|
|
}
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn int_literal(&mut self) -> ParseResult<Expression> {
|
|
use self::ExpressionKind::*;
|
|
let tok = self.token_handler.next();
|
|
match tok.get_kind() {
|
|
BinNumberSigil => {
|
|
let digits = self.digits()?;
|
|
let n = parse_binary(digits, tok)?;
|
|
Ok(Expression(NatLiteral(n), None))
|
|
},
|
|
HexLiteral(text) => {
|
|
let digits: String = text.chars().filter(|c| c.is_digit(16)).collect();
|
|
let n = parse_hex(digits, tok)?;
|
|
Ok(Expression(NatLiteral(n), None))
|
|
},
|
|
_ => return ParseError::new_with_token("Expected '0x' or '0b'", tok),
|
|
}
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn float_literal(&mut self) -> ParseResult<Expression> {
|
|
use self::ExpressionKind::*;
|
|
let tok = self.token_handler.peek();
|
|
let mut digits = self.digits()?;
|
|
if let Period = self.token_handler.peek_kind() {
|
|
self.token_handler.next();
|
|
digits.push_str(".");
|
|
digits.push_str(&self.digits()?);
|
|
match digits.parse::<f64>() {
|
|
Ok(f) => Ok(Expression(FloatLiteral(f), None)),
|
|
Err(e) => ParseError::new_with_token(format!("Float failed to parse with error: {}", e), tok),
|
|
|
|
}
|
|
} else {
|
|
match digits.parse::<u64>() {
|
|
Ok(d) => Ok(Expression(NatLiteral(d), None)),
|
|
Err(e) => ParseError::new_with_token(format!("Integer failed to parse with error: {}", e), tok),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[recursive_descent_method]
|
|
fn digits(&mut self) -> ParseResult<String> {
|
|
let mut ds = String::new();
|
|
loop {
|
|
match self.token_handler.peek_kind() {
|
|
Underscore => { self.token_handler.next(); continue; },
|
|
DigitGroup(ref s) => { self.token_handler.next(); ds.push_str(s)},
|
|
_ => break,
|
|
}
|
|
}
|
|
Ok(ds)
|
|
}
|
|
}
|
|
|
|
fn parse_binary(digits: String, tok: Token) -> 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_with_token("Encountered a character not '1' or '0 while parsing a binary literal", tok),
|
|
}
|
|
multiplier = match multiplier.checked_mul(2) {
|
|
Some(m) => m,
|
|
None => return ParseError::new_with_token("This binary expression will overflow", tok)
|
|
}
|
|
}
|
|
Ok(result)
|
|
}
|
|
|
|
fn parse_hex(digits: String, tok: Token) -> 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_with_token("Encountered a non-hex digit in a hex literal", tok),
|
|
}
|
|
multiplier = match multiplier.checked_mul(16) {
|
|
Some(m) => m,
|
|
None => return ParseError::new_with_token("This hex expression will overflow", tok)
|
|
}
|
|
}
|
|
Ok(result)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod parse_tests {
|
|
use ::std::rc::Rc;
|
|
use super::tokenize;
|
|
use super::ParseResult;
|
|
use crate::builtin::{PrefixOp, BinOp};
|
|
use crate::ast::{AST, Meta, Expression, Statement, IfExpressionBody, Discriminator, Pattern, PatternLiteral, TypeBody, Enumerator, ForBody, InvocationArgument};
|
|
use super::Statement::*;
|
|
use super::Declaration::*;
|
|
use super::Signature;
|
|
use super::TypeIdentifier::*;
|
|
use super::TypeSingletonName;
|
|
use super::ExpressionKind::*;
|
|
use super::Variant::*;
|
|
use super::ForBody::*;
|
|
|
|
fn parse(input: &str) -> ParseResult<AST> {
|
|
let tokens: Vec<crate::tokenizing::Token> = tokenize(input);
|
|
let mut parser = super::Parser::new(tokens);
|
|
parser.parse()
|
|
}
|
|
|
|
macro_rules! parse_test {
|
|
($string:expr, $correct:expr) => { assert_eq!(parse($string).unwrap(), $correct) };
|
|
}
|
|
macro_rules! parse_test_wrap_ast {
|
|
($string:expr, $correct:expr) => { parse_test!($string, AST(vec![$correct])) }
|
|
}
|
|
macro_rules! parse_error {
|
|
($string:expr) => { assert!(parse($string).is_err()) }
|
|
}
|
|
macro_rules! val {
|
|
($var:expr) => { Value(Rc::new($var.to_string())) }
|
|
}
|
|
macro_rules! ty {
|
|
($name:expr) => { Singleton(tys!($name)) }
|
|
}
|
|
macro_rules! tys {
|
|
($name:expr) => { TypeSingletonName { name: Rc::new($name.to_string()), params: vec![] } };
|
|
}
|
|
|
|
macro_rules! ex {
|
|
($expr_type:expr) => { Expression($expr_type, None) };
|
|
(m $expr_type:expr) => { Meta::new(Expression($expr_type, None)) };
|
|
(m $expr_type:expr, $type_anno:expr) => { Meta::new(Expression($expr_type, Some($type_anno))) };
|
|
(s $expr_text:expr) => {
|
|
{
|
|
let tokens: Vec<crate::tokenizing::Token> = tokenize($expr_text);
|
|
let mut parser = super::Parser::new(tokens);
|
|
parser.expression().unwrap()
|
|
}
|
|
};
|
|
}
|
|
|
|
macro_rules! inv {
|
|
($expr_type:expr) => { Meta::new(InvocationArgument::Positional($expr_type)) }
|
|
}
|
|
|
|
macro_rules! binexp {
|
|
($op:expr, $lhs:expr, $rhs:expr) => { BinExp(BinOp::from_sigil($op), bx!(Expression($lhs, None).into()), bx!(Expression($rhs, None).into())) }
|
|
}
|
|
macro_rules! prefexp {
|
|
($op:expr, $lhs:expr) => { PrefixExp(PrefixOp::from_sigil($op), bx!(Expression($lhs, None).into())) }
|
|
}
|
|
macro_rules! exst {
|
|
($expr_type:expr) => { Meta::new(Statement::ExpressionStatement(Expression($expr_type, None).into())) };
|
|
($expr_type:expr, $type_anno:expr) => { Meta::new(Statement::ExpressionStatement(Expression($expr_type, Some($type_anno)).into())) };
|
|
($op:expr, $lhs:expr, $rhs:expr) => { Meta::new(Statement::ExpressionStatement(ex!(binexp!($op, $lhs, $rhs)))) };
|
|
(s $statement_text:expr) => {
|
|
{
|
|
let tokens: Vec<crate::tokenizing::Token> = tokenize($statement_text);
|
|
let mut parser = super::Parser::new(tokens);
|
|
Meta::new(parser.statement().unwrap())
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_number_literals_and_binexps() {
|
|
parse_test_wrap_ast! { ".2", exst!(FloatLiteral(0.2)) };
|
|
parse_test_wrap_ast! { "8.1", exst!(FloatLiteral(8.1)) };
|
|
|
|
parse_test_wrap_ast! { "0b010", exst!(NatLiteral(2)) };
|
|
parse_test_wrap_ast! { "0b0_1_0_", exst!(NatLiteral(2)) }
|
|
|
|
parse_test_wrap_ast! {"0xff", exst!(NatLiteral(255)) };
|
|
parse_test_wrap_ast! {"0xf_f_", exst!(NatLiteral(255)) };
|
|
|
|
parse_test_wrap_ast! {"0xf_f_+1", exst!(binexp!("+", NatLiteral(255), NatLiteral(1))) };
|
|
|
|
parse_test! {"3; 4; 4.3", AST(
|
|
vec![exst!(NatLiteral(3)), exst!(NatLiteral(4)),
|
|
exst!(FloatLiteral(4.3))])
|
|
};
|
|
|
|
parse_test!("1 + 2 * 3", AST(vec!
|
|
[
|
|
exst!(binexp!("+", NatLiteral(1), binexp!("*", NatLiteral(2), NatLiteral(3))))
|
|
]));
|
|
|
|
parse_test!("1 * 2 + 3", AST(vec!
|
|
[
|
|
exst!(binexp!("+", binexp!("*", NatLiteral(1), NatLiteral(2)), NatLiteral(3)))
|
|
]));
|
|
|
|
parse_test!("1 && 2", AST(vec![exst!(binexp!("&&", NatLiteral(1), NatLiteral(2)))]));
|
|
|
|
parse_test!("1 + 2 * 3 + 4", AST(vec![exst!(
|
|
binexp!("+",
|
|
binexp!("+", NatLiteral(1), binexp!("*", NatLiteral(2), NatLiteral(3))),
|
|
NatLiteral(4)))]));
|
|
|
|
parse_test!("(1 + 2) * 3", AST(vec!
|
|
[exst!(binexp!("*", binexp!("+", NatLiteral(1), NatLiteral(2)), NatLiteral(3)))]));
|
|
|
|
parse_test!(".1 + .2", AST(vec![exst!(binexp!("+", FloatLiteral(0.1), FloatLiteral(0.2)))]));
|
|
parse_test!("1 / 2", AST(vec![exst!(binexp!("/", NatLiteral(1), NatLiteral(2)))]));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_tuples() {
|
|
parse_test!("()", AST(vec![exst!(TupleLiteral(vec![]))]));
|
|
parse_test!("(\"hella\", 34)", AST(vec![exst!(
|
|
TupleLiteral(
|
|
vec![ex!(s r#""hella""#).into(), ex!(s "34").into()]
|
|
)
|
|
)]));
|
|
parse_test!("((1+2), \"slough\")", AST(vec![exst!(TupleLiteral(vec![
|
|
ex!(binexp!("+", NatLiteral(1), NatLiteral(2))).into(),
|
|
ex!(StringLiteral(rc!(slough))).into(),
|
|
]))]))
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_identifiers() {
|
|
parse_test!("a", AST(vec![exst!(val!("a"))]));
|
|
parse_test!("some_value", AST(vec![exst!(val!("some_value"))]));
|
|
parse_test!("a + b", AST(vec![exst!(binexp!("+", val!("a"), val!("b")))]));
|
|
//parse_test!("a[b]", AST(vec![Expression(
|
|
//parse_test!("a[]", <- TODO THIS NEEDS TO FAIL
|
|
//parse_test("a()[b]()[d]")
|
|
//TODO fix this parsing stuff
|
|
/*
|
|
parse_test! { "perspicacity()[a]", AST(vec![
|
|
exst!(Index {
|
|
indexee: bx!(ex!(Call { f: bx!(ex!(val!("perspicacity"))), arguments: vec![] })),
|
|
indexers: vec![ex!(val!("a"))]
|
|
})
|
|
])
|
|
}
|
|
*/
|
|
parse_test!("a[b,c]", AST(vec![exst!(Index { indexee: bx!(ex!(m val!("a"))), indexers: vec![ex!(m val!("b")), ex!(m val!("c"))]} )]));
|
|
|
|
parse_test!("None", AST(vec![exst!(val!("None"))]));
|
|
parse_test!("Pandas { a: x + y }", AST(vec![
|
|
exst!(NamedStruct { name: rc!(Pandas), fields: vec![(rc!(a), ex!(m binexp!("+", val!("x"), val!("y"))))]})
|
|
]));
|
|
parse_test! { "Pandas { a: n, b: q, }",
|
|
AST(vec![
|
|
exst!(NamedStruct { name: rc!(Pandas), fields:
|
|
vec![(rc!(a), ex!(m val!("n"))), (rc!(b), ex!(m val!("q")))]
|
|
}
|
|
)
|
|
])
|
|
};
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_complicated_operators() {
|
|
parse_test!("a <- b", AST(vec![exst!(binexp!("<-", val!("a"), val!("b")))]));
|
|
parse_test!("a || b", AST(vec![exst!(binexp!("||", val!("a"), val!("b")))]));
|
|
parse_test!("a<>b", AST(vec![exst!(binexp!("<>", val!("a"), val!("b")))]));
|
|
parse_test!("a.b.c.d", AST(vec![exst!(binexp!(".",
|
|
binexp!(".",
|
|
binexp!(".", val!("a"), val!("b")),
|
|
val!("c")),
|
|
val!("d")))]));
|
|
parse_test!("-3", AST(vec![exst!(prefexp!("-", NatLiteral(3)))]));
|
|
parse_test!("-0.2", AST(vec![exst!(prefexp!("-", FloatLiteral(0.2)))]));
|
|
parse_test!("!3", AST(vec![exst!(prefexp!("!", NatLiteral(3)))]));
|
|
parse_test!("a <- -b", AST(vec![exst!(binexp!("<-", val!("a"), prefexp!("-", val!("b"))))]));
|
|
parse_test!("a <--b", AST(vec![exst!(binexp!("<--", val!("a"), val!("b")))]));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_functions() {
|
|
parse_test!("fn oi()", AST(vec![Meta::new(Declaration(FuncSig(Signature { name: rc!(oi), operator: false, params: vec![], type_anno: None })))]));
|
|
parse_test!("oi()", AST(vec![exst!(Call { f: bx!(ex!(m val!("oi"))), arguments: vec![] })]));
|
|
parse_test!("oi(a, 2 + 2)", AST(vec![exst!(Call
|
|
{ f: bx!(ex!(m val!("oi"))),
|
|
arguments: vec![inv!(ex!(val!("a"))).into(), inv!(ex!(binexp!("+", NatLiteral(2), NatLiteral(2)))).into()]
|
|
})]));
|
|
parse_error!("a(b,,c)");
|
|
|
|
parse_test!("fn a(b, c: Int): Int", AST(vec![Meta::new(Declaration(
|
|
FuncSig(Signature { name: rc!(a), operator: false, params: vec![
|
|
(rc!(b), None), (rc!(c), Some(ty!("Int")))
|
|
], type_anno: Some(ty!("Int")) })))]));
|
|
|
|
|
|
parse_test!("fn a(x) { x() }", AST(vec![Meta::new(Declaration(
|
|
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None },
|
|
vec![exst!(Call { f: bx!(ex!(m val!("x"))), arguments: vec![] })])))]));
|
|
parse_test!("fn a(x) {\n x() }", AST(vec![Meta::new(Declaration(
|
|
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None },
|
|
vec![exst!(Call { f: bx!(ex!(m val!("x"))), arguments: vec![] })])))]));
|
|
|
|
let multiline = r#"
|
|
fn a(x) {
|
|
x()
|
|
}
|
|
"#;
|
|
parse_test!(multiline, AST(vec![Meta::new(Declaration(
|
|
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None },
|
|
vec![exst!(Call { f: bx!(ex!(m val!("x"))), arguments: vec![] })])))]));
|
|
let multiline2 = r#"
|
|
fn a(x) {
|
|
|
|
x()
|
|
|
|
}
|
|
"#;
|
|
parse_test!(multiline2, AST(vec![Meta::new(Declaration(
|
|
FuncDecl(Signature { name: rc!(a), operator: false, params: vec![(rc!(x),None)], type_anno: None },
|
|
vec![exst!(s "x()")])))]));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_bools() {
|
|
parse_test!("false", AST(vec![exst!(BoolLiteral(false))]));
|
|
parse_test!("true", AST(vec![exst!(BoolLiteral(true))]));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_strings() {
|
|
parse_test!(r#""hello""#, AST(vec![exst!(StringLiteral(rc!(hello)))]));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_types() {
|
|
parse_test!("type Yolo = Yolo", AST(vec![Meta::new(Declaration(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![UnitStruct(rc!(Yolo))]), mutable: false} ))]));
|
|
parse_test!("type mut Yolo = Yolo", AST(vec![Meta::new(Declaration(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![UnitStruct(rc!(Yolo))]), mutable: true} ))]));
|
|
parse_test!("type alias Sex = Drugs", AST(vec![Meta::new(Declaration(TypeAlias(rc!(Sex), rc!(Drugs))))]));
|
|
parse_test!("type Sanchez = Miguel | Alejandro(Int, Option<a>) | Esperanza { a: Int, b: String }",
|
|
AST(vec![Meta::new(Declaration(TypeDecl{
|
|
name: tys!("Sanchez"),
|
|
body: TypeBody(vec![
|
|
UnitStruct(rc!(Miguel)),
|
|
TupleStruct(rc!(Alejandro), vec![
|
|
Singleton(TypeSingletonName { name: rc!(Int), params: vec![] }),
|
|
Singleton(TypeSingletonName { name: rc!(Option), params: vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })] }),
|
|
]),
|
|
Record{
|
|
name: rc!(Esperanza),
|
|
members: vec![
|
|
(rc!(a), Singleton(TypeSingletonName { name: rc!(Int), params: vec![] })),
|
|
(rc!(b), Singleton(TypeSingletonName { name: rc!(String), params: vec![] })),
|
|
]
|
|
}
|
|
]),
|
|
mutable: false
|
|
}))]));
|
|
|
|
parse_test!("type Jorge<a> = Diego | Kike(a)", AST(vec![
|
|
Meta::new(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
|
|
}
|
|
))]));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_bindings() {
|
|
parse_test!("let mut a = 10", AST(vec![Meta::new(Declaration(Binding { name: rc!(a), constant: false, type_anno: None, expr: ex!(m NatLiteral(10)) } ))]));
|
|
parse_test!("let a = 2 + 2", AST(vec![Meta::new(Declaration(Binding { name: rc!(a), constant: true, type_anno: None, expr: ex!(m binexp!("+", NatLiteral(2), NatLiteral(2))) }) )]));
|
|
parse_test!("let a: Nat = 2 + 2", AST(vec![Meta::new(Declaration(
|
|
Binding { name: rc!(a), constant: true, type_anno: Some(Singleton(TypeSingletonName { name: rc!(Nat), params: vec![] })),
|
|
expr: Meta::new(ex!(binexp!("+", NatLiteral(2), NatLiteral(2)))) }
|
|
))]));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_block_expressions() {
|
|
parse_test! {
|
|
"if a() then { b(); c() }", AST(vec![exst!(
|
|
IfExpression {
|
|
discriminator: bx! {
|
|
Discriminator::Simple(ex!(Call { f: bx!(ex!(m val!("a"))), arguments: vec![]}))
|
|
},
|
|
body: bx! {
|
|
IfExpressionBody::SimpleConditional(
|
|
vec![exst!(Call { f: bx!(ex!(m val!("b"))), arguments: vec![]}), exst!(Call { f: bx!(ex!(m val!("c"))), arguments: vec![] })],
|
|
None
|
|
)
|
|
}
|
|
}
|
|
)])
|
|
};
|
|
|
|
parse_test! {
|
|
"if a() then { b(); c() } else { q }", AST(vec![exst!(
|
|
IfExpression {
|
|
discriminator: bx! {
|
|
Discriminator::Simple(ex!(Call { f: bx!(ex!(m val!("a"))), arguments: vec![]}))
|
|
},
|
|
body: bx! {
|
|
IfExpressionBody::SimpleConditional(
|
|
vec![exst!(Call { f: bx!(ex!(m val!("b"))), arguments: vec![]}), exst!(Call { f: bx!(ex!(m val!("c"))), arguments: vec![] })],
|
|
Some(
|
|
vec![exst!(val!("q"))],
|
|
)
|
|
)
|
|
}
|
|
}
|
|
)])
|
|
};
|
|
|
|
/*
|
|
parse_test!("if a() then { b(); c() }", AST(vec![exst!(
|
|
IfExpression(bx!(ex!(Call { f: bx!(ex!(val!("a"))), arguments: vec![]})),
|
|
vec![exst!(Call { f: bx!(ex!(val!("b"))), arguments: vec![]}), exst!(Call { f: bx!(ex!(val!("c"))), arguments: vec![] })],
|
|
None)
|
|
)]));
|
|
parse_test!(r#"
|
|
if true then {
|
|
const a = 10
|
|
b
|
|
} else {
|
|
c
|
|
}"#,
|
|
AST(vec![exst!(IfExpression(bx!(ex!(BoolLiteral(true))),
|
|
vec![Declaration(Binding { name: rc!(a), constant: true, expr: ex!(NatLiteral(10)) }),
|
|
exst!(val!(rc!(b)))],
|
|
Some(vec![exst!(val!(rc!(c)))])))])
|
|
);
|
|
|
|
parse_test!("if a { b } else { c }", AST(vec![exst!(
|
|
IfExpression(bx!(ex!(val!("a"))),
|
|
vec![exst!(val!("b"))],
|
|
Some(vec![exst!(val!("c"))])))]));
|
|
|
|
parse_test!("if (A {a: 1}) { b } else { c }", AST(vec![exst!(
|
|
IfExpression(bx!(ex!(NamedStruct { name: rc!(A), fields: vec![(rc!(a), ex!(NatLiteral(1)))]})),
|
|
vec![exst!(val!("b"))],
|
|
Some(vec![exst!(val!("c"))])))]));
|
|
|
|
parse_error!("if A {a: 1} { b } else { c }");
|
|
*/
|
|
}
|
|
#[test]
|
|
fn parsing_interfaces() {
|
|
parse_test!("interface Unglueable { fn unglue(a: Glue); fn mar(): Glue }", AST(vec![
|
|
Meta::new(Declaration(Interface {
|
|
name: rc!(Unglueable),
|
|
signatures: vec![
|
|
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![] })) },
|
|
]
|
|
}))
|
|
]));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_impls() {
|
|
parse_test!("impl Heh { fn yolo(); fn swagg(); }", AST(vec![
|
|
Meta::new(
|
|
Declaration(Impl {
|
|
type_name: ty!("Heh"),
|
|
interface_name: None,
|
|
block: vec![
|
|
FuncSig(Signature { name: rc!(yolo), operator: false, params: vec![], type_anno: None }),
|
|
FuncSig(Signature { name: rc!(swagg), operator: false, params: vec![], type_anno: None })
|
|
] }))]));
|
|
|
|
parse_test!("impl Mondai for Lollerino { fn yolo(); fn swagg(); }", AST(vec![
|
|
Meta::new(Declaration(Impl {
|
|
type_name: ty!("Lollerino"),
|
|
interface_name: Some(TypeSingletonName { name: rc!(Mondai), params: vec![] }),
|
|
block: vec![
|
|
FuncSig(Signature { name: rc!(yolo), operator: false, params: vec![], type_anno: None}),
|
|
FuncSig(Signature { name: rc!(swagg), operator: false, params: vec![], type_anno: None })
|
|
] }))]));
|
|
|
|
parse_test!("impl Hella<T> for (Alpha, Omega) { }", AST(vec![
|
|
Meta::new(Declaration(Impl {
|
|
type_name: Tuple(vec![ty!("Alpha"), ty!("Omega")]),
|
|
interface_name: Some(TypeSingletonName { name: rc!(Hella), params: vec![ty!("T")] }),
|
|
block: vec![]
|
|
}))
|
|
]));
|
|
|
|
parse_test!("impl Option<WTFMate> { fn oi() }", AST(vec![
|
|
Meta::new(
|
|
Declaration(Impl {
|
|
type_name: Singleton(TypeSingletonName { name: rc!(Option), params: vec![ty!("WTFMate")]}),
|
|
interface_name: None,
|
|
block: vec![
|
|
FuncSig(Signature { name: rc!(oi), operator: false, params: vec![], type_anno: None }),
|
|
]
|
|
}))]));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_type_annotations() {
|
|
parse_test!("let a = b : Int", AST(vec![
|
|
Meta::new(
|
|
Declaration(Binding { name: rc!(a), constant: true, type_anno: None, expr:
|
|
ex!(m val!("b"), ty!("Int")) }))]));
|
|
|
|
parse_test!("a : Int", AST(vec![
|
|
exst!(val!("a"), ty!("Int"))
|
|
]));
|
|
|
|
parse_test!("a : Option<Int>", AST(vec![
|
|
exst!(val!("a"), Singleton(TypeSingletonName { name: rc!(Option), params: vec![ty!("Int")] }))
|
|
]));
|
|
|
|
parse_test!("a : KoreanBBQSpecifier<Kimchi, Option<Bulgogi> >", AST(vec![
|
|
exst!(val!("a"), Singleton(TypeSingletonName { name: rc!(KoreanBBQSpecifier), params: vec![
|
|
ty!("Kimchi"), Singleton(TypeSingletonName { name: rc!(Option), params: vec![ty!("Bulgogi")] })
|
|
] }))
|
|
]));
|
|
|
|
parse_test!("a : (Int, Yolo<a>)", AST(vec![
|
|
exst!(val!("a"), Tuple(
|
|
vec![ty!("Int"), Singleton(TypeSingletonName {
|
|
name: rc!(Yolo), params: vec![ty!("a")]
|
|
})]))]));
|
|
}
|
|
|
|
#[test]
|
|
fn parsing_lambdas() {
|
|
parse_test_wrap_ast! { r#"\(x) { x + 1}"#, exst!(
|
|
Lambda { params: vec![(rc!(x), None)], type_anno: None, body: vec![exst!(s "x + 1")] }
|
|
)
|
|
}
|
|
|
|
parse_test!(r#"\ (x: Int, y) { a;b;c;}"#, AST(vec![
|
|
exst!(Lambda {
|
|
params: vec![(rc!(x), Some(ty!("Int"))), (rc!(y), None)],
|
|
type_anno: None,
|
|
body: vec![exst!(s "a"), exst!(s "b"), exst!(s "c")]
|
|
})
|
|
]));
|
|
|
|
parse_test!(r#"\(x){y}(1)"#, AST(vec![
|
|
exst!(Call { f: bx!(ex!(m
|
|
Lambda {
|
|
params: vec![(rc!(x), None)],
|
|
type_anno: None,
|
|
body: vec![exst!(s "y")] }
|
|
)),
|
|
arguments: vec![inv!(ex!(NatLiteral(1))).into()] })]));
|
|
|
|
parse_test_wrap_ast! {
|
|
r#"\(x: Int): String { "q" }"#,
|
|
exst!(Lambda {
|
|
params: vec![(rc!(x), Some(ty!("Int")))],
|
|
type_anno: Some(ty!("String")),
|
|
body: vec![exst!(s r#""q""#)]
|
|
})
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn single_param_lambda() {
|
|
parse_test_wrap_ast! {
|
|
r"\x { x + 10 }",
|
|
exst!(Lambda {
|
|
params: vec![(rc!(x), None)],
|
|
type_anno: None,
|
|
body: vec![exst!(s r"x + 10")]
|
|
})
|
|
}
|
|
|
|
parse_test_wrap_ast! {
|
|
r"\x: Nat { x + 10 }",
|
|
exst!(Lambda {
|
|
params: vec![(rc!(x), Some(ty!("Nat")))],
|
|
type_anno: None,
|
|
body: vec![exst!(s r"x + 10")]
|
|
})
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn more_advanced_lambdas() {
|
|
parse_test! {
|
|
r#"fn wahoo() { let a = 10; \(x) { x + a } };
|
|
wahoo()(3) "#, AST(vec![
|
|
exst!(s r"fn wahoo() { let a = 10; \(x) { x + a } }"),
|
|
exst! {
|
|
Call {
|
|
f: bx!(ex!(m Call { f: bx!(ex!(m val!("wahoo"))), arguments: vec![] })),
|
|
arguments: vec![inv!(ex!(NatLiteral(3))).into()],
|
|
}
|
|
}
|
|
])
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn list_literals() {
|
|
parse_test! {
|
|
"[1,2]", AST(vec![
|
|
exst!(ListLiteral(vec![ex!(m NatLiteral(1)), ex!(m NatLiteral(2))]))])
|
|
};
|
|
}
|
|
|
|
#[test]
|
|
fn while_expr() {
|
|
parse_test! {
|
|
"while { }", AST(vec![
|
|
exst!(WhileExpression { condition: None, body: vec![] })])
|
|
}
|
|
|
|
parse_test! {
|
|
"while a == b { }", AST(vec![
|
|
exst!(WhileExpression { condition: Some(bx![ex![m binexp!("==", val!("a"), val!("b"))]]), body: vec![] })])
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn for_expr() {
|
|
parse_test! {
|
|
"for { a <- maybeValue } return 1", AST(vec![
|
|
exst!(ForExpression {
|
|
enumerators: vec![Enumerator { id: rc!(a), generator: ex!(m val!("maybeValue")) }],
|
|
body: bx!(MonadicReturn(Meta::new(ex!(s "1"))))
|
|
})])
|
|
}
|
|
|
|
parse_test! {
|
|
"for n <- someRange { f(n); }", AST(vec![
|
|
exst!(ForExpression { enumerators: vec![Enumerator { id: rc!(n), generator: ex!(m val!("someRange"))}],
|
|
body: bx!(ForBody::StatementBlock(vec![exst!(s "f(n)")]))
|
|
})])
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn patterns() {
|
|
parse_test_wrap_ast! {
|
|
"if x is Some(a) then { 4 } else { 9 }", exst!(
|
|
IfExpression {
|
|
discriminator: bx!(Discriminator::Simple(ex!(s "x"))),
|
|
body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(rc!(Some), vec![Pattern::Literal(PatternLiteral::VarPattern(rc!(a)))]), vec![exst!(s "4")], Some(vec![exst!(s "9")]))) }
|
|
)
|
|
}
|
|
|
|
parse_test_wrap_ast! {
|
|
"if x is Some(a) then 4 else 9", exst!(
|
|
IfExpression {
|
|
discriminator: bx!(Discriminator::Simple(ex!(s "x"))),
|
|
body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(rc!(Some), vec![Pattern::Literal(PatternLiteral::VarPattern(rc!(a)))]), vec![exst!(s "4")], Some(vec![exst!(s "9")]))) }
|
|
)
|
|
}
|
|
|
|
parse_test_wrap_ast! {
|
|
"if x is Something { a, b: x } then { 4 } else { 9 }", exst!(
|
|
IfExpression {
|
|
discriminator: bx!(Discriminator::Simple(ex!(s "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![exst!(s "4")], Some(vec![exst!(s "9")])))
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn pattern_literals() {
|
|
parse_test_wrap_ast! {
|
|
"if x is -1 then 1 else 2",
|
|
exst!(
|
|
IfExpression {
|
|
discriminator: bx!(Discriminator::Simple(ex!(s "x"))),
|
|
body: bx!(IfExpressionBody::SimplePatternMatch(
|
|
Pattern::Literal(PatternLiteral::NumPattern { neg: true, num: NatLiteral(1) }),
|
|
vec![exst!(NatLiteral(1))],
|
|
Some(vec![exst!(NatLiteral(2))]),
|
|
))
|
|
}
|
|
)
|
|
}
|
|
|
|
parse_test_wrap_ast! {
|
|
"if x is 1 then 1 else 2",
|
|
exst!(
|
|
IfExpression {
|
|
discriminator: bx!(Discriminator::Simple(ex!(s "x"))),
|
|
body: bx!(IfExpressionBody::SimplePatternMatch(
|
|
Pattern::Literal(PatternLiteral::NumPattern { neg: false, num: NatLiteral(1) }),
|
|
vec![exst!(s "1")],
|
|
Some(vec![exst!(s "2")]),
|
|
))
|
|
}
|
|
)
|
|
}
|
|
|
|
parse_test! {
|
|
"if x is true then 1 else 2", AST(vec![
|
|
exst!(
|
|
IfExpression {
|
|
discriminator: bx!(Discriminator::Simple(ex!(s "x"))),
|
|
body: bx!(IfExpressionBody::SimplePatternMatch(
|
|
Pattern::Literal(PatternLiteral::BoolPattern(true)),
|
|
vec![exst!(NatLiteral(1))],
|
|
Some(vec![exst!(NatLiteral(2))]),
|
|
))
|
|
}
|
|
)
|
|
])
|
|
}
|
|
|
|
parse_test_wrap_ast! {
|
|
"if x is \"gnosticism\" then 1 else 2",
|
|
exst!(
|
|
IfExpression {
|
|
discriminator: bx!(Discriminator::Simple(ex!(s "x"))),
|
|
body: bx!(IfExpressionBody::SimplePatternMatch(
|
|
Pattern::Literal(PatternLiteral::StringPattern(rc!(gnosticism))),
|
|
vec![exst!(s "1")],
|
|
Some(vec![exst!(s "2")]),
|
|
))
|
|
}
|
|
)
|
|
}
|
|
}
|
|
}
|