Skip to content

Commit 010266b

Browse files
committed
feat: add comments-support
1 parent db6a2c5 commit 010266b

File tree

8 files changed

+321
-89
lines changed

8 files changed

+321
-89
lines changed

src/ast/__snapshots__/ast_test.snap

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,106 @@
33
ast.File{
44
Name: "some",
55
Body: {
6-
ast.TypeDeclaration{Id:"Status", Kind:"int"},
6+
ast.TypeDeclaration{
7+
BaseDeclaration: ast.BaseDeclaration{
8+
LeadingComments: {
9+
{Value:" hello\n"},
10+
},
11+
TrailingComments: {
12+
},
13+
},
14+
Id: "Status",
15+
Kind: "int",
16+
},
717
ast.ConstDeclaration{
8-
Declarators: {
9-
{Kind:"Status", Id:"Todo", Value:"iota"},
10-
{Kind:"", Id:"Done", Value:""},
11-
{Kind:"", Id:"Pending", Value:""},
12-
{Kind:"", Id:"InProgress", Value:""},
18+
BaseDeclaration: ast.BaseDeclaration{},
19+
Declarators: {
20+
{
21+
BaseDeclaration: ast.BaseDeclaration{
22+
LeadingComments: {
23+
{Value:" 代办\n"},
24+
},
25+
TrailingComments: {
26+
{Value:" 59todo\n"},
27+
},
28+
},
29+
Kind: "Status",
30+
Id: "Todo",
31+
Value: "iota",
32+
},
33+
{
34+
BaseDeclaration: ast.BaseDeclaration{
35+
LeadingComments: {
36+
{Value:" 已完成\n"},
37+
},
38+
TrailingComments: {
39+
},
40+
},
41+
Kind: "",
42+
Id: "Done",
43+
Value: "",
44+
},
45+
{
46+
BaseDeclaration: ast.BaseDeclaration{
47+
LeadingComments: {
48+
},
49+
TrailingComments: {
50+
},
51+
},
52+
Kind: "",
53+
Id: "Pending",
54+
Value: "",
55+
},
56+
{
57+
BaseDeclaration: ast.BaseDeclaration{
58+
LeadingComments: {
59+
},
60+
TrailingComments: {
61+
},
62+
},
63+
Kind: "",
64+
Id: "InProgress",
65+
Value: "",
66+
},
67+
},
68+
},
69+
ast.TypeDeclaration{
70+
BaseDeclaration: ast.BaseDeclaration{
71+
LeadingComments: {
72+
},
73+
TrailingComments: {
74+
},
1375
},
76+
Id: "Sex",
77+
Kind: "string",
1478
},
15-
ast.TypeDeclaration{Id:"Sex", Kind:"string"},
1679
ast.ConstDeclaration{
17-
Declarators: {
18-
{Kind:"Sex", Id:"Female", Value:"female"},
19-
{Kind:"Sex", Id:"Male", Value:"male"},
80+
BaseDeclaration: ast.BaseDeclaration{},
81+
Declarators: {
82+
{
83+
BaseDeclaration: ast.BaseDeclaration{
84+
LeadingComments: {
85+
{Value:" dsdas\n"},
86+
},
87+
TrailingComments: {
88+
},
89+
},
90+
Kind: "Sex",
91+
Id: "Female",
92+
Value: "female",
93+
},
94+
{
95+
BaseDeclaration: ast.BaseDeclaration{
96+
LeadingComments: {
97+
},
98+
TrailingComments: {
99+
{Value:"hhh\n"},
100+
},
101+
},
102+
Kind: "Sex",
103+
Id: "Male",
104+
Value: "male",
105+
},
20106
},
21107
},
22108
},

src/ast/ast.go

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,29 @@ type AstGenerator struct {
1111
Tokens []token.Token
1212
index int
1313
currentToken token.Token
14+
usedComments map[token.Token]bool
1415
}
1516

1617
func (a *AstGenerator) nextToken(reportErrorWhenIsNull bool) (token.Token, error) {
17-
a.index += 1
18+
for i := a.index + 1; ; i++ {
19+
if i >= len(a.Tokens) {
20+
if reportErrorWhenIsNull {
21+
a.reportTokenError()
22+
}
1823

19-
if a.index >= len(a.Tokens) {
20-
if reportErrorWhenIsNull {
21-
a.reportTokenError()
24+
return token.Token{}, errors.New("Overflow")
2225
}
2326

24-
return token.Token{}, errors.New("Overflow")
25-
}
27+
tok := a.Tokens[i]
2628

27-
a.currentToken = a.Tokens[a.index]
29+
if tok.Type == token.LineComment {
30+
continue
31+
}
32+
33+
a.index = i
34+
a.currentToken = a.Tokens[a.index]
35+
break
36+
}
2837

2938
return a.currentToken, nil
3039
}
@@ -75,9 +84,51 @@ func (a *AstGenerator) initFile() File {
7584
return file
7685
}
7786

87+
func (a *AstGenerator) resolveComments(node *BaseDeclaration, leading bool) {
88+
currentToken := a.currentToken
89+
comments := []Comment{}
90+
91+
if leading {
92+
for i := a.index - 1; i >= 0; i-- {
93+
tok := a.Tokens[i]
94+
95+
if tok.Type == token.LineComment && !a.usedComments[tok] {
96+
comment := Comment{}
97+
comment.Value = tok.Value
98+
comments = append([]Comment{comment}, comments...)
99+
a.usedComments[tok] = true
100+
} else {
101+
break
102+
}
103+
}
104+
105+
node.LeadingComments = comments
106+
} else {
107+
for i := a.index + 1; i < len(a.Tokens); i++ {
108+
tok := a.Tokens[i]
109+
110+
if tok.Start[0] != currentToken.Start[0] {
111+
break
112+
}
113+
114+
if tok.Type == token.LineComment {
115+
comments = []Comment{{Value: tok.Value}}
116+
a.usedComments[tok] = true
117+
break
118+
}
119+
120+
break
121+
}
122+
123+
node.TrailingComments = comments
124+
}
125+
}
126+
78127
func (a *AstGenerator) readTypeDeclaration() TypeDeclaration {
79128
d := TypeDeclaration{}
80129

130+
a.resolveComments(&d.BaseDeclaration, true)
131+
81132
next, _ := a.nextToken(true)
82133
d.Id = next.Value
83134

@@ -89,6 +140,8 @@ func (a *AstGenerator) readTypeDeclaration() TypeDeclaration {
89140
d.Kind = String
90141
}
91142

143+
a.resolveComments(&d.BaseDeclaration, false)
144+
92145
return d
93146
}
94147

@@ -107,6 +160,7 @@ func (a *AstGenerator) readConstDeclaration() ConstDeclaration {
107160
break
108161
}
109162

163+
a.resolveComments(&decl.BaseDeclaration, true)
110164
a.match(token.Identifier)
111165
prev := a.currentToken
112166
decl.Id = a.currentToken.Value
@@ -125,11 +179,13 @@ func (a *AstGenerator) readConstDeclaration() ConstDeclaration {
125179

126180
if a.currentToken.Type == token.StringValue || a.currentToken.Type == token.IntValue || a.currentToken.Type == token.IOTA {
127181
decl.Value = a.currentToken.Value
182+
a.resolveComments(&decl.BaseDeclaration, false)
128183
declarators = append(declarators, decl)
129184
} else {
130185
a.reportTokenError()
131186
}
132187
} else {
188+
a.resolveComments(&decl.BaseDeclaration, false)
133189
declarators = append(declarators, decl)
134190
if a.currentToken.Type == token.RightParentheses {
135191
break
@@ -141,7 +197,7 @@ func (a *AstGenerator) readConstDeclaration() ConstDeclaration {
141197
a.matchNextLine()
142198
}
143199

144-
return ConstDeclaration{declarators}
200+
return ConstDeclaration{Declarators: declarators}
145201
}
146202

147203
func (a *AstGenerator) match(t token.TokenType) {
@@ -153,6 +209,10 @@ func (a *AstGenerator) match(t token.TokenType) {
153209
func (a *AstGenerator) matchNextLine() {
154210
next := a.Tokens[a.index+1]
155211

212+
if next.Type == token.LineComment {
213+
next = a.Tokens[a.index+2]
214+
}
215+
156216
if next.Type == token.Semicolon {
157217
a.nextToken(true)
158218
return
@@ -192,5 +252,5 @@ func (a *AstGenerator) Gen() File {
192252
}
193253

194254
func NewAstGenerator(tokens []token.Token) AstGenerator {
195-
return AstGenerator{Tokens: tokens, index: -1}
255+
return AstGenerator{Tokens: tokens, index: -1, usedComments: map[token.Token]bool{}}
196256
}

src/ast/ast_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestNormal(t *testing.T) {
1515
a := path.Join(wd, "../test-cases/normal.go")
1616
parser := token.NewParser(a)
1717
tokens := parser.Parse()
18-
ast := AstGenerator{Tokens: tokens, index: -1}
18+
ast := NewAstGenerator(tokens)
1919

2020
result := ast.Gen()
2121

src/ast/type.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,28 @@ const (
77
Int = "int"
88
)
99

10+
type BaseDeclaration struct {
11+
LeadingComments []Comment
12+
TrailingComments []Comment
13+
}
14+
1015
type TypeDeclaration struct {
16+
BaseDeclaration
1117
Id string
1218
Kind TypeKind
1319
}
1420

1521
type ConstDeclaration struct {
22+
BaseDeclaration
1623
Declarators []ConstDeclarator
1724
}
1825

26+
type Comment struct {
27+
Value string
28+
}
29+
1930
type ConstDeclarator struct {
31+
BaseDeclaration
2032
Kind string
2133
Id string
2234
Value string

src/generator/__snapshots__/generator_test.snap

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
[TestNormal - 1]
33
namespace some {
44
export enum Sex {
5+
// dsdas
56
Female = 'female',
6-
Male = 'male',
7+
Male = 'male', //hhh
8+
79
}
810
export enum Status {
9-
Todo = 0,
11+
// 已完成
1012
Done = 1,
11-
Pending = 2,
1213
InProgress = 3,
14+
Pending = 2,
15+
// 代办
16+
Todo = 0, // 59todo
17+
1318
}
1419
}
1520

0 commit comments

Comments
 (0)