parser: allow `const x = 0` consts outside of const blocks
parent
8e6f3a707b
commit
ad78875a8c
|
@ -179,6 +179,7 @@ pub:
|
|||
pub mut:
|
||||
fields []ConstField // all the const fields in the `const (...)` block
|
||||
end_comments []Comment // comments that after last const field
|
||||
is_block bool // const() block
|
||||
}
|
||||
|
||||
pub struct StructDecl {
|
||||
|
|
|
@ -1992,7 +1992,10 @@ pub fn (mut f Fmt) const_decl(it ast.ConstDecl) {
|
|||
f.writeln('const ()\n')
|
||||
return
|
||||
}
|
||||
f.writeln('const (')
|
||||
f.write('const ')
|
||||
if it.is_block {
|
||||
f.writeln(' (')
|
||||
}
|
||||
mut max := 0
|
||||
for field in it.fields {
|
||||
if field.name.len > max {
|
||||
|
@ -2017,7 +2020,11 @@ pub fn (mut f Fmt) const_decl(it ast.ConstDecl) {
|
|||
}
|
||||
f.comments_after_last_field(it.end_comments)
|
||||
f.indent--
|
||||
f.writeln(')\n')
|
||||
if it.is_block {
|
||||
f.writeln(')\n')
|
||||
} else {
|
||||
f.writeln('')
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut f Fmt) global_decl(it ast.GlobalDecl) {
|
||||
|
|
|
@ -1799,11 +1799,16 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
|
|||
end_pos := p.tok.position()
|
||||
const_pos := p.tok.position()
|
||||
p.check(.key_const)
|
||||
is_block := p.tok.kind == .lpar
|
||||
/*
|
||||
if p.tok.kind != .lpar {
|
||||
p.error_with_pos('const declaration is missing parentheses `( ... )`', const_pos)
|
||||
return ast.ConstDecl{}
|
||||
}
|
||||
p.next() // (
|
||||
*/
|
||||
if is_block {
|
||||
p.next() // (
|
||||
}
|
||||
mut fields := []ast.ConstField{}
|
||||
mut comments := []ast.Comment{}
|
||||
for {
|
||||
|
@ -1840,14 +1845,20 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
|
|||
fields << field
|
||||
p.global_scope.register(field)
|
||||
comments = []
|
||||
if !is_block {
|
||||
break
|
||||
}
|
||||
}
|
||||
p.top_level_statement_end()
|
||||
p.check(.rpar)
|
||||
if is_block {
|
||||
p.check(.rpar)
|
||||
}
|
||||
return ast.ConstDecl{
|
||||
pos: start_pos.extend_with_last_line(end_pos, p.prev_tok.line_nr)
|
||||
fields: fields
|
||||
is_pub: is_pub
|
||||
end_comments: comments
|
||||
is_block: is_block
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
pub const (
|
||||
pub const (
|
||||
a = b
|
||||
c = a + b
|
||||
b = 1
|
||||
|
@ -6,6 +6,8 @@ pub const (
|
|||
e = 9
|
||||
)
|
||||
|
||||
pub const x = 10
|
||||
|
||||
fn test_const() {
|
||||
assert a == 1
|
||||
assert d == 11
|
||||
|
@ -21,13 +23,12 @@ fn foo_decode(name string) ?Foo {
|
|||
if name == 'baz' {
|
||||
return error('baz is not allowed')
|
||||
}
|
||||
|
||||
return Foo{name}
|
||||
}
|
||||
|
||||
pub const (
|
||||
pub const (
|
||||
def = foo_decode('baz') or { Foo{} }
|
||||
bar = foo_decode('bar')?
|
||||
bar = foo_decode('bar') ?
|
||||
)
|
||||
|
||||
fn test_opt_const() {
|
||||
|
|
Loading…
Reference in New Issue