From 4c81c36d67ef9fa8aa83de5356ee2555caea68d1 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 26 Sep 2017 22:10:13 -0700 Subject: [PATCH] Parse type annotations Not using them yet --- src/schala_lang/parsing.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index 406ba0b..4ea2a95 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -291,8 +291,9 @@ binding_declaration: 'var' IDENTIFIER '=' expression | 'const' IDENTIFIER '=' expression type_anno := ':' type +type := IDENTIFIER (LAngleBracket type RAngleBracket)* -expression := precedence_expr +expression := precedence_expr type_anno+ precedence_expr := prefix_expr prefix_expr := prefix_op primary @@ -565,7 +566,29 @@ impl Parser { }); parse_method!(expression(&mut self) -> ParseResult { - self.precedence_expr(Operation::min_precedence()) + let expr_body = self.precedence_expr(Operation::min_precedence()); + let type_anno = match self.peek() { + Colon => { + self.next(); + Some(self.type_anno()?) + }, + _ => None + }; + expr_body + }); + + parse_method!(type_anno(&mut self) -> ParseResult<()> { + let type_name = self.identifier()?; + let param = match self.peek() { + LAngleBracket => { + self.next(); + let param = self.type_anno()?; + expect!(self, RAngleBracket, "Expected '>'"); + Some(param) + }, + _ => None + }; + Ok(()) }); // this implements Pratt parsing, see http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/