Debian Codegraph @

stress/v2/stressql/parser.go

raw symbols SCIP
1 package stressql
2
3 import (
4 "bufio"
5 "bytes"
6 "io"
7 "log"
8 "os"
9 "strings"
10
11 "github.com/influxdata/influxdb/stress/v2/statement"
12 stressql "github.com/influxdata/influxdb/stress/v2/stressql/statement"
13 "github.com/influxdata/influxql"
14 )
15
16 // Token represents a lexical token.
17 type Token int
18
19 // These are the lexical tokens used by the file parser
20 const (
21 ILLEGAL Token = iota
22 EOF
23 STATEMENT
24 BREAK
25 )
26
27 var eof = rune(0)
28
29 func check(e error) {
30 if e != nil {
31 log.Fatal(e)
32 }
33 }
34
35 func isNewline(r rune) bool {
36 return r == '\n'
37 }
38
39 // Scanner scans the file and tokenizes the raw text
40 type Scanner struct {
41 r *bufio.Reader
42 }
43
44 // NewScanner returns a Scanner
45 func NewScanner(r io.Reader) *Scanner {
46 return &Scanner{r: bufio.NewReader(r)}
47 }
48
49 func (s *Scanner) read() rune {
50 ch, _, err := s.r.ReadRune()
51 if err != nil {
52 return eof
53 }
54 return ch
55 }
56
57 func (s *Scanner) unread() { _ = s.r.UnreadRune() }
58
59 func (s *Scanner) peek() rune {
60 ch := s.read()
61 s.unread()
62 return ch
63 }
64
65 // Scan moves the Scanner forward one character
66 func (s *Scanner) Scan() (tok Token, lit string) {
67 ch := s.read()
68
69 if isNewline(ch) {
70 s.unread()
71 return s.scanNewlines()
72 } else if ch == eof {
73 return EOF, ""
74 } else {
75 s.unread()
76 return s.scanStatements()
77 }
78 // golint marks as unreachable code
79 // return ILLEGAL, string(ch)
80 }
81
82 func (s *Scanner) scanNewlines() (tok Token, lit string) {
83 var buf bytes.Buffer
84 buf.WriteRune(s.read())
85
86 for {
87 if ch := s.read(); ch == eof {
88 break
89 } else if !isNewline(ch) {
90 s.unread()
91 break
92 } else {
93 buf.WriteRune(ch)
94 }
95 }
96
97 return BREAK, buf.String()
98 }
99
100 func (s *Scanner) scanStatements() (tok Token, lit string) {
101 var buf bytes.Buffer
102 buf.WriteRune(s.read())
103
104 for {
105 if ch := s.read(); ch == eof {
106 break
107 } else if isNewline(ch) && isNewline(s.peek()) {
108 s.unread()
109 break
110 } else if isNewline(ch) {
111 s.unread()
112 buf.WriteRune(ch)
113 } else {
114 buf.WriteRune(ch)
115 }
116 }
117
118 return STATEMENT, buf.String()
119 }
120
121 // ParseStatements takes a configFile and returns a slice of Statements
122 func ParseStatements(file string) ([]statement.Statement, error) {
123 seq := []statement.Statement{}
124
125 f, err := os.Open(file)
126 check(err)
127
128 s := NewScanner(f)
129
130 for {
131 t, l := s.Scan()
132
133 if t == EOF {
134 break
135 }
136 _, err := influxql.ParseStatement(l)
137 if err == nil {
138
139 seq = append(seq, &statement.InfluxqlStatement{Query: l, StatementID: stressql.RandStr(10)})
140 } else if t == BREAK {
141 continue
142 } else {
143 f := strings.NewReader(l)
144 p := stressql.NewParser(f)
145 s, err := p.Parse()
146 if err != nil {
147 return nil, err
148 }
149 seq = append(seq, s)
150
151 }
152 }
153
154 f.Close()
155
156 return seq, nil
157
158 }