From 5d976d841b49a3bee018649b295b44e574b69b3c Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 24 Mar 2020 17:07:27 +0100 Subject: [PATCH] v2: raw strings --- vlib/builtin/array_test.v | 1 - vlib/v/ast/ast.v | 4 +++- vlib/v/checker/checker.v | 2 +- vlib/v/parser/parser.v | 12 ++++++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index 685cfddd1d..58fbf65a56 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -267,7 +267,6 @@ fn test_reverse() { for i, _ in d { assert d[i] == b[b.len - i - 1] } - e := []int f := e.reverse() assert f.len == 0 diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 0d78a94270..63f68fd709 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -56,7 +56,9 @@ pub: pub struct StringLiteral { pub: - val string + val string + is_raw bool + is_c bool } // 'name: $name' diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2618a80818..08133d32c3 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -602,7 +602,7 @@ fn (c mut Checker) stmt(node ast.Stmt) { value_type := c.table.value_type(typ) if value_type == table.void_type { typ_sym := c.table.get_type_symbol(typ) - c.error('for in: cannot index $typ_sym.name', it.pos) + c.error('for in: cannot index `$typ_sym.name`', it.pos) } it.cond_type = typ it.kind = sym.kind diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index c84990cfd7..c4e33901cf 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -594,6 +594,11 @@ pub fn (p mut Parser) name_expr() ast.Expr { typ: map_type } } + // Raw string (`s := r'hello \n ') + if p.tok.lit == 'r' && p.peek_tok.kind == .string { + // && p.prev_tok.kind != .str_dollar { + return p.string_expr() + } known_var := p.scope.known_var(p.tok.lit) if p.peek_tok.kind == .dot && !known_var && (is_c || p.known_import(p.tok.lit) || p.mod.all_after('.') == p.tok.lit) { if is_c { @@ -1248,10 +1253,17 @@ fn (p mut Parser) if_expr() ast.IfExpr { } fn (p mut Parser) string_expr() ast.Expr { + is_raw := p.tok.kind == .name && p.tok.lit == 'r' + is_cstr := p.tok.kind == .name && p.tok.lit == 'c' + if is_raw || is_cstr { + p.next() + } mut node := ast.Expr{} val := p.tok.lit node = ast.StringLiteral{ val: val + is_raw: is_raw + is_c: is_cstr } if p.peek_tok.kind != .str_dollar { p.next()