forked from araddon/qlbridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (101 loc) · 2.96 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
package main
import (
"fmt"
"github.com/araddon/qlbridge/lex"
"strings"
)
/*
This example is meant to show how to create a new
Dialect Language with a keyword"SUBSCRIBETO"
then Lex an example of this syntax
*/
var (
// We need a token to recognize our "SUBSCRIBETO" keyword
// in our PUBSUB dialect
TokenSubscribeTo lex.TokenType = 1000
// We are going to create our own Dialect now
// that uses a "SUBSCRIBETO" keyword
pubsub = &lex.Clause{Token: TokenSubscribeTo, Clauses: []*lex.Clause{
{Token: TokenSubscribeTo, Lexer: lex.LexColumns},
{Token: lex.TokenFrom, Lexer: LexMaybe},
{Token: lex.TokenWhere, Lexer: lex.LexColumns, Optional: true},
}}
ourDialect = &lex.Dialect{
Name: "Subscribe To", Statements: []*lex.Clause{pubsub},
}
)
func init() {
// inject any new tokens into QLBridge.Lex describing the custom tokens we created
lex.TokenNameMap[TokenSubscribeTo] = &lex.TokenInfo{Description: "subscribeto"}
// OverRide the Identity Characters in lexer to allow a dash in identity
lex.IDENTITY_CHARS = "_./-"
lex.LoadTokenInfo()
ourDialect.Init()
}
func verifyLexerTokens(l *lex.Lexer, tokens []lex.Token) {
for _, goodToken := range tokens {
tok := l.NextToken()
if tok.T != goodToken.T || tok.V != goodToken.V {
panic(fmt.Sprintf("bad token: %v but wanted %v\n", tok, goodToken))
} else {
fmt.Printf("Got good token: %v\n", tok)
}
}
}
// Custom lexer for our maybe hash function
//
// SUBSCRIBE
// valuect(item) AS stuff
// FROM maybe(stuff)
// WHERE x = y
//
func LexMaybe(l *lex.Lexer) lex.StateFn {
l.SkipWhiteSpaces()
keyWord := strings.ToLower(l.PeekWord())
switch keyWord {
case "maybe":
l.ConsumeWord("maybe")
l.Emit(lex.TokenIdentity)
return lex.LexExpressionOrIdentity
}
return lex.LexExpressionOrIdentity
}
func Tok(tok lex.TokenType, val string) lex.Token { return lex.Token{T: tok, V: val} }
func main() {
/* Many *ql languages support some type of columnar layout such as:
name = value, name2 = value2
*/
l := lex.NewLexer(`
SUBSCRIBETO
count(x), Name
FROM ourstream
WHERE
k = REPLACE(LOWER(Name),"cde","xxx");`, ourDialect)
verifyLexerTokens(l,
[]lex.Token{
Tok(TokenSubscribeTo, "SUBSCRIBETO"),
Tok(lex.TokenUdfExpr, "count"),
Tok(lex.TokenLeftParenthesis, "("),
Tok(lex.TokenIdentity, "x"),
Tok(lex.TokenRightParenthesis, ")"),
Tok(lex.TokenComma, ","),
Tok(lex.TokenIdentity, "Name"),
Tok(lex.TokenFrom, "FROM"),
Tok(lex.TokenIdentity, "ourstream"),
Tok(lex.TokenWhere, "WHERE"),
Tok(lex.TokenIdentity, "k"),
Tok(lex.TokenEqual, "="),
Tok(lex.TokenUdfExpr, "REPLACE"),
Tok(lex.TokenLeftParenthesis, "("),
Tok(lex.TokenUdfExpr, "LOWER"),
Tok(lex.TokenLeftParenthesis, "("),
Tok(lex.TokenIdentity, "Name"),
Tok(lex.TokenRightParenthesis, ")"),
Tok(lex.TokenComma, ","),
Tok(lex.TokenValue, "cde"),
Tok(lex.TokenComma, ","),
Tok(lex.TokenValue, "xxx"),
Tok(lex.TokenRightParenthesis, ")"),
Tok(lex.TokenEOS, ";"),
})
}