1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

double quotes for denoting strings

This commit is contained in:
Alexander Medvednikov 2019-09-21 16:26:25 +03:00
parent b1e1536d56
commit a232b215a7
2 changed files with 18 additions and 10 deletions

View File

@ -33,6 +33,7 @@ mut:
prev_tok Token
fn_name string // needed for @FN
should_print_line_on_error bool
quote byte // which quote is used to denote current string: ' or "
}
fn new_scanner(file_path string) &Scanner {
@ -359,9 +360,7 @@ fn (s mut Scanner) scan() ScanRes {
return scan_res(.mod, '')
case `?`:
return scan_res(.question, '')
case single_quote:
return scan_res(.str, s.ident_string())
case double_quote:
case single_quote, double_quote:
return scan_res(.str, s.ident_string())
case `\``: // ` // apostrophe balance comment. do not remove
return scan_res(.chartoken, s.ident_char())
@ -690,8 +689,14 @@ fn (s Scanner) count_symbol_before(p int, sym byte) int {
// println('array out of bounds $idx len=$a.len')
// This is really bad. It needs a major clean up
fn (s mut Scanner) ident_string() string {
// println("\nidentString() at char=", string(s.text[s.pos]),
// "chard=", s.text[s.pos], " pos=", s.pos, "txt=", s.text[s.pos:s.pos+7])
q := s.text[s.pos]
if (q == single_quote || q == double_quote) && !s.inside_string{
s.quote = q
}
//if s.file_path.contains('string_test') {
//println('\nident_string() at char=${s.text[s.pos].str()}')
//println('linenr=$s.line_nr quote= $qquote ${qquote.str()}')
//}
mut start := s.pos
s.inside_string = false
slash := `\\`
@ -703,7 +708,7 @@ fn (s mut Scanner) ident_string() string {
c := s.text[s.pos]
prevc := s.text[s.pos - 1]
// end of string
if c == `\'` && (prevc != slash || (prevc == slash && s.text[s.pos - 2] == slash)) {
if c == s.quote && (prevc != slash || (prevc == slash && s.text[s.pos - 2] == slash)) {
// handle '123\\' slash at the end
break
}
@ -734,7 +739,7 @@ fn (s mut Scanner) ident_string() string {
}
}
mut lit := ''
if s.text[start] == `\'` {
if s.text[start] == s.quote {
start++
}
mut end := s.pos

View File

@ -56,7 +56,7 @@ fn test_ge() {
assert b >= (a)
assert c >= (b)
assert d >= (c)
assert !(c >= d)
assert !(c >= d)
assert e >= (a)
}
@ -398,8 +398,8 @@ fn test_title() {
s.to_upper()
assert s.title() == 'Hello World'
s.to_lower()
assert s.title() == 'Hello World'
}
assert s.title() == 'Hello World'
}
fn test_for_loop() {
mut i := 0
@ -421,6 +421,9 @@ fn test_for_loop_two() {
fn test_quote() {
a := `'`
println("testing double quotes")
b := "hi"
assert b == 'hi'
assert a.str() == '\''
}