Skip to content

Commit

Permalink
Merge pull request #97 from matoous/md/comment-directives
Browse files Browse the repository at this point in the history
feat(grammar): capture comment directives
  • Loading branch information
aryx authored Dec 6, 2022
2 parents dfbef73 + e5f17e1 commit 06432b8
Show file tree
Hide file tree
Showing 5 changed files with 31,304 additions and 23,405 deletions.
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

0 comments on commit 06432b8

Please sign in to comment.