fmt: initial support for -debug

pull/4708/head
Delyan Angelov 2020-05-04 17:22:41 +03:00
parent 23df9b052e
commit 5eb71c4a13
5 changed files with 18 additions and 8 deletions

View File

@ -152,7 +152,7 @@ fn (foptions &FormatOptions) format_file(file string) {
parent: 0 parent: 0
}) })
// checker.check(file_ast) // checker.check(file_ast)
formatted_content := fmt.fmt(file_ast, table) formatted_content := fmt.fmt(file_ast, table, foptions.is_debug)
file_name := os.file_name(file) file_name := os.file_name(file)
vfmt_output_path := os.join_path(os.temp_dir(), 'vfmt_' + file_name) vfmt_output_path := os.join_path(os.temp_dir(), 'vfmt_' + file_name)
os.write_file(vfmt_output_path, formatted_content) os.write_file(vfmt_output_path, formatted_content)

View File

@ -9,7 +9,9 @@ Options:
-diff Display only diffs between the formatted source and the original source. -diff Display only diffs between the formatted source and the original source.
-l List files whose formatting differs from vfmt. -l List files whose formatting differs from vfmt.
-w Write result to (source) file(s) instead of to stdout. -w Write result to (source) file(s) instead of to stdout.
-2 Use the new V parser/vfmt. NB: this is EXPERIMENTAL for now. -debug Print the kinds of encountered AST statements/expressions on stderr.
The new vfmt is much faster and more forgiving.
It also may EAT some of your code for now. NB: vfmt after 2020/04/01 is based on the new AST compiler code, and
Please be careful, and make frequent BACKUPS, when running with -vfmt2 . thus is much faster, and more flexible than before.
It may also EAT some of your code for now, so please be careful, and
make frequent BACKUPS.

View File

@ -28,15 +28,17 @@ mut:
auto_imports []string // automatically inserted imports that the user forgot to specify auto_imports []string // automatically inserted imports that the user forgot to specify
import_pos int // position of the imports in the resulting string for later autoimports insertion import_pos int // position of the imports in the resulting string for later autoimports insertion
used_imports []string // to remove unused imports used_imports []string // to remove unused imports
is_debug bool
} }
pub fn fmt(file ast.File, table &table.Table) string { pub fn fmt(file ast.File, table &table.Table, is_debug bool) string {
mut f := Fmt{ mut f := Fmt{
out: strings.new_builder(1000) out: strings.new_builder(1000)
out_imports: strings.new_builder(200) out_imports: strings.new_builder(200)
table: table table: table
indent: 0 indent: 0
file: file file: file
is_debug: is_debug
} }
f.cur_mod = 'main' f.cur_mod = 'main'
for stmt in file.stmts { for stmt in file.stmts {
@ -141,6 +143,9 @@ fn (mut f Fmt) stmts(stmts []ast.Stmt) {
} }
fn (mut f Fmt) stmt(node ast.Stmt) { fn (mut f Fmt) stmt(node ast.Stmt) {
if f.is_debug {
eprintln('stmt: ${node.position():-42} | node: ${typeof(node):-20}')
}
match node { match node {
ast.AssignStmt { ast.AssignStmt {
for i, ident in it.left { for i, ident in it.left {
@ -492,6 +497,9 @@ fn (f &Fmt) type_to_str(t table.Type) string {
} }
fn (mut f Fmt) expr(node ast.Expr) { fn (mut f Fmt) expr(node ast.Expr) {
if f.is_debug {
eprintln('expr: ${node.position():-42} | node: ${typeof(node):-20} | ${node.str()}')
}
match node { match node {
ast.AnonFn { ast.AnonFn {
f.fn_decl(it.decl) f.fn_decl(it.decl)

View File

@ -45,7 +45,7 @@ fn test_fmt() {
file_ast := parser.parse_file(ipath, table, .parse_comments, &pref.Preferences{}, &ast.Scope{ file_ast := parser.parse_file(ipath, table, .parse_comments, &pref.Preferences{}, &ast.Scope{
parent: 0 parent: 0
}) })
result_ocontent := fmt.fmt(file_ast, table) result_ocontent := fmt.fmt(file_ast, table, false)
if expected_ocontent != result_ocontent { if expected_ocontent != result_ocontent {
fmt_bench.fail() fmt_bench.fail()
eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.')) eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.'))

View File

@ -47,7 +47,7 @@ fn test_fmt() {
file_ast := parser.parse_file(ipath, table, .parse_comments, &pref.Preferences{}, &ast.Scope{ file_ast := parser.parse_file(ipath, table, .parse_comments, &pref.Preferences{}, &ast.Scope{
parent: 0 parent: 0
}) })
result_ocontent := fmt.fmt(file_ast, table) result_ocontent := fmt.fmt(file_ast, table, false)
if expected_ocontent != result_ocontent { if expected_ocontent != result_ocontent {
fmt_bench.fail() fmt_bench.fail()
eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.')) eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.'))