Skip to content

Commit aef83ba

Browse files
committed
all: @[attr] syntax
1 parent f4eede8 commit aef83ba

6 files changed

Lines changed: 37 additions & 8 deletions

File tree

vlib/v/ast/attr.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub:
2323
kind AttrKind
2424
ct_opt bool // true for [if user_defined_name?]
2525
pos token.Pos
26+
has_at bool // new syntax `@[attr]`
2627
pub mut:
2728
ct_expr Expr // .kind == comptime_define, for [if !name]
2829
ct_evaled bool // whether ct_skip has been evaluated already

vlib/v/fmt/attrs.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ pub fn (mut f Fmt) attrs(attrs []ast.Attr) {
1717
f.single_line_attrs(sorted_attrs[i..])
1818
break
1919
}
20+
if attr.has_at {
21+
f.write('@')
22+
}
2023
f.writeln('[${attr}]')
2124
}
2225
}
@@ -35,6 +38,9 @@ pub fn (mut f Fmt) single_line_attrs(attrs []ast.Attr, options AttrsOptions) {
3538
if options.inline {
3639
f.write(' ')
3740
}
41+
if attrs[0].has_at {
42+
f.write('@')
43+
}
3844
f.write('[')
3945
for i, attr in sorted_attrs {
4046
if i > 0 {

vlib/v/parser/parser.v

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,14 @@ fn (mut p Parser) top_stmt() ast.Stmt {
723723
}
724724
}
725725
}
726+
.at {
727+
if p.peek_tok.kind == .lsbr {
728+
p.attributes()
729+
continue
730+
} else {
731+
return p.error('@[attr] expected')
732+
}
733+
}
726734
.lsbr {
727735
// attrs are stored in `p.attrs`
728736
p.attributes()
@@ -1769,11 +1777,20 @@ fn (mut p Parser) is_attributes() bool {
17691777

17701778
// when is_top_stmt is true attrs are added to p.attrs
17711779
fn (mut p Parser) attributes() {
1772-
p.check(.lsbr)
1780+
mut is_at := false
1781+
if p.tok.kind == .lsbr {
1782+
// [attr]
1783+
p.check(.lsbr)
1784+
} else if p.tok.kind == .at {
1785+
// @[attr]
1786+
p.check(.at)
1787+
p.check(.lsbr)
1788+
is_at = true
1789+
}
17731790
mut has_ctdefine := false
17741791
for p.tok.kind != .rsbr {
17751792
start_pos := p.tok.pos()
1776-
attr := p.parse_attr()
1793+
attr := p.parse_attr(is_at)
17771794
if p.attrs.contains(attr.name) && attr.name != 'wasm_export' {
17781795
p.error_with_pos('duplicate attribute `${attr.name}`', start_pos.extend(p.prev_tok.pos()))
17791796
return
@@ -1809,7 +1826,7 @@ fn (mut p Parser) attributes() {
18091826
}
18101827
}
18111828

1812-
fn (mut p Parser) parse_attr() ast.Attr {
1829+
fn (mut p Parser) parse_attr(is_at bool) ast.Attr {
18131830
mut kind := ast.AttrKind.plain
18141831
apos := p.prev_tok.pos()
18151832
if p.tok.kind == .key_unsafe {
@@ -1882,6 +1899,7 @@ fn (mut p Parser) parse_attr() ast.Attr {
18821899
ct_expr: comptime_cond
18831900
ct_opt: comptime_cond_opt
18841901
pos: apos.extend(p.tok.pos())
1902+
has_at: is_at
18851903
}
18861904
}
18871905

@@ -3431,7 +3449,7 @@ fn (mut p Parser) parse_number_literal() ast.Expr {
34313449
fn (mut p Parser) module_decl() ast.Module {
34323450
mut module_attrs := []ast.Attr{}
34333451
mut attrs_pos := p.tok.pos()
3434-
for p.tok.kind == .lsbr {
3452+
for p.tok.kind == .lsbr || p.tok.kind == .at {
34353453
p.attributes()
34363454
}
34373455
module_attrs << p.attrs
@@ -3959,7 +3977,7 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
39593977
uses_exprs = true
39603978
}
39613979
mut attrs := []ast.Attr{}
3962-
if p.tok.kind == .lsbr {
3980+
if p.tok.kind == .lsbr || p.tok.kind == .at {
39633981
p.attributes()
39643982
attrs << p.attrs
39653983
enum_attrs[val] = attrs

vlib/v/parser/struct.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
269269
// Comments after type (same line)
270270
prev_attrs := p.attrs
271271
p.attrs = []
272-
if p.tok.kind == .lsbr {
272+
if p.tok.kind == .lsbr || p.tok.kind == .at {
273273
p.inside_struct_attr_decl = true
274274
// attrs are stored in `p.attrs`
275275
p.attributes()

vlib/v/scanner/scanner.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,10 @@ fn (mut s Scanner) text_scan() token.Token {
886886
return s.new_token(.comma, '', 1)
887887
}
888888
`@` {
889+
// @[attr]
890+
if s.text[s.pos + 1] == `[` {
891+
return s.new_token(.at, '', 1)
892+
}
889893
mut name := ''
890894
if nextc != `\0` {
891895
s.pos++

vlib/v/tests/enum_attr_test.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import json
22

33
enum Foo {
4-
yay [json: 'A'; yay]
5-
foo [foo; json: 'B']
4+
yay @[json: 'A'; yay]
5+
foo @[foo; json: 'B']
66
}
77

88
struct FooStruct {

0 commit comments

Comments
 (0)