smacker/go-tree-sitter

C++ Parser Not Recognizing Comments in Macro Definitions

Opened this issue · 0 comments

I am currently using tree-sitter for parsing C++ files, and I have encountered an issue where the parser is not consistently recognizing comments in macro definitions.

Steps to Reproduce:

A C++ file containing the following lines:

#define TEST_1 // comment
#define TEST_2 1 // comment

Here is go test code

func Test_testComment(t *testing.T) {
	var parser = sitter.NewParser()
	parser.SetLanguage(cpp.GetLanguage())
	content := `#define TEST_1 // comment
#define TEST_2 1 // comment`

	tree, _ := parser.ParseCtx(context.Background(), nil, []byte(content))

	iter := sitter.NewIterator(tree.RootNode(), sitter.BFSMode)

	iter.ForEach(func(n *sitter.Node) error {
		if n.Type() == "comment" {
			fmt.Println("StartPoint: ", n.StartPoint())
			fmt.Println("EndPoint:", n.EndPoint())
		}
		return nil
	})
}

Output:

=== RUN   Test_testComment
StartPoint:  {0 15}
EndPoint: {0 25}

Expected Result:

Output two inline comment.

Actual Result:

got one inline comment.

Environment:
os: linux ubuntu 22 and macos
go version: 1.22.4
go-tree-sitter version: v0.0.0-20240625050157-a31a98a7c0f6