mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parser: add p.show()
to ease debugging parser problems, that need more context
Using it, produces output like this on stdout: ``` >> vlib/v/tests/typeof_type_test.v:4: keyword `typeof` token `(` token `[` keyword `fn` token `(` name `s` name `string` >> vlib/v/tests/typeof_type_test.v:8: keyword `typeof` token `(` token `[` name `int` token `]` token `)` token `.` >> vlib/v/tests/typeof_type_test.v:9: keyword `typeof` token `(` token `[` name `int` token `]` token `)` token `.` >> vlib/v/tests/typeof_type_test.v:13: keyword `typeof` token `(` token `[` name `u32` token `]` token `)` token `.` >> vlib/v/tests/typeof_type_test.v:14: keyword `typeof` token `(` token `[` name `u32` token `]` token `)` token `.` >> vlib/v/tests/typeof_type_test.v:18: keyword `typeof` token `(` token `[` name `string` token `]` token `)` token `.` ``` Note: this tracing output is *deliberately on `stdout`*, instead of `stderr`, so that you can filter it more easily, without saving it to a file first, i.e. you can use it while developing/debugging parser issues like this: `./v -o vnew cmd/v && ./vnew some_file.v | grep some_file.v` -> this will show only the parsing context for the constructs you are debugging, for that specific file *only*, instead of for all of `builtin` and the imported files etc.
This commit is contained in:
parent
cf3dda2a58
commit
50b73abc62
@ -21,6 +21,7 @@ mut:
|
||||
file_base string // "hello.v"
|
||||
file_name string // "/home/user/hello.v"
|
||||
file_name_dir string // "/home/user"
|
||||
file_display_path string // just "hello.v", when your current folder for the compilation is "/home/user/", otherwise the full path "/home/user/hello.v"
|
||||
unique_prefix string // a hash of p.file_name, used for making anon fn generation unique
|
||||
file_backend_mode ast.Language // .c for .c.v|.c.vv|.c.vsh files; .js for .js.v files, .amd64/.rv32/other arches for .amd64.v/.rv32.v/etc. files, .v otherwise.
|
||||
comments_mode scanner.CommentsMode = .skip_comments
|
||||
@ -178,10 +179,15 @@ pub fn (mut p Parser) free_scanner() {
|
||||
}
|
||||
}
|
||||
|
||||
const normalised_working_folder = (os.real_path(os.getwd()) + os.path_separator).replace('\\',
|
||||
'/')
|
||||
|
||||
pub fn (mut p Parser) set_path(path string) {
|
||||
p.file_name = path
|
||||
p.file_base = os.base(path)
|
||||
p.file_name_dir = os.dir(path)
|
||||
p.file_display_path = os.real_path(p.file_name).replace_once(parser.normalised_working_folder,
|
||||
'').replace('\\', '/')
|
||||
p.inside_vlib_file = p.file_name_dir.contains('vlib')
|
||||
p.inside_test_file = p.file_base.ends_with('_test.v') || p.file_base.ends_with('_test.vv')
|
||||
|| p.file_base.all_before_last('.v').all_before_last('.').ends_with('_test')
|
||||
@ -4167,3 +4173,23 @@ fn (mut p Parser) trace(fbase string, message string) {
|
||||
println('> p.trace | ${fbase:-10s} | ${message}')
|
||||
}
|
||||
}
|
||||
|
||||
[params]
|
||||
struct ParserShowParams {
|
||||
msg string
|
||||
reach int = 3
|
||||
}
|
||||
|
||||
fn (mut p Parser) show(params ParserShowParams) {
|
||||
mut context := []string{}
|
||||
for i in -params.reach .. params.reach + 1 {
|
||||
x := p.peek_token(i).str()
|
||||
if i == 0 {
|
||||
context << ' ${x:-30s} '
|
||||
continue
|
||||
}
|
||||
context << x
|
||||
}
|
||||
location := '${p.file_display_path}:${p.tok.line_nr}:'
|
||||
println('>> ${location:-40s} ${params.msg} ${context.join(' ')}')
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user