No need to pass sql.parser into sql.scanner

This commit is contained in:
Tobie Morgan Hitchcock 2018-01-10 13:07:09 +00:00
parent 558eba3797
commit b54e551353
13 changed files with 61 additions and 62 deletions

View file

@ -18,25 +18,25 @@ func (p *parser) parseData() (exp Expr, err error) {
if tok, _, exi := p.mightBe(SET, DIFF, MERGE, CONTENT); exi { if tok, _, exi := p.mightBe(SET, DIFF, MERGE, CONTENT); exi {
if p.is(tok, SET) { if is(tok, SET) {
if exp, err = p.parseSet(); err != nil { if exp, err = p.parseSet(); err != nil {
return nil, err return nil, err
} }
} }
if p.is(tok, DIFF) { if is(tok, DIFF) {
if exp, err = p.parseDiff(); err != nil { if exp, err = p.parseDiff(); err != nil {
return nil, err return nil, err
} }
} }
if p.is(tok, MERGE) { if is(tok, MERGE) {
if exp, err = p.parseMerge(); err != nil { if exp, err = p.parseMerge(); err != nil {
return nil, err return nil, err
} }
} }
if p.is(tok, CONTENT) { if is(tok, CONTENT) {
if exp, err = p.parseContent(); err != nil { if exp, err = p.parseContent(); err != nil {
return nil, err return nil, err
} }

View file

@ -239,7 +239,7 @@ func (p *parser) parseType() (t, k string, err error) {
return return
} }
if !p.contains(t, allowedTypes) { if !contains(t, allowedTypes) {
err = &ParseError{Found: t, Expected: allowedTypes} err = &ParseError{Found: t, Expected: allowedTypes}
return return
} }
@ -520,7 +520,7 @@ func (p *parser) parsePart() (exp Expr, err error) {
// then we will parse anything from here on // then we will parse anything from here on
// as an IF expression clause. // as an IF expression clause.
if p.is(tok, IF) { if is(tok, IF) {
return p.parseIfel() return p.parseIfel()
} }
@ -528,7 +528,7 @@ func (p *parser) parsePart() (exp Expr, err error) {
// bracket, then we will parse this complete // bracket, then we will parse this complete
// expression part as a subquery. // expression part as a subquery.
if p.is(tok, LPAREN) { if is(tok, LPAREN) {
return p.parseSubq() return p.parseSubq()
} }
@ -544,7 +544,7 @@ func (p *parser) parsePart() (exp Expr, err error) {
// function call, then check to see if the // function call, then check to see if the
// token is in the list of allowed tokens. // token is in the list of allowed tokens.
if !p.in(tok, toks) { if !in(tok, toks) {
err = &ParseError{Found: lit, Expected: []string{"expression"}} err = &ParseError{Found: lit, Expected: []string{"expression"}}
} }
@ -860,7 +860,7 @@ func (p *parser) parseJoin() (exp Expr, err error) {
tok, _, _ := p.scan() tok, _, _ := p.scan()
if !p.in(tok, toks) { if !in(tok, toks) {
p.unscan() p.unscan()
return return
} }
@ -890,7 +890,7 @@ func (p *parser) parseStep() (exp Expr, err error) {
// bracket, then we will parse this complete // bracket, then we will parse this complete
// expression part as a subquery. // expression part as a subquery.
if p.is(tok, LPAREN) { if is(tok, LPAREN) {
return p.parseSubp() return p.parseSubp()
} }
@ -898,7 +898,7 @@ func (p *parser) parseStep() (exp Expr, err error) {
// expression, then check to see if the // expression, then check to see if the
// token is in the list of allowed tokens. // token is in the list of allowed tokens.
if !p.in(tok, toks) { if !in(tok, toks) {
p.unscan() p.unscan()
exp = nil exp = nil
} }

View file

@ -41,31 +41,31 @@ func (p *parser) parseDefineFieldStatement() (stmt *DefineFieldStatement, err er
break break
} }
if p.is(tok, TYPE) { if is(tok, TYPE) {
if stmt.Type, stmt.Kind, err = p.parseType(); err != nil { if stmt.Type, stmt.Kind, err = p.parseType(); err != nil {
return nil, err return nil, err
} }
} }
if p.is(tok, VALUE) { if is(tok, VALUE) {
if stmt.Value, err = p.parseExpr(); err != nil { if stmt.Value, err = p.parseExpr(); err != nil {
return nil, err return nil, err
} }
} }
if p.is(tok, ASSERT) { if is(tok, ASSERT) {
if stmt.Assert, err = p.parseExpr(); err != nil { if stmt.Assert, err = p.parseExpr(); err != nil {
return nil, err return nil, err
} }
} }
if p.is(tok, PRIORITY) { if is(tok, PRIORITY) {
if stmt.Priority, err = p.parsePriority(); err != nil { if stmt.Priority, err = p.parsePriority(); err != nil {
return nil, err return nil, err
} }
} }
if p.is(tok, PERMISSIONS) { if is(tok, PERMISSIONS) {
if stmt.Perms, err = p.parsePerms(); err != nil { if stmt.Perms, err = p.parsePerms(); err != nil {
return nil, err return nil, err
} }

View file

@ -26,19 +26,19 @@ func (p *parser) parseInfoStatement() (stmt *InfoStatement, err error) {
return nil, err return nil, err
} }
if p.is(stmt.Kind, NAMESPACE) { if is(stmt.Kind, NAMESPACE) {
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil {
return nil, err return nil, err
} }
} }
if p.is(stmt.Kind, DATABASE) { if is(stmt.Kind, DATABASE) {
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil {
return nil, err return nil, err
} }
} }
if p.is(stmt.Kind, TABLE) { if is(stmt.Kind, TABLE) {
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil {
return nil, err return nil, err
} }

View file

@ -30,13 +30,13 @@ func (p *parser) parseDefineLoginStatement() (stmt *DefineLoginStatement, err er
return nil, err return nil, err
} }
if p.is(stmt.Kind, NAMESPACE) { if is(stmt.Kind, NAMESPACE) {
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil {
return nil, err return nil, err
} }
} }
if p.is(stmt.Kind, DATABASE) { if is(stmt.Kind, DATABASE) {
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil {
return nil, err return nil, err
} }
@ -70,13 +70,13 @@ func (p *parser) parseRemoveLoginStatement() (stmt *RemoveLoginStatement, err er
return nil, err return nil, err
} }
if p.is(stmt.Kind, NAMESPACE) { if is(stmt.Kind, NAMESPACE) {
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil {
return nil, err return nil, err
} }
} }
if p.is(stmt.Kind, DATABASE) { if is(stmt.Kind, DATABASE) {
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil {
return nil, err return nil, err
} }

View file

@ -66,7 +66,7 @@ func newParser(c *fibre.Context) *parser {
func parseBytes(c *fibre.Context, i []byte) (*Query, error) { func parseBytes(c *fibre.Context, i []byte) (*Query, error) {
p := newParser(c) p := newParser(c)
r := bytes.NewReader(i) r := bytes.NewReader(i)
p.s = newScanner(p, r) p.s = newScanner(r)
return p.parse() return p.parse()
} }
@ -74,14 +74,14 @@ func parseBytes(c *fibre.Context, i []byte) (*Query, error) {
func parseString(c *fibre.Context, i string) (*Query, error) { func parseString(c *fibre.Context, i string) (*Query, error) {
p := newParser(c) p := newParser(c)
r := strings.NewReader(i) r := strings.NewReader(i)
p.s = newScanner(p, r) p.s = newScanner(r)
return p.parse() return p.parse()
} }
// parseBuffer parses a buffer. // parseBuffer parses a buffer.
func parseBuffer(c *fibre.Context, r io.Reader) (*Query, error) { func parseBuffer(c *fibre.Context, r io.Reader) (*Query, error) {
p := newParser(c) p := newParser(c)
p.s = newScanner(p, r) p.s = newScanner(r)
return p.parse() return p.parse()
} }
@ -234,7 +234,7 @@ func (p *parser) mightBe(expected ...Token) (tok Token, lit string, found bool)
tok, lit, _ = p.scan() tok, lit, _ = p.scan()
if found = p.in(tok, expected); !found { if found = in(tok, expected); !found {
p.unscan() p.unscan()
} }
@ -246,7 +246,7 @@ func (p *parser) shouldBe(expected ...Token) (tok Token, lit string, err error)
tok, lit, _ = p.scan() tok, lit, _ = p.scan()
if !p.in(tok, expected) { if !in(tok, expected) {
p.unscan() p.unscan()
err = &ParseError{Found: lit, Expected: lookup(expected)} err = &ParseError{Found: lit, Expected: lookup(expected)}
} }

View file

@ -28,7 +28,7 @@ func (p *parser) parsePerms() (exp *PermExpression, err error) {
return exp, err return exp, err
} }
if p.is(tok, NONE, FULL, WHERE) { if is(tok, NONE, FULL, WHERE) {
var expr Expr var expr Expr
@ -52,7 +52,7 @@ func (p *parser) parsePerms() (exp *PermExpression, err error) {
} }
if p.is(tok, FOR) { if is(tok, FOR) {
for { for {

View file

@ -28,13 +28,12 @@ import (
type scanner struct { type scanner struct {
b []rune // any runes before b []rune // any runes before
a []rune // any runes after a []rune // any runes after
p *parser
r *bufio.Reader r *bufio.Reader
} }
// newScanner returns a new instance of Scanner. // newScanner returns a new instance of Scanner.
func newScanner(p *parser, r io.Reader) *scanner { func newScanner(r io.Reader) *scanner {
return &scanner{p: p, r: bufio.NewReader(r)} return &scanner{r: bufio.NewReader(r)}
} }
// scan returns the next token and literal value. // scan returns the next token and literal value.
@ -352,15 +351,15 @@ func (s *scanner) scanParams(chp ...rune) (tok Token, lit string, val interface{
tok, lit, _ = s.scanIdiom() tok, lit, _ = s.scanIdiom()
if s.p.is(tok, THING) { if is(tok, THING) {
return ILLEGAL, lit, val return ILLEGAL, lit, val
} }
if s.p.is(tok, REGION) { if is(tok, REGION) {
return ILLEGAL, lit, val return ILLEGAL, lit, val
} }
if s.p.is(tok, ILLEGAL) { if is(tok, ILLEGAL) {
return ILLEGAL, lit, val return ILLEGAL, lit, val
} }
@ -378,11 +377,11 @@ func (s *scanner) scanQuoted(chp ...rune) (tok Token, lit string, val interface{
tok, lit, _ = s.scanString(chp...) tok, lit, _ = s.scanString(chp...)
if s.p.is(tok, REGION) { if is(tok, REGION) {
return ILLEGAL, lit, val return ILLEGAL, lit, val
} }
if s.p.is(tok, ILLEGAL) { if is(tok, ILLEGAL) {
return ILLEGAL, lit, val return ILLEGAL, lit, val
} }
@ -415,11 +414,11 @@ func (s *scanner) scanSection(chp ...rune) (tok Token, lit string, val interface
tok, lit, _ = s.scanString(chp...) tok, lit, _ = s.scanString(chp...)
if s.p.is(tok, REGION) { if is(tok, REGION) {
return ILLEGAL, lit, val return ILLEGAL, lit, val
} }
if s.p.is(tok, ILLEGAL) { if is(tok, ILLEGAL) {
return ILLEGAL, lit, val return ILLEGAL, lit, val
} }

View file

@ -33,27 +33,27 @@ func (p *parser) parseDefineScopeStatement() (stmt *DefineScopeStatement, err er
break break
} }
if p.is(tok, SESSION) { if is(tok, SESSION) {
if stmt.Time, err = p.parseDuration(); err != nil { if stmt.Time, err = p.parseDuration(); err != nil {
return nil, err return nil, err
} }
} }
if p.is(tok, SIGNUP) { if is(tok, SIGNUP) {
_, _, _ = p.mightBe(AS) _, _, _ = p.mightBe(AS)
if stmt.Signup, err = p.parseExpr(); err != nil { if stmt.Signup, err = p.parseExpr(); err != nil {
return nil, err return nil, err
} }
} }
if p.is(tok, SIGNIN) { if is(tok, SIGNIN) {
_, _, _ = p.mightBe(AS) _, _, _ = p.mightBe(AS)
if stmt.Signin, err = p.parseExpr(); err != nil { if stmt.Signin, err = p.parseExpr(); err != nil {
return nil, err return nil, err
} }
} }
if p.is(tok, CONNECT) { if is(tok, CONNECT) {
_, _, _ = p.mightBe(AS) _, _, _ = p.mightBe(AS)
if stmt.Connect, err = p.parseExpr(); err != nil { if stmt.Connect, err = p.parseExpr(); err != nil {
return nil, err return nil, err

View file

@ -33,25 +33,25 @@ func (p *parser) parseDefineTableStatement() (stmt *DefineTableStatement, err er
break break
} }
if p.is(tok, DROP) { if is(tok, DROP) {
stmt.Drop = true stmt.Drop = true
} }
if p.is(tok, SCHEMAFULL) { if is(tok, SCHEMAFULL) {
stmt.Full = true stmt.Full = true
} }
if p.is(tok, SCHEMALESS) { if is(tok, SCHEMALESS) {
stmt.Full = false stmt.Full = false
} }
if p.is(tok, PERMISSIONS) { if is(tok, PERMISSIONS) {
if stmt.Perms, err = p.parsePerms(); err != nil { if stmt.Perms, err = p.parsePerms(); err != nil {
return nil, err return nil, err
} }
} }
if p.is(tok, AS) { if is(tok, AS) {
stmt.Lock = true stmt.Lock = true

View file

@ -30,19 +30,19 @@ func (p *parser) parseDefineTokenStatement() (stmt *DefineTokenStatement, err er
return nil, err return nil, err
} }
if p.is(stmt.Kind, NAMESPACE) { if is(stmt.Kind, NAMESPACE) {
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil {
return nil, err return nil, err
} }
} }
if p.is(stmt.Kind, DATABASE) { if is(stmt.Kind, DATABASE) {
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil {
return nil, err return nil, err
} }
} }
if p.is(stmt.Kind, SCOPE) { if is(stmt.Kind, SCOPE) {
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil {
return nil, err return nil, err
} }
@ -55,13 +55,13 @@ func (p *parser) parseDefineTokenStatement() (stmt *DefineTokenStatement, err er
break break
} }
if p.is(tok, TYPE) { if is(tok, TYPE) {
if stmt.Type, err = p.parseAlgorithm(); err != nil { if stmt.Type, err = p.parseAlgorithm(); err != nil {
return nil, err return nil, err
} }
} }
if p.is(tok, VALUE) { if is(tok, VALUE) {
if stmt.Code, err = p.parseBinary(); err != nil { if stmt.Code, err = p.parseBinary(); err != nil {
return nil, err return nil, err
} }
@ -97,19 +97,19 @@ func (p *parser) parseRemoveTokenStatement() (stmt *RemoveTokenStatement, err er
return nil, err return nil, err
} }
if p.is(stmt.Kind, NAMESPACE) { if is(stmt.Kind, NAMESPACE) {
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil {
return nil, err return nil, err
} }
} }
if p.is(stmt.Kind, DATABASE) { if is(stmt.Kind, DATABASE) {
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil {
return nil, err return nil, err
} }
} }
if p.is(stmt.Kind, SCOPE) { if is(stmt.Kind, SCOPE) {
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil {
return nil, err return nil, err
} }

View file

@ -32,7 +32,7 @@ func (p *parser) parseUseStatement() (stmt *UseStatement, err error) {
for { for {
if p.is(tok, NAMESPACE, NS) { if is(tok, NAMESPACE, NS) {
_, stmt.NS, err = p.shouldBe(IDENT, STRING, NUMBER, DOUBLE, DATE, TIME) _, stmt.NS, err = p.shouldBe(IDENT, STRING, NUMBER, DOUBLE, DATE, TIME)
if err != nil { if err != nil {
@ -49,7 +49,7 @@ func (p *parser) parseUseStatement() (stmt *UseStatement, err error) {
} }
if p.is(tok, DATABASE, DB) { if is(tok, DATABASE, DB) {
_, stmt.DB, err = p.shouldBe(IDENT, STRING, NUMBER, DOUBLE, DATE, TIME) _, stmt.DB, err = p.shouldBe(IDENT, STRING, NUMBER, DOUBLE, DATE, TIME)
if err != nil { if err != nil {

View file

@ -24,7 +24,7 @@ import (
json "github.com/hjson/hjson-go" json "github.com/hjson/hjson-go"
) )
func (p *parser) in(token Token, tokens []Token) bool { func in(token Token, tokens []Token) bool {
for _, t := range tokens { for _, t := range tokens {
if token == t { if token == t {
@ -36,7 +36,7 @@ func (p *parser) in(token Token, tokens []Token) bool {
} }
func (p *parser) is(token Token, tokens ...Token) bool { func is(token Token, tokens ...Token) bool {
for _, t := range tokens { for _, t := range tokens {
if token == t { if token == t {
@ -48,7 +48,7 @@ func (p *parser) is(token Token, tokens ...Token) bool {
} }
func (p *parser) contains(search string, strings []string) bool { func contains(search string, strings []string) bool {
for _, str := range strings { for _, str := range strings {
if str == search { if str == search {