-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
90 lines (75 loc) · 2.27 KB
/
parser.go
File metadata and controls
90 lines (75 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package chunkx
import (
"context"
"fmt"
"github.com/gomantics/chunkx/languages"
sitter "github.com/smacker/go-tree-sitter"
)
// Parser provides language-agnostic parsing capabilities using tree-sitter.
type Parser struct {
parser *sitter.Parser
}
// NewParser creates a new parser instance.
func NewParser() *Parser {
return &Parser{
parser: sitter.NewParser(),
}
}
// ParseResult contains the parsed AST and metadata.
type ParseResult struct {
Tree *sitter.Tree
Language languages.LanguageName
Source []byte
}
// Parse parses the given code using the specified language.
func (p *Parser) Parse(code string, language languages.LanguageName) (*ParseResult, error) {
lang, ok := languages.GetLanguageConfig(language)
if !ok {
return nil, &LanguageError{
Language: language,
Err: ErrUnsupportedLanguage,
}
}
// Generic language doesn't support tree-sitter parsing
if lang.GetParser == nil {
return nil, &LanguageError{
Language: language,
Err: ErrNoASTSupport,
}
}
p.parser.SetLanguage(lang.GetParser())
sourceCode := []byte(code)
tree, err := p.parser.ParseCtx(context.Background(), nil, sourceCode)
if err != nil {
return nil, &LanguageError{
Language: language,
Err: fmt.Errorf("%w: %w", ErrParseFailed, err),
}
}
return &ParseResult{
Tree: tree,
Language: lang.Name,
Source: sourceCode,
}, nil
}
// ParseFile parses code from a file, auto-detecting the language.
func (p *Parser) ParseFile(filepath string, code string) (*ParseResult, error) {
lang, ok := languages.DetectLanguage(filepath)
if !ok {
return nil, fmt.Errorf("cannot detect language for file: %s", filepath)
}
return p.Parse(code, lang.Name)
}
// GetNodeText returns the text content of a node.
func GetNodeText(node *sitter.Node, source []byte) string {
return string(source[node.StartByte():node.EndByte()])
}
// GetNodeSize calculates the size of a node using the provided token counter.
func GetNodeSize(node *sitter.Node, source []byte, counter TokenCounter) (int, error) {
text := GetNodeText(node, source)
return counter.CountTokens(text)
}
// GetLineNumbers returns the start and end line numbers for a node (1-based).
func GetLineNumbers(node *sitter.Node) (int, int) {
return int(node.StartPoint().Row) + 1, int(node.EndPoint().Row) + 1
}