Skip to content

Commit

Permalink
[Go] Fix compiler directive syntax patterns
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kortschak committed Feb 20, 2022
1 parent 1f94fc2 commit 6c8f18e
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 5 deletions.
65 changes: 64 additions & 1 deletion Go/Go.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ variables:
# functions and types.
ident: \b(?!{{keyword}})[[:alpha:]_][[:alnum:]_]*\b

# Directive comment key.
directive: \b[[:alpha:]_][[:alnum:]_]*\b

# Line directive comment key.
line_directive: (line) ([a-zA-Z0-9. :]*(?::[0-9]+){1,2})

# Cgo export and gccgo extern directive.
export_directive: (export|extern) ({{ident}})

# Single line only
inline_comment: /[*](?:[^*]|[*](?!/))*[*]/

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

# Go also has line renumbering; line directives specify the source
# position for the character immediately following the comment as
# having come from the specified file, line and column. Both single
# line and block comment line directives are valid.
- match: (//){{line_directive}}$
captures:
1: punctuation.definition.comment.go
2: meta.keyword.annotation.go
3: meta.variable.function.go
push:
- meta_scope: meta.annotation.go comment.line.go
- match: \S+
scope: meta.variable.parameter.go
# End the annotation scope at EOL, but stretch the comment scope
# indefinitely to the right.
- match: $
set: pop-line-comment

- match: (/\*){{line_directive}}(\*/)
captures:
1: punctuation.definition.comment.go
2: meta.keyword.annotation.go
3: meta.variable.function.go
4: punctuation.definition.comment.go
push:
- meta_scope: meta.annotation.go comment.block.go
- match: \S+
scope: meta.variable.parameter.go
# End the annotation scope at EOL, but stretch the comment scope
# indefinitely to the right.
- match: $
set: pop-line-comment

# Finally, Cgo and gccgo have directives for exporting functions to C.
- match: (//){{export_directive}}$
captures:
1: punctuation.definition.comment.go
2: meta.keyword.annotation.go
3: meta.variable.function.go
push:
- meta_scope: meta.annotation.go comment.line.go
- match: \S+
scope: meta.variable.parameter.go
# End the annotation scope at EOL, but stretch the comment scope
# indefinitely to the right.
- match: $
set: pop-line-comment

# Line comment
- match: //
scope: punctuation.definition.comment.go
Expand Down
79 changes: 75 additions & 4 deletions Go/syntax_test_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,91 @@ You may have to disable Go-specific linters when working on this file.
//go
// ^ -comment -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^ comment.line.go -meta.annotation
// ^^^^ comment.line.go -meta.annotation

//go:
// ^ -comment -meta -punctuation
// ^ -comment -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation
// ^^^^^ comment.line.go -meta.annotation

//go:generate one two three
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation

//lint:ignore U1000 Reason.
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation

//lint:file-ignore Reason.
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation

//line :10
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation

//line file.rl:10
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation

//line file.rl:100:10
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation

/*line :10*/
// ^ -comment
// ^^^^^^^^^^^^ meta.annotation.go comment.block.go
// ^ -comment.block.go -meta.annotation

/*line file.rl:10*/
// ^ -comment
// ^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.block.go
// ^ -comment.block.go -meta.annotation

/*line file.rl:100:10*/
// ^ -comment
// ^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.go comment.block.go
// ^ -comment.block.go -meta.annotation

/*line :10 */
// ^ -comment
// ^^^^^^^^^^^^^ comment.block.go -meta.annotation
// ^ -comment

/*line file.rl:10 */
// ^ -comment
// ^^^^^^^^^^^^^^^^^^^^ comment.block.go -meta.annotation
// ^ -comment

/*line file.rl:100:10 */
// ^ -comment
// ^^^^^^^^^^^^^^^^^^^^^^^^ comment.block.go -meta.annotation
// ^ -comment

//export myfunc
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation

//extern myfunc
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^ meta.annotation.go comment.line.go
// ^ comment.line.go -meta.annotation


// # Imports

Expand Down

0 comments on commit 6c8f18e

Please sign in to comment.