2019-06-08 23:59:49 -07:00
|
|
|
//! # Parsing
|
|
|
|
//! This module is where the recursive-descent parsing methods live.
|
|
|
|
//!
|
|
|
|
//!
|
2019-06-16 16:07:27 -07:00
|
|
|
//! # Schala EBNF Grammar
|
2019-06-09 01:08:32 -07:00
|
|
|
//! 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.
|
2019-06-08 23:59:49 -07:00
|
|
|
//!
|
|
|
|
//! ## Top level structure
|
|
|
|
//!
|
2019-06-14 02:28:14 -07:00
|
|
|
//! ```text
|
2019-06-08 23:59:49 -07:00
|
|
|
//! program := (statement delimiter)* EOF
|
|
|
|
//! delimiter := NEWLINE | ";"
|
|
|
|
//! statement := expression | declaration
|
|
|
|
//! block := "{" (statement delimiter)* "}"
|
|
|
|
//! declaration := type_declaration | func_declaration | binding_declaration | impl_declaration
|
|
|
|
//! ```
|
2019-06-09 01:08:32 -07:00
|
|
|
//! ## Declarations
|
|
|
|
//!
|
|
|
|
//! ### Types
|
2019-06-14 02:28:14 -07:00
|
|
|
//! ```text
|
2019-06-08 23:59:49 -07:00
|
|
|
//! 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
|
|
|
|
//! ```
|
2019-06-09 01:08:32 -07:00
|
|
|
//! ### Functions
|
|
|
|
//!
|
2019-06-14 02:28:14 -07:00
|
|
|
//! ```text
|
2019-06-08 23:59:49 -07:00
|
|
|
//! 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+
|
|
|
|
//! ```
|
2019-06-09 01:08:32 -07:00
|
|
|
//!
|
|
|
|
//! ### Variable bindings
|
2019-06-14 02:28:14 -07:00
|
|
|
//! ```text binding_declaration := "let" "mut"? IDENTIFIER "=" expresion```
|
2019-06-09 01:08:32 -07:00
|
|
|
//!
|
|
|
|
//! ### Interfaces
|
|
|
|
//!
|
2019-06-14 02:28:14 -07:00
|
|
|
//! ```text
|
2019-06-08 23:59:49 -07:00
|
|
|
//! 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)* "}"
|
|
|
|
//! ```
|
2019-06-09 01:08:32 -07:00
|
|
|
//!
|
|
|
|
//! ### Type Annotations
|
|
|
|
//!
|
2019-06-14 02:28:14 -07:00
|
|
|
//! ```text
|
2019-06-09 01:08:32 -07:00
|
|
|
//! type_anno := ":" type_name
|
2019-06-08 23:59:49 -07:00
|
|
|
//! 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)* ">"
|
|
|
|
//! ```
|
2019-06-09 01:08:32 -07:00
|
|
|
//!
|
2019-06-08 23:59:49 -07:00
|
|
|
//! ## Expressions
|
2019-06-14 02:28:14 -07:00
|
|
|
//! ```text
|
2019-06-08 23:59:49 -07:00
|
|
|
//! expression := precedence_expr type_anno+
|
|
|
|
//! precedence_expr := prefix_expr
|
|
|
|
//! prefix_expr := prefix_op call_expr
|
|
|
|
//! prefix_op := "+" | "-" | "!" | "~"
|
2019-06-11 17:20:20 -07:00
|
|
|
//! call_expr := index_expr ( "(" invocation_list ")" )* | ε
|
|
|
|
//! invocation_list := invocation_argument ("," invocation_argument)* | ε
|
|
|
|
//! invocation_argument := expression | IDENTIFIER "=" expression | "_"
|
2019-06-08 23:59:49 -07:00
|
|
|
//! index_expr := primary ( "[" (expression ("," (expression)* | ε) "]" )*
|
|
|
|
//! primary := literal | paren_expr | if_expr | for_expr | while_expr | identifier_expr | lambda_expr | anonymous_struct | list_expr
|
2019-06-09 01:08:32 -07:00
|
|
|
//! expr_or_block := "{" (statement delimiter)* "}" | expr
|
2019-06-08 23:59:49 -07:00
|
|
|
//! ```
|
2019-06-09 01:08:32 -07:00
|
|
|
//!
|
|
|
|
//! ### Primary expressions
|
|
|
|
//!
|
2019-06-14 02:28:14 -07:00
|
|
|
//! ```text
|
2019-06-08 23:59:49 -07:00
|
|
|
//! list_expr := "[" (expression, ",")* "]"
|
2019-06-09 01:12:19 -07:00
|
|
|
//! lambda_expr := "\\" lambda_param_list type_anno+ nonempty_func_body
|
2019-06-08 23:59:49 -07:00
|
|
|
//! lambda_param_list := formal_param_list | formal_param
|
2019-06-09 01:12:19 -07:00
|
|
|
//! paren_expr := "(" paren_inner ")"
|
2019-06-08 23:59:49 -07:00
|
|
|
//! paren_inner := (expression ",")*
|
2019-08-31 23:39:01 -07:00
|
|
|
//! identifier_expr := qualified_identifier | named_struct
|
|
|
|
//! qualified_identifier := IDENTIFIER ("::" IDENTIFIER)*
|
2019-06-08 23:59:49 -07:00
|
|
|
//! ```
|
2019-06-09 01:08:32 -07:00
|
|
|
//!
|
|
|
|
//! ## Literals
|
2019-06-14 02:28:14 -07:00
|
|
|
//! ```text
|
2019-06-08 23:59:49 -07:00
|
|
|
//! literal := "true" | "false" | number_literal | STR_LITERAL
|
2019-08-31 23:39:01 -07:00
|
|
|
//! named_struct := qualified_identifier record_block
|
2019-06-08 23:59:49 -07:00
|
|
|
//! 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)
|
2019-06-09 01:12:19 -07:00
|
|
|
//! digits := (digit_group underscore*)+
|
|
|
|
//! digit_group := DIGIT+
|
2019-06-08 23:59:49 -07:00
|
|
|
//! ```
|
2019-06-09 01:08:32 -07:00
|
|
|
//!
|
|
|
|
//! ### Patterns
|
2019-06-14 02:28:14 -07:00
|
|
|
//! ```text
|
2019-06-08 23:59:49 -07:00
|
|
|
//! pattern := "(" (pattern, ",")* ")" | simple_pattern
|
|
|
|
//! simple_pattern := pattern_literal | record_pattern | tuple_struct_pattern
|
2019-09-03 21:14:12 -07:00
|
|
|
//! pattern_literal := "true" | "false" | signed_number_literal | STR_LITERAL | qualified_identifier
|
2019-06-08 23:59:49 -07:00
|
|
|
//! signed_number_literal := "-"? number_literal
|
2019-09-03 21:14:12 -07:00
|
|
|
//! record_pattern := qualified_identifier "{" (record_pattern_entry, ",")* "}"
|
2019-06-08 23:59:49 -07:00
|
|
|
//! record_pattern_entry := IDENTIFIER | IDENTIFIER ":" Pattern
|
2019-09-03 21:14:12 -07:00
|
|
|
//! tuple_struct_pattern := qualified_identifier "(" (pattern, ",")* ")"
|
2019-06-08 23:59:49 -07:00
|
|
|
//! ```
|
2019-06-09 01:08:32 -07:00
|
|
|
//!
|
|
|
|
//! ### If-expressions
|
2019-06-14 02:28:14 -07:00
|
|
|
//! ```text
|
2019-06-08 23:59:49 -07:00
|
|
|
//! 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
|
|
|
|
//! ```
|
2019-06-09 01:08:32 -07:00
|
|
|
//!
|
|
|
|
//! ### While expressions
|
2019-06-14 02:28:14 -07:00
|
|
|
//! ```text
|
2019-06-08 23:59:49 -07:00
|
|
|
//! while_expr := "while" while_cond "{" (statement delimiter)* "}"
|
|
|
|
//! while_cond := ε | expression | expression "is" pattern //TODO maybe is-expresions should be primary
|
|
|
|
//! ```
|
2019-06-09 01:08:32 -07:00
|
|
|
//!
|
2019-06-08 23:59:49 -07:00
|
|
|
//! //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
|
2019-06-09 01:08:32 -07:00
|
|
|
//! ### For-expressions
|
2019-06-14 02:28:14 -07:00
|
|
|
//! ```text
|
2019-06-08 23:59:49 -07:00
|
|
|
//! 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.
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
|
2019-06-16 14:59:11 -07:00
|
|
|
mod test;
|
|
|
|
|
2017-08-29 05:08:09 -07:00
|
|
|
use std::rc::Rc;
|
2019-08-14 07:25:45 -07:00
|
|
|
use std::str::FromStr;
|
2017-08-29 04:27:07 -07:00
|
|
|
|
2019-01-07 13:00:37 -08:00
|
|
|
use crate::tokenizing::*;
|
|
|
|
use crate::tokenizing::Kw::*;
|
|
|
|
use crate::tokenizing::TokenKind::*;
|
2017-09-06 09:42:29 -07:00
|
|
|
|
2019-01-07 13:00:37 -08:00
|
|
|
use crate::ast::*;
|
2018-06-04 19:25:40 -07:00
|
|
|
|
2019-06-08 23:59:49 -07:00
|
|
|
/// Represents a parsing error
|
2018-07-11 02:32:29 -07:00
|
|
|
#[derive(Debug)]
|
2017-09-11 02:07:17 -07:00
|
|
|
pub struct ParseError {
|
|
|
|
pub msg: String,
|
2019-01-08 01:04:46 -08:00
|
|
|
pub token: Token
|
2017-09-11 02:07:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ParseError {
|
2019-02-20 02:05:24 -08:00
|
|
|
fn new_with_token<T, M>(msg: M, token: Token) -> ParseResult<T> where M: Into<String> {
|
|
|
|
Err(ParseError { msg: msg.into(), token })
|
2019-01-05 17:18:10 -08:00
|
|
|
}
|
2017-09-11 02:07:17 -07:00
|
|
|
}
|
|
|
|
|
2019-06-08 23:59:49 -07:00
|
|
|
/// Represents either a successful parsing result or a ParseError
|
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
|
|
|
|
2019-06-08 23:59:49 -07:00
|
|
|
/// Main data structure for doing parsing.
|
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,
|
2019-09-18 09:56:11 -07:00
|
|
|
id_store: ItemIdStore,
|
2018-02-11 22:10:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ParserRestrictions {
|
|
|
|
no_struct_literal: bool
|
2017-09-09 01:25:11 -07:00
|
|
|
}
|
|
|
|
|
2018-10-21 16:33:21 -07:00
|
|
|
struct TokenHandler {
|
2019-06-14 00:44:54 -07:00
|
|
|
tokens: Vec<Token>,
|
2019-06-14 00:21:32 -07:00
|
|
|
idx: usize,
|
2019-01-05 17:18:10 -08:00
|
|
|
end_of_file: (usize, usize),
|
2018-10-21 16:33:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenHandler {
|
|
|
|
fn new(tokens: Vec<Token>) -> TokenHandler {
|
2019-01-05 17:18:10 -08:00
|
|
|
let end_of_file = match tokens.last() {
|
|
|
|
None => (0, 0),
|
2019-01-08 02:11:19 -08:00
|
|
|
Some(t) => (t.line_num, t.char_num)
|
2019-01-05 17:18:10 -08:00
|
|
|
};
|
2019-06-14 00:44:54 -07:00
|
|
|
TokenHandler { idx: 0, tokens, end_of_file }
|
2018-10-21 16:33:21 -07:00
|
|
|
}
|
|
|
|
|
2019-01-05 18:38:18 -08:00
|
|
|
fn peek_kind(&mut self) -> TokenKind {
|
2019-06-14 00:44:54 -07:00
|
|
|
self.peek().kind
|
2018-10-21 16:33:21 -07:00
|
|
|
}
|
2019-06-14 01:30:53 -07:00
|
|
|
|
|
|
|
fn peek_kind_n(&mut self, n: usize) -> TokenKind {
|
|
|
|
self.peek_n(n).kind
|
|
|
|
}
|
2019-01-05 18:38:18 -08:00
|
|
|
fn peek(&mut self) -> Token {
|
2019-06-14 00:44:54 -07:00
|
|
|
self.tokens.get(self.idx).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})
|
2018-10-21 16:33:21 -07:00
|
|
|
}
|
2019-08-31 23:39:01 -07:00
|
|
|
/// calling peek_n(0) is the same thing as peek()
|
2019-06-14 01:30:53 -07:00
|
|
|
fn peek_n(&mut self, n: usize) -> Token {
|
|
|
|
self.tokens.get(self.idx + n).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})
|
|
|
|
}
|
2019-01-05 17:50:54 -08:00
|
|
|
fn next(&mut self) -> Token {
|
2019-06-14 00:44:54 -07:00
|
|
|
self.idx += 1;
|
|
|
|
self.tokens.get(self.idx - 1).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})
|
2018-10-21 16:33:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-09 01:25:11 -07:00
|
|
|
impl Parser {
|
2019-06-08 23:59:49 -07:00
|
|
|
/// Create a new parser initialized with some tokens.
|
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,
|
2019-09-18 09:56:11 -07:00
|
|
|
restrictions: ParserRestrictions { no_struct_literal: false },
|
|
|
|
id_store: ItemIdStore::new(),
|
2018-02-11 22:10:21 -08:00
|
|
|
}
|
2017-09-09 01:25:11 -07:00
|
|
|
}
|
|
|
|
|
2019-06-08 23:59:49 -07:00
|
|
|
/// Parse all loaded tokens up to this point.
|
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> {
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2019-08-12 09:32:04 -07:00
|
|
|
pub fn format_parse_trace(&self) -> String {
|
|
|
|
let mut buf = String::new();
|
|
|
|
for r in self.parse_record.iter() {
|
2018-10-20 14:27:00 -07:00
|
|
|
let mut indent = String::new();
|
|
|
|
for _ in 0..r.level {
|
|
|
|
indent.push(' ');
|
|
|
|
}
|
2019-08-12 09:32:04 -07:00
|
|
|
buf.push_str(&format!("{}Production `{}`, token: {}\n", indent, r.production_name, r.next_token))
|
|
|
|
}
|
|
|
|
buf
|
2018-10-20 14:27:00 -07:00
|
|
|
}
|
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 {
|
2019-01-05 17:40:05 -08:00
|
|
|
($self:expr, $token_kind:pat) => { expect!($self, $token_kind if true) };
|
|
|
|
($self:expr, $expected_kind:pat if $cond:expr) => {
|
|
|
|
{
|
2019-01-05 18:38:18 -08:00
|
|
|
let tok = $self.token_handler.peek();
|
2019-01-05 17:40:05 -08:00
|
|
|
match tok.get_kind() {
|
2019-01-05 18:29:24 -08:00
|
|
|
$expected_kind if $cond => $self.token_handler.next(),
|
2019-01-05 17:40:05 -08:00
|
|
|
actual_kind => {
|
|
|
|
let msg = format!("Expected {}, got {:?}", print_token_pattern!($expected_kind), actual_kind);
|
2019-02-20 02:05:24 -08:00
|
|
|
return ParseError::new_with_token(msg, tok);
|
2019-01-05 17:40:05 -08:00
|
|
|
}
|
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 {
|
2019-01-05 18:38:18 -08:00
|
|
|
let peek = $self.token_handler.peek();
|
|
|
|
match peek.get_kind() {
|
2017-09-30 13:46:50 -07:00
|
|
|
$end | EOF => break,
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
if !$strictness {
|
2019-01-05 18:38:18 -08:00
|
|
|
match peek.get_kind() {
|
2019-01-05 18:29:24 -08:00
|
|
|
$( $delim )|* => { $self.token_handler.next(); continue },
|
2017-09-30 13:46:50 -07:00
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
2017-09-29 14:10:49 -07:00
|
|
|
acc.push($self.$parse_fn()?);
|
2019-01-05 18:38:18 -08:00
|
|
|
match $self.token_handler.peek().get_kind() {
|
2019-01-05 18:29:24 -08:00
|
|
|
$( $delim )|* => { $self.token_handler.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
|
|
|
|
2017-09-11 02:07:17 -07:00
|
|
|
impl Parser {
|
2019-06-08 23:59:49 -07:00
|
|
|
/// `program := (statement delimiter)* EOF`
|
|
|
|
/// `delimiter := NEWLINE | ';'`
|
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 {
|
2019-01-05 18:38:18 -08:00
|
|
|
match self.token_handler.peek().get_kind() {
|
2017-09-15 04:23:39 -07:00
|
|
|
EOF => break,
|
|
|
|
Newline | Semicolon => {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2017-09-15 04:23:39 -07:00
|
|
|
continue;
|
|
|
|
},
|
2019-01-04 22:58:25 -08:00
|
|
|
_ => statements.push(
|
2019-02-21 01:49:15 -08:00
|
|
|
Meta::new(self.statement()?)
|
2019-01-04 22:58:25 -08:00
|
|
|
),
|
2017-09-15 04:23:39 -07:00
|
|
|
}
|
|
|
|
}
|
2019-09-18 09:56:11 -07:00
|
|
|
Ok(AST { id: self.id_store.fresh(), statements })
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-11 02:07:17 -07:00
|
|
|
|
2019-06-08 23:59:49 -07:00
|
|
|
/// `statement := expression | declaration`
|
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
|
2019-09-17 02:25:11 -07:00
|
|
|
let kind = match self.token_handler.peek().get_kind() {
|
|
|
|
Keyword(Type) => self.type_declaration().map(|decl| { StatementKind::Declaration(decl) }),
|
|
|
|
Keyword(Func)=> self.func_declaration().map(|func| { StatementKind::Declaration(func) }),
|
|
|
|
Keyword(Let) => self.binding_declaration().map(|decl| StatementKind::Declaration(decl)),
|
|
|
|
Keyword(Interface) => self.interface_declaration().map(|decl| StatementKind::Declaration(decl)),
|
|
|
|
Keyword(Impl) => self.impl_declaration().map(|decl| StatementKind::Declaration(decl)),
|
|
|
|
_ => self.expression().map(|expr| { StatementKind::Expression(expr.into()) } ),
|
|
|
|
}?;
|
2019-09-18 10:07:20 -07:00
|
|
|
Ok(Statement { kind, id: self.id_store.fresh() })
|
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> {
|
2019-01-05 18:38:18 -08:00
|
|
|
if let Keyword(Alias) = self.token_handler.peek_kind() {
|
2017-10-04 22:02:31 -07:00
|
|
|
self.type_alias()
|
|
|
|
} else {
|
2019-01-05 18:38:18 -08:00
|
|
|
let mutable = if let Keyword(Mut) = self.token_handler.peek_kind() {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.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()?;
|
2019-06-16 16:07:27 -07:00
|
|
|
expect!(self, Equals);
|
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()?;
|
2019-06-16 16:07:27 -07:00
|
|
|
expect!(self, Equals);
|
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 {
|
2019-01-05 18:38:18 -08:00
|
|
|
if let Pipe = self.token_handler.peek_kind() {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2017-10-08 18:47:57 -07:00
|
|
|
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()?;
|
2019-01-05 18:38:18 -08:00
|
|
|
match self.token_handler.peek_kind() {
|
2017-10-08 19:02:52 -07:00
|
|
|
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);
|
2019-01-24 20:47:20 -08:00
|
|
|
Ok(Record {name, members: typed_identifier_list })
|
2017-10-08 19:02:52 -07:00
|
|
|
},
|
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()?;
|
2019-01-05 18:38:18 -08:00
|
|
|
if let LCurlyBrace = self.token_handler.peek_kind() {
|
2018-11-05 18:50:45 -08:00
|
|
|
let statements = self.nonempty_func_body()?;
|
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));
|
2019-01-05 18:38:18 -08:00
|
|
|
let (name, operator) = match self.token_handler.peek_kind() {
|
2018-08-24 16:49:59 -07:00
|
|
|
Operator(s) => {
|
|
|
|
let name = s.clone();
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2018-08-24 16:49:59 -07:00
|
|
|
(name, true)
|
|
|
|
},
|
|
|
|
_ => (self.identifier()?, false)
|
|
|
|
};
|
2018-11-05 18:50:45 -08:00
|
|
|
let params = self.formal_param_list()?;
|
2019-01-05 18:38:18 -08:00
|
|
|
let type_anno = match self.token_handler.peek_kind() {
|
2017-10-08 21:21:02 -07:00
|
|
|
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-11-05 18:50:45 -08:00
|
|
|
#[recursive_descent_method]
|
2019-02-21 01:49:15 -08:00
|
|
|
fn nonempty_func_body(&mut self) -> ParseResult<Vec<Meta<Statement>>> {
|
2019-01-04 22:58:25 -08:00
|
|
|
let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict);
|
2019-02-21 01:49:15 -08:00
|
|
|
Ok(statements.into_iter().map(|s| Meta::new(s)).collect())
|
2018-11-05 18:50:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[recursive_descent_method]
|
|
|
|
fn formal_param_list(&mut self) -> ParseResult<Vec<FormalParam>> {
|
|
|
|
Ok(delimited!(self, LParen, formal_param, Comma, RParen))
|
|
|
|
}
|
|
|
|
|
2019-06-16 14:56:52 -07:00
|
|
|
//TODO needs to support default values
|
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()?;
|
2019-06-16 14:56:52 -07:00
|
|
|
let anno = match self.token_handler.peek_kind() {
|
2017-10-08 20:55:05 -07:00
|
|
|
Colon => Some(self.type_anno()?),
|
|
|
|
_ => None
|
|
|
|
};
|
2019-06-16 16:07:27 -07:00
|
|
|
let default = match self.token_handler.peek_kind() {
|
2019-06-16 21:36:59 -07:00
|
|
|
Equals => {
|
|
|
|
self.token_handler.next();
|
2019-09-02 14:13:53 -07:00
|
|
|
Some(self.expression()?.into())
|
2019-06-16 21:36:59 -07:00
|
|
|
},
|
2019-06-16 16:07:27 -07:00
|
|
|
_ => None
|
|
|
|
};
|
2019-06-16 14:56:52 -07:00
|
|
|
Ok(FormalParam { name, anno, default })
|
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));
|
2019-01-05 18:38:18 -08:00
|
|
|
let constant = match self.token_handler.peek_kind() {
|
2018-07-11 16:44:15 -07:00
|
|
|
Keyword(Kw::Mut) => {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2018-07-11 16:44:15 -07:00
|
|
|
false
|
|
|
|
}
|
|
|
|
_ => true
|
2017-09-17 06:04:24 -07:00
|
|
|
};
|
|
|
|
let name = self.identifier()?;
|
2019-02-20 22:44:45 -08:00
|
|
|
let type_anno = if let Colon = self.token_handler.peek_kind() {
|
|
|
|
Some(self.type_anno()?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2019-06-16 16:07:27 -07:00
|
|
|
expect!(self, Equals);
|
2019-02-21 19:07:07 -08:00
|
|
|
let expr = self.expression()?.into();
|
2017-09-17 06:04:24 -07:00
|
|
|
|
2019-02-20 22:44:45 -08:00
|
|
|
Ok(Declaration::Binding { name, constant, type_anno, 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));
|
2019-01-08 00:51:56 -08:00
|
|
|
let first = self.type_singleton_name()?;
|
2019-01-05 18:38:18 -08:00
|
|
|
let second = if let Keyword(For) = self.token_handler.peek_kind() {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.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()?;
|
2019-01-08 00:51:56 -08:00
|
|
|
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 }
|
|
|
|
})
|
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())?;
|
2019-01-05 18:38:18 -08:00
|
|
|
let type_anno = match self.token_handler.peek_kind() {
|
2017-09-28 23:55:10 -07:00
|
|
|
Colon => Some(self.type_anno()?),
|
2017-09-26 22:10:13 -07:00
|
|
|
_ => None
|
|
|
|
};
|
2019-07-10 18:52:25 -07:00
|
|
|
if let Some(_) = expr_body.type_anno {
|
2019-01-08 01:00:40 -08:00
|
|
|
return ParseError::new_with_token("Bad parse state encountered", self.token_handler.peek());
|
2017-09-27 22:27:50 -07:00
|
|
|
}
|
2019-07-10 18:52:25 -07:00
|
|
|
expr_body.type_anno = type_anno;
|
2017-09-27 22:27:50 -07:00
|
|
|
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::*;
|
2019-01-05 18:38:18 -08:00
|
|
|
Ok(match self.token_handler.peek_kind() {
|
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()?,
|
2019-01-05 18:38:18 -08:00
|
|
|
params: match self.token_handler.peek_kind() {
|
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(),
|
2019-01-05 18:38:18 -08:00
|
|
|
next_token: format!("{}", self.token_handler.peek().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 {
|
2019-01-05 18:38:18 -08:00
|
|
|
let new_precedence = match BinOp::get_precedence_from_token(&self.token_handler.peek_kind()) {
|
2018-08-19 20:33:50 -07:00
|
|
|
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;
|
|
|
|
}
|
2019-01-05 18:29:24 -08:00
|
|
|
let next_tok = self.token_handler.next();
|
2019-01-05 17:50:54 -08:00
|
|
|
let operation = match BinOp::from_sigil_token(&next_tok.kind) {
|
2018-08-19 20:33:50 -07:00
|
|
|
Some(sigil) => sigil,
|
|
|
|
None => unreachable!()
|
2017-09-12 05:16:37 -07:00
|
|
|
};
|
|
|
|
let rhs = self.precedence_expr(new_precedence)?;
|
2019-09-18 14:15:05 -07:00
|
|
|
lhs = Expression::new(self.id_store.fresh(), ExpressionKind::BinExp(operation, bx!(lhs.into()), bx!(rhs.into())));
|
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> {
|
2019-01-05 18:38:18 -08:00
|
|
|
match self.token_handler.peek_kind() {
|
2018-02-23 04:10:00 -08:00
|
|
|
Operator(ref op) if PrefixOp::is_prefix(&*op) => {
|
2019-01-05 18:29:24 -08:00
|
|
|
let sigil = match self.token_handler.next().kind {
|
2017-09-17 17:44:06 -07:00
|
|
|
Operator(op) => op,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let expr = self.primary()?;
|
2019-08-14 07:25:45 -07:00
|
|
|
let prefix_op = PrefixOp::from_str(sigil.as_str()).unwrap();
|
2019-07-10 18:52:25 -07:00
|
|
|
Ok(Expression::new(
|
2019-09-18 14:15:05 -07:00
|
|
|
self.id_store.fresh(),
|
2019-08-14 07:25:45 -07:00
|
|
|
ExpressionKind::PrefixExp(prefix_op, bx!(expr.into()))
|
2019-01-05 16:02:30 -08:00
|
|
|
))
|
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-11-06 02:40:10 -08:00
|
|
|
let mut expr = self.index_expr()?;
|
2019-01-05 18:38:18 -08:00
|
|
|
while let LParen = self.token_handler.peek_kind() {
|
2019-06-12 03:28:46 -07:00
|
|
|
let arguments = delimited!(self, LParen, invocation_argument, Comma, RParen);
|
2019-09-02 14:41:09 -07:00
|
|
|
let arguments = arguments.into_iter().collect();
|
2019-09-18 14:15:05 -07:00
|
|
|
expr = Expression::new(self.id_store.fresh(), ExpressionKind::Call { f: bx!(expr.into()), arguments }); //TODO no type anno is incorrect
|
2018-11-06 02:40:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(expr)
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-03-06 02:51:45 -08:00
|
|
|
|
2019-06-11 17:20:20 -07:00
|
|
|
#[recursive_descent_method]
|
2019-06-12 03:28:46 -07:00
|
|
|
fn invocation_argument(&mut self) -> ParseResult<InvocationArgument> {
|
|
|
|
Ok(match self.token_handler.peek_kind() {
|
|
|
|
Underscore => {
|
|
|
|
self.token_handler.next();
|
|
|
|
InvocationArgument::Ignored
|
|
|
|
},
|
|
|
|
Identifier(s) => {
|
2019-06-14 02:28:14 -07:00
|
|
|
match self.token_handler.peek_kind_n(1) {
|
2019-06-16 16:07:27 -07:00
|
|
|
Equals => {
|
2019-06-14 02:28:14 -07:00
|
|
|
self.token_handler.next();
|
2019-06-12 03:28:46 -07:00
|
|
|
self.token_handler.next();
|
2019-09-02 14:13:53 -07:00
|
|
|
let expr = self.expression()?.into();
|
2019-06-12 03:28:46 -07:00
|
|
|
InvocationArgument::Keyword { name: s.clone(), expr }
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
let expr = self.expression()?;
|
2019-09-02 14:13:53 -07:00
|
|
|
InvocationArgument::Positional(expr.into())
|
2019-06-12 03:28:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-09-02 14:13:53 -07:00
|
|
|
_ => InvocationArgument::Positional(self.expression()?.into())
|
2019-06-12 03:28:46 -07:00
|
|
|
})
|
2019-06-11 17:20:20 -07: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()?;
|
2019-01-05 18:38:18 -08:00
|
|
|
Ok(if let LSquareBracket = self.token_handler.peek_kind() {
|
2019-02-21 19:07:07 -08:00
|
|
|
let indexers = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket)
|
|
|
|
.into_iter().map(|ex| ex.into()).collect();
|
2019-09-18 14:15:05 -07:00
|
|
|
Expression::new(self.id_store.fresh(), ExpressionKind::Index {
|
|
|
|
indexee: bx!(Expression::new(self.id_store.fresh(), primary.kind).into()),
|
2018-03-06 02:51:45 -08:00
|
|
|
indexers,
|
2019-07-10 18:52:25 -07:00
|
|
|
})
|
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> {
|
2019-01-05 18:38:18 -08:00
|
|
|
match self.token_handler.peek_kind() {
|
2018-03-04 02:11:22 -08:00
|
|
|
LCurlyBrace => self.curly_brace_expr(),
|
2018-11-05 18:50:45 -08:00
|
|
|
Backslash => self.lambda_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> {
|
2019-02-21 19:07:07 -08:00
|
|
|
let exprs = delimited!(self, LSquareBracket, expression, Comma, RSquareBracket)
|
|
|
|
.into_iter().map(|ex| ex.into()).collect();
|
2019-09-18 14:15:05 -07:00
|
|
|
Ok(Expression::new(self.id_store.fresh(), ExpressionKind::ListLiteral(exprs)))
|
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> {
|
2019-01-08 01:00:40 -08:00
|
|
|
ParseError::new_with_token("Not implemented", self.token_handler.peek())
|
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-11-05 18:50:45 -08:00
|
|
|
expect!(self, Backslash);
|
2018-11-06 02:58:57 -08:00
|
|
|
let params = self.lambda_param_list()?;
|
2019-01-05 18:38:18 -08:00
|
|
|
let type_anno = match self.token_handler.peek_kind() {
|
2018-11-05 19:10:34 -08:00
|
|
|
Colon => Some(self.type_anno()?),
|
|
|
|
_ => None,
|
|
|
|
};
|
2018-11-05 18:50:45 -08:00
|
|
|
let body = self.nonempty_func_body()?;
|
2019-09-18 14:15:05 -07:00
|
|
|
Ok(Expression::new(self.id_store.fresh(), ExpressionKind::Lambda { params, type_anno, body })) //TODO need to handle types somehow
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2018-03-04 02:11:22 -08:00
|
|
|
|
2018-11-06 02:58:57 -08:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn lambda_param_list(&mut self) -> ParseResult<Vec<FormalParam>> {
|
2019-01-05 18:38:18 -08:00
|
|
|
if let LParen = self.token_handler.peek_kind() {
|
2018-11-06 02:58:57 -08:00
|
|
|
self.formal_param_list()
|
|
|
|
} else {
|
|
|
|
let single_param = self.formal_param()?;
|
|
|
|
Ok(vec![single_param])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn paren_expr(&mut self) -> ParseResult<Expression> {
|
2019-02-21 01:26:51 -08:00
|
|
|
use self::ExpressionKind::*;
|
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() {
|
2019-09-18 14:15:05 -07:00
|
|
|
0 => Ok(Expression::new(self.id_store.fresh(), TupleLiteral(vec![]))),
|
2018-02-11 22:10:21 -08:00
|
|
|
1 => Ok(inner.pop().unwrap()),
|
2019-01-05 16:06:55 -08:00
|
|
|
_ => {
|
2019-02-21 01:49:15 -08:00
|
|
|
let inner: Vec<Meta<Expression>> = inner.into_iter().map(|ex| ex.into()).collect();
|
2019-09-18 14:15:05 -07:00
|
|
|
Ok(Expression::new(self.id_store.fresh(), TupleLiteral(inner)))
|
2019-01-05 16:06:55 -08:00
|
|
|
}
|
2018-02-11 22:10:21 -08:00
|
|
|
}
|
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> {
|
2019-02-21 01:26:51 -08:00
|
|
|
use self::ExpressionKind::*;
|
2019-08-31 23:39:01 -07:00
|
|
|
let qualified_identifier = self.qualified_identifier()?;
|
2019-01-05 18:38:18 -08:00
|
|
|
Ok(match self.token_handler.peek_kind() {
|
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()?;
|
2019-09-18 14:15:05 -07:00
|
|
|
Expression::new(self.id_store.fresh(), NamedStruct { name: Meta::new(qualified_identifier), fields })
|
2017-10-13 18:56:02 -07:00
|
|
|
},
|
2019-09-18 14:15:05 -07:00
|
|
|
_ => Expression::new(self.id_store.fresh(), Value(Meta::new(qualified_identifier)))
|
2017-10-08 22:52:05 -07:00
|
|
|
})
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2017-09-13 20:10:06 -07:00
|
|
|
|
2019-08-31 23:39:01 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn qualified_identifier(&mut self) -> ParseResult<QualifiedName> {
|
2019-09-19 01:34:21 -07:00
|
|
|
let mut components = vec![self.identifier()?];
|
2019-08-31 23:39:01 -07:00
|
|
|
loop {
|
|
|
|
match (self.token_handler.peek_kind(), self.token_handler.peek_kind_n(1)) {
|
|
|
|
(Colon, Colon) => {
|
|
|
|
self.token_handler.next(); self.token_handler.next();
|
2019-09-19 01:34:21 -07:00
|
|
|
components.push(self.identifier()?);
|
2019-08-31 23:39:01 -07:00
|
|
|
},
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
2019-09-19 01:34:21 -07:00
|
|
|
Ok(QualifiedName { id: self.id_store.fresh(), components })
|
2019-08-31 23:39:01 -07:00
|
|
|
}
|
|
|
|
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
2019-02-21 19:07:07 -08:00
|
|
|
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()
|
|
|
|
)
|
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
|
|
|
});
|
|
|
|
|
2019-01-05 18:38:18 -08:00
|
|
|
let body = Box::new(match self.token_handler.peek_kind() {
|
2018-06-20 02:07:11 -07:00
|
|
|
Keyword(Kw::Then) => self.conditional()?,
|
|
|
|
Keyword(Kw::Is) => self.simple_pattern_match()? ,
|
|
|
|
_ => self.guard_block()?
|
2018-06-19 02:05:25 -07:00
|
|
|
});
|
|
|
|
|
2019-09-18 14:15:05 -07:00
|
|
|
Ok(Expression::new(self.id_store.fresh(), ExpressionKind::IfExpression { discriminator, body }))
|
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()?;
|
2019-01-05 18:38:18 -08:00
|
|
|
let ref next = self.token_handler.peek_kind();
|
2018-08-19 21:25:07 -07:00
|
|
|
Ok(if let Some(op) = BinOp::from_sigil_token(next) {
|
2019-09-02 14:13:53 -07:00
|
|
|
Discriminator::BinOp(lhs.into(), op)
|
2018-08-19 21:25:07 -07:00
|
|
|
} else {
|
2019-09-02 14:13:53 -07:00
|
|
|
Discriminator::Simple(lhs.into())
|
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>> {
|
2019-01-05 18:38:18 -08:00
|
|
|
Ok(if let Keyword(Kw::Else) = self.token_handler.peek_kind() {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.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 {
|
2019-01-05 18:38:18 -08:00
|
|
|
match self.token_handler.peek_kind() {
|
2018-11-05 13:07:08 -08:00
|
|
|
RCurlyBrace | EOF => break,
|
2019-01-05 18:29:24 -08:00
|
|
|
Semicolon | Newline => { self.token_handler.next(); continue},
|
2018-11-05 13:07:08 -08:00
|
|
|
_ => {
|
|
|
|
let guard_arm = self.guard_arm()?;
|
|
|
|
guards.push(guard_arm);
|
|
|
|
loop {
|
2019-01-05 18:38:18 -08:00
|
|
|
match self.token_handler.peek_kind() {
|
2019-01-05 18:29:24 -08:00
|
|
|
Semicolon | Newline => { self.token_handler.next(); continue; },
|
2018-11-05 13:07:08 -08:00
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
2019-01-05 18:38:18 -08:00
|
|
|
if let RCurlyBrace = self.token_handler.peek_kind() {
|
2018-11-05 13:07:08 -08:00
|
|
|
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> {
|
2019-01-05 18:38:18 -08:00
|
|
|
Ok(match self.token_handler.peek_kind() {
|
2018-07-13 22:12:30 -07:00
|
|
|
Keyword(Kw::Is) => {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2018-07-13 22:12:30 -07:00
|
|
|
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() => {
|
2019-01-05 18:29:24 -08:00
|
|
|
let op = BinOp::from_sigil_token(&self.token_handler.next().kind).unwrap();
|
2018-10-19 11:02:10 -07:00
|
|
|
let precedence = op.get_precedence();
|
2019-07-10 18:52:25 -07:00
|
|
|
let Expression { kind, .. } = self.precedence_expr(precedence)?;
|
|
|
|
Guard::HalfExpr(HalfExpr { op: Some(op), expr: kind })
|
2018-08-22 16:41:31 -07:00
|
|
|
},
|
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 ->
|
2019-07-10 18:52:25 -07:00
|
|
|
let Expression { kind, .. } = self.prefix_expr()?;
|
|
|
|
Guard::HalfExpr(HalfExpr { op: None, expr: kind })
|
2018-08-22 16:41:31 -07:00
|
|
|
}
|
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> {
|
2019-01-05 18:38:18 -08:00
|
|
|
if let LParen = self.token_handler.peek_kind() {
|
2018-07-01 01:26:19 -07:00
|
|
|
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
|
|
|
|
2019-09-03 21:14:12 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn simple_pattern(&mut self) -> ParseResult<Pattern> {
|
2019-09-06 02:23:04 -07:00
|
|
|
Ok(match self.token_handler.peek_kind() {
|
2019-09-03 21:14:12 -07:00
|
|
|
Identifier(_) => {
|
2019-09-06 02:23:04 -07:00
|
|
|
let qualified_name = self.qualified_identifier()?;
|
2019-09-03 21:14:12 -07:00
|
|
|
match self.token_handler.peek_kind() {
|
|
|
|
LCurlyBrace => {
|
2019-09-04 10:53:52 -07:00
|
|
|
let members = delimited!(self, LCurlyBrace, record_pattern_entry, Comma, RCurlyBrace);
|
2019-09-07 19:08:50 -07:00
|
|
|
Pattern::Record(Meta::new(qualified_name), members)
|
2019-09-03 21:14:12 -07:00
|
|
|
},
|
|
|
|
LParen => {
|
2019-09-04 10:53:52 -07:00
|
|
|
let members = delimited!(self, LParen, pattern, Comma, RParen);
|
2019-09-07 19:08:50 -07:00
|
|
|
Pattern::TupleStruct(Meta::new(qualified_name), members)
|
2019-09-03 21:14:12 -07:00
|
|
|
},
|
|
|
|
_ => {
|
2019-09-07 19:08:50 -07:00
|
|
|
Pattern::VarOrName(Meta::new(qualified_name))
|
2019-09-03 21:14:12 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2019-09-06 02:23:04 -07:00
|
|
|
_ => self.pattern_literal()?
|
|
|
|
})
|
2019-09-03 21:14:12 -07:00
|
|
|
}
|
|
|
|
|
2019-09-04 10:53:52 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn pattern_literal(&mut self) -> ParseResult<Pattern> {
|
2019-09-06 02:23:04 -07:00
|
|
|
let tok = self.token_handler.peek();
|
2019-09-06 02:30:18 -07:00
|
|
|
Ok(match tok.get_kind() {
|
2019-09-04 10:53:52 -07:00
|
|
|
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)
|
2019-09-06 02:23:04 -07:00
|
|
|
})
|
2019-09-04 10:53:52 -07:00
|
|
|
}
|
|
|
|
|
2019-09-03 21:14:12 -07:00
|
|
|
/*
|
2018-10-19 02:56:11 -07:00
|
|
|
#[recursive_descent_method]
|
|
|
|
fn simple_pattern(&mut self) -> ParseResult<Pattern> {
|
2019-01-05 17:32:49 -08:00
|
|
|
Ok({
|
2019-01-05 18:38:18 -08:00
|
|
|
let tok = self.token_handler.peek();
|
2019-01-05 17:32:49 -08:00
|
|
|
match tok.get_kind() {
|
|
|
|
Identifier(_) => {
|
|
|
|
let id = self.identifier()?;
|
2019-01-05 18:38:18 -08:00
|
|
|
match self.token_handler.peek_kind() {
|
2019-01-05 17:32:49 -08:00
|
|
|
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) => {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2019-01-05 17:32:49 -08:00
|
|
|
Pattern::Literal(PatternLiteral::BoolPattern(true))
|
|
|
|
},
|
|
|
|
Keyword(Kw::False) => {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2019-01-05 17:32:49 -08:00
|
|
|
Pattern::Literal(PatternLiteral::BoolPattern(false))
|
|
|
|
},
|
|
|
|
StrLiteral(s) => {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2019-01-05 17:32:49 -08:00
|
|
|
Pattern::Literal(PatternLiteral::StringPattern(s))
|
|
|
|
},
|
|
|
|
DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.signed_number_literal()?,
|
|
|
|
Operator(ref op) if **op == "-" => self.signed_number_literal()?,
|
|
|
|
Underscore => {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2019-01-05 17:32:49 -08:00
|
|
|
Pattern::Ignored
|
|
|
|
},
|
2019-02-20 02:05:24 -08:00
|
|
|
other => return ParseError::new_with_token(format!("{:?} is not a valid Pattern", other), tok)
|
2019-01-05 17:32:49 -08:00
|
|
|
}
|
2018-06-24 02:15:08 -07:00
|
|
|
})
|
2018-10-19 02:56:11 -07:00
|
|
|
}
|
2019-09-03 21:14:12 -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> {
|
2019-01-05 18:38:18 -08:00
|
|
|
let neg = match self.token_handler.peek_kind() {
|
2018-08-21 19:57:45 -07:00
|
|
|
Operator(ref op) if **op == "-" => {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2018-08-21 19:57:45 -07:00
|
|
|
true
|
|
|
|
},
|
|
|
|
_ => false
|
|
|
|
};
|
2019-07-10 18:52:25 -07:00
|
|
|
let Expression { kind, .. } = self.number_literal()?;
|
|
|
|
Ok(Pattern::Literal(PatternLiteral::NumPattern { neg, num: kind }))
|
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()?;
|
2019-01-05 18:38:18 -08:00
|
|
|
Ok(match self.token_handler.peek_kind() {
|
2018-07-13 21:21:27 -07:00
|
|
|
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> {
|
2019-01-04 22:58:25 -08:00
|
|
|
let block = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict);
|
2019-02-21 01:49:15 -08:00
|
|
|
Ok(block.into_iter().map(|s| { Meta::new(s) }).collect())
|
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> {
|
2019-01-05 18:38:18 -08:00
|
|
|
match self.token_handler.peek_kind() {
|
2018-08-19 15:58:31 -07:00
|
|
|
LCurlyBrace => self.block(),
|
|
|
|
_ => {
|
|
|
|
let expr = self.expression()?;
|
2019-09-18 10:07:20 -07:00
|
|
|
let s = Statement { id: self.id_store.fresh(), kind: StatementKind::Expression(expr.into()) };
|
2019-09-17 02:25:11 -07:00
|
|
|
Ok(vec![Meta::new(s)])
|
2018-08-19 15:58:31 -07:00
|
|
|
}
|
|
|
|
}
|
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> {
|
2019-02-21 01:26:51 -08:00
|
|
|
use self::ExpressionKind::*;
|
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;
|
2019-02-21 19:07:07 -08:00
|
|
|
x?.map(|expr| bx!(expr.into()))
|
2018-05-12 23:49:02 -07:00
|
|
|
};
|
|
|
|
let body = self.block()?;
|
2019-09-18 14:15:05 -07:00
|
|
|
Ok(Expression::new(self.id_store.fresh(), WhileExpression {condition, body}))
|
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>> {
|
2019-01-05 18:38:18 -08:00
|
|
|
Ok(match self.token_handler.peek_kind() {
|
2018-05-12 23:49:02 -07:00
|
|
|
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));
|
2019-01-05 18:38:18 -08:00
|
|
|
let enumerators = if let LCurlyBrace = self.token_handler.peek_kind() {
|
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()?);
|
2019-09-18 14:15:05 -07:00
|
|
|
Ok(Expression::new(self.id_store.fresh(), ExpressionKind::ForExpression { enumerators, body }))
|
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 == "<-");
|
2019-02-21 19:07:07 -08:00
|
|
|
let generator = self.expression()?.into();
|
2018-05-13 01:40:14 -07:00
|
|
|
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::*;
|
2019-01-05 18:38:18 -08:00
|
|
|
let tok = self.token_handler.peek();
|
2019-01-05 17:40:05 -08:00
|
|
|
Ok(match tok.get_kind() {
|
2018-05-13 01:40:14 -07:00
|
|
|
LCurlyBrace => {
|
2018-05-13 02:50:43 -07:00
|
|
|
let statements = delimited!(self, LCurlyBrace, statement, Newline | Semicolon, RCurlyBrace, nonstrict);
|
2019-02-21 01:49:15 -08:00
|
|
|
StatementBlock(statements.into_iter().map(|s| Meta::new(s)).collect())
|
2018-05-13 01:40:14 -07:00
|
|
|
},
|
|
|
|
Keyword(Kw::Return) => {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2019-02-21 19:07:07 -08:00
|
|
|
MonadicReturn(self.expression()?.into())
|
2018-05-13 01:40:14 -07:00
|
|
|
},
|
2019-01-05 17:40:05 -08:00
|
|
|
_ => return ParseError::new_with_token("for expressions must end in a block or 'return'", tok),
|
2018-05-13 01:40:14 -07:00
|
|
|
})
|
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>> {
|
2019-01-05 18:29:24 -08:00
|
|
|
let tok = self.token_handler.next();
|
2019-01-05 17:50:54 -08:00
|
|
|
match tok.get_kind() {
|
2017-09-11 15:42:49 -07:00
|
|
|
Identifier(s) => Ok(s),
|
2019-02-20 02:05:24 -08:00
|
|
|
p => ParseError::new_with_token(format!("Expected an identifier, got {:?}", p), tok),
|
2017-09-11 15:42:49 -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 literal(&mut self) -> ParseResult<Expression> {
|
2019-02-21 01:26:51 -08:00
|
|
|
use self::ExpressionKind::*;
|
2019-01-05 17:18:10 -08:00
|
|
|
|
2019-01-05 18:38:18 -08:00
|
|
|
let tok = self.token_handler.peek();
|
2019-01-05 17:28:35 -08:00
|
|
|
match tok.get_kind() {
|
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) => {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2019-09-18 14:15:05 -07:00
|
|
|
let id = self.id_store.fresh();
|
|
|
|
Ok(Expression::new(id, BoolLiteral(true)))
|
2017-09-27 22:27:50 -07:00
|
|
|
},
|
|
|
|
Keyword(Kw::False) => {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2019-09-18 14:15:05 -07:00
|
|
|
let id = self.id_store.fresh();
|
|
|
|
Ok(Expression::new(id, BoolLiteral(false)))
|
2017-09-27 22:27:50 -07:00
|
|
|
},
|
2017-09-16 15:57:48 -07:00
|
|
|
StrLiteral(s) => {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2019-09-18 14:15:05 -07:00
|
|
|
let id = self.id_store.fresh();
|
|
|
|
Ok(Expression::new(id, StringLiteral(s.clone())))
|
2017-09-16 15:57:48 -07:00
|
|
|
}
|
2019-02-20 02:05:24 -08:00
|
|
|
e => ParseError::new_with_token(format!("Expected a literal expression, got {:?}", e), tok),
|
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> {
|
2019-01-05 18:38:18 -08:00
|
|
|
match self.token_handler.peek_kind() {
|
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> {
|
2019-02-21 01:26:51 -08:00
|
|
|
use self::ExpressionKind::*;
|
2019-01-05 18:29:24 -08:00
|
|
|
let tok = self.token_handler.next();
|
2019-01-05 17:50:54 -08:00
|
|
|
match tok.get_kind() {
|
2018-11-17 01:10:23 -08:00
|
|
|
BinNumberSigil => {
|
|
|
|
let digits = self.digits()?;
|
2019-01-08 01:00:40 -08:00
|
|
|
let n = parse_binary(digits, tok)?;
|
2019-09-18 14:15:05 -07:00
|
|
|
Ok(Expression::new(self.id_store.fresh(), NatLiteral(n)))
|
2018-11-17 01:10:23 -08:00
|
|
|
},
|
|
|
|
HexLiteral(text) => {
|
|
|
|
let digits: String = text.chars().filter(|c| c.is_digit(16)).collect();
|
2019-01-08 01:00:40 -08:00
|
|
|
let n = parse_hex(digits, tok)?;
|
2019-09-18 14:15:05 -07:00
|
|
|
Ok(Expression::new(self.id_store.fresh(), NatLiteral(n)))
|
2018-11-17 01:10:23 -08:00
|
|
|
},
|
2019-01-05 17:50:54 -08:00
|
|
|
_ => return ParseError::new_with_token("Expected '0x' or '0b'", tok),
|
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 float_literal(&mut self) -> ParseResult<Expression> {
|
2019-02-21 01:26:51 -08:00
|
|
|
use self::ExpressionKind::*;
|
2019-01-08 01:00:40 -08:00
|
|
|
let tok = self.token_handler.peek();
|
2017-09-11 02:38:27 -07:00
|
|
|
let mut digits = self.digits()?;
|
2019-01-05 18:38:18 -08:00
|
|
|
if let Period = self.token_handler.peek_kind() {
|
2019-01-05 18:29:24 -08:00
|
|
|
self.token_handler.next();
|
2017-09-11 02:38:27 -07:00
|
|
|
digits.push_str(".");
|
|
|
|
digits.push_str(&self.digits()?);
|
|
|
|
match digits.parse::<f64>() {
|
2019-09-18 14:15:05 -07:00
|
|
|
Ok(f) => Ok(Expression::new(self.id_store.fresh(), FloatLiteral(f))),
|
2019-02-20 02:05:24 -08:00
|
|
|
Err(e) => ParseError::new_with_token(format!("Float failed to parse with error: {}", e), tok),
|
2017-09-11 02:38:27 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match digits.parse::<u64>() {
|
2019-09-18 14:15:05 -07:00
|
|
|
Ok(d) => Ok(Expression::new(self.id_store.fresh(), NatLiteral(d))),
|
2019-02-20 02:05:24 -08:00
|
|
|
Err(e) => ParseError::new_with_token(format!("Integer failed to parse with error: {}", e), tok),
|
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 {
|
2019-01-05 18:38:18 -08:00
|
|
|
match self.token_handler.peek_kind() {
|
2019-01-05 18:29:24 -08:00
|
|
|
Underscore => { self.token_handler.next(); continue; },
|
|
|
|
DigitGroup(ref s) => { self.token_handler.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
|
|
|
|
2019-01-08 01:00:40 -08:00
|
|
|
fn parse_binary(digits: String, tok: Token) -> ParseResult<u64> {
|
2017-09-11 23:27:15 -07:00
|
|
|
let mut result: u64 = 0;
|
|
|
|
let mut multiplier = 1;
|
|
|
|
for d in digits.chars().rev() {
|
|
|
|
match d {
|
|
|
|
'1' => result += multiplier,
|
|
|
|
'0' => (),
|
2019-01-08 01:00:40 -08:00
|
|
|
_ => return ParseError::new_with_token("Encountered a character not '1' or '0 while parsing a binary literal", tok),
|
2017-09-11 23:27:15 -07:00
|
|
|
}
|
2018-02-21 03:52:16 -08:00
|
|
|
multiplier = match multiplier.checked_mul(2) {
|
|
|
|
Some(m) => m,
|
2019-01-08 01:00:40 -08:00
|
|
|
None => return ParseError::new_with_token("This binary expression will overflow", tok)
|
2018-02-21 03:52:16 -08:00
|
|
|
}
|
2017-09-11 23:27:15 -07:00
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2019-01-08 01:00:40 -08:00
|
|
|
fn parse_hex(digits: String, tok: Token) -> ParseResult<u64> {
|
2018-01-08 05:57:36 -08:00
|
|
|
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,
|
2019-01-08 01:00:40 -08:00
|
|
|
None => return ParseError::new_with_token("Encountered a non-hex digit in a hex literal", tok),
|
2018-01-08 05:57:36 -08:00
|
|
|
}
|
2018-02-21 03:52:16 -08:00
|
|
|
multiplier = match multiplier.checked_mul(16) {
|
|
|
|
Some(m) => m,
|
2019-01-08 01:00:40 -08:00
|
|
|
None => return ParseError::new_with_token("This hex expression will overflow", tok)
|
2018-02-21 03:52:16 -08:00
|
|
|
}
|
2018-01-08 05:57:36 -08:00
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|