Skip to content

Commit

Permalink
[Go] Fix compiler directive syntax patterns (#3242)
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.

These should be marked as valid directives:

```
//go:linkname
//go:cgo_import_dynamic
//lint:ignore U1000 This is here for user documentation.
//lint:ignore-file Autogenerated.
//line file.rl:3732:45
//line :3732:45:45
//line :3732 :45:45
/*line file.rl:3732:45*/
/*line :3732:45:45*/
/*line :3732 :45:45*/
//export myfunc
//extern myfunc
```

These should not be marked:

```
/*line file.rl:3732:45 */
/*line :3732:45:45 */
/*line :3732 :45:45 */
//go:
//go
```

Co-authored-by: DeathAxe <[email protected]>
  • Loading branch information
kortschak and deathaxe authored Mar 6, 2022
1 parent d5f5033 commit a49bdef
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 14 deletions.
61 changes: 52 additions & 9 deletions Go/Go.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ variables:
# functions and types.
ident: \b(?!{{keyword}})[[:alpha:]_][[:alnum:]_]*\b

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

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

Expand Down Expand Up @@ -118,25 +121,59 @@ 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]+[a-z-]*)(:)({{directive}})([ ]?)
scope: meta.annotation.go
captures:
1: punctuation.definition.comment.go
2: meta.keyword.annotation.go
3: meta.punctuation.accessor.colon.go
4: 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
5: meta.punctuation.separator.space.go
push: pop-line-annotation

# 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)( ))([a-zA-Z0-9. :]*(?::[0-9]+){1,2})$\n?
scope: comment.line.go
captures:
1: meta.annotation.go
2: punctuation.definition.comment.go
3: meta.variable.function.go
4: meta.punctuation.separator.space.go
5: meta.annotation.parameters.go

- match: ((/\*)(line)( ))([a-zA-Z0-9. :]*(?::[0-9]+){1,2})(\*/)
scope: comment.block.go
captures:
1: meta.annotation.go
2: punctuation.definition.comment.begin.go
3: meta.variable.function.go
4: meta.punctuation.separator.space.go
5: meta.annotation.parameters.go
6: meta.annotation.go punctuation.definition.comment.end.go

# Finally, Cgo and gccgo have directives for exporting functions to C.
- match: ((//)(export|extern)( ))({{ident}})$\n?
scope: comment.line.go
captures:
1: meta.annotation.go
2: punctuation.definition.comment.go
3: meta.variable.function.go
4: meta.punctuation.separator.space.go
5: meta.annotation.parameters.go

# Line comment
- match: //
Expand All @@ -155,6 +192,12 @@ contexts:
captures:
1: punctuation.definition.comment.go

pop-line-annotation:
- meta_scope: comment.line.go
- meta_content_scope: meta.annotation.parameters.go
- match: $\n?
pop: true

pop-line-comment:
- meta_scope: comment.line.go
# Including the newline allows the scope to visually stretch to the right,
Expand Down
120 changes: 115 additions & 5 deletions Go/syntax_test_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,130 @@ 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.go
// ^^ meta.keyword.annotation.go
// ^^^^^^^^ meta.variable.function.go
// ^^^^^^^^^^^^^ comment.line.go meta.annotation.parameters.go
// ^ comment.line.go -meta.annotation

//go-sumtype:decl MySumType
// ^ -comment -meta -punctuation
// ^^ punctuation.definition.comment.go
// ^^^^^^^^^^^^^^^^^^ comment.line.go meta.annotation.go
// ^^^^^^^^^^ meta.keyword.annotation.go
// ^^^^ meta.variable.function.go
// ^^^^^^^^^ comment.line.go meta.annotation.parameters.go
// ^ comment.line.go -meta.annotation

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

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

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

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

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

/*line :10*/
// ^ -comment
// ^^^^^^^ comment.block.go meta.annotation.go
// ^^^^ meta.variable.function.go
// ^^^ comment.block.go meta.annotation.parameters.go
// ^^ comment.block.go punctuation.definition.comment.end.go
// ^ -comment.block.go -meta.annotation

/*line file.rl:10*/
// ^ -comment
// ^^^^^^^ comment.block.go meta.annotation.go
// ^^ punctuation.definition.comment.begin.go
// ^^^^ meta.variable.function.go
// ^^^^^^^^^^ comment.block.go meta.annotation.parameters.go
// ^^ comment.block.go punctuation.definition.comment.end.go
// ^ -comment.block.go -meta.annotation

/*line file.rl:100:10*/
// ^ -comment
// ^^^^^^^ comment.block.go meta.annotation.go
// ^^ punctuation.definition.comment.begin.go
// ^^^^ meta.variable.function.go
// ^^^^^^^^^^^^^^ comment.block.go meta.annotation.parameters.go
// ^^ comment.block.go punctuation.definition.comment.end.go
// ^ -comment.block.go -meta.annotation

/*line :10 */
// ^ -comment
// ^^^^^^^^^^^^^ comment.block.go -meta.annotation
// ^^ punctuation.definition.comment.begin.go
// ^^ punctuation.definition.comment.end.go
// ^ -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
// ^^^^^^^^^ comment.line.go meta.annotation.go
// ^^^^^^ comment.line.go meta.annotation.parameters.go
// ^ comment.line.go -meta.annotation

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


// # Imports

Expand Down

0 comments on commit a49bdef

Please sign in to comment.