Parse string literals

This commit is contained in:
greg 2017-09-16 15:57:48 -07:00
parent fbeb101e7f
commit d5c3227966

View File

@ -304,7 +304,7 @@ primary := literal | paren_expr | identifier_expr
paren_expr := LParen expression RParen paren_expr := LParen expression RParen
identifier_expr := call_expr | index_expr | IDENTIFIER 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 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)* | ε) «]» index_expr := «[» (expression («,» (expression)* | ε) «]»
@ -407,6 +407,7 @@ pub enum Variant {
pub enum Expression { pub enum Expression {
IntLiteral(u64), IntLiteral(u64),
FloatLiteral(f64), FloatLiteral(f64),
StringLiteral(Rc<String>),
BinExp(Operation, Box<Expression>, Box<Expression>), BinExp(Operation, Box<Expression>, Box<Expression>),
Variable(Rc<String>), Variable(Rc<String>),
Call { Call {
@ -624,7 +625,11 @@ impl Parser {
parse_method!(literal(&mut self) -> ParseResult<Expression> { parse_method!(literal(&mut self) -> ParseResult<Expression> {
match self.peek() { match self.peek() {
DigitGroup(_) | HexNumberSigil | BinNumberSigil | Period => self.number_literal(), 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 { 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 { 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] #[test]
fn parsing_types() { fn parsing_types() {
parse_test!("type Yolo = Yolo", AST(vec![Declaration(TypeDecl(rc!(Yolo), TypeBody(vec![Variant::Singleton(rc!(Yolo))])))])); parse_test!("type Yolo = Yolo", AST(vec![Declaration(TypeDecl(rc!(Yolo), TypeBody(vec![Variant::Singleton(rc!(Yolo))])))]));