parser: check if guard condition (#13765)

pull/13776/head
yuyi 2022-03-18 22:49:20 +08:00 committed by GitHub
parent 54b0a2aa62
commit bb2ddb98a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 0 deletions

View File

@ -107,6 +107,10 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
p.check(.decl_assign)
comments << p.eat_comments()
expr := p.expr(0)
if expr !in [ast.CallExpr, ast.IndexExpr, ast.PrefixExpr] {
p.error_with_pos('if guard condition expression is illegal, it should return optional',
expr.pos())
}
cond = ast.IfGuardExpr{
vars: vars

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/if_guard_cond_err.vv:16:16: error: if guard condition expression is illegal, it should return optional
14 | fp.usage_example('GOOG AAPL')
15 | _ := fp.bool('version', `v`, false, 'version information.')
16 | if args := fp.finalize() && args.len > 0 {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
17 | return args
18 | } else {

View File

@ -0,0 +1,28 @@
import os
import flag
const version = "v0.1.0"
// getting command line options and arguments
// returns the arguments
fn get_args() ?[]string {
mut fp := flag.new_flag_parser(os.args)
fp.application('ticker')
fp.version(version)
fp.description('A CLI yahoo ticker app')
fp.skip_executable()
fp.usage_example('GOOG AAPL')
_ := fp.bool('version', `v`, false, 'version information.')
if args := fp.finalize() && args.len > 0 {
return args
} else {
eprintln(err.msg())
println(fp.usage())
return none
}
}
fn main() {
tickers := get_args() or { return }
println(tickers)
}