fix a string interpolation bug

pull/2967/head
Alexander Medvednikov 2019-12-03 13:08:57 +03:00
parent 89d40566f4
commit 66f271f100
11 changed files with 61 additions and 40 deletions

View File

@ -555,6 +555,13 @@ fn test_c_r() {
println('$c') println('$c')
r := 50 r := 50
println('$r') println('$r')
}
fn test_inter_before_comp_if() {
s := '123'
// This used to break ('123 $....')
$if linux {
println(s)
}
} }

View File

@ -167,7 +167,7 @@ fn (p mut Parser) name_expr() string {
} }
// Raw string (`s := r'hello \n ') // Raw string (`s := r'hello \n ')
if (name == 'r' || name == 'c') && p.peek() == .str && p.prev_tok != .dollar { if (name == 'r' || name == 'c') && p.peek() == .str && p.prev_tok != .str_dollar {
p.string_expr() p.string_expr()
return 'string' return 'string'
} }

View File

@ -2265,7 +2265,7 @@ fn (p mut Parser) string_expr() {
} }
str := p.lit str := p.lit
// No ${}, just return a simple string // No ${}, just return a simple string
if p.peek() != .dollar || is_raw { if p.peek() != .str_dollar || is_raw {
f := if is_raw { cescaped_path(str) } else { format_str(str) } f := if is_raw { cescaped_path(str) } else { format_str(str) }
// `C.puts('hi')` => `puts("hi");` // `C.puts('hi')` => `puts("hi");`
/* /*
@ -2299,11 +2299,11 @@ fn (p mut Parser) string_expr() {
p.lit = p.lit.replace('%', '%%') p.lit = p.lit.replace('%', '%%')
format += format_str(p.lit) format += format_str(p.lit)
p.next()// skip $ p.next()// skip $
if p.tok != .dollar { if p.tok != .str_dollar {
continue continue
} }
// Handle .dollar // Handle .dollar
p.check(.dollar) p.check(.str_dollar)
// If there's no string after current token, it means we are in // If there's no string after current token, it means we are in
// a complex expression (`${...}`) // a complex expression (`${...}`)
if p.peek() != .str { if p.peek() != .str {

View File

@ -390,8 +390,13 @@ fn (s mut Scanner) scan() ScanRes {
return scan_res(.lcbr, '') return scan_res(.lcbr, '')
} }
`$` { `$` {
// if s.inter_start {
if s.inside_string {// || s.inter_start {
return scan_res(.str_dollar, '')
} else {
return scan_res(.dollar, '') return scan_res(.dollar, '')
} }
}
`}` { `}` {
// s = `hello $name !` // s = `hello $name !`
// s = `hello ${name} !` // s = `hello ${name} !`

View File

@ -318,7 +318,7 @@ fn (p mut Parser) struct_init(typ string) string {
} }
field_typ := field.typ field_typ := field.typ
if !p.builtin_mod && field_typ.ends_with('*') && p.mod != 'os' { //&& if !p.builtin_mod && field_typ.ends_with('*') && p.mod != 'os' { //&&
p.warn('pointer field `${typ}.${field.name}` must be initialized') p.warn('reference field `${typ}.${field.name}` must be initialized')
} }
// init map fields // init map fields
if field_typ.starts_with('map_') { if field_typ.starts_with('map_') {

View File

@ -41,6 +41,7 @@ enum TokenKind {
amp amp
hash hash
dollar dollar
str_dollar
left_shift left_shift
righ_shift righ_shift
//at // @ //at // @
@ -197,6 +198,7 @@ fn build_token_str() []string {
s[TokenKind.mline_comment] = '/* mline comment */' s[TokenKind.mline_comment] = '/* mline comment */'
s[TokenKind.nl] = 'NLL' s[TokenKind.nl] = 'NLL'
s[TokenKind.dollar] = '$' s[TokenKind.dollar] = '$'
s[TokenKind.str_dollar] = '$2'
s[TokenKind.key_assert] = 'assert' s[TokenKind.key_assert] = 'assert'
s[TokenKind.key_struct] = 'struct' s[TokenKind.key_struct] = 'struct'
s[TokenKind.key_if] = 'if' s[TokenKind.key_if] = 'if'

View File

@ -13,16 +13,20 @@ pub:
pub const ( pub const (
Blue = Color { r: 0, g: 0, b: 255 } Blue = Color { r: 0, g: 0, b: 255 }
blue = Color { r: 0, g: 0, b: 255 }
Red = Color { r: 255, g: 0, b: 0 } Red = Color { r: 255, g: 0, b: 0 }
Green = Color { r: 0, g: 255, b: 0 } Green = Color { r: 0, g: 255, b: 0 }
green = Color { r: 0, g: 255, b: 0 } green = Color { r: 0, g: 255, b: 0 }
Yellow = Color { r: 255, g: 255, b: 0 } Yellow = Color { r: 255, g: 255, b: 0 }
Orange = Color { r: 255, g: 165, b: 0 } Orange = Color { r: 255, g: 165, b: 0 }
orange = Color { r: 255, g: 165, b: 0 }
Purple = Color { r: 128, g: 0, b: 128 } Purple = Color { r: 128, g: 0, b: 128 }
Black = Color { r: 0, g: 0, b: 0 } Black = Color { r: 0, g: 0, b: 0 }
black = Color { r: 0, g: 0, b: 0 }
Gray = Color { r: 128, g: 128, b: 128 } Gray = Color { r: 128, g: 128, b: 128 }
gray = Color { r: 128, g: 128, b: 128 }
Indigo = Color { r: 75, g: 0, b: 130 } Indigo = Color { r: 75, g: 0, b: 130 }
Pink = Color { r: 255, g: 192, b: 203 } Pink = Color { r: 255, g: 192, b: 203 }
Violet = Color { r: 238, g: 130, b: 238 } Violet = Color { r: 238, g: 130, b: 238 }
@ -32,9 +36,11 @@ pub const (
// Shades // Shades
DarkBlue = Color { r: 0, g: 0, b: 139 } DarkBlue = Color { r: 0, g: 0, b: 139 }
DarkGray = Color { r: 169, g: 169, b: 169 } DarkGray = Color { r: 169, g: 169, b: 169 }
dark_gray = Color { r: 169, g: 169, b: 169 }
DarkGreen = Color { r: 0, g: 100, b: 0 } DarkGreen = Color { r: 0, g: 100, b: 0 }
DarkRed = Color { r: 139, g: 0, b: 0 } DarkRed = Color { r: 139, g: 0, b: 0 }
LightBlue = Color { r: 173, g: 216, b: 230 } LightBlue = Color { r: 173, g: 216, b: 230 }
light_blue = Color { r: 173, g: 216, b: 230 }
LightGray = Color { r: 211, g: 211, b: 211 } LightGray = Color { r: 211, g: 211, b: 211 }
LightGreen = Color { r: 144, g: 238, b: 144 } LightGreen = Color { r: 144, g: 238, b: 144 }
LightRed = Color { r: 255, g: 204, b: 203 } LightRed = Color { r: 255, g: 204, b: 203 }

View File

@ -6,7 +6,7 @@ module http
import os import os
fn download_file(url, out string) bool { pub fn download_file(url, out string) bool {
s := http.get(url) or { return false } s := http.get(url) or { return false }
os.write_file(out, s.text) os.write_file(out, s.text)
return true return true

View File

@ -27,7 +27,7 @@ fn download_cb(ptr voidptr, size, nmemb size_t, userp voidptr) {
*/ */
} }
fn download_file_with_progress(url, out string, cb downloadfn, cb_finished fn()) { pub fn download_file_with_progress(url, out string, cb downloadfn, cb_finished fn()) {
/* /*
curl := C.curl_easy_init() curl := C.curl_easy_init()
if isnil(curl) { if isnil(curl) {

View File

@ -21,10 +21,11 @@ pub:
typ string // GET POST typ string // GET POST
data string data string
url string url string
ws_func voidptr
user_ptr voidptr
verbose bool verbose bool
user_agent string user_agent string
mut:
user_ptr voidptr
ws_func voidptr
} }
pub struct Response { pub struct Response {

View File

@ -738,7 +738,7 @@ pub fn clear() {
} }
} }
fn on_segfault(f voidptr) { pub fn on_segfault(f voidptr) {
$if windows { $if windows {
return return
} }