Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(grammar): capture comment directives #97

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions corpus/source_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,54 @@ const d
(comment)
(const_declaration (const_spec (identifier))))

================================================================================
Directive comments
================================================================================

package main

//extern test_extern
func testExtern() {}

//export text_export
func textExport() {}

//line test:123
type testLine int

//nolint:unused
type Custom int

---

(source_file
(package_clause
(package_identifier))
(comment
(directive))
(function_declaration
(identifier)
(parameter_list)
(block))
(comment
(directive))
(function_declaration
(identifier)
(parameter_list)
(block))
(comment
(directive))
(type_declaration
(type_spec
(type_identifier)
(type_identifier)))
(comment
(directive))
(type_declaration
(type_spec
(type_identifier)
(type_identifier))))

============================================
Non-ascii variable names
============================================
Expand Down
10 changes: 7 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -895,15 +895,19 @@ module.exports = grammar({
false: $ => 'false',
iota: $ => 'iota',

// https://tip.golang.org/doc/comment#syntax
directive: $ => token(/(line |extern |export |[a-z0-9]+:[a-z0-9])/),

// http://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment/36328890#36328890
comment: $ => token(choice(
seq('//', /.*/),
comment: $ => choice(
seq('//', $.directive, /.*/),
seq('// ', /.*/),
seq(
'/*',
/[^*]*\*+([^/*][^*]*\*+)*/,
'/'
)
))
)
}
})

Expand Down
89 changes: 55 additions & 34 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -6720,43 +6720,64 @@
"type": "STRING",
"value": "iota"
},
"comment": {
"directive": {
"type": "TOKEN",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "//"
},
{
"type": "PATTERN",
"value": ".*"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "/*"
},
{
"type": "PATTERN",
"value": "[^*]*\\*+([^/*][^*]*\\*+)*"
},
{
"type": "STRING",
"value": "/"
}
]
}
]
"type": "PATTERN",
"value": "(line |extern |export |[a-z0-9]+:[a-z0-9])"
}
},
"comment": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "//"
},
{
"type": "SYMBOL",
"name": "directive"
},
{
"type": "PATTERN",
"value": ".*"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "// "
},
{
"type": "PATTERN",
"value": ".*"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "/*"
},
{
"type": "PATTERN",
"value": "[^*]*\\*+([^/*][^*]*\\*+)*"
},
{
"type": "STRING",
"value": "/"
}
]
}
]
}
},
"extras": [
Expand Down
35 changes: 31 additions & 4 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,21 @@
}
}
},
{
"type": "comment",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "directive",
"named": true
}
]
}
},
{
"type": "communication_case",
"named": true,
Expand Down Expand Up @@ -2603,6 +2618,18 @@
"type": "/",
"named": false
},
{
"type": "/*",
"named": false
},
{
"type": "//",
"named": false
},
{
"type": "// ",
"named": false
},
{
"type": "/=",
"named": false
Expand Down Expand Up @@ -2695,10 +2722,6 @@
"type": "chan",
"named": false
},
{
"type": "comment",
"named": true
},
{
"type": "const",
"named": false
Expand All @@ -2715,6 +2738,10 @@
"type": "defer",
"named": false
},
{
"type": "directive",
"named": true
},
{
"type": "else",
"named": false
Expand Down
Loading