From d5c3227966c2782b9408b4c8f5624b6b0ddf6dd8 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 16 Sep 2017 15:57:48 -0700 Subject: [PATCH] Parse string literals --- src/schala_lang/parsing.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index d2ca1e1..6b1f5eb 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -304,7 +304,7 @@ primary := literal | paren_expr | identifier_expr paren_expr := LParen expression RParen identifier_expr := call_expr | index_expr | IDENTIFIER -literal := «true» | «false» | number_literal | str_literal +literal := «true» | «false» | number_literal | STR_LITERAL call_expr := IDENTIFIER «(» expr_list «)» //TODO maybe make this optional? or no, have a bare identifier meant to be used as method taken care of in eval index_expr := «[» (expression («,» (expression)* | ε) «]» @@ -407,6 +407,7 @@ pub enum Variant { pub enum Expression { IntLiteral(u64), FloatLiteral(f64), + StringLiteral(Rc), BinExp(Operation, Box, Box), Variable(Rc), Call { @@ -624,7 +625,11 @@ impl Parser { parse_method!(literal(&mut self) -> ParseResult { match self.peek() { DigitGroup(_) | HexNumberSigil | BinNumberSigil | Period => self.number_literal(), - t => panic!("trying to parse {:?}", t), + StrLiteral(s) => { + self.next(); + Ok(Expression::StringLiteral(s)) + } + e => ParseError::new(&format!("Expected a literal expression, got {:?}", e)), } }); @@ -717,7 +722,7 @@ mod parse_tests { } macro_rules! parse_test { - ($string:expr, $correct:expr) => { assert_eq!(parse(tokenize($string)).unwrap(), $correct) } + ($string:expr, $correct:expr) => { assert_eq!(parse(tokenize($string)).0.unwrap(), $correct) } } macro_rules! binexp { @@ -786,6 +791,11 @@ mod parse_tests { })])); } + #[test] + fn parsing_strings() { + parse_test!(r#""hello""#, AST(vec![Expression(StringLiteral(rc!(hello)))])); + } + #[test] fn parsing_types() { parse_test!("type Yolo = Yolo", AST(vec![Declaration(TypeDecl(rc!(Yolo), TypeBody(vec![Variant::Singleton(rc!(Yolo))])))]));