|
1 | 1 | package ast
|
2 | 2 |
|
3 |
| -// TODO: add more nodes from https://github.com/kcl-lang/kcl/blob/main/kclvm/ast/src/ast.rs |
| 3 | +// Module is an abstract syntax tree for a single KCL file. |
| 4 | +type Module struct { |
| 5 | + Filename string `json:"filename"` |
| 6 | + Pkg string `json:"pkg"` |
| 7 | + Doc *Node[string] `json:"doc"` |
| 8 | + Body []*Node[Stmt] `json:"body"` |
| 9 | + Comments []*Node[Comment] `json:"comments"` |
| 10 | +} |
4 | 11 |
|
5 |
| -// Pos denotes the struct tuple (filename, line, column, end_line, end_column). |
6 |
| -type Pos struct { |
7 |
| - Filename string `json:"filename"` |
8 |
| - Line uint64 `json:"line"` |
9 |
| - Column uint64 `json:"column"` |
10 |
| - EndLine uint64 `json:"end_line"` |
11 |
| - EndColumn uint64 `json:"end_column"` |
| 12 | +// NewModule creates a new Module instance |
| 13 | +func NewModule() *Module { |
| 14 | + return &Module{ |
| 15 | + Body: make([]*Node[Stmt], 0), |
| 16 | + Comments: make([]*Node[Comment], 0), |
| 17 | + } |
12 | 18 | }
|
13 | 19 |
|
14 |
| -// Node is the file, line, and column number information that all AST nodes need to contain. |
15 |
| -type Node interface { |
16 |
| - Pos() Pos |
17 |
| - Index() string |
| 20 | +// Node is the file, line and column number information that all AST nodes need to contain. |
| 21 | +// In fact, column and end_column are the counts of character. For example, `\t` is counted as 1 character, |
| 22 | +// so it is recorded as 1 here, but generally col is 4. |
| 23 | +type Node[T any] struct { |
| 24 | + ID AstIndex `json:"id,omitempty"` |
| 25 | + Node T `json:"node,omitempty"` |
| 26 | + Pos |
18 | 27 | }
|
19 | 28 |
|
20 | 29 | // AstIndex represents a unique identifier for AST nodes.
|
21 | 30 | type AstIndex string
|
22 | 31 |
|
| 32 | +// Pos denotes the struct tuple (filename, line, column, end_line, end_column). |
| 33 | +type Pos struct { |
| 34 | + Filename string `json:"filename,omitempty"` |
| 35 | + Line int64 `json:"line,omitempty"` |
| 36 | + Column int64 `json:"column,omitempty"` |
| 37 | + EndLine int64 `json:"end_line,omitempty"` |
| 38 | + EndColumn int64 `json:"end_column,omitempty"` |
| 39 | +} |
| 40 | + |
23 | 41 | // Comment node.
|
24 | 42 | type Comment struct {
|
25 | 43 | Text string
|
|
0 commit comments