v/vlib/v/scanner/scanner.v

1038 lines
23 KiB
V
Raw Normal View History

2020-01-23 21:04:46 +01:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
2019-12-22 02:34:37 +01:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module scanner
import (
os
v.token
2020-03-28 21:51:45 +01:00
v.pref
2019-12-22 02:34:37 +01:00
)
const (
single_quote = `\'`
double_quote = `"`
error_context_before = 2 // how many lines of source context to print before the pointer line
error_context_after = 2 // ^^^ same, but after
)
pub struct Scanner {
mut:
file_path string
text string
pos int
line_nr int
last_nl_pos int // for calculating column
inside_string bool
inter_start bool // for hacky string interpolation TODO simplify
inter_end bool
debug bool
line_comment string
2019-12-28 09:43:22 +01:00
// prev_tok TokenKind
2019-12-22 02:34:37 +01:00
started bool
fn_name string // needed for @FN
print_line_on_error bool
print_colored_error bool
print_rel_paths_on_error bool
quote byte // which quote is used to denote current string: ' or "
line_ends []int // the positions of source lines ends (i.e. \n signs)
2019-12-28 09:43:22 +01:00
nr_lines int // total number of lines in the source file that were scanned
2019-12-22 02:34:37 +01:00
is_vh bool // Keep newlines
is_fmt bool // Used only for skipping ${} in strings, since we need literal
// string values when generating formatted code.
2020-02-29 17:51:35 +01:00
comments_mode CommentsMode
2019-12-22 02:34:37 +01:00
}
2020-02-29 17:51:35 +01:00
pub enum CommentsMode {
skip_comments
parse_comments
}
2019-12-22 02:34:37 +01:00
// new scanner from file.
2020-02-29 17:51:35 +01:00
pub fn new_scanner_file(file_path string, comments_mode CommentsMode) &Scanner {
2019-12-22 02:34:37 +01:00
if !os.exists(file_path) {
verror("$file_path doesn't exist")
}
2019-12-28 09:43:22 +01:00
mut raw_text := os.read_file(file_path) or {
2019-12-22 02:34:37 +01:00
verror('scanner: failed to open $file_path')
return 0
}
// BOM check
if raw_text.len >= 3 {
c_text := raw_text.str
if c_text[0] == 0xEF && c_text[1] == 0xBB && c_text[2] == 0xBF {
// skip three BOM bytes
offset_from_begin := 3
raw_text = tos(c_text[offset_from_begin], vstrlen(c_text) - offset_from_begin)
}
}
2020-02-29 17:51:35 +01:00
mut s := new_scanner(raw_text, comments_mode) // .skip_comments)
2019-12-28 09:43:22 +01:00
// s.init_fmt()
2019-12-22 02:34:37 +01:00
s.file_path = file_path
return s
}
2020-02-18 20:20:15 +01:00
const (
is_fmt = os.getenv('VEXE').contains('vfmt')
)
2019-12-22 02:34:37 +01:00
// new scanner from string.
2020-02-29 17:51:35 +01:00
pub fn new_scanner(text string, comments_mode CommentsMode) &Scanner {
2019-12-22 02:34:37 +01:00
return &Scanner{
text: text
print_line_on_error: true
print_colored_error: true
print_rel_paths_on_error: true
2020-02-18 20:20:15 +01:00
is_fmt: is_fmt
2020-02-29 17:51:35 +01:00
comments_mode: comments_mode
2019-12-22 02:34:37 +01:00
}
}
fn (s &Scanner) scan_res(tok_kind token.Kind, lit string) token.Token {
return token.Token{
kind: tok_kind
lit: lit
line_nr: s.line_nr + 1
pos: s.pos
}
2019-12-22 02:34:37 +01:00
}
fn (s mut Scanner) ident_name() string {
start := s.pos
2020-02-25 22:58:51 +01:00
s.pos++
for s.pos < s.text.len && (is_name_char(s.text[s.pos]) || s.text[s.pos].is_digit()) {
2019-12-22 02:34:37 +01:00
s.pos++
}
name := s.text[start..s.pos]
s.pos--
return name
}
2020-02-07 22:10:48 +01:00
const (
num_sep = `_` // char used as number separator
)
fn filter_num_sep(txt byteptr, start int, end int) string {
2020-02-29 17:51:35 +01:00
unsafe{
2020-02-07 22:10:48 +01:00
mut b := malloc(end - start + 1) // add a byte for the endstring 0
mut i := start
mut i1 := 0
for i < end {
if txt[i] != num_sep {
2020-02-07 22:10:48 +01:00
b[i1] = txt[i]
i1++
}
i++
}
2020-02-07 22:10:48 +01:00
b[i1] = 0 // C string compatibility
return string{
2020-02-13 01:06:34 +01:00
str: b
len: i1
}
}
}
fn (s mut Scanner) ident_bin_number() string {
mut has_wrong_digit := false
mut first_wrong_digit := `\0`
start_pos := s.pos
s.pos += 2 // skip '0b'
2020-02-25 22:58:51 +01:00
for s.pos < s.text.len {
c := s.text[s.pos]
if !c.is_bin_digit() && c != num_sep {
if (!c.is_digit() && !c.is_letter()) || s.inside_string {
break
2020-02-29 17:51:35 +01:00
}
else if !has_wrong_digit {
has_wrong_digit = true
first_wrong_digit = c
}
}
s.pos++
}
if start_pos + 2 == s.pos {
s.error('number part of this binary is not provided')
}
else if has_wrong_digit {
s.error('this binary number has unsuitable digit `${first_wrong_digit.str()}`')
}
number := filter_num_sep(s.text.str, start_pos, s.pos)
s.pos--
return number
}
2019-12-22 02:34:37 +01:00
fn (s mut Scanner) ident_hex_number() string {
mut has_wrong_digit := false
mut first_wrong_digit := `\0`
2019-12-22 02:34:37 +01:00
start_pos := s.pos
s.pos += 2 // skip '0x'
2020-02-25 22:58:51 +01:00
for s.pos < s.text.len {
2019-12-22 02:34:37 +01:00
c := s.text[s.pos]
if !c.is_hex_digit() && c != num_sep {
if !c.is_letter() || s.inside_string {
break
2020-02-29 17:51:35 +01:00
}
else if !has_wrong_digit {
has_wrong_digit = true
first_wrong_digit = c
}
2019-12-22 02:34:37 +01:00
}
s.pos++
}
if start_pos + 2 == s.pos {
s.error('number part of this hexadecimal is not provided')
}
else if has_wrong_digit {
s.error('this hexadecimal number has unsuitable digit `${first_wrong_digit.str()}`')
}
number := filter_num_sep(s.text.str, start_pos, s.pos)
2019-12-22 02:34:37 +01:00
s.pos--
return number
}
fn (s mut Scanner) ident_oct_number() string {
mut has_wrong_digit := false
mut first_wrong_digit := `\0`
2019-12-22 02:34:37 +01:00
start_pos := s.pos
2020-02-23 12:33:07 +01:00
s.pos += 2 // skip '0o'
2020-02-25 22:58:51 +01:00
for s.pos < s.text.len {
2019-12-22 02:34:37 +01:00
c := s.text[s.pos]
2020-02-23 12:33:07 +01:00
if !c.is_oct_digit() && c != num_sep {
if (!c.is_digit() && !c.is_letter()) || s.inside_string {
break
2020-02-29 17:51:35 +01:00
}
else if !has_wrong_digit {
has_wrong_digit = true
first_wrong_digit = c
}
2019-12-22 02:34:37 +01:00
}
s.pos++
}
if start_pos + 2 == s.pos {
s.error('number part of this octal is not provided')
}
else if has_wrong_digit {
s.error('this octal number has unsuitable digit `${first_wrong_digit.str()}`')
}
number := filter_num_sep(s.text.str, start_pos, s.pos)
2019-12-22 02:34:37 +01:00
s.pos--
return number
}
fn (s mut Scanner) ident_dec_number() string {
mut has_wrong_digit := false
mut first_wrong_digit := `\0`
2019-12-22 02:34:37 +01:00
start_pos := s.pos
// scan integer part
for s.pos < s.text.len {
2020-02-25 22:58:51 +01:00
c := s.text[s.pos]
if !c.is_digit() && c != num_sep {
if !c.is_letter() || c in [`e`, `E`] || s.inside_string {
break
}
else if !has_wrong_digit {
has_wrong_digit = true
2020-02-25 22:58:51 +01:00
first_wrong_digit = c
}
}
2019-12-22 02:34:37 +01:00
s.pos++
}
// e.g. 1..9
// we just return '1' and don't scan '..9'
if s.expect('..', s.pos) {
number := filter_num_sep(s.text.str, start_pos, s.pos)
2019-12-22 02:34:37 +01:00
s.pos--
return number
}
// scan fractional part
if s.pos < s.text.len && s.text[s.pos] == `.` {
s.pos++
for s.pos < s.text.len {
2020-02-25 22:58:51 +01:00
c := s.text[s.pos]
if !c.is_digit() {
if !c.is_letter() || c in [`e`, `E`] || s.inside_string {
break
}
else if !has_wrong_digit {
has_wrong_digit = true
2020-02-25 22:58:51 +01:00
first_wrong_digit = c
}
}
2019-12-22 02:34:37 +01:00
s.pos++
}
}
// scan exponential part
mut has_exponential_part := false
2020-02-17 02:35:01 +01:00
if s.expect('e', s.pos) || s.expect('E', s.pos) {
s.pos++
exp_start_pos := s.pos
if s.pos < s.text.len && s.text[s.pos] in [`-`, `+`] {
2020-02-17 02:35:01 +01:00
s.pos++
}
for s.pos < s.text.len {
2020-02-25 22:58:51 +01:00
c := s.text[s.pos]
if !c.is_digit() {
if !c.is_letter() || s.inside_string {
break
}
else if !has_wrong_digit {
has_wrong_digit = true
2020-02-25 22:58:51 +01:00
first_wrong_digit = c
}
}
2019-12-22 02:34:37 +01:00
s.pos++
}
if exp_start_pos == s.pos {
s.error('exponent has no digits')
}
has_exponential_part = true
}
// error check: 1.23.4, 123.e+3.4
if s.pos < s.text.len && s.text[s.pos] == `.` {
if has_exponential_part {
s.error('exponential part should be integer')
}
else {
s.error('too many decimal points in number')
}
}
if has_wrong_digit {
s.error('this number has unsuitable digit `${first_wrong_digit.str()}`')
}
number := filter_num_sep(s.text.str, start_pos, s.pos)
2019-12-22 02:34:37 +01:00
s.pos--
return number
}
fn (s mut Scanner) ident_number() string {
if s.expect('0b', s.pos) {
return s.ident_bin_number()
}
else if s.expect('0x', s.pos) {
2019-12-22 02:34:37 +01:00
return s.ident_hex_number()
}
else if s.expect('0o', s.pos) {
2020-02-23 12:33:07 +01:00
return s.ident_oct_number()
}
else {
2019-12-22 02:34:37 +01:00
return s.ident_dec_number()
}
}
fn (s mut Scanner) skip_whitespace() {
// if s.is_vh { println('vh') return }
for s.pos < s.text.len && s.text[s.pos].is_space() {
2019-12-22 02:34:37 +01:00
if is_nl(s.text[s.pos]) && s.is_vh {
return
}
// Count \r\n as one line
if is_nl(s.text[s.pos]) && !s.expect('\r\n', s.pos - 1) {
s.inc_line_number()
}
s.pos++
}
}
fn (s mut Scanner) end_of_file() token.Token {
2019-12-22 02:34:37 +01:00
s.pos = s.text.len
s.inc_line_number()
2019-12-29 06:50:08 +01:00
return s.scan_res(.eof, '')
2019-12-22 02:34:37 +01:00
}
pub fn (s mut Scanner) scan() token.Token {
2020-02-29 17:51:35 +01:00
// if s.comments_mode == .parse_comments {
// println('\nscan()')
// }
2019-12-22 02:34:37 +01:00
// if s.line_comment != '' {
// s.fgenln('// LC "$s.line_comment"')
// s.line_comment = ''
// }
if s.started {
s.pos++
}
s.started = true
if s.pos >= s.text.len {
return s.end_of_file()
}
if !s.inside_string {
s.skip_whitespace()
}
// End of $var, start next string
if s.inter_end {
if s.text[s.pos] == s.quote {
s.inter_end = false
2020-03-15 00:46:08 +01:00
return s.scan_res(.string, '')
2019-12-22 02:34:37 +01:00
}
s.inter_end = false
2020-03-15 00:46:08 +01:00
return s.scan_res(.string, s.ident_string())
2019-12-22 02:34:37 +01:00
}
s.skip_whitespace()
// end of file
if s.pos >= s.text.len {
return s.end_of_file()
}
// handle each char
c := s.text[s.pos]
mut nextc := `\0`
if s.pos + 1 < s.text.len {
nextc = s.text[s.pos + 1]
}
// name or keyword
if is_name_char(c) {
name := s.ident_name()
// tmp hack to detect . in ${}
// Check if not .eof to prevent panic
next_char := if s.pos + 1 < s.text.len { s.text[s.pos + 1] } else { `\0` }
if token.is_key(name) {
2019-12-29 06:50:08 +01:00
return s.scan_res(token.key_to_token(name), '')
2019-12-22 02:34:37 +01:00
}
// 'asdf $b' => "b" is the last name in the string, dont start parsing string
// at the next ', skip it
if s.inside_string {
if next_char == s.quote {
s.inter_end = true
s.inter_start = false
s.inside_string = false
}
}
// end of `$expr`
// allow `'$a.b'` and `'$a.c()'`
if s.inter_start && next_char != `.` && next_char != `(` {
s.inter_end = true
s.inter_start = false
}
if s.pos == 0 && next_char == ` ` {
// If a single letter name at the start of the file, increment
// Otherwise the scanner would be stuck at s.pos = 0
s.pos++
}
2019-12-29 06:50:08 +01:00
return s.scan_res(.name, name)
2019-12-22 02:34:37 +01:00
}
// `123`, `.123`
else if c.is_digit() || (c == `.` && nextc.is_digit()) {
if !s.inside_string {
2020-02-29 17:51:35 +01:00
// In C ints with `0` prefix are octal (in V they're decimal), so discarding heading zeros is needed.
mut start_pos := s.pos
for start_pos < s.text.len && s.text[start_pos] == `0` {
start_pos++
}
2020-02-29 17:51:35 +01:00
mut prefix_zero_num := start_pos - s.pos // how many prefix zeros should be jumped
// for 0b, 0o, 0x the heading zero shouldn't be jumped
if start_pos == s.text.len || (c == `0` && !s.text[start_pos].is_digit()) {
prefix_zero_num--
}
2020-02-29 17:51:35 +01:00
s.pos += prefix_zero_num // jump these zeros
}
2019-12-22 02:34:37 +01:00
num := s.ident_number()
2019-12-29 06:50:08 +01:00
return s.scan_res(.number, num)
2019-12-22 02:34:37 +01:00
}
// Handle `'$fn()'`
if c == `)` && s.inter_start {
s.inter_end = true
s.inter_start = false
next_char := if s.pos + 1 < s.text.len { s.text[s.pos + 1] } else { `\0` }
if next_char == s.quote {
s.inside_string = false
}
2019-12-29 06:50:08 +01:00
return s.scan_res(.rpar, '')
2019-12-22 02:34:37 +01:00
}
// all other tokens
match c {
`+` {
if nextc == `+` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.inc, '')
2019-12-22 02:34:37 +01:00
}
else if nextc == `=` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.plus_assign, '')
2019-12-22 02:34:37 +01:00
}
2019-12-29 06:50:08 +01:00
return s.scan_res(.plus, '')
2019-12-22 02:34:37 +01:00
}
`-` {
if nextc == `-` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.dec, '')
2019-12-22 02:34:37 +01:00
}
else if nextc == `=` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.minus_assign, '')
2019-12-22 02:34:37 +01:00
}
2019-12-29 06:50:08 +01:00
return s.scan_res(.minus, '')
2019-12-22 02:34:37 +01:00
}
`*` {
if nextc == `=` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.mult_assign, '')
2019-12-22 02:34:37 +01:00
}
2019-12-29 06:50:08 +01:00
return s.scan_res(.mul, '')
2019-12-22 02:34:37 +01:00
}
`^` {
if nextc == `=` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.xor_assign, '')
2019-12-22 02:34:37 +01:00
}
2019-12-29 06:50:08 +01:00
return s.scan_res(.xor, '')
2019-12-22 02:34:37 +01:00
}
`%` {
if nextc == `=` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.mod_assign, '')
2019-12-22 02:34:37 +01:00
}
2019-12-29 06:50:08 +01:00
return s.scan_res(.mod, '')
2019-12-22 02:34:37 +01:00
}
`?` {
2019-12-29 06:50:08 +01:00
return s.scan_res(.question, '')
2019-12-22 02:34:37 +01:00
}
single_quote, double_quote {
2020-03-15 00:46:08 +01:00
return s.scan_res(.string, s.ident_string())
2019-12-22 02:34:37 +01:00
}
`\`` {
// ` // apostrophe balance comment. do not remove
2019-12-29 06:50:08 +01:00
return s.scan_res(.chartoken, s.ident_char())
2019-12-22 02:34:37 +01:00
}
`(` {
2019-12-29 06:50:08 +01:00
return s.scan_res(.lpar, '')
2019-12-22 02:34:37 +01:00
}
`)` {
2019-12-29 06:50:08 +01:00
return s.scan_res(.rpar, '')
2019-12-22 02:34:37 +01:00
}
`[` {
2019-12-29 06:50:08 +01:00
return s.scan_res(.lsbr, '')
2019-12-22 02:34:37 +01:00
}
`]` {
2019-12-29 06:50:08 +01:00
return s.scan_res(.rsbr, '')
2019-12-22 02:34:37 +01:00
}
`{` {
// Skip { in `${` in strings
if s.inside_string {
return s.scan()
}
2019-12-29 06:50:08 +01:00
return s.scan_res(.lcbr, '')
2019-12-22 02:34:37 +01:00
}
`$` {
if s.inside_string {
2019-12-29 06:50:08 +01:00
return s.scan_res(.str_dollar, '')
2019-12-22 02:34:37 +01:00
}
else {
2019-12-29 06:50:08 +01:00
return s.scan_res(.dollar, '')
2019-12-22 02:34:37 +01:00
}
}
`}` {
// s = `hello $name !`
// s = `hello ${name} !`
if s.inside_string {
s.pos++
if s.text[s.pos] == s.quote {
s.inside_string = false
2020-03-15 00:46:08 +01:00
return s.scan_res(.string, '')
2019-12-22 02:34:37 +01:00
}
2020-03-15 00:46:08 +01:00
return s.scan_res(.string, s.ident_string())
2019-12-22 02:34:37 +01:00
}
else {
2019-12-29 06:50:08 +01:00
return s.scan_res(.rcbr, '')
2019-12-22 02:34:37 +01:00
}
}
`&` {
if nextc == `=` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.and_assign, '')
2019-12-22 02:34:37 +01:00
}
if nextc == `&` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.and, '')
2019-12-22 02:34:37 +01:00
}
2019-12-29 06:50:08 +01:00
return s.scan_res(.amp, '')
2019-12-22 02:34:37 +01:00
}
`|` {
if nextc == `|` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.logical_or, '')
2019-12-22 02:34:37 +01:00
}
if nextc == `=` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.or_assign, '')
2019-12-22 02:34:37 +01:00
}
2019-12-29 06:50:08 +01:00
return s.scan_res(.pipe, '')
2019-12-22 02:34:37 +01:00
}
`,` {
2019-12-29 06:50:08 +01:00
return s.scan_res(.comma, '')
2019-12-22 02:34:37 +01:00
}
`@` {
s.pos++
name := s.ident_name()
// @FN => will be substituted with the name of the current V function
2020-03-28 21:51:45 +01:00
// @VEXE => will be substituted with the path to the V compiler
2019-12-22 02:34:37 +01:00
// @FILE => will be substituted with the path of the V source file
// @LINE => will be substituted with the V line number where it appears (as a string).
// @COLUMN => will be substituted with the column where it appears (as a string).
// @VHASH => will be substituted with the shortened commit hash of the V compiler (as a string).
// This allows things like this:
// println( 'file: ' + @FILE + ' | line: ' + @LINE + ' | fn: ' + @FN)
// ... which is useful while debugging/tracing
if name == 'FN' {
2020-03-15 00:46:08 +01:00
return s.scan_res(.string, s.fn_name)
2019-12-22 02:34:37 +01:00
}
2020-03-28 21:51:45 +01:00
if name == 'VEXE' {
vexe := pref.vexe_path()
return s.scan_res(.string, cescaped_path( vexe ) )
}
2019-12-22 02:34:37 +01:00
if name == 'FILE' {
2020-03-20 16:41:18 +01:00
return s.scan_res(.string, cescaped_path(os.real_path(s.file_path)))
2019-12-22 02:34:37 +01:00
}
if name == 'LINE' {
2020-03-15 00:46:08 +01:00
return s.scan_res(.string, (s.line_nr + 1).str())
2019-12-22 02:34:37 +01:00
}
if name == 'COLUMN' {
2020-03-15 00:46:08 +01:00
return s.scan_res(.string, (s.current_column()).str())
2019-12-22 02:34:37 +01:00
}
if name == 'VHASH' {
2020-03-15 00:46:08 +01:00
return s.scan_res(.string, vhash())
2019-12-22 02:34:37 +01:00
}
if !token.is_key(name) {
s.error('@ must be used before keywords (e.g. `@type string`)')
}
2019-12-29 06:50:08 +01:00
return s.scan_res(.name, name)
2019-12-22 02:34:37 +01:00
}
/*
case `\r`:
if nextc == `\n` {
s.pos++
s.last_nl_pos = s.pos
2019-12-29 06:50:08 +01:00
return s.scan_res(.nl, '')
2019-12-22 02:34:37 +01:00
}
}
case `\n`:
s.last_nl_pos = s.pos
2019-12-29 06:50:08 +01:00
return s.scan_res(.nl, '')
2019-12-22 02:34:37 +01:00
}
*/
`.` {
if nextc == `.` {
s.pos++
if s.text[s.pos + 1] == `.` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.ellipsis, '')
2019-12-22 02:34:37 +01:00
}
2019-12-29 06:50:08 +01:00
return s.scan_res(.dotdot, '')
2019-12-22 02:34:37 +01:00
}
2019-12-29 06:50:08 +01:00
return s.scan_res(.dot, '')
2019-12-22 02:34:37 +01:00
}
`#` {
start := s.pos + 1
s.ignore_line()
if nextc == `!` {
// treat shebang line (#!) as a comment
s.line_comment = s.text[start + 1..s.pos].trim_space()
// s.fgenln('// shebang line "$s.line_comment"')
return s.scan()
}
hash := s.text[start..s.pos]
2019-12-29 06:50:08 +01:00
return s.scan_res(.hash, hash.trim_space())
2019-12-22 02:34:37 +01:00
}
`>` {
if nextc == `=` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.ge, '')
2019-12-22 02:34:37 +01:00
}
else if nextc == `>` {
if s.pos + 2 < s.text.len && s.text[s.pos + 2] == `=` {
s.pos += 2
2020-02-28 17:16:24 +01:00
return s.scan_res(.right_shift_assign, '')
2019-12-22 02:34:37 +01:00
}
s.pos++
2020-02-04 12:50:58 +01:00
return s.scan_res(.right_shift, '')
2019-12-22 02:34:37 +01:00
}
else {
2019-12-29 06:50:08 +01:00
return s.scan_res(.gt, '')
2019-12-22 02:34:37 +01:00
}
}
0xE2 {
// case `≠`:
if nextc == 0x89 && s.text[s.pos + 2] == 0xA0 {
s.pos += 2
2019-12-29 06:50:08 +01:00
return s.scan_res(.ne, '')
2019-12-22 02:34:37 +01:00
}
// ⩽
else if nextc == 0x89 && s.text[s.pos + 2] == 0xBD {
s.pos += 2
2019-12-29 06:50:08 +01:00
return s.scan_res(.le, '')
2019-12-22 02:34:37 +01:00
}
// ⩾
else if nextc == 0xA9 && s.text[s.pos + 2] == 0xBE {
s.pos += 2
2019-12-29 06:50:08 +01:00
return s.scan_res(.ge, '')
2019-12-22 02:34:37 +01:00
}
}
`<` {
if nextc == `=` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.le, '')
2019-12-22 02:34:37 +01:00
}
else if nextc == `<` {
if s.pos + 2 < s.text.len && s.text[s.pos + 2] == `=` {
s.pos += 2
2019-12-29 06:50:08 +01:00
return s.scan_res(.left_shift_assign, '')
2019-12-22 02:34:37 +01:00
}
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.left_shift, '')
2019-12-22 02:34:37 +01:00
}
else {
2019-12-29 06:50:08 +01:00
return s.scan_res(.lt, '')
2019-12-22 02:34:37 +01:00
}
}
`=` {
if nextc == `=` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.eq, '')
2019-12-22 02:34:37 +01:00
}
else if nextc == `>` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.arrow, '')
2019-12-22 02:34:37 +01:00
}
else {
2019-12-29 06:50:08 +01:00
return s.scan_res(.assign, '')
2019-12-22 02:34:37 +01:00
}
}
`:` {
if nextc == `=` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.decl_assign, '')
2019-12-22 02:34:37 +01:00
}
else {
2019-12-29 06:50:08 +01:00
return s.scan_res(.colon, '')
2019-12-22 02:34:37 +01:00
}
}
`;` {
2019-12-29 06:50:08 +01:00
return s.scan_res(.semicolon, '')
2019-12-22 02:34:37 +01:00
}
`!` {
if nextc == `=` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.ne, '')
2019-12-22 02:34:37 +01:00
}
else {
2019-12-29 06:50:08 +01:00
return s.scan_res(.not, '')
2019-12-22 02:34:37 +01:00
}
}
`~` {
2019-12-29 06:50:08 +01:00
return s.scan_res(.bit_not, '')
2019-12-22 02:34:37 +01:00
}
`/` {
if nextc == `=` {
s.pos++
2019-12-29 06:50:08 +01:00
return s.scan_res(.div_assign, '')
2019-12-22 02:34:37 +01:00
}
if nextc == `/` {
start := s.pos + 1
s.ignore_line()
s.line_comment = s.text[start + 1..s.pos]
2020-02-29 17:51:35 +01:00
// if s.comments_mode == .parse_comments {
// println('line c $s.line_comment')
// }
comment := s.line_comment.trim_space()
// s.line_comment = comment
if s.comments_mode == .parse_comments {
// println('line c "$comment" z=')
// fix line_nr, \n was read, and the comment is marked
// on the next line
s.pos--
// println("'" + s.text[s.pos].str() + "'")
// s.line_nr--
return s.scan_res(.line_comment, comment)
2019-12-22 02:34:37 +01:00
}
// s.fgenln('// ${s.prev_tok.str()} "$s.line_comment"')
// Skip the comment (return the next token)
return s.scan()
}
// Multiline comments
if nextc == `*` {
start := s.pos + 2
2019-12-22 02:34:37 +01:00
mut nest_count := 1
// Skip comment
for nest_count > 0 {
s.pos++
if s.pos >= s.text.len {
s.line_nr--
s.error('comment not terminated')
}
if s.text[s.pos] == `\n` {
s.inc_line_number()
continue
}
if s.expect('/*', s.pos) {
nest_count++
continue
}
if s.expect('*/', s.pos) {
nest_count--
}
}
s.pos++
if s.comments_mode == .parse_comments {
comment := s.text[start..(s.pos-1)].trim_space()
return s.scan_res(.mline_comment, comment)
2019-12-22 02:34:37 +01:00
}
// Skip if not in fmt mode
return s.scan()
}
2019-12-29 06:50:08 +01:00
return s.scan_res(.div, '')
2019-12-22 02:34:37 +01:00
}
2019-12-28 09:43:22 +01:00
else {}
}
2019-12-22 02:34:37 +01:00
$if windows {
if c == `\0` {
return s.end_of_file()
}
}
s.error('invalid character `${c.str()}`')
return s.end_of_file()
}
fn (s &Scanner) current_column() int {
return s.pos - s.last_nl_pos
}
fn (s Scanner) count_symbol_before(p int, sym byte) int {
mut count := 0
for i := p; i >= 0; i-- {
if s.text[i] != sym {
break
}
count++
}
return count
}
fn (s mut Scanner) ident_string() string {
q := s.text[s.pos]
is_quote := q == single_quote || q == double_quote
is_raw := is_quote && s.text[s.pos - 1] == `r`
if is_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 := `\\`
for {
s.pos++
if s.pos >= s.text.len {
break
}
c := s.text[s.pos]
prevc := s.text[s.pos - 1]
// end of string
if c == s.quote && (prevc != slash || (prevc == slash && s.text[s.pos - 2] == slash)) {
// handle '123\\' slash at the end
break
}
if c == `\n` {
s.inc_line_number()
}
// Don't allow \0
if c == `0` && s.pos > 2 && s.text[s.pos - 1] == slash {
2019-12-28 09:43:22 +01:00
if s.pos < s.text.len - 1 && s.text[s.pos + 1].is_digit() {}
2019-12-22 02:34:37 +01:00
else {
s.error('0 character in a string literal')
}
}
// Don't allow \x00
if c == `0` && s.pos > 5 && s.expect('\\x0', s.pos - 3) {
s.error('0 character in a string literal')
}
// ${var} (ignore in vfmt mode)
if c == `{` && prevc == `$` && !is_raw && !s.is_fmt && s.count_symbol_before(s.pos - 2, slash) % 2 == 0 {
s.inside_string = true
// so that s.pos points to $ at the next step
s.pos -= 2
break
}
// $var
if is_name_char(c) && prevc == `$` && !s.is_fmt && !is_raw && s.count_symbol_before(s.pos - 2, slash) % 2 == 0 {
s.inside_string = true
s.inter_start = true
s.pos -= 2
break
}
}
mut lit := ''
if s.text[start] == s.quote {
start++
}
mut end := s.pos
if s.inside_string {
end++
}
2019-12-28 09:43:22 +01:00
if start > s.pos {}
2019-12-22 02:34:37 +01:00
else {
lit = s.text[start..end]
}
return lit
}
fn (s mut Scanner) ident_char() string {
start := s.pos
slash := `\\`
mut len := 0
for {
s.pos++
if s.pos >= s.text.len {
break
}
if s.text[s.pos] != slash {
len++
}
double_slash := s.expect('\\\\', s.pos - 2)
if s.text[s.pos] == `\`` && (s.text[s.pos - 1] != slash || double_slash) {
// ` // apostrophe balance comment. do not remove
if double_slash {
len++
}
break
}
}
len--
c := s.text[start + 1..s.pos]
if len != 1 {
u := c.ustring()
if u.len != 1 {
s.error('invalid character literal (more than one character)\n' + 'use quotes for strings, backticks for characters')
}
}
if c == '\\`' {
return '`'
}
// Escapes a `'` character
return if c == "\'" { '\\' + c } else { c }
}
fn (s &Scanner) expect(want string, start_pos int) bool {
end_pos := start_pos + want.len
if start_pos < 0 || start_pos >= s.text.len {
return false
}
if end_pos < 0 || end_pos > s.text.len {
return false
}
for pos in start_pos .. end_pos {
if s.text[pos] != want[pos - start_pos] {
return false
}
}
return true
}
fn (s mut Scanner) debug_tokens() {
s.pos = 0
s.started = false
s.debug = true
2020-03-07 22:26:26 +01:00
fname := s.file_path.all_after(os.path_separator)
2019-12-22 02:34:37 +01:00
println('\n===DEBUG TOKENS $fname===')
for {
tok := s.scan()
tok_kind := tok.kind
lit := tok.lit
print(tok_kind.str())
2019-12-22 02:34:37 +01:00
if lit != '' {
println(' `$lit`')
}
else {
println('')
}
if tok_kind == .eof {
2019-12-22 02:34:37 +01:00
println('============ END OF DEBUG TOKENS ==================')
break
}
}
}
fn (s mut Scanner) ignore_line() {
s.eat_to_end_of_line()
s.inc_line_number()
}
fn (s mut Scanner) eat_to_end_of_line() {
for s.pos < s.text.len && s.text[s.pos] != `\n` {
s.pos++
}
}
fn (s mut Scanner) inc_line_number() {
s.last_nl_pos = s.pos
s.line_nr++
s.line_ends << s.pos
if s.line_nr > s.nr_lines {
s.nr_lines = s.line_nr
}
}
fn (s Scanner) line(n int) string {
mut res := ''
if n >= 0 && n < s.line_ends.len {
nline_start := if n == 0 { 0 } else { s.line_ends[n - 1] }
nline_end := s.line_ends[n]
if nline_start <= nline_end {
res = s.text[nline_start..nline_end]
}
}
return res.trim_right('\r\n').trim_left('\r\n')
}
fn is_name_char(c byte) bool {
return c == `_` || c.is_letter()
}
[inline]
fn is_nl(c byte) bool {
return c == `\r` || c == `\n`
}
fn contains_capital(s string) bool {
for c in s {
if c >= `A` && c <= `Z` {
return true
}
}
return false
}
// HTTPRequest bad
// HttpRequest good
fn good_type_name(s string) bool {
if s.len < 4 {
return true
}
for i in 2 .. s.len {
if s[i].is_capital() && s[i - 1].is_capital() && s[i - 2].is_capital() {
return false
}
}
return true
}
pub fn (s &Scanner) error(msg string) {
println('$s.line_nr : $msg')
exit(1)
}
pub fn verror(s string) {
println('V error: $s')
2020-03-03 00:00:30 +01:00
os.flush()
2019-12-22 02:34:37 +01:00
exit(1)
}
pub fn vhash() string {
mut buf := [50]byte
buf[0] = 0
C.snprintf(charptr(buf), 50, '%s', C.V_COMMIT_HASH)
return tos_clone(buf)
}
pub fn cescaped_path(s string) string {
return s.replace('\\', '\\\\')
}