Skip to content

Commit 6eefc86

Browse files
authored
Add files via upload
1 parent 47c7ebd commit 6eefc86

8 files changed

Lines changed: 1905 additions & 0 deletions

File tree

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module miniSQL
2+
3+
go 1.17

main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"miniSQL/src/parser"
8+
)
9+
10+
func main() {
11+
s := strings.NewReader("creat table tabel_name where key <= 1")
12+
l := parser.NewScanner(s)
13+
for {
14+
tok, str := l.Scan()
15+
fmt.Print(tok)
16+
fmt.Print(" ")
17+
fmt.Println(str)
18+
if tok == 1 {
19+
break
20+
}
21+
}
22+
}

src/lexer/lexer.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package lexer
2+
3+
type Token int
4+
5+
// 工具类:接受由Scanner传入的经过基本处理的token,通过lookahead来赋予该token更多信息
6+
type Tokenizer interface {
7+
FromStrLit(lit string, TokenType Token, lastToken int) int
8+
}
9+
10+
// 工具类:初步处理输入,得到基础的token
11+
type Scanner interface {
12+
Scan() (tok Token, lit string)
13+
}
14+
15+
// 模块类:将输入解析为token的总体工具类
16+
type LexerImpl struct {
17+
scanner *Scanner // 处理输入的工具类
18+
tokenizer Tokenizer // 进一步赋予token信息的工具类
19+
Result interface{}
20+
}
21+
22+
// 存储类:存储token解析的结果,最终的Lex()主要利用这个对象来返回结果
23+
type LexerResult struct {
24+
Token int
25+
Literal string
26+
}

src/parser/error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package parser

src/parser/lexerWrapper.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package parser
2+
3+
import (
4+
"miniSQL/src/lexer"
5+
)
6+
7+
// lex的包装类
8+
type lexerWrapper struct {
9+
impl *lexer.LexerImpl
10+
// channelSend chan<- types.DStatements
11+
lastLiteral string // 向前看一位
12+
err error
13+
}

src/parser/scanner.go

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
package parser
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"io"
7+
)
8+
9+
type Token int
10+
11+
// 词法分析中的初步结果(部分内容可以经过tokenize来形成语义更加明确的token)
12+
const (
13+
// 特殊标记
14+
ILLEGAL Token = iota
15+
EOF
16+
WS // 空白字符
17+
// 常规类型数据
18+
IDENT // ID,此时我们并不区分关键词,而是归类到同一类
19+
INTEGER // 整数
20+
FLOAT // 浮点数
21+
STRING // 字符串
22+
// 其他标记
23+
ASTERISK // *
24+
COMMA // ,
25+
LEFT_PARENTHESIS // (
26+
RIGHT_PARENTHESIS // )
27+
SEMICOLON // ;
28+
EQUAL // =
29+
ANGLE_LEFT // <
30+
ANGLE_LEFT_EQUAL //<=
31+
ANGLE_RIGHT_EQUAL //>=
32+
ANGLE_RIGHT // >
33+
NOT_EQUAL // <> or !=
34+
POINT // .
35+
)
36+
37+
type State int // 状态机的状态
38+
39+
const (
40+
STATE_INIT State = iota
41+
STATE_INTEGER
42+
STATE_POINT
43+
STATE_FRACTION
44+
STATE_IDENT
45+
STATE_ANGLE_LEFT
46+
STATE_ANGLE_RIGHT
47+
STATE_END
48+
)
49+
50+
type CharType int // 单个字符的数据类型
51+
52+
const (
53+
NUM CharType = iota
54+
CHAR
55+
SPECIAL_SYMBOL
56+
ILLEGAL_SYMBOL
57+
SPACE
58+
UNDERLINE
59+
)
60+
61+
// eof represents a marker rune for the end of the reader.
62+
var eof = rune(0)
63+
64+
type InputScanner struct {
65+
r *bufio.Reader
66+
apostropne bool // apostropne is true means
67+
}
68+
69+
func NewScanner(r io.Reader) *InputScanner {
70+
return &InputScanner{r: bufio.NewReader(r), apostropne: false}
71+
}
72+
73+
// scanner不断从输入流中读取数据,尝试拼接出一个个初步解析的token
74+
func (s *InputScanner) Scan() (tok Token, lit string) {
75+
ch := s.read()
76+
var buf bytes.Buffer
77+
state := STATE_INIT
78+
for state != STATE_END {
79+
if checkCharType(ch) == ILLEGAL_SYMBOL {
80+
return ILLEGAL, string(ch)
81+
}
82+
// buf.WriteRune(ch)
83+
switch state {
84+
case STATE_INIT:
85+
switch checkCharType(ch) {
86+
case NUM:
87+
buf.WriteRune(ch)
88+
state = STATE_INTEGER
89+
case CHAR:
90+
buf.WriteRune(ch)
91+
state = STATE_IDENT
92+
case SPECIAL_SYMBOL:
93+
switch ch {
94+
case eof:
95+
return EOF, ""
96+
case '.':
97+
return POINT, string(ch)
98+
case '*':
99+
return ASTERISK, string(ch)
100+
case ',':
101+
return COMMA, string(ch)
102+
case '(':
103+
return LEFT_PARENTHESIS, string(ch)
104+
case ')':
105+
return RIGHT_PARENTHESIS, string(ch)
106+
case ';':
107+
return SEMICOLON, string(ch)
108+
case '=':
109+
return EQUAL, string(ch)
110+
case '<':
111+
buf.WriteRune(ch)
112+
state = STATE_ANGLE_LEFT
113+
case '>':
114+
buf.WriteRune(ch)
115+
state = STATE_ANGLE_RIGHT
116+
}
117+
case SPACE:
118+
case UNDERLINE:
119+
return ILLEGAL, string(ch)
120+
}
121+
case STATE_INTEGER:
122+
switch checkCharType(ch) {
123+
case NUM:
124+
buf.WriteRune(ch)
125+
case CHAR, SPACE, UNDERLINE:
126+
s.unread()
127+
return INTEGER, buf.String()
128+
case SPECIAL_SYMBOL:
129+
if ch == '.' {
130+
buf.WriteRune(ch)
131+
state = STATE_POINT
132+
} else {
133+
s.unread()
134+
return INTEGER, buf.String()
135+
}
136+
}
137+
case STATE_POINT:
138+
switch checkCharType(ch) {
139+
case NUM:
140+
buf.WriteRune(ch)
141+
state = STATE_FRACTION
142+
case CHAR, SPECIAL_SYMBOL, SPACE, UNDERLINE:
143+
return ILLEGAL, string(ch)
144+
}
145+
case STATE_FRACTION:
146+
switch checkCharType(ch) {
147+
case NUM:
148+
buf.WriteRune(ch)
149+
case CHAR, SPECIAL_SYMBOL, SPACE, UNDERLINE:
150+
s.unread()
151+
return FLOAT, buf.String()
152+
}
153+
case STATE_IDENT:
154+
switch checkCharType(ch) {
155+
case NUM, CHAR, UNDERLINE:
156+
buf.WriteRune(ch)
157+
case SPECIAL_SYMBOL, SPACE:
158+
s.unread()
159+
return IDENT, buf.String()
160+
}
161+
case STATE_ANGLE_LEFT:
162+
switch checkCharType(ch) {
163+
case NUM, CHAR, SPACE:
164+
s.unread()
165+
return ANGLE_LEFT, buf.String()
166+
case SPECIAL_SYMBOL:
167+
// ch = s.read()
168+
if ch == '=' {
169+
return ANGLE_LEFT_EQUAL, "<="
170+
} else if ch == '>' {
171+
return NOT_EQUAL, "<>"
172+
} else {
173+
s.unread()
174+
return ANGLE_LEFT, buf.String()
175+
}
176+
}
177+
case STATE_ANGLE_RIGHT:
178+
switch checkCharType(ch) {
179+
case NUM, CHAR, SPACE:
180+
s.unread()
181+
return ANGLE_RIGHT, buf.String()
182+
case SPECIAL_SYMBOL:
183+
// ch = s.read()
184+
if ch == '=' {
185+
return ANGLE_RIGHT_EQUAL, ">="
186+
} else {
187+
s.unread()
188+
return ANGLE_RIGHT, buf.String()
189+
}
190+
}
191+
}
192+
ch = s.read()
193+
}
194+
195+
return ILLEGAL, string(ch)
196+
}
197+
198+
// read reads the next rune from the buffered reader.
199+
// Returns the rune(0) if an error occurs (or io.EOF is returned).
200+
func (s *InputScanner) read() rune {
201+
ch, _, err := s.r.ReadRune()
202+
if err != nil {
203+
return eof
204+
}
205+
return ch
206+
}
207+
208+
// unread places the previously read rune back on the reader.
209+
func (s *InputScanner) unread() { _ = s.r.UnreadRune() }
210+
211+
func checkCharType(ch rune) CharType {
212+
if ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' {
213+
return CHAR
214+
} else if ch >= '0' && ch <= '9' {
215+
// fmt.Println("检测到数字")
216+
return NUM
217+
} else if ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' {
218+
return SPACE
219+
} else if ch == '.' || ch == '*' || ch == ',' || ch == '(' || ch == ')' || ch == ';' || ch == '=' || ch == '<' || ch == '>' || ch == eof {
220+
return SPECIAL_SYMBOL
221+
} else if ch == '_' {
222+
return UNDERLINE
223+
} else {
224+
return ILLEGAL_SYMBOL
225+
}
226+
}

0 commit comments

Comments
 (0)