@@ -9,7 +9,8 @@ import v.pref
99import v.token
1010
1111const (
12- supported_comptime_calls = ['html' , 'tmpl' , 'env' , 'embed_file' , 'pkgconfig' ]
12+ supported_comptime_calls = ['html' , 'tmpl' , 'env' , 'embed_file' , 'pkgconfig' , 'compile_error' ,
13+ 'compile_warn' ]
1314 comptime_types = ['Map' , 'Array' , 'Int' , 'Float' , 'Struct' , 'Interface' , 'Enum' ,
1415 'Sumtype' ]
1516)
@@ -85,9 +86,9 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
8586 err_node := ast.ComptimeCall{
8687 scope: 0
8788 }
89+ start_pos := p.tok.pos ()
8890 p.check (.dollar)
89- start_pos := p.prev_tok.pos ()
90- error_msg := 'only `\$ tmpl()`, `\$ env()`, `\$ embed_file()`, `\$ pkgconfig()` and `\$ vweb.html()` comptime functions are supported right now'
91+ error_msg := 'only `\$ tmpl()`, `\$ env()`, `\$ embed_file()`, `\$ pkgconfig()`, `\$ vweb.html()`, `\$ compile_error()` and `\$ compile_warn()` comptime functions are supported right now'
9192 if p.peek_tok.kind == .dot {
9293 name := p.check_name () // skip `vweb.html()` TODO
9394 if name != 'vweb' {
@@ -96,7 +97,7 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
9697 }
9798 p.check (.dot)
9899 }
99- method_name := p.check_name () // (.name)
100+ method_name := p.check_name ()
100101 if method_name ! in parser.supported_comptime_calls {
101102 p.error (error_msg)
102103 return err_node
@@ -105,31 +106,18 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
105106 is_html := method_name == 'html'
106107 // $env('ENV_VAR_NAME')
107108 p.check (.lpar)
108- spos := p.tok.pos ()
109- if method_name == 'env' {
110- s := p.tok.lit
111- p.check (.string)
112- p.check (.rpar)
113- return ast.ComptimeCall{
114- scope: 0
115- method_name: method_name
116- args_var: s
117- is_env: true
118- env_pos: spos
119- pos: spos.extend (p.prev_tok.pos ())
120- }
121- }
122- if method_name == 'pkgconfig' {
109+ if method_name in ['env' , 'pkgconfig' , 'compile_error' , 'compile_warn' ] {
123110 s := p.tok.lit
124111 p.check (.string)
125112 p.check (.rpar)
126113 return ast.ComptimeCall{
127114 scope: 0
128115 method_name: method_name
129116 args_var: s
130- is_pkgconfig: true
131- env_pos: spos
132- pos: spos.extend (p.prev_tok.pos ())
117+ is_env: method_name == 'env'
118+ is_pkgconfig: method_name == 'pkgconfig'
119+ env_pos: start_pos
120+ pos: start_pos.extend (p.prev_tok.pos ())
133121 }
134122 }
135123 literal_string_param := if is_html { '' } else { p.tok.lit }
@@ -152,7 +140,7 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
152140 // Validate that the epath exists, and that it is actually a file.
153141 if epath == '' {
154142 p.error_with_pos ('supply a valid relative or absolute file path to the file to embed' ,
155- spos )
143+ start_pos )
156144 return err_node
157145 }
158146 if ! p.pref.is_fmt {
@@ -163,12 +151,12 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
163151 epath = os.real_path (os.join_path_single (os.dir (p.file_name), epath))
164152 if ! os.exists (epath) {
165153 p.error_with_pos ('"$epath " does not exist so it cannot be embedded' ,
166- spos )
154+ start_pos )
167155 return err_node
168156 }
169157 if ! os.is_file (epath) {
170158 p.error_with_pos ('"$epath " is not a file so it cannot be embedded' ,
171- spos )
159+ start_pos )
172160 return err_node
173161 }
174162 } else {
0 commit comments