Skip to content

Commit

Permalink
tree-sitter: rewrite comment grammar, detach line- and block comments (
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm authored Mar 29, 2024
1 parent fdf58fd commit 0077997
Show file tree
Hide file tree
Showing 20 changed files with 247,289 additions and 227,851 deletions.
6 changes: 5 additions & 1 deletion src/analyzer/psi/Comment.v
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module psi

pub struct Comment {
pub struct LineComment {
PsiElementImpl
}

pub struct BlockComment {
PsiElementImpl
}
2 changes: 1 addition & 1 deletion src/analyzer/psi/EnumFieldDeclaration.v
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn (f &EnumFieldDeclaration) doc_comment() string {
return stub.comment
}

if comment := f.find_child_by_type(.comment) {
if comment := f.find_child_by_type(.line_comment) {
return comment.get_text().trim_string_left('//').trim(' \t')
}

Expand Down
2 changes: 1 addition & 1 deletion src/analyzer/psi/FieldDeclaration.v
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn (f &FieldDeclaration) doc_comment() string {
return stub.comment
}

if comment := f.find_child_by_type(.comment) {
if comment := f.find_child_by_type(.line_comment) {
return comment.get_text().trim_string_left('//').trim(' \t')
}

Expand Down
6 changes: 3 additions & 3 deletions src/analyzer/psi/doc_comment_extractor.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import strings
pub fn extract_doc_comment(el PsiElement) string {
el_start_line := el.node.start_point().row
mut comment := el.prev_sibling() or { return '' }
if comment !is Comment {
if comment !is LineComment {
comment = comment.prev_sibling() or { return '' }
}

mut comments := []PsiElement{}

for comment is Comment {
for comment is LineComment {
comment_start_line := comment.node.start_point().row

if comment_start_line + 1 + u32(comments.len) != el_start_line {
Expand All @@ -29,7 +29,7 @@ pub fn extract_doc_comment(el PsiElement) string {
mut field_eol_comment := ''
if el is FieldDeclaration {
if next := el.next_sibling() {
if next is Comment {
if next is LineComment {
comment_start_line := next.node.start_point().row
if comment_start_line == el_start_line {
field_eol_comment = next.get_text().trim_string_left('//').trim_space()
Expand Down
10 changes: 8 additions & 2 deletions src/analyzer/psi/element_factory.v
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,14 @@ pub fn create_element(node AstNode, containing_file &PsiFile) PsiElement {
}
}

if node.type_name == .comment {
return Comment{
if node.type_name == .line_comment {
return LineComment{
PsiElementImpl: base_node
}
}

if node.type_name == .block_comment {
return BlockComment{
PsiElementImpl: base_node
}
}
Expand Down
10 changes: 6 additions & 4 deletions tree_sitter_v/bindings/node_types.v
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum NodeType {
attributes
binary_expression
block
block_comment
break_statement
c_string_literal
call_expression
Expand Down Expand Up @@ -92,6 +93,7 @@ pub enum NodeType {
label_definition
label_reference
labeled_statement
line_comment
literal
literal_attribute
lock_expression
Expand Down Expand Up @@ -139,6 +141,7 @@ pub enum NodeType {
selector_expression
send_statement
shared_type
shebang
short_element_list
short_lambda
signature
Expand Down Expand Up @@ -171,7 +174,6 @@ pub enum NodeType {
var_definition_list
visibility_modifiers
wrong_pointer_type
comment
escape_sequence
false_
float_literal
Expand All @@ -182,7 +184,6 @@ pub enum NodeType {
none_
pseudo_compile_time_identifier
rune_literal
shebang
true_
}

Expand Down Expand Up @@ -341,6 +342,7 @@ const node_type_name_to_enum = {
'attributes': NodeType.attributes
'binary_expression': NodeType.binary_expression
'block': NodeType.block
'block_comment': NodeType.block_comment
'break_statement': NodeType.break_statement
'c_string_literal': NodeType.c_string_literal
'call_expression': NodeType.call_expression
Expand Down Expand Up @@ -402,6 +404,7 @@ const node_type_name_to_enum = {
'label_definition': NodeType.label_definition
'label_reference': NodeType.label_reference
'labeled_statement': NodeType.labeled_statement
'line_comment': NodeType.line_comment
'literal': NodeType.literal
'literal_attribute': NodeType.literal_attribute
'lock_expression': NodeType.lock_expression
Expand Down Expand Up @@ -449,6 +452,7 @@ const node_type_name_to_enum = {
'selector_expression': NodeType.selector_expression
'send_statement': NodeType.send_statement
'shared_type': NodeType.shared_type
'shebang': NodeType.shebang
'short_element_list': NodeType.short_element_list
'short_lambda': NodeType.short_lambda
'signature': NodeType.signature
Expand Down Expand Up @@ -481,7 +485,6 @@ const node_type_name_to_enum = {
'var_definition_list': NodeType.var_definition_list
'visibility_modifiers': NodeType.visibility_modifiers
'wrong_pointer_type': NodeType.wrong_pointer_type
'comment': NodeType.comment
'escape_sequence': NodeType.escape_sequence
'false': NodeType.false_
'float_literal': NodeType.float_literal
Expand All @@ -492,6 +495,5 @@ const node_type_name_to_enum = {
'none': NodeType.none_
'pseudo_compile_time_identifier': NodeType.pseudo_compile_time_identifier
'rune_literal': NodeType.rune_literal
'shebang': NodeType.shebang
'true': NodeType.true_
}
21 changes: 11 additions & 10 deletions tree_sitter_v/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const list_separator = choice(semi, ',');
module.exports = grammar({
name: 'v',

extras: ($) => [$.comment, /\s/],
extras: ($) => [/\s/, $.line_comment, $.block_comment],

word: ($) => $.identifier,

Expand Down Expand Up @@ -124,18 +124,19 @@ module.exports = grammar({
),
),

shebang: (_) => token(/\#\!([^\\\r\n]+)+/),
shebang: (_) => seq('#!', /.*/),

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

block_comment: (_) =>
seq(
'/*',
token(choice(/(?:[^/][^*]+\/\*+[^/][^*]+)+(?:[^*][^/]+\*+\/[^*][^/]+)+/, /[^*]*\*/)),
'/',
),

comment: ($) => choice($.line_comment, $.block_comment),

module_clause: ($) => seq(optional($.attributes), 'module', $.identifier),

import_list: ($) => repeat1($.import_declaration),
Expand Down
3 changes: 2 additions & 1 deletion tree_sitter_v/queries/helix.highlights.scm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
(comment)
(line_comment)
(block_comment)
(shebang)
] @comment

Expand Down
3 changes: 2 additions & 1 deletion tree_sitter_v/queries/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ERROR) @error

[
(comment)
(line_comment)
(block_comment)
(shebang)
] @comment

Expand Down
97 changes: 71 additions & 26 deletions tree_sitter_v/src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0077997

Please sign in to comment.