Skip to content

Commit 969f932

Browse files
committed
fix: tok error
1 parent aed79c8 commit 969f932

8 files changed

Lines changed: 68 additions & 28 deletions

File tree

cmd/v5.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ func main() {
3232

3333
f, err := os.OpenFile(*out, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
3434
if err != nil {
35-
fmt.Println(err.Error())
35+
fmt.Println("openFile err: ", err.Error())
3636
}
3737
defer f.Close()
3838
wr = f
3939
}
4040

4141
creator := v5.ParseSql(string(bs))
42+
4243
if err := creator.Save(wr); err != nil {
4344
fmt.Println(err.Error())
4445
}

models/shop_order.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v5/ast.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ type StmtNode interface {
111111
node
112112
Receive(nod node, lexer yyLexer)
113113
}
114+
114115
type CreateTableStmt struct {
115116
NameNode
116117
ColumnsStmt *ColumnsStmt
@@ -131,6 +132,7 @@ func (c *CreateTableStmt) Receive(nod node, s yyLexer) {
131132
default:
132133
unexpectedNode(nod, s)
133134
}
135+
134136
}
135137
func (c *CreateTableStmt) Type() NodeType {
136138
return ndCreateTableStmt

v5/gen.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ func (c *Creator) StructName() string {
5252
return exportName(c.NameNode.s)
5353
}
5454
func (c *Creator) Create() *ast.GenDecl {
55+
5556
fields := make([]*ast.Field, 0, len(*c.ColumnsStmt))
5657
for _, field := range *c.ColumnsStmt {
5758
fields = append(fields, &ast.Field{
5859
Names: []*ast.Ident{
5960
&ast.Ident{
60-
Name: field.NameNode.s,
61+
Name: exportName(field.NameNode.s),
6162
},
6263
},
6364
Type: &ast.Ident{
@@ -164,28 +165,32 @@ func (c *Creator) Save(wr io.Writer) error {
164165
Name: "models",
165166
NamePos: token.Pos(len("Package") + 2),
166167
}}
167-
fn := func(col *ColumnStmt) bool {
168-
return col.t == "timestamp"
169-
}
170-
if c.ColumnsStmt.ExistF(fn) {
171-
ff.Decls = append(ff.Decls, &ast.GenDecl{
172-
Tok: token.IMPORT,
173-
Specs: []ast.Spec{
174-
&ast.ImportSpec{
175-
Path: &ast.BasicLit{
176-
Kind: token.STRING,
177-
Value: fmt.Sprintf("\"%s\"", "time"),
168+
169+
if c.ColumnsStmt != nil {
170+
fn := func(col *ColumnStmt) bool {
171+
return col.t == "timestamp"
172+
}
173+
if c.ColumnsStmt.ExistF(fn) {
174+
ff.Decls = append(ff.Decls, &ast.GenDecl{
175+
Tok: token.IMPORT,
176+
Specs: []ast.Spec{
177+
&ast.ImportSpec{
178+
Path: &ast.BasicLit{
179+
Kind: token.STRING,
180+
Value: fmt.Sprintf("\"%s\"", "time"),
181+
},
178182
},
179183
},
180-
},
181-
})
184+
})
185+
}
182186
}
187+
183188
ff.Decls = append(ff.Decls,
184189
c.Create(),
185190
c.PkFunc(),
186191
c.TableFunc(),
187192
)
188-
fmt.Println("len:", len(ff.Decls))
193+
//fmt.Println("len:", len(ff.Decls))
189194
if _, err := wr.Write([]byte(cm)); err != nil {
190195
return err
191196
}

v5/lexer.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ type lex struct {
1313
size int
1414
pos int
1515
err error
16-
Stmt CreateTableStmt
16+
Stmt *CreateTableStmt
1717
prev *yySymType
1818
}
1919

20-
func (l *lex) SetStmt(st CreateTableStmt) {
20+
func (l *lex) SetStmt(st *CreateTableStmt) {
2121
l.Stmt = st
2222
}
2323
func (l *lex) Reduced(rule, state int, lval *yySymType) bool {
@@ -50,8 +50,8 @@ func (l *lex) Lex(lval *yySymType) int {
5050
lval.pos = l.pos
5151
defer func() {
5252
l.prev = lval
53+
//fmt.Printf("lval: %+v \n", lval)
5354
}()
54-
fmt.Println(string(ch), ch)
5555
switch ch {
5656
case '`':
5757
return TILDE
@@ -74,8 +74,9 @@ func (l *lex) Lex(lval *yySymType) int {
7474
if isLetter(ch) {
7575
str := l.readString()
7676
tok := findKeyword(str)
77+
7778
if tok > 0 {
78-
return tok + 1
79+
return tok
7980
}
8081
lval.s = str
8182
return tString

v5/out.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v5/parse.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func ParseSql(sql string) *Creator {
2929
pos: 0,
3030
}
3131
yyParse(&l)
32-
c := Creator(l.Stmt)
32+
//fmt.Printf("stmt: %+v \n", l.Stmt)
33+
c := Creator(*l.Stmt)
3334
return &c
3435
}

v5/parser.y

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
%{
22
package v5
3-
43
var ftm map[string]int //field type map
54
func init() {
65
ftm = map[string]int{
@@ -39,13 +38,13 @@ func init() {
3938
ColumnStmt TableIndexListStmt TableIndexStmt NameListStmt
4039

4140

42-
%token LPAREN '('
41+
%token LPAREN "("
4342
LBRACK "["
4443
LBRACE "{"
4544
COMMA ","
4645
PERIOD "."
4746

48-
RPAREN ')'
47+
RPAREN ")"
4948
RBRACK "]"
5049
RBRACE "}"
5150
SEMICOLON ";"
@@ -87,9 +86,13 @@ func init() {
8786
tpDate
8887
tpDatetime
8988
tpTimestamp
89+
90+
%start main
91+
9092
%%
93+
9194
main: CreateTableStmt {
92-
yylex.(*lex).Stmt=*$1.(*CreateTableStmt)
95+
yylex.(*lex).Stmt=$1.( *CreateTableStmt)
9396
}
9497

9598
CreateTableStmt:

0 commit comments

Comments
 (0)