From a00125d4a5224c3a3ab706b9287549d02710d539 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sat, 13 Nov 2021 13:45:52 -0800 Subject: [PATCH] Make string literals work properly --- schala-lang/language/src/ast/mod.rs | 1 + schala-lang/language/src/parsing/new.rs | 13 ++++++++++--- schala-lang/language/src/parsing/test.rs | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/schala-lang/language/src/ast/mod.rs b/schala-lang/language/src/ast/mod.rs index 90dcb54..2d9495e 100644 --- a/schala-lang/language/src/ast/mod.rs +++ b/schala-lang/language/src/ast/mod.rs @@ -197,6 +197,7 @@ pub struct TypeSingletonName { pub enum ExpressionKind { NatLiteral(u64), FloatLiteral(f64), + //TODO StringLiteral variant needs to support prefixes StringLiteral(Rc), BoolLiteral(bool), BinExp(BinOp, Box, Box), diff --git a/schala-lang/language/src/parsing/new.rs b/schala-lang/language/src/parsing/new.rs index 134ef15..8f516ae 100644 --- a/schala-lang/language/src/parsing/new.rs +++ b/schala-lang/language/src/parsing/new.rs @@ -435,12 +435,19 @@ peg::parser! { } } + //TODO need to do something with prefix in the AST rule string_literal() -> ExpressionKind = - s:bare_string_literal(){ ExpressionKind::StringLiteral(Rc::new(s.to_string())) } + prefix:identifier()? s:bare_string_literal(){ ExpressionKind::StringLiteral(Rc::new(s.to_string())) } - //TODO string escapes, prefixes rule bare_string_literal() -> &'input str = - "\"" items:$([^ '"' ]*) "\"" { items } + "\"" s:$(string_component()*) "\"" { s } + + rule string_component() -> &'input str = + r#"\\"# { "\\" } / + r#"\""# { "\"" } / + r#"\t"# { "\t" } / + r#"\n"# { "\n" } / + ch:$([^ '"' ]) { ch } rule bool_literal() -> ExpressionKind = "true" { ExpressionKind::BoolLiteral(true) } / "false" { ExpressionKind::BoolLiteral(false) } diff --git a/schala-lang/language/src/parsing/test.rs b/schala-lang/language/src/parsing/test.rs index fab7538..e47d6ae 100644 --- a/schala-lang/language/src/parsing/test.rs +++ b/schala-lang/language/src/parsing/test.rs @@ -135,7 +135,17 @@ fn basic_literals() { assert_expr!("0xf_f_", expr(NatLiteral(255))); assert_expr!("false", expr(BoolLiteral(false))); assert_expr!("true", expr(BoolLiteral(true))); +} + +#[test] +fn string_literals() { + use ExpressionKind::*; + + assert_expr!(r#""""#, expr(StringLiteral(rc("")))); assert_expr!(r#""hello""#, expr(StringLiteral(rc("hello")))); + assert_expr!(r#"b"some bytestring""#, expr(StringLiteral(rc("some bytestring")))); + //NOTE I'm not 100% sure this case is correct, but I'll deal with it later + assert_expr!(r#""Do \n \" escapes work\t""#, expr(StringLiteral(rc(r#"Do \n \" escapes work\t"#)))); } #[test] @@ -1309,3 +1319,12 @@ fn comments() { let source = "5//no man\n"; assert_ast!(source, vec![exst(NatLiteral(5))]); } + +//TODO support backtick operators like this +/* +#[test] +fn backtick_operators() { + let output = token_kinds("1 `plus` 2"); + assert_eq!(output, vec![digit!("1"), op!("plus"), digit!("2")]); +} +*/