cgen: escape quotes & nl in string literals
parent
bb5034f3fe
commit
99398ba652
|
@ -24,7 +24,7 @@ struct Parser {
|
||||||
pref &pref.Preferences
|
pref &pref.Preferences
|
||||||
mut:
|
mut:
|
||||||
scanner &Scanner
|
scanner &Scanner
|
||||||
// Preferences shared from V struct
|
// Preferences shared from V struct
|
||||||
tokens []Token
|
tokens []Token
|
||||||
token_idx int
|
token_idx int
|
||||||
prev_stuck_token_idx int
|
prev_stuck_token_idx int
|
||||||
|
@ -180,7 +180,7 @@ fn (v mut V) new_parser_from_file(path string) Parser {
|
||||||
p = {
|
p = {
|
||||||
p |
|
p |
|
||||||
file_path:path,
|
file_path:path,
|
||||||
file_path_dir: path_dir,
|
file_path_dir:path_dir,
|
||||||
file_name:path.all_after(os.path_separator),
|
file_name:path.all_after(os.path_separator),
|
||||||
file_platform:path_platform,
|
file_platform:path_platform,
|
||||||
file_pcguard:path_pcguard,
|
file_pcguard:path_pcguard,
|
||||||
|
@ -872,11 +872,7 @@ fn (p mut Parser) type_decl() {
|
||||||
}
|
}
|
||||||
p.cgen.consts << '#define SumType_${name}_$child_type_name $idx // DEF2'
|
p.cgen.consts << '#define SumType_${name}_$child_type_name $idx // DEF2'
|
||||||
ctype_names << child_type_name
|
ctype_names << child_type_name
|
||||||
sum_variants << if p.mod in ['builtin', 'main'] || child_type_name in builtin_types {
|
sum_variants << if p.mod in ['builtin', 'main'] || child_type_name in builtin_types { child_type_name } else { p.prepend_mod(child_type_name) }
|
||||||
child_type_name
|
|
||||||
} else {
|
|
||||||
p.prepend_mod(child_type_name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if done {
|
if done {
|
||||||
break
|
break
|
||||||
|
@ -2598,10 +2594,9 @@ fn (p mut Parser) char_expr() {
|
||||||
|
|
||||||
fn format_str(_str string) string {
|
fn format_str(_str string) string {
|
||||||
// TODO don't call replace 3 times for every string, do this in scanner.v
|
// TODO don't call replace 3 times for every string, do this in scanner.v
|
||||||
mut str := _str.replace('"', '\\"')
|
return _str.replace_each(['"', '\\"',
|
||||||
str = str.replace('\r\n', '\\n')
|
'\n', '\\n',
|
||||||
str = str.replace('\n', '\\n')
|
'\r\n', '\\n'])
|
||||||
return str
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// m := map[string]int{}
|
// m := map[string]int{}
|
||||||
|
|
|
@ -809,11 +809,14 @@ fn (g mut Gen) expr(node ast.Expr) {
|
||||||
ast.StringLiteral {
|
ast.StringLiteral {
|
||||||
// In C calls we have to generate C strings
|
// In C calls we have to generate C strings
|
||||||
// `C.printf("hi")` => `printf("hi");`
|
// `C.printf("hi")` => `printf("hi");`
|
||||||
|
escaped_val := it.val.replace_each(['"', '\\"',
|
||||||
|
'\n', '\\n',
|
||||||
|
'\r\n', '\\n'])
|
||||||
if g.is_c_call {
|
if g.is_c_call {
|
||||||
g.write('"$it.val"')
|
g.write('"$escaped_val"')
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
g.write('tos3("$it.val")')
|
g.write('tos3("$escaped_val")')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// `user := User{name: 'Bob'}`
|
// `user := User{name: 'Bob'}`
|
||||||
|
|
Loading…
Reference in New Issue