2019-10-13 15:37:43 +02:00
|
|
|
module compiler
|
2019-09-29 19:37:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
os
|
|
|
|
term
|
|
|
|
)
|
2019-12-19 22:29:37 +01:00
|
|
|
// ////////////////////////////////////////////////////////////////////////////////////////////////
|
2019-09-29 19:37:39 +02:00
|
|
|
// NB: The code in this file is organized in layers (between the ///// lines).
|
|
|
|
// This allows for easier keeping in sync of error/warn functions.
|
|
|
|
// The functions in each of the layers, call the functions from the layers *below*.
|
|
|
|
// The functions in each of the layers, also have more details about the warn/error situation,
|
|
|
|
// so they can display more informative message, so please call the lowest level variant you can.
|
2019-12-19 22:29:37 +01:00
|
|
|
// ////////////////////////////////////////////////////////////////////////////////////////////////
|
2019-09-29 19:37:39 +02:00
|
|
|
// TLDR: If you have a token index, call:
|
2019-12-19 22:29:37 +01:00
|
|
|
// p.error_with_token_index(msg, token_index)
|
2019-09-29 19:37:39 +02:00
|
|
|
// ... not just :
|
2019-12-19 22:29:37 +01:00
|
|
|
// p.error(msg)
|
|
|
|
// ////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-09-29 19:37:39 +02:00
|
|
|
|
|
|
|
fn (p mut Parser) error(s string) {
|
|
|
|
// no positioning info, so just assume that the last token was the culprit:
|
2019-12-19 22:29:37 +01:00
|
|
|
p.error_with_token_index(s, p.token_idx - 1)
|
2019-09-29 19:37:39 +02:00
|
|
|
}
|
|
|
|
|
2020-01-06 00:28:29 +01:00
|
|
|
fn (p mut Parser) warn_or_error(s string) {
|
|
|
|
if p.pref.is_prod {
|
|
|
|
p.error(s)
|
|
|
|
} else {
|
|
|
|
p.warn(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-29 19:37:39 +02:00
|
|
|
fn (p mut Parser) warn(s string) {
|
2019-12-19 22:29:37 +01:00
|
|
|
p.warn_with_token_index(s, p.token_idx - 1)
|
2019-09-29 19:37:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) production_error_with_token_index(e string, tokenindex int) {
|
|
|
|
if p.pref.is_prod {
|
2019-12-19 22:29:37 +01:00
|
|
|
p.error_with_token_index(e, tokenindex)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.warn_with_token_index(e, tokenindex)
|
2019-09-29 19:37:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) error_with_token_index(s string, tokenindex int) {
|
2019-12-19 22:29:37 +01:00
|
|
|
p.error_with_position(s, p.scanner.get_scanner_pos_of_token(p.tokens[tokenindex]))
|
2019-09-29 19:37:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) warn_with_token_index(s string, tokenindex int) {
|
2019-12-19 22:29:37 +01:00
|
|
|
p.warn_with_position(s, p.scanner.get_scanner_pos_of_token(p.tokens[tokenindex]))
|
2019-09-29 19:37:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) error_with_position(s string, sp ScannerPos) {
|
|
|
|
p.print_error_context()
|
2019-12-19 22:29:37 +01:00
|
|
|
e := normalized_error(s)
|
|
|
|
p.scanner.goto_scanner_position(sp)
|
2019-09-29 19:37:39 +02:00
|
|
|
p.scanner.error_with_col(e, sp.pos - sp.last_nl_pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) warn_with_position(s string, sp ScannerPos) {
|
2019-12-17 15:28:25 +01:00
|
|
|
if p.scanner.is_fmt {
|
|
|
|
return
|
|
|
|
}
|
2019-09-29 19:37:39 +02:00
|
|
|
// on a warning, restore the scanner state after printing the warning:
|
|
|
|
cpos := p.scanner.get_scanner_pos()
|
2019-12-19 22:29:37 +01:00
|
|
|
e := normalized_error(s)
|
|
|
|
p.scanner.goto_scanner_position(sp)
|
2019-09-29 19:37:39 +02:00
|
|
|
p.scanner.warn_with_col(e, sp.pos - sp.last_nl_pos)
|
2019-12-19 22:29:37 +01:00
|
|
|
p.scanner.goto_scanner_position(cpos)
|
2019-09-29 19:37:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (s &Scanner) error(msg string) {
|
|
|
|
s.error_with_col(msg, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (s &Scanner) warn(msg string) {
|
|
|
|
s.warn_with_col(msg, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (s &Scanner) warn_with_col(msg string, col int) {
|
2019-12-05 16:55:27 +01:00
|
|
|
fullpath := s.get_error_filepath()
|
2019-09-29 19:37:39 +02:00
|
|
|
color_on := s.is_color_output_on()
|
2019-12-19 22:29:37 +01:00
|
|
|
final_message := if color_on { term.bold(term.bright_blue(msg)) } else { msg }
|
2019-09-29 19:37:39 +02:00
|
|
|
eprintln('warning: ${fullpath}:${s.line_nr+1}:${col}: $final_message')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (s &Scanner) error_with_col(msg string, col int) {
|
2019-12-05 16:55:27 +01:00
|
|
|
fullpath := s.get_error_filepath()
|
2019-09-29 19:37:39 +02:00
|
|
|
color_on := s.is_color_output_on()
|
2019-12-19 22:29:37 +01:00
|
|
|
final_message := if color_on { term.red(term.bold(msg)) } else { msg }
|
2019-09-29 19:37:39 +02:00
|
|
|
// The filepath:line:col: format is the default C compiler
|
|
|
|
// error output format. It allows editors and IDE's like
|
|
|
|
// emacs to quickly find the errors in the output
|
|
|
|
// and jump to their source with a keyboard shortcut.
|
2019-10-01 14:39:19 +02:00
|
|
|
// NB: using only the filename may lead to inability of IDE/editors
|
|
|
|
// to find the source file, when the IDE has a different working folder than v itself.
|
2019-09-29 19:37:39 +02:00
|
|
|
eprintln('${fullpath}:${s.line_nr + 1}:${col}: $final_message')
|
2019-12-18 06:13:31 +01:00
|
|
|
if s.print_line_on_error && s.nlines > 0 {
|
2019-12-19 22:29:37 +01:00
|
|
|
context_start_line := imax(0, (s.line_nr - error_context_before))
|
|
|
|
context_end_line := imin(s.nlines - 1, (s.line_nr + error_context_after + 1))
|
2019-09-29 19:37:39 +02:00
|
|
|
for cline := context_start_line; cline < context_end_line; cline++ {
|
2019-12-19 22:29:37 +01:00
|
|
|
line := '${(cline+1):5d}| ' + s.line(cline)
|
2019-09-29 19:37:39 +02:00
|
|
|
coloredline := if cline == s.line_nr && color_on { term.red(line) } else { line }
|
2019-12-19 22:29:37 +01:00
|
|
|
eprintln(coloredline)
|
|
|
|
if cline != s.line_nr {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-29 19:37:39 +02:00
|
|
|
// The pointerline should have the same spaces/tabs as the offending
|
|
|
|
// line, so that it prints the ^ character exactly on the *same spot*
|
|
|
|
// where it is needed. That is the reason we can not just
|
|
|
|
// use strings.repeat(` `, col) to form it.
|
2019-12-05 16:55:27 +01:00
|
|
|
mut pointerline := []string
|
2019-12-19 22:29:37 +01:00
|
|
|
for i, c in line {
|
2019-09-29 19:37:39 +02:00
|
|
|
if i < col {
|
|
|
|
x := if c.is_space() { c } else { ` ` }
|
|
|
|
pointerline << x.str()
|
|
|
|
continue
|
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
pointerline << if color_on { term.bold(term.blue('^')) } else { '^' }
|
2019-09-29 19:37:39 +02:00
|
|
|
break
|
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
eprintln(' ' + pointerline.join(''))
|
2019-09-29 19:37:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
// ////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// / Misc error helper functions, can be called by any of the functions above
|
|
|
|
[inline]
|
|
|
|
fn (p &Parser) cur_tok_index() int {
|
|
|
|
return p.token_idx - 1
|
|
|
|
}
|
2019-09-29 19:37:39 +02:00
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
[inline]
|
|
|
|
fn imax(a, b int) int {
|
|
|
|
return if a > b { a } else { b }
|
|
|
|
}
|
2019-09-29 19:37:39 +02:00
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
[inline]
|
|
|
|
fn imin(a, b int) int {
|
|
|
|
return if a < b { a } else { b }
|
|
|
|
}
|
2019-09-29 19:37:39 +02:00
|
|
|
|
2019-10-01 14:39:19 +02:00
|
|
|
fn (s &Scanner) get_error_filepath() string {
|
2019-10-30 10:15:33 +01:00
|
|
|
verror_paths_override := os.getenv('VERROR_PATHS')
|
|
|
|
use_relative_paths := match verror_paths_override {
|
2019-12-19 22:29:37 +01:00
|
|
|
'relative'{
|
|
|
|
true
|
|
|
|
}
|
|
|
|
'absolute'{
|
|
|
|
false
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s.print_rel_paths_on_error}}
|
2019-10-30 10:15:33 +01:00
|
|
|
if use_relative_paths {
|
2020-03-07 22:26:26 +01:00
|
|
|
workdir := os.getwd() + os.path_separator
|
2019-10-19 11:03:04 +02:00
|
|
|
if s.file_path.starts_with(workdir) {
|
2019-12-19 22:29:37 +01:00
|
|
|
return s.file_path.replace(workdir, '')
|
2019-10-19 11:03:04 +02:00
|
|
|
}
|
2019-10-01 14:39:19 +02:00
|
|
|
return s.file_path
|
|
|
|
}
|
2020-03-20 16:41:18 +01:00
|
|
|
return os.real_path(s.file_path)
|
2019-10-01 14:39:19 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 19:37:39 +02:00
|
|
|
fn (s &Scanner) is_color_output_on() bool {
|
2019-12-18 06:13:31 +01:00
|
|
|
return s.print_colored_error && term.can_show_color_on_stderr()
|
2019-09-29 19:37:39 +02:00
|
|
|
}
|
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
fn (p mut Parser) print_error_context() {
|
2019-09-29 19:37:39 +02:00
|
|
|
// Dump all vars and types for debugging
|
|
|
|
if p.pref.is_debug {
|
|
|
|
// os.write_to_file('/var/tmp/lang.types', '')//pes(p.table.types))
|
|
|
|
os.write_file('fns.txt', p.table.debug_fns())
|
|
|
|
}
|
2020-03-06 18:53:29 +01:00
|
|
|
if p.pref.verbosity.is_higher_or_equal(.level_three) {
|
2019-09-29 19:37:39 +02:00
|
|
|
println('pass=$p.pass fn=`$p.cur_fn.name`\n')
|
|
|
|
}
|
|
|
|
p.cgen.save()
|
|
|
|
// V up hint
|
|
|
|
cur_path := os.getwd()
|
2019-12-19 22:29:37 +01:00
|
|
|
if !p.pref.is_repl && !p.pref.is_test && (p.file_path.contains('v/compiler') || cur_path.contains('v/compiler')) {
|
2019-09-29 19:37:39 +02:00
|
|
|
println('\n=========================')
|
|
|
|
println('It looks like you are building V. It is being frequently updated every day.')
|
2019-12-19 22:29:37 +01:00
|
|
|
println("If you didn\'t modify V\'s code, most likely there was a change that ")
|
2019-09-29 19:37:39 +02:00
|
|
|
println('lead to this error.')
|
|
|
|
println('\nRun `v up`, that will most likely fix it.')
|
2019-12-19 22:29:37 +01:00
|
|
|
// println('\nIf this doesn\'t help, re-install V from source or download a precompiled' + ' binary from\nhttps://vlang.io.')
|
|
|
|
println("\nIf this doesn\'t help, please create a GitHub issue.")
|
2019-09-29 19:37:39 +02:00
|
|
|
println('=========================\n')
|
|
|
|
}
|
|
|
|
if p.pref.is_debug {
|
|
|
|
print_backtrace()
|
|
|
|
}
|
|
|
|
// p.scanner.debug_tokens()
|
|
|
|
}
|
|
|
|
|
2020-01-12 01:46:25 +01:00
|
|
|
fn ienv_default(ename string, idefault int) int {
|
|
|
|
es := os.getenv(ename)
|
|
|
|
if es.len == 0 { return idefault }
|
|
|
|
return es.int()
|
|
|
|
}
|
|
|
|
|
|
|
|
// print_current_tokens/1 pretty prints the current token context, like this:
|
|
|
|
// // Your label: tokens[ 32] = Token{ .line: 8, .pos: 93, .tok: 85 } = mut
|
|
|
|
// // Your label: tokens[> 33] = Token{ .line: 8, .pos: 95, .tok: 1 } = b
|
|
|
|
// // Your label: tokens[ 34] = Token{ .line: 8, .pos: 98, .tok: 31 } = :=
|
|
|
|
// It is useful while debugging the v compiler itself. > marks p.token_idx
|
|
|
|
fn (p &Parser) print_current_tokens(label string){
|
|
|
|
btokens := ienv_default('V_BTOKENS', 5)
|
|
|
|
atokens := ienv_default('V_ATOKENS', 5)
|
|
|
|
ctoken_idx := p.token_idx
|
|
|
|
stoken_idx := imax(0, ctoken_idx - btokens)
|
|
|
|
etoken_idx := imin( ctoken_idx + atokens + 1, p.tokens.len)
|
|
|
|
for i := stoken_idx; i < etoken_idx; i++ {
|
|
|
|
idx := if i == ctoken_idx {
|
|
|
|
'>${i:3d}'
|
|
|
|
} else {
|
|
|
|
' ${i:3d}'
|
|
|
|
}
|
|
|
|
eprintln('$label: tokens[$idx] = ' + p.tokens[ i ].detailed_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 16:55:27 +01:00
|
|
|
fn normalized_error(s string) string {
|
2019-12-30 10:48:08 +01:00
|
|
|
mut res := s
|
|
|
|
if !res.contains('__') {
|
|
|
|
// `[]int` instead of `array_int`
|
|
|
|
res = res.replace('array_', '[]')
|
|
|
|
}
|
|
|
|
res = res.replace('__', '.')
|
|
|
|
res = res.replace('Option_', '?')
|
|
|
|
res = res.replace('main.', '')
|
|
|
|
res = res.replace('ptr_', '&')
|
|
|
|
res = res.replace('_dot_', '.')
|
2019-12-05 16:55:27 +01:00
|
|
|
if res.contains('_V_MulRet_') {
|
2019-12-30 10:48:08 +01:00
|
|
|
res = res.replace('_V_MulRet_', '(')
|
|
|
|
res = res.replace('_V_', ', ')
|
|
|
|
res = res[..res.len - 1] + ')"' //"// quote balance comment. do not remove
|
2019-12-05 16:55:27 +01:00
|
|
|
}
|
|
|
|
return res
|
2019-09-29 19:37:39 +02:00
|
|
|
}
|
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
// ////////////////////////////////////////////////////////////////////////////////////////////////
|
2019-09-29 19:37:39 +02:00
|
|
|
// The goal of ScannerPos is to track the current scanning position,
|
|
|
|
// so that if there is an error found later, v could show a more accurate
|
|
|
|
// position about where the error initially was.
|
|
|
|
// NB: The fields of ScannerPos *should be kept synchronized* with the
|
|
|
|
// corresponding fields in Scanner.
|
|
|
|
struct ScannerPos {
|
|
|
|
mut:
|
2019-12-19 22:29:37 +01:00
|
|
|
pos int
|
|
|
|
line_nr int
|
|
|
|
last_nl_pos int
|
2019-09-29 19:37:39 +02:00
|
|
|
}
|
|
|
|
|
2019-12-11 01:24:26 +01:00
|
|
|
pub fn (s ScannerPos) str() string {
|
2019-09-29 19:37:39 +02:00
|
|
|
return 'ScannerPos{ ${s.pos:5d} , ${s.line_nr:5d} , ${s.last_nl_pos:5d} }'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (s &Scanner) get_scanner_pos() ScannerPos {
|
2019-12-19 22:29:37 +01:00
|
|
|
return ScannerPos{
|
|
|
|
pos: s.pos
|
|
|
|
line_nr: s.line_nr
|
|
|
|
last_nl_pos: s.last_nl_pos
|
|
|
|
}
|
2019-09-29 19:37:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (s mut Scanner) goto_scanner_position(scp ScannerPos) {
|
|
|
|
s.pos = scp.pos
|
|
|
|
s.line_nr = scp.line_nr
|
|
|
|
s.last_nl_pos = scp.last_nl_pos
|
|
|
|
}
|
|
|
|
|
2019-12-06 13:24:53 +01:00
|
|
|
fn (s &Scanner) get_last_nl_from_pos(_pos int) int {
|
2019-12-19 22:29:37 +01:00
|
|
|
pos := if _pos >= s.text.len { s.text.len - 1 } else { _pos }
|
2019-11-23 17:31:28 +01:00
|
|
|
for i := pos; i >= 0; i-- {
|
|
|
|
if s.text[i] == `\n` {
|
|
|
|
return i
|
2019-09-29 19:37:39 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-23 17:31:28 +01:00
|
|
|
return 0
|
2019-09-29 19:37:39 +02:00
|
|
|
}
|
|
|
|
|
2019-12-06 13:24:53 +01:00
|
|
|
fn (s &Scanner) get_scanner_pos_of_token(tok &Token) ScannerPos {
|
2019-11-23 17:31:28 +01:00
|
|
|
return ScannerPos{
|
|
|
|
pos: tok.pos
|
|
|
|
line_nr: tok.line_nr
|
|
|
|
last_nl_pos: s.get_last_nl_from_pos(tok.pos)
|
|
|
|
}
|
2019-09-29 19:37:39 +02:00
|
|
|
}
|
2019-10-22 23:56:25 +02:00
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
// /////////////////////////////
|
2019-11-10 19:54:28 +01:00
|
|
|
fn (p mut Parser) mutable_arg_error(i int, arg Var, f Fn) {
|
2019-12-19 22:29:37 +01:00
|
|
|
mut dots_example := 'mut $p.lit'
|
2019-11-10 19:54:28 +01:00
|
|
|
if i > 0 {
|
|
|
|
dots_example = '.., ' + dots_example
|
|
|
|
}
|
|
|
|
if i < f.args.len - 1 {
|
|
|
|
dots_example = dots_example + ',..'
|
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
p.error('`$arg.name` is a mutable argument, you need to provide `mut`: ' + '`$f.name ($dots_example)`')
|
2019-12-05 16:55:27 +01:00
|
|
|
}
|
2019-11-10 19:54:28 +01:00
|
|
|
|
2019-10-22 23:56:25 +02:00
|
|
|
const (
|
2019-12-19 22:29:37 +01:00
|
|
|
warn_match_arrow = '=> is no longer needed in match statements, use\n' + 'match foo {
|
2019-10-22 23:56:25 +02:00
|
|
|
1 { bar }
|
|
|
|
2 { baz }
|
|
|
|
else { ... }
|
|
|
|
}'
|
2019-12-19 22:29:37 +01:00
|
|
|
// make_receiver_mutable =
|
2019-11-04 12:35:10 +01:00
|
|
|
err_used_as_value = 'used as value'
|
2019-12-19 22:29:37 +01:00
|
|
|
and_or_error = 'use `()` to make the boolean expression clear\n' + 'for example: `(a && b) || c` instead of `a && b || c`'
|
|
|
|
err_modify_bitfield = 'to modify a bitfield flag use the methods: set, clear, toggle. and to check for flag use: has'
|
2019-10-22 23:56:25 +02:00
|
|
|
)
|