double quotes for denoting strings

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

View File

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