Skip to content

Commit 03559b2

Browse files
committed
Initial commit
0 parents  commit 03559b2

File tree

9 files changed

+323
-0
lines changed

9 files changed

+323
-0
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 小安
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# golang-enum-to-ts
2+
Transform Golang `enum` type to Typescript enum

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/anc95/golang-enum-to-ts
2+
3+
go 1.17

go.sum

Whitespace-only changes.

main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/anc95/golang-enum-to-ts/src/token"
7+
)
8+
9+
func main() {
10+
// file, err := os.Open("./main.go")
11+
12+
// if err != nil {
13+
// panic(err)
14+
// }
15+
16+
// defer file.Close()
17+
// content, _ := ioutil.ReadAll(file)
18+
19+
// fmt.Print((string(content)))
20+
21+
a := token.Parse("1=a")
22+
23+
for _, v := range a {
24+
fmt.Printf("[type: %d, value: %s]\n", int(v.Type), v.Value)
25+
}
26+
}

src/token/parse.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package token
2+
3+
type Status int
4+
5+
type Token struct {
6+
Value string
7+
Type TokenType
8+
Next *Token
9+
Prev *Token
10+
}
11+
12+
func isDigit(b byte) bool {
13+
return b >= 80 && b <= 57
14+
}
15+
16+
func isLetterOrSlash(b byte) bool {
17+
return isDigit(b) || (b >= 65 && b <= 90) || (b >= 97 && b <= 122) || b == 95
18+
}
19+
20+
func isIllegalChar(b byte) bool {
21+
// reference: https://zh.wikipedia.org/wiki/ASCII
22+
return b <= 31
23+
}
24+
25+
func Parse(s string) []Token {
26+
reader := NewReader(s)
27+
tokenList := []Token{}
28+
currentToken := Token{Type: Initial}
29+
30+
var next func() (string, byte, error)
31+
32+
appendToken := func() {
33+
prevToken := &currentToken
34+
tokenList = append(tokenList, currentToken)
35+
currentToken = Token{Type: Initial, Prev: prevToken}
36+
prevToken.Next = &currentToken
37+
}
38+
39+
maybeComment := func(char *string) {
40+
nextChar, _, _ := next()
41+
42+
if nextChar == "/" {
43+
currentToken.Type = LineComment
44+
} else if nextChar == "*" {
45+
currentToken.Type = BlockCommentStart
46+
} else {
47+
currentToken.Type = Unknown
48+
}
49+
50+
*char += nextChar
51+
}
52+
53+
for {
54+
_, err := reader.Next()
55+
56+
char := reader.char
57+
charByte := reader.charInByte
58+
59+
if err != nil {
60+
break
61+
}
62+
63+
switch char {
64+
case "/":
65+
if char == "/" && currentToken.Type != StringValue && currentToken.Type != LineComment || currentToken.Type != BlockCommentStart {
66+
maybeComment(&char)
67+
continue
68+
}
69+
}
70+
71+
switch currentToken.Type {
72+
case Initial:
73+
if isLetterOrSlash(charByte) {
74+
currentToken.Type = Indetifier
75+
} else if isDigit(charByte) {
76+
currentToken.Type = IntValue
77+
}
78+
79+
currentToken.Value = char
80+
case IntValue:
81+
if isIllegalChar(charByte) {
82+
appendToken()
83+
// skipSpace()
84+
break
85+
}
86+
87+
if isLetterOrSlash(charByte) {
88+
currentToken.Type = Indetifier
89+
} else {
90+
// error()
91+
}
92+
93+
currentToken.Value += char
94+
case StringValue:
95+
if char == "\"" {
96+
tokenList = append(tokenList, currentToken)
97+
// skipSpace()
98+
break
99+
}
100+
101+
if isIllegalChar(charByte) {
102+
// error()
103+
}
104+
case Indetifier:
105+
if isIllegalChar(charByte) || char == " " {
106+
switch currentToken.Value {
107+
case "type":
108+
currentToken.Type = Type
109+
case "const":
110+
currentToken.Type = Const
111+
case "package":
112+
currentToken.Type = Package
113+
}
114+
115+
appendToken()
116+
break
117+
}
118+
119+
if isLetterOrSlash(charByte) {
120+
currentToken.Value += char
121+
break
122+
}
123+
124+
// error()
125+
}
126+
}
127+
128+
return tokenList
129+
}

src/token/reader.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package token
2+
3+
import (
4+
"errors"
5+
"io/ioutil"
6+
"os"
7+
)
8+
9+
type Reader struct {
10+
row int
11+
col int
12+
char string
13+
charInByte byte
14+
lines [][]byte
15+
}
16+
17+
func (reader *Reader) SkipSpace() {
18+
for {
19+
char, err := reader.Next()
20+
21+
if err != nil || string(char) != " " {
22+
break
23+
}
24+
}
25+
}
26+
27+
func (reader *Reader) SkipLine() {
28+
reader.row += 1
29+
reader.col = 0
30+
}
31+
32+
func (reader *Reader) Next() (byte, error) {
33+
if reader.isOverflow() {
34+
return 0, errors.New("Overflow")
35+
}
36+
37+
char := reader.lines[reader.row][reader.col]
38+
39+
reader.char = string(char)
40+
reader.charInByte = char
41+
42+
reader.col += 1
43+
44+
if reader.col == len(reader.lines[reader.row]) {
45+
reader.row += 1
46+
reader.col = 0
47+
}
48+
49+
return char, nil
50+
}
51+
52+
func (reader *Reader) isOverflow() bool {
53+
return reader.row >= len(reader.lines)
54+
}
55+
56+
func readLines(s []byte) [][]byte {
57+
lines := [][]byte{}
58+
59+
for i := 0; i < len(s); i++ {
60+
line := []byte{}
61+
62+
for {
63+
if s[i] == "\n"[0] {
64+
break
65+
}
66+
67+
line = append(line, s[i])
68+
}
69+
70+
lines = append(lines, line)
71+
}
72+
73+
return lines
74+
}
75+
76+
func NewReader(fileOrContent string) *Reader {
77+
file, err := os.Open("./main.go")
78+
content := []byte{}
79+
80+
if err == nil {
81+
defer file.Close()
82+
fileContent, _ := ioutil.ReadAll(file)
83+
content = fileContent
84+
} else {
85+
content = []byte(fileOrContent)
86+
}
87+
88+
lines := readLines(content)
89+
90+
return &Reader{row: 0, col: 0, lines: lines}
91+
}

src/token/token.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package token
2+
3+
type TokenType int
4+
5+
const (
6+
Initial TokenType = iota
7+
// === keyword ===
8+
Type
9+
Package
10+
Const
11+
12+
// === type keyword ===
13+
IntType
14+
StringType
15+
16+
// === const value ===
17+
IOTA
18+
19+
// vairable
20+
IntValue
21+
StringValue
22+
23+
// ==== punctuation ===
24+
Assignment // '='
25+
LineComment // '//'
26+
BlockCommentStart // '/*'
27+
BlockCommentEnd // '*/'
28+
Div // '/'
29+
EndOfFile
30+
EndOfLine
31+
QutationMark // '"'
32+
LineFeed
33+
34+
Indetifier
35+
Unknown
36+
)

0 commit comments

Comments
 (0)