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