parser: small fixes to make silent mod work with example/json (#7246)

pull/7262/head
Daniel Däschle 2020-12-11 06:55:39 +01:00 committed by GitHub
parent a2f7e0636d
commit 8823430c71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 3 deletions

View File

@ -610,3 +610,4 @@ jobs:
./v test-parser examples/hanoi.v ./v test-parser examples/hanoi.v
./v test-parser examples/fibonacci.v ./v test-parser examples/fibonacci.v
./v test-parser examples/cli.v ./v test-parser examples/cli.v
./v test-parser examples/json.v

View File

@ -264,7 +264,7 @@ fn (mut c Checker) check_file_in_main(file ast.File) bool {
} }
fn (mut c Checker) check_valid_snake_case(name string, identifier string, pos token.Position) { fn (mut c Checker) check_valid_snake_case(name string, identifier string, pos token.Position) {
if !c.pref.is_vweb && (name[0] == `_` || name.contains('._')) { if !c.pref.is_vweb && name.len > 0 && (name[0] == `_` || name.contains('._')) {
c.error('$identifier `$name` cannot start with `_`', pos) c.error('$identifier `$name` cannot start with `_`', pos)
} }
if !c.pref.experimental && !c.pref.translated && util.contains_capital(name) { if !c.pref.experimental && !c.pref.translated && util.contains_capital(name) {
@ -280,7 +280,7 @@ fn stripped_name(name string) string {
fn (mut c Checker) check_valid_pascal_case(name string, identifier string, pos token.Position) { fn (mut c Checker) check_valid_pascal_case(name string, identifier string, pos token.Position) {
sname := stripped_name(name) sname := stripped_name(name)
if !sname[0].is_capital() && !c.pref.translated { if sname.len > 0 && !sname[0].is_capital() && !c.pref.translated {
c.error('$identifier `$name` must begin with capital letter', pos) c.error('$identifier `$name` must begin with capital letter', pos)
} }
} }
@ -1391,7 +1391,7 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
// println(fn_name) // println(fn_name)
// } // }
if fn_name == 'json.encode' { if fn_name == 'json.encode' {
} else if fn_name == 'json.decode' { } else if fn_name == 'json.decode' && call_expr.args.len > 0 {
expr := call_expr.args[0].expr expr := call_expr.args[0].expr
if expr !is ast.Type { if expr !is ast.Type {
typ := typeof(expr) typ := typeof(expr)

View File

@ -199,6 +199,10 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
// or should it be a property of the arg, like this ptr/mut becomes indistinguishable // or should it be a property of the arg, like this ptr/mut becomes indistinguishable
rec_type_pos = p.tok.position() rec_type_pos = p.tok.position()
rec_type = p.parse_type_with_mut(rec_mut) rec_type = p.parse_type_with_mut(rec_mut)
if rec_type.idx() == 0 {
// error is set in parse_type
return ast.FnDecl{}
}
rec_type_pos = rec_type_pos.extend(p.prev_tok.position()) rec_type_pos = rec_type_pos.extend(p.prev_tok.position())
if is_amp && rec_mut { if is_amp && rec_mut {
p.error('use `(mut f Foo)` or `(f &Foo)` instead of `(mut f &Foo)`') p.error('use `(mut f Foo)` or `(f &Foo)` instead of `(mut f &Foo)`')

View File

@ -21,6 +21,10 @@ pub fn (mut p Parser) parse_array_type() table.Type {
// array // array
p.check(.rsbr) p.check(.rsbr)
elem_type := p.parse_type() elem_type := p.parse_type()
if elem_type.idx() == 0 {
// error is set in parse_type
return 0
}
mut nr_dims := 1 mut nr_dims := 1
// detect attr // detect attr
not_attr := p.peek_tok.kind != .name && p.peek_tok2.kind !in [.semicolon, .rsbr] not_attr := p.peek_tok.kind != .name && p.peek_tok2.kind !in [.semicolon, .rsbr]
@ -165,6 +169,10 @@ pub fn (mut p Parser) parse_type() table.Type {
if p.tok.kind != .lcbr { if p.tok.kind != .lcbr {
pos := p.tok.position() pos := p.tok.position()
typ = p.parse_any_type(language, nr_muls > 0, true) typ = p.parse_any_type(language, nr_muls > 0, true)
if typ.idx() == 0 {
// error is set in parse_type
return 0
}
if typ == table.void_type { if typ == table.void_type {
p.error_with_pos('use `?` instead of `?void`', pos) p.error_with_pos('use `?` instead of `?void`', pos)
return 0 return 0

View File

@ -194,6 +194,10 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
} }
} }
typ = p.parse_type() typ = p.parse_type()
if typ.idx() == 0 {
// error is set in parse_type
return ast.StructDecl{}
}
type_pos = p.prev_tok.position() type_pos = p.prev_tok.position()
field_pos = field_start_pos.extend(type_pos) field_pos = field_start_pos.extend(type_pos)
} }