Skip to content

Commit fc7dff3

Browse files
authored
Merge pull request #2 from anc95/develop
chore: add ast
2 parents b0c35d6 + 3fdc2ac commit fc7dff3

File tree

9 files changed

+422
-54
lines changed

9 files changed

+422
-54
lines changed

main.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
package main
22

3-
import (
4-
"fmt"
5-
6-
"github.com/anc95/golang-enum-to-ts/src/token"
7-
)
8-
93
func main() {
10-
parser := token.NewParser("func() {\n hell\n xxx\n dsdasdsa\n \n} \na=1\ntype C string //hello\nconst ( A C = 1 \n B \n D")
11-
12-
a := parser.Parse()
13-
for _, v := range a {
14-
fmt.Printf("[type: %d, value: %s]\n", int(v.Type), v.Value)
15-
}
4+
// nothing now
165
}

src/ast/__snapshots__/ast_test.snap

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
[TestNormal - 1]
3+
ast.File{
4+
Name: "some",
5+
Body: {
6+
ast.TypeDeclaration{Id:"Status", Kind:"string"},
7+
ast.ConstDeclaration{
8+
Declarators: {
9+
{Kind:"Status", Id:"Todo", Value:"iota"},
10+
{Kind:"", Id:"Done", Value:""},
11+
{Kind:"", Id:"InProgress", Value:""},
12+
},
13+
},
14+
ast.TypeDeclaration{Id:"Sex", Kind:"int"},
15+
ast.ConstDeclaration{
16+
Declarators: {
17+
{Kind:"Sex", Id:"Female", Value:"female"},
18+
{Kind:"Sex", Id:"Male", Value:"male"},
19+
},
20+
},
21+
},
22+
}
23+
---

src/ast/ast.go

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,190 @@
11
package ast
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/anc95/golang-enum-to-ts/src/token"
8+
)
9+
10+
type AstGenerator struct {
11+
Tokens []token.Token
12+
index int
13+
currentToken token.Token
14+
}
15+
16+
func (a *AstGenerator) nextToken(reportErrorWhenIsNull bool) (token.Token, error) {
17+
a.index += 1
18+
19+
if a.index >= len(a.Tokens) {
20+
if reportErrorWhenIsNull {
21+
a.reportTokenError()
22+
}
23+
24+
return token.Token{}, errors.New("Overflow")
25+
}
26+
27+
a.currentToken = a.Tokens[a.index]
28+
29+
return a.currentToken, nil
30+
}
31+
32+
func (a *AstGenerator) backToken() {
33+
a.index -= 1
34+
35+
a.currentToken = a.Tokens[a.index]
36+
}
37+
38+
func (a *AstGenerator) reportTokenError() {
39+
panic(fmt.Sprintf("Unexpected token at:%s", a.currentToken.Value))
40+
}
41+
42+
func (a *AstGenerator) initFile() File {
43+
file := File{Body: []interface{}{}}
44+
45+
for {
46+
item, err := a.nextToken(false)
47+
48+
if err != nil {
49+
panic("Cant find package declaration")
50+
}
51+
52+
if item.Type == token.LineComment {
53+
continue
54+
}
55+
56+
if item.Type == token.Package {
57+
nextItem, err := a.nextToken(false)
58+
59+
if err != nil {
60+
panic("Cant find package declaration")
61+
}
62+
63+
if nextItem.Type == token.Identifier {
64+
file.Name = nextItem.Value
65+
} else {
66+
a.reportTokenError()
67+
}
68+
69+
break
70+
}
71+
72+
panic("Cant find package declaration")
73+
}
74+
75+
return file
76+
}
77+
78+
func (a *AstGenerator) readTypeDeclaration() TypeDeclaration {
79+
d := TypeDeclaration{}
80+
81+
next, _ := a.nextToken(true)
82+
d.Id = next.Value
83+
84+
next, _ = a.nextToken(true)
85+
86+
if next.Type != token.IntType {
87+
d.Kind = Int
88+
} else {
89+
d.Kind = String
90+
}
91+
92+
return d
93+
}
94+
95+
func (a *AstGenerator) readConstDeclaration() ConstDeclaration {
96+
declarators := []ConstDeclarator{}
97+
98+
a.nextToken(true)
99+
a.match(token.LeftParentheses)
100+
101+
for {
102+
decl := ConstDeclarator{}
103+
104+
a.nextToken(true)
105+
106+
if a.currentToken.Type == token.RightParentheses {
107+
break
108+
}
109+
110+
a.match(token.Identifier)
111+
prev := a.currentToken
112+
decl.Id = a.currentToken.Value
113+
114+
a.nextToken(true)
115+
116+
if a.currentToken.Type != token.Semicolon && prev.Start[0] == a.currentToken.Start[0] {
117+
if a.currentToken.Type != token.Assignment {
118+
a.match(token.Identifier)
119+
decl.Kind = a.currentToken.Value
120+
a.nextToken(true)
121+
}
122+
123+
a.match(token.Assignment)
124+
a.nextToken(true)
125+
126+
if a.currentToken.Type == token.StringValue || a.currentToken.Type == token.IntValue || a.currentToken.Type == token.IOTA {
127+
decl.Value = a.currentToken.Value
128+
declarators = append(declarators, decl)
129+
} else {
130+
a.reportTokenError()
131+
}
132+
} else {
133+
declarators = append(declarators, decl)
134+
if a.currentToken.Type == token.RightParentheses {
135+
break
136+
}
137+
}
138+
139+
a.matchNextLine()
140+
}
141+
142+
return ConstDeclaration{declarators}
143+
}
144+
145+
func (a *AstGenerator) match(t token.TokenType) {
146+
if t != a.currentToken.Type {
147+
a.reportTokenError()
148+
}
149+
}
150+
151+
func (a *AstGenerator) matchNextLine() {
152+
next := a.Tokens[a.index+1]
153+
154+
if next.Type == token.Semicolon {
155+
a.nextToken(true)
156+
return
157+
}
158+
159+
if next.Start[0] > a.currentToken.Start[0] {
160+
return
161+
}
162+
163+
a.reportTokenError()
164+
}
165+
166+
func (a *AstGenerator) Gen() File {
167+
file := a.initFile()
168+
169+
for {
170+
item, err := a.nextToken(false)
171+
172+
if err != nil {
173+
break
174+
}
175+
176+
switch item.Type {
177+
case token.LineComment:
178+
case token.Unknown:
179+
continue
180+
case token.Type:
181+
file.Body = append(file.Body, a.readTypeDeclaration())
182+
case token.Const:
183+
file.Body = append(file.Body, a.readConstDeclaration())
184+
default:
185+
a.reportTokenError()
186+
}
187+
}
188+
189+
return file
190+
}

src/ast/ast_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ast
2+
3+
import (
4+
"os"
5+
"path"
6+
"testing"
7+
8+
"github.com/anc95/golang-enum-to-ts/src/token"
9+
"github.com/gkampitakis/go-snaps/snaps"
10+
)
11+
12+
func TestNormal(t *testing.T) {
13+
wd, _ := os.Getwd()
14+
15+
a := path.Join(wd, "../test-cases/normal.go")
16+
parser := token.NewParser(a)
17+
tokens := parser.Parse()
18+
ast := AstGenerator{Tokens: tokens, index: -1}
19+
20+
result := ast.Gen()
21+
22+
snaps.MatchSnapshot(t, result)
23+
}

src/ast/type.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ast
2+
3+
type TypeKind string
4+
5+
const (
6+
String TypeKind = "string"
7+
Int = "int"
8+
)
9+
10+
type TypeDeclaration struct {
11+
Id string
12+
Kind TypeKind
13+
}
14+
15+
type ConstDeclaration struct {
16+
Declarators []ConstDeclarator
17+
}
18+
19+
type ConstDeclarator struct {
20+
Kind string
21+
Id string
22+
Value string
23+
}
24+
25+
type File struct {
26+
Name string
27+
Body []interface{}
28+
}

src/test-cases/normal.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,17 @@ type Status int
55
const (
66
Todo Status = iota
77
Done
8+
Pending
9+
InProgress
810
)
11+
12+
type Sex string
13+
14+
const (
15+
Female Sex = "female"
16+
Male Sex = "male"
17+
)
18+
19+
func Abctext() {
20+
//dadsad
21+
}

0 commit comments

Comments
 (0)