-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.go
79 lines (59 loc) · 1.2 KB
/
repl.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
package cql
import (
"bufio"
"bytes"
"fmt"
"os"
"github.com/antlr4-go/antlr/v4"
parser "github.com/hawyar/cql/internal"
)
const (
CQLPrefix = "cql> "
ExitCmd = ".exit"
)
type Repl struct {
scanner *bufio.Scanner
buff *bytes.Buffer
listener CQLListener
}
func NewREPL() *Repl {
return &Repl{
scanner: bufio.NewScanner(os.Stdin),
listener: *NewCQLListener(),
buff: new(bytes.Buffer),
}
}
func (r *Repl) Start() {
fmt.Println("Welcome to the CQL repl!\nType 'help' for help.")
for {
r.read()
r.eval()
}
}
func (r *Repl) read() {
fmt.Print(CQLPrefix)
if !r.scanner.Scan() {
return
}
next := r.scanner.Text()
if next == ExitCmd {
fmt.Println("Exiting...")
os.Exit(0)
}
r.buff.WriteString(next)
}
func (r *Repl) eval() {
input := r.buff.String()
fmt.Println("current buffer:", input)
lexer := parser.NewcqlLexer(antlr.NewInputStream(input))
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
parser := parser.NewcqlParser(stream)
antlr.ParseTreeWalkerDefault.Walk(r.listener, parser.Library())
if stream.LT(1).GetTokenType() == antlr.TokenEOF {
fmt.Println("complete statement")
r.reset()
}
}
func (r *Repl) reset() {
r.buff.Reset()
}