Skip to content

Commit 08cc856

Browse files
authored
Merge pull request #8 from anc95/feat-comments-support
feat: add comments-support
2 parents 6d0b650 + 54d3c7e commit 08cc856

File tree

8 files changed

+307
-89
lines changed

8 files changed

+307
-89
lines changed

src/ast/__snapshots__/ast_test.snap

Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,105 @@
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+
},
86+
TrailingComments: {
87+
},
88+
},
89+
Kind: "Sex",
90+
Id: "Female",
91+
Value: "female",
92+
},
93+
{
94+
BaseDeclaration: ast.BaseDeclaration{
95+
LeadingComments: {
96+
},
97+
TrailingComments: {
98+
{Value:"hhh\n"},
99+
},
100+
},
101+
Kind: "Sex",
102+
Id: "Male",
103+
Value: "male",
104+
},
20105
},
21106
},
22107
},

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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
namespace some {
44
export enum Sex {
55
Female = 'female',
6-
Male = 'male',
6+
Male = 'male', //hhh
7+
78
}
89
export enum Status {
9-
Todo = 0,
10+
// 已完成
1011
Done = 1,
11-
Pending = 2,
1212
InProgress = 3,
13+
Pending = 2,
14+
// 代办
15+
Todo = 0, // 59todo
16+
1317
}
1418
}
1519

0 commit comments

Comments
 (0)