compiler: add output mode for errors and warnings, support for `-silent` flag
							parent
							
								
									01de1b6375
								
							
						
					
					
						commit
						5f0ad0f562
					
				|  | @ -124,6 +124,9 @@ fn parse_args(args []string) (&pref.Preferences, string) { | ||||||
| 			'-v' { | 			'-v' { | ||||||
| 				res.is_verbose = true | 				res.is_verbose = true | ||||||
| 			} | 			} | ||||||
|  | 			'-silent' { | ||||||
|  | 				res.output_mode = .silent | ||||||
|  | 			} | ||||||
| 			'-cg' { | 			'-cg' { | ||||||
| 				res.is_debug = true | 				res.is_debug = true | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|  | @ -5,6 +5,7 @@ module ast | ||||||
| 
 | 
 | ||||||
| import v.token | import v.token | ||||||
| import v.table | import v.table | ||||||
|  | import v.errors | ||||||
| 
 | 
 | ||||||
| pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl | pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl | ||||||
| 
 | 
 | ||||||
|  | @ -306,6 +307,8 @@ pub: | ||||||
| 	global_scope &Scope | 	global_scope &Scope | ||||||
| pub mut: | pub mut: | ||||||
| 	imports      []Import | 	imports      []Import | ||||||
|  | 	errors 	     []errors.Error | ||||||
|  | 	warnings     []errors.Warning | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub struct IdentFn { | pub struct IdentFn { | ||||||
|  |  | ||||||
|  | @ -211,6 +211,14 @@ pub fn (b Builder) find_module_path(mod, fpath string) ?string { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn (b &Builder) print_warnings_and_errors() { | fn (b &Builder) print_warnings_and_errors() { | ||||||
|  | 	if b.pref.output_mode == .silent { | ||||||
|  | 		if b.checker.nr_errors > 0 { | ||||||
|  | 			exit(1) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	if b.pref.is_verbose && b.checker.nr_warnings > 1 { | 	if b.pref.is_verbose && b.checker.nr_warnings > 1 { | ||||||
| 		println('$b.checker.nr_warnings warnings') | 		println('$b.checker.nr_warnings warnings') | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -2204,21 +2204,25 @@ fn (mut c Checker) warn_or_error(message string, pos token.Position, warn bool) | ||||||
| 	// }
 | 	// }
 | ||||||
| 	if warn { | 	if warn { | ||||||
| 		c.nr_warnings++ | 		c.nr_warnings++ | ||||||
| 		c.warnings << errors.Warning{ | 		wrn := errors.Warning{ | ||||||
| 			reporter: errors.Reporter.checker | 			reporter: errors.Reporter.checker | ||||||
| 			pos: pos | 			pos: pos | ||||||
| 			file_path: c.file.path | 			file_path: c.file.path | ||||||
| 			message: message | 			message: message | ||||||
| 		} | 		} | ||||||
|  | 		c.file.warnings << wrn | ||||||
|  | 		c.warnings << wrn | ||||||
| 	} else { | 	} else { | ||||||
| 		c.nr_errors++ | 		c.nr_errors++ | ||||||
| 		if pos.line_nr !in c.error_lines { | 		if pos.line_nr !in c.error_lines { | ||||||
| 			c.errors << errors.Error{ | 			err := errors.Error{ | ||||||
| 				reporter: errors.Reporter.checker | 				reporter: errors.Reporter.checker | ||||||
| 				pos: pos | 				pos: pos | ||||||
| 				file_path: c.file.path | 				file_path: c.file.path | ||||||
| 				message: message | 				message: message | ||||||
| 			} | 			} | ||||||
|  | 			c.file.errors << err | ||||||
|  | 			c.errors << err | ||||||
| 			c.error_lines << pos.line_nr | 			c.error_lines << pos.line_nr | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -9,6 +9,7 @@ import v.token | ||||||
| import v.table | import v.table | ||||||
| import v.pref | import v.pref | ||||||
| import v.util | import v.util | ||||||
|  | import v.errors | ||||||
| import term | import term | ||||||
| import os | import os | ||||||
| 
 | 
 | ||||||
|  | @ -43,6 +44,8 @@ mut: | ||||||
| 	inside_match_case bool // to separate `match_expr { }` from `Struct{}`
 | 	inside_match_case bool // to separate `match_expr { }` from `Struct{}`
 | ||||||
| 	is_stmt_ident     bool // true while the beginning of a statement is an ident/selector
 | 	is_stmt_ident     bool // true while the beginning of a statement is an ident/selector
 | ||||||
| 	expecting_type    bool // `is Type`, expecting type
 | 	expecting_type    bool // `is Type`, expecting type
 | ||||||
|  | 	errors            []errors.Error | ||||||
|  | 	warnings          []errors.Warning | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // for tests
 | // for tests
 | ||||||
|  | @ -79,6 +82,8 @@ pub fn parse_file(path string, b_table &table.Table, comments_mode scanner.Comme | ||||||
| 			start_pos: 0 | 			start_pos: 0 | ||||||
| 			parent: 0 | 			parent: 0 | ||||||
| 		} | 		} | ||||||
|  | 		errors: []errors.Error{}, | ||||||
|  | 		warnings: []errors.Warning{}, | ||||||
| 		global_scope: global_scope | 		global_scope: global_scope | ||||||
| 	} | 	} | ||||||
| 	// comments_mode: comments_mode
 | 	// comments_mode: comments_mode
 | ||||||
|  | @ -121,7 +126,9 @@ pub fn parse_file(path string, b_table &table.Table, comments_mode scanner.Comme | ||||||
| 		imports: p.ast_imports | 		imports: p.ast_imports | ||||||
| 		stmts: stmts | 		stmts: stmts | ||||||
| 		scope: p.scope | 		scope: p.scope | ||||||
| 		global_scope: p.global_scope | 		global_scope: p.global_scope, | ||||||
|  | 		errors: p.errors, | ||||||
|  | 		warnings: p.warnings | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -549,28 +556,46 @@ fn (mut p Parser) range_expr(low ast.Expr) ast.Expr { | ||||||
| 	return node | 	return node | ||||||
| } | } | ||||||
| */ | */ | ||||||
| pub fn (p &Parser) error(s string) { | pub fn (mut p Parser) error(s string) { | ||||||
| 	p.error_with_pos(s, p.tok.position()) | 	p.error_with_pos(s, p.tok.position()) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub fn (p &Parser) warn(s string) { | pub fn (mut p Parser) warn(s string) { | ||||||
| 	p.warn_with_pos(s, p.tok.position()) | 	p.warn_with_pos(s, p.tok.position()) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub fn (p &Parser) error_with_pos(s string, pos token.Position) { | pub fn (mut p Parser) error_with_pos(s string, pos token.Position) { | ||||||
| 	mut kind := 'error:' | 	mut kind := 'error:' | ||||||
| 	if p.pref.is_verbose { | 	if p.pref.output_mode == .stdout { | ||||||
| 		print_backtrace() | 		if p.pref.is_verbose { | ||||||
| 		kind = 'parser error:' | 			print_backtrace() | ||||||
|  | 			kind = 'parser error:' | ||||||
|  | 		} | ||||||
|  | 		ferror := util.formatted_error(kind, s, p.file_name, pos) | ||||||
|  | 		eprintln(ferror) | ||||||
|  | 		exit(1) | ||||||
|  | 	} else { | ||||||
|  | 		p.errors << errors.Error{ | ||||||
|  | 			file_path: p.file_name, | ||||||
|  | 			pos: pos, | ||||||
|  | 			reporter: .parser, | ||||||
|  | 			message: s | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 	ferror := util.formatted_error(kind, s, p.file_name, pos) |  | ||||||
| 	eprintln(ferror) |  | ||||||
| 	exit(1) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub fn (p &Parser) warn_with_pos(s string, pos token.Position) { | pub fn (mut p Parser) warn_with_pos(s string, pos token.Position) { | ||||||
| 	ferror := util.formatted_error('warning:', s, p.file_name, pos) | 	if p.pref.output_mode == .stdout { | ||||||
| 	eprintln(ferror) | 		ferror := util.formatted_error('warning:', s, p.file_name, pos) | ||||||
|  | 		eprintln(ferror) | ||||||
|  | 	} else { | ||||||
|  | 		p.warnings << errors.Warning{ | ||||||
|  | 			file_path: p.file_name, | ||||||
|  | 			pos: pos, | ||||||
|  | 			reporter: .parser, | ||||||
|  | 			message: s | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub fn (mut p Parser) parse_ident(is_c, is_js bool) ast.Ident { | pub fn (mut p Parser) parse_ident(is_c, is_js bool) ast.Ident { | ||||||
|  |  | ||||||
|  | @ -12,6 +12,11 @@ pub enum BuildMode { | ||||||
| 	build_module | 	build_module | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | pub enum OutputMode { | ||||||
|  | 	stdout | ||||||
|  | 	silent | ||||||
|  | } | ||||||
|  | 
 | ||||||
| pub enum Backend { | pub enum Backend { | ||||||
| 	c            // The (default) C backend
 | 	c            // The (default) C backend
 | ||||||
| 	js           // The JavaScript backend
 | 	js           // The JavaScript backend
 | ||||||
|  | @ -23,6 +28,7 @@ pub mut: | ||||||
| 	os                  OS   // the OS to compile for
 | 	os                  OS   // the OS to compile for
 | ||||||
| 	backend             Backend | 	backend             Backend | ||||||
| 	build_mode          BuildMode | 	build_mode          BuildMode | ||||||
|  | 	output_mode         OutputMode = .stdout | ||||||
| 	//verbosity           VerboseLevel
 | 	//verbosity           VerboseLevel
 | ||||||
| 	is_verbose bool | 	is_verbose bool | ||||||
| 	// nofmt            bool   // disable vfmt
 | 	// nofmt            bool   // disable vfmt
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue