parser: move flag logic to checker (#6239)
							parent
							
								
									5526954fdc
								
							
						
					
					
						commit
						d663f57d43
					
				|  | @ -615,8 +615,10 @@ pub: | ||||||
| // #include etc
 | // #include etc
 | ||||||
| pub struct HashStmt { | pub struct HashStmt { | ||||||
| pub: | pub: | ||||||
| 	val string |  | ||||||
| 	mod string | 	mod string | ||||||
|  | 	pos token.Position | ||||||
|  | pub mut: | ||||||
|  | 	val string | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* | /* | ||||||
|  |  | ||||||
|  | @ -2171,7 +2171,9 @@ fn (mut c Checker) stmt(node ast.Stmt) { | ||||||
| 		} | 		} | ||||||
| 		ast.GotoLabel {} | 		ast.GotoLabel {} | ||||||
| 		ast.GotoStmt {} | 		ast.GotoStmt {} | ||||||
| 		ast.HashStmt {} | 		ast.HashStmt { | ||||||
|  | 			c.hash_stmt(node) | ||||||
|  | 		} | ||||||
| 		ast.Import { | 		ast.Import { | ||||||
| 			c.import_stmt(node) | 			c.import_stmt(node) | ||||||
| 		} | 		} | ||||||
|  | @ -2200,6 +2202,47 @@ fn (mut c Checker) stmt(node ast.Stmt) { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | fn (mut c Checker) hash_stmt(mut node ast.HashStmt) { | ||||||
|  | 	if c.pref.backend == .js { | ||||||
|  | 		if !c.file.path.ends_with('.js.v') { | ||||||
|  | 			c.error('Hash statements are only allowed in backend specific files such "x.js.v"', | ||||||
|  | 				node.pos) | ||||||
|  | 		} | ||||||
|  | 		if c.mod == 'main' { | ||||||
|  | 			c.error('Hash statements are not allowed in the main module. Please place them in a separate module.', | ||||||
|  | 				node.pos) | ||||||
|  | 		} | ||||||
|  | 	} else if node.val.starts_with('include') { | ||||||
|  | 		mut flag := node.val[8..] | ||||||
|  | 		if flag.contains('@VROOT') { | ||||||
|  | 			vroot := util.resolve_vroot(flag, c.file.path) or { | ||||||
|  | 				c.error(err, node.pos) | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  | 			node.val = 'include $vroot' | ||||||
|  | 		} | ||||||
|  | 	} else if node.val.starts_with('flag') { | ||||||
|  | 		// #flag linux -lm
 | ||||||
|  | 		mut flag := node.val[5..] | ||||||
|  | 		// expand `@VROOT` to its absolute path
 | ||||||
|  | 		if flag.contains('@VROOT') { | ||||||
|  | 			flag = util.resolve_vroot(flag, c.file.path) or { | ||||||
|  | 				c.error(err, node.pos) | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		for deprecated in ['@VMOD', '@VMODULE', '@VPATH', '@VLIB_PATH'] { | ||||||
|  | 			if flag.contains(deprecated) { | ||||||
|  | 				c.error('$deprecated had been deprecated, use @VROOT instead.', node.pos) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		// println('adding flag "$flag"')
 | ||||||
|  | 		c.table.parse_cflag(flag, c.mod, c.pref.compile_defines_all) or { | ||||||
|  | 			c.error(err, node.pos) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
| fn (mut c Checker) import_stmt(imp ast.Import) { | fn (mut c Checker) import_stmt(imp ast.Import) { | ||||||
| 	for sym in imp.syms { | 	for sym in imp.syms { | ||||||
| 		name := '$imp.mod\.$sym.name' | 		name := '$imp.mod\.$sym.name' | ||||||
|  |  | ||||||
|  | @ -6,7 +6,6 @@ module parser | ||||||
| import os | import os | ||||||
| import v.ast | import v.ast | ||||||
| import v.pref | import v.pref | ||||||
| import v.vmod |  | ||||||
| import v.table | import v.table | ||||||
| import vweb.tmpl | import vweb.tmpl | ||||||
| 
 | 
 | ||||||
|  | @ -17,67 +16,14 @@ const ( | ||||||
| 	supported_ccompilers = ['tinyc', 'clang', 'mingw', 'msvc', 'gcc'] | 	supported_ccompilers = ['tinyc', 'clang', 'mingw', 'msvc', 'gcc'] | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| fn (mut p Parser) resolve_vroot(flag string) string { |  | ||||||
| 	mut mcache := vmod.get_cache() |  | ||||||
| 	vmod_file_location := mcache.get_by_folder(p.file_name_dir) |  | ||||||
| 	if vmod_file_location.vmod_file.len == 0 { |  | ||||||
| 		// There was no actual v.mod file found.
 |  | ||||||
| 		p.error('To use @VROOT, you need' + ' to have a "v.mod" file in $p.file_name_dir,' + ' or in one of its parent folders.') |  | ||||||
| 	} |  | ||||||
| 	vmod_path := vmod_file_location.vmod_folder |  | ||||||
| 	if p.pref.is_fmt { |  | ||||||
| 		return flag |  | ||||||
| 	} |  | ||||||
| 	return flag.replace('@VROOT', os.real_path(vmod_path)) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // // #include, #flag, #v
 | // // #include, #flag, #v
 | ||||||
| fn (mut p Parser) hash() ast.HashStmt { | fn (mut p Parser) hash() ast.HashStmt { | ||||||
| 	mut val := p.tok.lit | 	mut val := p.tok.lit | ||||||
| 	p.next() | 	p.next() | ||||||
| 	if p.pref.backend == .js { |  | ||||||
| 		if !p.file_name.ends_with('.js.v') { |  | ||||||
| 			p.error('Hash statements are only allowed in backend specific files such "x.js.v"') |  | ||||||
| 		} |  | ||||||
| 		if p.mod == 'main' { |  | ||||||
| 			p.error('Hash statements are not allowed in the main module. Please place them in a separate module.') |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	if val.starts_with('include') { |  | ||||||
| 		mut flag := val[8..] |  | ||||||
| 		if flag.contains('@VROOT') { |  | ||||||
| 			vroot := p.resolve_vroot(flag) |  | ||||||
| 			val = 'include $vroot' |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	if val.starts_with('flag') { |  | ||||||
| 		// #flag linux -lm
 |  | ||||||
| 		mut flag := val[5..] |  | ||||||
| 		// expand `@VROOT` to its absolute path
 |  | ||||||
| 		if flag.contains('@VROOT') { |  | ||||||
| 			flag = p.resolve_vroot(flag) |  | ||||||
| 		} |  | ||||||
| 		for deprecated in ['@VMOD', '@VMODULE', '@VPATH', '@VLIB_PATH'] { |  | ||||||
| 			if flag.contains(deprecated) { |  | ||||||
| 				p.error('$deprecated had been deprecated, use @VROOT instead.') |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 		// println('adding flag "$flag"')
 |  | ||||||
| 		p.table.parse_cflag(flag, p.mod, p.pref.compile_defines_all) or { |  | ||||||
| 			p.error(err) |  | ||||||
| 		} |  | ||||||
| 		/* |  | ||||||
| 		words := val.split(' ') |  | ||||||
| 		if words.len > 1 && words[1] in supported_platforms { |  | ||||||
| 			if p.pref.os == .mac && words[1] == 'darwin' { |  | ||||||
| 				p.pref.cflags += val.after('darwin') |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 		*/ |  | ||||||
| 	} |  | ||||||
| 	return ast.HashStmt{ | 	return ast.HashStmt{ | ||||||
| 		val: val | 		val: val | ||||||
| 		mod: p.mod | 		mod: p.mod | ||||||
|  | 		pos: p.prev_tok.position() | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -5,6 +5,7 @@ module util | ||||||
| 
 | 
 | ||||||
| import os | import os | ||||||
| import v.pref | import v.pref | ||||||
|  | import v.vmod | ||||||
| 
 | 
 | ||||||
| pub const ( | pub const ( | ||||||
| 	v_version = '0.1.29' | 	v_version = '0.1.29' | ||||||
|  | @ -112,6 +113,17 @@ pub fn set_vroot_folder(vroot_path string) { | ||||||
| 	os.setenv('VEXE', os.real_path(os.join_path(vroot_path, vname)), true) | 	os.setenv('VEXE', os.real_path(os.join_path(vroot_path, vname)), true) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | pub fn resolve_vroot(str, dir string) ?string { | ||||||
|  | 	mut mcache := vmod.get_cache() | ||||||
|  | 	vmod_file_location := mcache.get_by_folder(dir) | ||||||
|  | 	if vmod_file_location.vmod_file.len == 0 { | ||||||
|  | 		// There was no actual v.mod file found.
 | ||||||
|  | 		return error('To use @VROOT, you need to have a "v.mod" file in $dir, or in one of its parent folders.') | ||||||
|  | 	} | ||||||
|  | 	vmod_path := vmod_file_location.vmod_folder | ||||||
|  | 	return str.replace('@VROOT', os.real_path(vmod_path)) | ||||||
|  | } | ||||||
|  | 
 | ||||||
| pub fn launch_tool(is_verbose bool, tool_name string, args []string) { | pub fn launch_tool(is_verbose bool, tool_name string, args []string) { | ||||||
| 	vexe := pref.vexe_path() | 	vexe := pref.vexe_path() | ||||||
| 	vroot := os.dir(vexe) | 	vroot := os.dir(vexe) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue