-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
147 lines (117 loc) · 2.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"io/ioutil"
"log"
//"os"
)
//see
//http://golang.org/src/pkg/text/template/parse/lex.go
/*
Top Level
First Pass:
- split code into tokens
- find functions signatures and code blocks
Second Pass:
- parse statements
-
*/
var (
BGN_BLK = 0 // {
END_BLK = 1 // }
BGN_PAREN = 2
END_PAREN = 3
VERB_FUNC = 4 // func keyword
STATEMENT = 5 // statement until ;
)
//depth
//tokens are trees
type Token struct {
File string //use int
Line int //line number
Offset int //offset
}
type Parser struct {
TokenList []Token
}
func (self *Parser) Next() {
}
/*
*/
type Cursor struct {
Idx int
Line int
}
//
func extractBlock(in []byte, cursor Cursor) (string, []byte) {
if in[cursor.Idx] != byte('{') {
log.Panic()
}
d := 1
max := len(in)
for i := cursor.Idx; i < max; i++ {
if in[i] == byte('{') {
d++ //increment depth
continue
}
if in[i] == byte('}') {
d--
if d == 0 {
break //end bracket
}
}
if in[i] == byte('\n') {
cursor.Line++
}
continue
}
if d != 0 {
log.Panic("error unterminated block")
}
//do something
return "", nil
}
func getNextTok(in []byte, cursor *Cursor) (string, []byte) {
for i, c := range in {
if c == byte(' ') {
return string(in[:i]), in[i:]
}
}
return "", nil
}
/*
func getNextTok(in []byte) (string, []byte) {
for i, c := range in {
if c == byte(' ') {
return string(in[:i]), in[i:]
}
}
return "", nil
}
*/
func main() {
data, err := ioutil.ReadFile("testfile.c")
if err != nil {
log.Fatal(err)
}
var cursor Cursor
fmt.Printf("read %d bytes: %q\n", len(data), data)
tok, data := getNextTok(data, &cursor)
fmt.Printf("split: %s, %s \n", tok, data)
switch {
case tok == "import":
//start import block
fmt.Printf("tok, start import= %s \n", tok)
case tok == "var":
//start var definition
fmt.Printf("tok, start var= %s \n", tok)
case tok == "func":
//start function definit
fmt.Printf("tok, start func= %s \n", tok)
case tok == "type":
//start struct definition
fmt.Printf("tok, start type= %s \n", tok)
default:
fmt.Printf("error: line, char ... expecting valid symbol \n")
}
}