Skip to content

Commit

Permalink
chore: add token position info
Browse files Browse the repository at this point in the history
  • Loading branch information
anc95 committed Feb 27, 2022
1 parent 1ad5a18 commit db6a2c5
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 78 deletions.
8 changes: 4 additions & 4 deletions src/generator/__snapshots__/generator_test.snap
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

[TestNormal - 1]
namespace some {
export enum Sex {
Female = 'female',
Male = 'male',
}
export enum Status {
Todo = 0,
Done = 1,
Pending = 2,
InProgress = 3,
}
export enum Sex {
Female = 'female',
Male = 'male',
}
}

---
11 changes: 9 additions & 2 deletions src/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generator

import (
"fmt"
"sort"
"strconv"

"github.com/anc95/golang-enum-to-ts/src/ast"
Expand Down Expand Up @@ -75,10 +76,16 @@ func GenerateTS(f ast.File) string {
enumValue := getEnum(f)
result := ""

for key, val := range enumValue {
keys := make([]string, 0)
for k, _ := range enumValue {
keys = append(keys, k)
}
sort.Strings(keys)

for _, key := range keys {
ret := ""

for k, v := range val {
for k, v := range enumValue[key] {
if ret != "" {
ret += "\n"
}
Expand Down
1 change: 1 addition & 0 deletions src/test-cases/normal.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package some

// hello
type Status int

const (
Expand Down
126 changes: 66 additions & 60 deletions src/token/__snapshots__/token_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,182 +4,188 @@
{
Value: "package",
Type: "Package",
Start: {0, 6},
End: {0, 0},
Start: {0, 0},
End: {0, 6},
},
{
Value: "some",
Type: "Identifier",
Start: {0, 11},
End: {0, 0},
Start: {0, 8},
End: {0, 11},
},
{
Value: " hello\n",
Type: "LineComment",
Start: {2, 1},
End: {2, 8},
},
{
Value: "type",
Type: "Type",
Start: {2, 3},
End: {0, 0},
Start: {3, 0},
End: {3, 3},
},
{
Value: "Status",
Type: "Identifier",
Start: {2, 10},
End: {0, 0},
Start: {3, 5},
End: {3, 10},
},
{
Value: "int",
Type: "IntType",
Start: {2, 14},
End: {0, 0},
Start: {3, 12},
End: {3, 14},
},
{
Value: "const",
Type: "Const",
Start: {4, 5},
End: {0, 0},
Start: {5, 0},
End: {5, 5},
},
{
Value: "(",
Type: "LeftParentheses",
Start: {4, 6},
End: {0, 0},
Start: {5, 6},
End: {5, 6},
},
{
Value: "Todo",
Type: "Identifier",
Start: {5, 4},
End: {0, 0},
Start: {6, 1},
End: {6, 4},
},
{
Value: "Status",
Type: "Identifier",
Start: {5, 11},
End: {0, 0},
Start: {6, 6},
End: {6, 11},
},
{
Value: "=",
Type: "Assignment",
Start: {5, 13},
End: {0, 0},
Start: {6, 13},
End: {6, 13},
},
{
Value: "iota",
Type: "IOTA",
Start: {5, 18},
End: {0, 0},
Start: {6, 15},
End: {6, 18},
},
{
Value: "Done",
Type: "Identifier",
Start: {6, 4},
End: {0, 0},
Start: {7, 1},
End: {7, 4},
},
{
Value: "Pending",
Type: "Identifier",
Start: {7, 7},
End: {0, 0},
Start: {8, 1},
End: {8, 7},
},
{
Value: "InProgress",
Type: "Identifier",
Start: {8, 10},
End: {0, 0},
Start: {9, 1},
End: {9, 10},
},
{
Value: ")",
Type: "RightParentheses",
Start: {9, 0},
End: {0, 0},
Start: {10, 0},
End: {10, 0},
},
{
Value: "type",
Type: "Type",
Start: {11, 3},
End: {0, 0},
Start: {12, 0},
End: {12, 3},
},
{
Value: "Sex",
Type: "Identifier",
Start: {11, 7},
End: {0, 0},
Start: {12, 5},
End: {12, 7},
},
{
Value: "string",
Type: "StringType",
Start: {11, 14},
End: {0, 0},
Start: {12, 9},
End: {12, 14},
},
{
Value: "const",
Type: "Const",
Start: {13, 5},
End: {0, 0},
Start: {14, 0},
End: {14, 5},
},
{
Value: "(",
Type: "LeftParentheses",
Start: {13, 6},
End: {0, 0},
Start: {14, 6},
End: {14, 6},
},
{
Value: "Female",
Type: "Identifier",
Start: {14, 6},
End: {0, 0},
Start: {15, 1},
End: {15, 6},
},
{
Value: "Sex",
Type: "Identifier",
Start: {14, 10},
End: {0, 0},
Start: {15, 8},
End: {15, 10},
},
{
Value: "=",
Type: "Assignment",
Start: {14, 12},
End: {0, 0},
Start: {15, 12},
End: {15, 12},
},
{
Value: "female",
Type: "StringValue",
Start: {14, 21},
End: {0, 0},
Start: {15, 14},
End: {15, 21},
},
{
Value: "Male",
Type: "Identifier",
Start: {15, 4},
End: {0, 0},
Start: {16, 1},
End: {16, 4},
},
{
Value: "Sex",
Type: "Identifier",
Start: {15, 10},
End: {0, 0},
Start: {16, 8},
End: {16, 10},
},
{
Value: "=",
Type: "Assignment",
Start: {15, 12},
End: {0, 0},
Start: {16, 12},
End: {16, 12},
},
{
Value: "male",
Type: "StringValue",
Start: {15, 19},
End: {0, 0},
Start: {16, 14},
End: {16, 19},
},
{
Value: ")",
Type: "RightParentheses",
Start: {16, 0},
End: {0, 0},
Start: {17, 0},
End: {17, 0},
},
{
Value: "func Abctext() {\n\t//dadsad\n}\n",
Type: "Unknown",
Start: {18, 0},
End: {0, 0},
Start: {19, 0},
End: {21, 1},
},
}
---
38 changes: 27 additions & 11 deletions src/token/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ func (parser *Parser) appendToken() {

func (parser *Parser) setCurrentTokenType(t TokenType) {
parser.CurrentToken.Type = t
parser.CurrentToken.Start = [2]int{parser.Reader.row, parser.Reader.col}

if t == Unknown {
if len(parser.CurrentToken.Value) == 0 {
parser.CurrentToken.Start = [2]int{parser.Reader.row, parser.Reader.col}
}

switch t {
case Unknown:
parser.CurrentToken.Start = [2]int{parser.Reader.row, 0}

index := len(parser.Tokens) - 1
Expand All @@ -44,16 +48,31 @@ func (parser *Parser) setCurrentTokenType(t TokenType) {
parser.Tokens = parser.Tokens[0 : index+1]

parser.CurrentToken.Value = parser.collectUnknown()
} else if t == Assignment {
case Assignment:
parser.CurrentToken.Value = "="
} else if t == LeftParentheses {
case LeftParentheses:
parser.CurrentToken.Value = "("
} else if t == RightParentheses {
case RightParentheses:
parser.CurrentToken.Value = ")"
} else if t == Semicolon {
case Semicolon:
parser.CurrentToken.Value = ";"
case LineComment:
parser.CurrentToken.Value = parser.collectLineComment()
case StringValue:
parser.CurrentToken.Value = parser.collectString()
case IntValue:
parser.CurrentToken.Value = parser.collectInt()
case Identifier:
if len(parser.CurrentToken.Value) > 0 {
break
}

parser.CurrentToken.Value = parser.collectIdentifier()
parser.setCurrentTokenType(parser.getIdentifierTokenType(parser.CurrentToken.Value))
return
}

parser.CurrentToken.End = [2]int{parser.Reader.row, parser.Reader.col}
parser.appendToken()
}

Expand Down Expand Up @@ -116,6 +135,7 @@ func (parser *Parser) collectLineComment() string {
result := string(row[parser.Reader.col+1:])

parser.Reader.SkipLine()
parser.Reader.Back()

return result
}
Expand Down Expand Up @@ -218,23 +238,19 @@ func (parser *Parser) Parse() []Token {
}

if string(nextCharInByte) == "/" {
parser.CurrentToken.Value = parser.collectLineComment()
parser.setCurrentTokenType(LineComment)
} else if string(nextCharInByte) == "*" {
parser.setCurrentTokenType(LeftParentheses)
} else {
parser.setCurrentTokenType(Unknown)
}
case "\"":
parser.CurrentToken.Value = parser.collectString()
parser.setCurrentTokenType(StringValue)
default:
if IsDigit(charInByte) {
parser.CurrentToken.Value = parser.collectInt()
parser.setCurrentTokenType(IntValue)
} else if IsLetterOrSlash(charInByte) {
parser.CurrentToken.Value = parser.collectIdentifier()
parser.setCurrentTokenType(parser.getIdentifierTokenType(parser.CurrentToken.Value))
parser.setCurrentTokenType(Identifier)
} else {
parser.setCurrentTokenType(Unknown)
}
Expand Down
2 changes: 1 addition & 1 deletion src/token/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (reader *Reader) Next() (byte, error) {
}

func (reader *Reader) Back() {
if reader.col == 0 {
if reader.col <= 0 {
reader.row -= 1

if reader.row >= 0 {
Expand Down

0 comments on commit db6a2c5

Please sign in to comment.