Skip to content

Commit

Permalink
tokenizer: read_string
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Nov 19, 2024
1 parent ac814a7 commit 6f31668
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
45 changes: 44 additions & 1 deletion src/compiler/tokenizer/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ fn (mut t Tokenizer) read_number() string {

fn (mut t Tokenizer) read_char() string {
start := t.pos
is_bytelit := t.pos > 0 && t.text[t.pos - 1] == `b`
// is_bytelit := t.pos > 0 && t.text[t.pos - 1] == `b`

mut len := 0
for {
Expand Down Expand Up @@ -346,6 +346,49 @@ fn (mut t Tokenizer) read_char() string {
return ch
}

fn (mut t Tokenizer) read_string() string {
start_pos := t.current_pos()
start := t.pos
start_char := t.current_char()
is_raw := t.pos > 0 && t.text[t.pos - 1] == `r`
// is_cstr := t.pos > 0 && t.text[t.pos - 1] == `c`
mut backslash_count := if start_char == backslash { 1 } else { 0 }
mut n_cr_chars := 0
for {
t.pos++
if t.pos >= t.text.len {
t.pos = start
report.error('unfinished string literal', start_pos)
return ''
}
c := t.current_char()
if c == backslash {
backslash_count++
}
// end of string
if c == `"` && (is_raw || backslash_count & 1 == 0) {
break // handle `\\` at the end
}
if c == cr {
n_cr_chars++
}
if c == lf {
t.inc_line_number()
}
if c != backslash_count {
backslash_count = 0
}
}
mut lit := ''
if start <= t.pos {
lit = t.text[start + 1..t.pos]
if n_cr_chars > 0 {
lit = lit.replace('\r', '')
}
}
return lit
}

fn (mut t Tokenizer) next() token.Token {
for {
cidx := t.tidx
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/util/mod.v
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn is_valid_name(c u8) bool {

pub fn read_file(path string) string {
return skip_bom(os.read_file(path) or {
// we use `ic_error` because this should not happen
// we use `ic_fatal` because this should not happen
report.ic_fatal(err.msg())
})
}
Expand Down

0 comments on commit 6f31668

Please sign in to comment.