v2: raw strings

pull/4110/head
Alexander Medvednikov 2020-03-24 17:07:27 +01:00
parent 4d3362358f
commit 5d976d841b
4 changed files with 16 additions and 3 deletions

View File

@ -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

View File

@ -57,6 +57,8 @@ pub:
pub struct StringLiteral {
pub:
val string
is_raw bool
is_c bool
}
// 'name: $name'

View File

@ -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

View File

@ -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()