From ad78875a8c06d0173ce70f7b84052fce69351c5e Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 30 Dec 2020 02:15:44 +0100 Subject: [PATCH] parser: allow `const x = 0` consts outside of const blocks --- vlib/v/ast/ast.v | 1 + vlib/v/fmt/fmt.v | 11 +++++++++-- vlib/v/parser/parser.v | 15 +++++++++++++-- vlib/v/tests/const_test.v | 9 +++++---- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index bdecbdfc5a..013ed1c13e 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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 { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 0abcf8cd2c..923df31df3 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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) { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 5f1c78e763..a742db3624 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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 } } diff --git a/vlib/v/tests/const_test.v b/vlib/v/tests/const_test.v index e55e279081..b326ab192e 100644 --- a/vlib/v/tests/const_test.v +++ b/vlib/v/tests/const_test.v @@ -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() {