tools: add --check flag to v ast (#23938)

This commit is contained in:
kbkpbot 2025-03-15 17:54:07 +08:00 committed by GitHub
parent c69b125162
commit d16aa2547b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,6 +8,7 @@ import v.parser
import v.ast import v.ast
import v.pref import v.pref
import v.errors import v.errors
import v.checker
import strings import strings
struct Context { struct Context {
@ -17,6 +18,7 @@ mut:
is_print bool is_print bool
is_terse bool is_terse bool
is_skip_defaults bool is_skip_defaults bool
check bool
hide_names map[string]bool hide_names map[string]bool
} }
@ -43,6 +45,7 @@ fn main() {
ctx.is_compile = fp.bool('compile', `c`, false, 'watch the .v file for changes, rewrite the .json file, *AND* generate a .c file too on any change') ctx.is_compile = fp.bool('compile', `c`, false, 'watch the .v file for changes, rewrite the .json file, *AND* generate a .c file too on any change')
ctx.is_terse = fp.bool('terse', `t`, false, 'terse output, only with tree node names (AST structure), no details') ctx.is_terse = fp.bool('terse', `t`, false, 'terse output, only with tree node names (AST structure), no details')
ctx.is_skip_defaults = fp.bool('skip-defaults', `s`, false, 'skip properties that have default values like false, 0, "", etc') ctx.is_skip_defaults = fp.bool('skip-defaults', `s`, false, 'skip properties that have default values like false, 0, "", etc')
ctx.check = fp.bool('check', `k`, false, 'run v.checker as well (it may modify the AST)')
hfields := fp.string_multi('hide', 0, 'hide the specified fields. You can give several, by separating them with `,`').join(',') hfields := fp.string_multi('hide', 0, 'hide the specified fields. You can give several, by separating them with `,`').join(',')
for hf in hfields.split(',') { for hf in hfields.split(',') {
ctx.hide_names[hf] = true ctx.hide_names[hf] = true
@ -170,7 +173,12 @@ fn json(file string) string {
pref: pref_ pref: pref_
} }
// parse file with comment // parse file with comment
ast_file := parser.parse_file(file, mut t.table, .parse_comments, t.pref) mut ast_file := parser.parse_file(file, mut t.table, .parse_comments, t.pref)
if context.check {
mut the_checker := checker.new_checker(t.table, pref_)
the_checker.check(mut ast_file)
}
t.root = t.ast_file(ast_file) t.root = t.ast_file(ast_file)
// generate the ast string // generate the ast string
s := json_print(mut t.root) s := json_print(mut t.root)