Skip to content

Commit 6c8f18e

Browse files
committed
[Go] Fix compiler directive syntax patterns
This adds line and export directive syntax patterns and generalises the //go:name pattern for use in other contexts such as linter directives. It also fixes //go:name matching to avoid matching invalid directive comments.
1 parent 1f94fc2 commit 6c8f18e

File tree

2 files changed

+139
-5
lines changed

2 files changed

+139
-5
lines changed

Go/Go.sublime-syntax

+64-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ variables:
7979
# functions and types.
8080
ident: \b(?!{{keyword}})[[:alpha:]_][[:alnum:]_]*\b
8181

82+
# Directive comment key.
83+
directive: \b[[:alpha:]_][[:alnum:]_]*\b
84+
85+
# Line directive comment key.
86+
line_directive: (line) ([a-zA-Z0-9. :]*(?::[0-9]+){1,2})
87+
88+
# Cgo export and gccgo extern directive.
89+
export_directive: (export|extern) ({{ident}})
90+
8291
# Single line only
8392
inline_comment: /[*](?:[^*]|[*](?!/))*[*]/
8493

@@ -118,12 +127,18 @@ contexts:
118127
#
119128
# //go:some_directive arg0 arg1 ...
120129
#
130+
# These have been adopted outside the gc compiler for use in linting
131+
# directives and pragmas for other compiler suites where they take the
132+
# more generalised form of:
133+
#
134+
# //namespace:some_directive ...
135+
#
121136
# They're not part of the language spec, may not be recognized by some Go
122137
# compilers, and may stop working in future releases. Therefore,
123138
# highlighting them as compiler pragmas could be misleading. We scope them
124139
# as plain comments by default, and add some detailed meta scopes for
125140
# enterprising users wishing to add more color.
126-
- match: (//)(go)(:)({{ident}})?
141+
- match: (//)([a-z]+)(:)({{directive}}(?:.*)){1,}
127142
captures:
128143
1: punctuation.definition.comment.go
129144
2: meta.keyword.annotation.go
@@ -138,6 +153,54 @@ contexts:
138153
- match: $
139154
set: pop-line-comment
140155

156+
# Go also has line renumbering; line directives specify the source
157+
# position for the character immediately following the comment as
158+
# having come from the specified file, line and column. Both single
159+
# line and block comment line directives are valid.
160+
- match: (//){{line_directive}}$
161+
captures:
162+
1: punctuation.definition.comment.go
163+
2: meta.keyword.annotation.go
164+
3: meta.variable.function.go
165+
push:
166+
- meta_scope: meta.annotation.go comment.line.go
167+
- match: \S+
168+
scope: meta.variable.parameter.go
169+
# End the annotation scope at EOL, but stretch the comment scope
170+
# indefinitely to the right.
171+
- match: $
172+
set: pop-line-comment
173+
174+
- match: (/\*){{line_directive}}(\*/)
175+
captures:
176+
1: punctuation.definition.comment.go
177+
2: meta.keyword.annotation.go
178+
3: meta.variable.function.go
179+
4: punctuation.definition.comment.go
180+
push:
181+
- meta_scope: meta.annotation.go comment.block.go
182+
- match: \S+
183+
scope: meta.variable.parameter.go
184+
# End the annotation scope at EOL, but stretch the comment scope
185+
# indefinitely to the right.
186+
- match: $
187+
set: pop-line-comment
188+
189+
# Finally, Cgo and gccgo have directives for exporting functions to C.
190+
- match: (//){{export_directive}}$
191+
captures:
192+
1: punctuation.definition.comment.go
193+
2: meta.keyword.annotation.go
194+
3: meta.variable.function.go
195+
push:
196+
- meta_scope: meta.annotation.go comment.line.go
197+
- match: \S+
198+
scope: meta.variable.parameter.go
199+
# End the annotation scope at EOL, but stretch the comment scope
200+
# indefinitely to the right.
201+
- match: $
202+
set: pop-line-comment
203+
141204
# Line comment
142205
- match: //
143206
scope: punctuation.definition.comment.go

Go/syntax_test_go.go

+75-4
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,91 @@ You may have to disable Go-specific linters when working on this file.
5656
//go
5757
// ^ -comment -punctuation
5858
// ^^ punctuation.definition.comment.go
59-
// ^^^^^ comment.line.go -meta.annotation
59+
// ^^^^ comment.line.go -meta.annotation
6060

6161
//go:
62-
// ^ -comment -meta -punctuation
62+
// ^ -comment -punctuation
6363
// ^^ punctuation.definition.comment.go
64-
// ^^^^^ meta.annotation.go comment.line.go
65-
// ^ comment.line.go -meta.annotation
64+
// ^^^^^ comment.line.go -meta.annotation
6665

6766
//go:generate one two three
6867
// ^ -comment -meta -punctuation
6968
// ^^ punctuation.definition.comment.go
7069
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
7170
// ^ comment.line.go -meta.annotation
7271

72+
//lint:ignore U1000 Reason.
73+
// ^ -comment -meta -punctuation
74+
// ^^ punctuation.definition.comment.go
75+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
76+
// ^ comment.line.go -meta.annotation
77+
78+
//lint:file-ignore Reason.
79+
// ^ -comment -meta -punctuation
80+
// ^^ punctuation.definition.comment.go
81+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
82+
// ^ comment.line.go -meta.annotation
83+
84+
//line :10
85+
// ^ -comment -meta -punctuation
86+
// ^^ punctuation.definition.comment.go
87+
// ^^^^^^^^^^ meta.annotation.go comment.line.go
88+
// ^ comment.line.go -meta.annotation
89+
90+
//line file.rl:10
91+
// ^ -comment -meta -punctuation
92+
// ^^ punctuation.definition.comment.go
93+
// ^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
94+
// ^ comment.line.go -meta.annotation
95+
96+
//line file.rl:100:10
97+
// ^ -comment -meta -punctuation
98+
// ^^ punctuation.definition.comment.go
99+
// ^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
100+
// ^ comment.line.go -meta.annotation
101+
102+
/*line :10*/
103+
// ^ -comment
104+
// ^^^^^^^^^^^^ meta.annotation.go comment.block.go
105+
// ^ -comment.block.go -meta.annotation
106+
107+
/*line file.rl:10*/
108+
// ^ -comment
109+
// ^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.block.go
110+
// ^ -comment.block.go -meta.annotation
111+
112+
/*line file.rl:100:10*/
113+
// ^ -comment
114+
// ^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.block.go
115+
// ^ -comment.block.go -meta.annotation
116+
117+
/*line :10 */
118+
// ^ -comment
119+
// ^^^^^^^^^^^^^ comment.block.go -meta.annotation
120+
// ^ -comment
121+
122+
/*line file.rl:10 */
123+
// ^ -comment
124+
// ^^^^^^^^^^^^^^^^^^^^ comment.block.go -meta.annotation
125+
// ^ -comment
126+
127+
/*line file.rl:100:10 */
128+
// ^ -comment
129+
// ^^^^^^^^^^^^^^^^^^^^^^^^ comment.block.go -meta.annotation
130+
// ^ -comment
131+
132+
//export myfunc
133+
// ^ -comment -meta -punctuation
134+
// ^^ punctuation.definition.comment.go
135+
// ^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
136+
// ^ comment.line.go -meta.annotation
137+
138+
//extern myfunc
139+
// ^ -comment -meta -punctuation
140+
// ^^ punctuation.definition.comment.go
141+
// ^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
142+
// ^ comment.line.go -meta.annotation
143+
73144

74145
// # Imports
75146

0 commit comments

Comments
 (0)