Improve SQL DEFINE TOKEN statements

This commit is contained in:
Tobie Morgan Hitchcock 2016-11-22 19:10:42 +00:00
parent 063de926ed
commit 75b5f2f412
3 changed files with 80 additions and 3 deletions

View file

@ -240,7 +240,7 @@ func (p *parser) parseString() (string, error) {
func (p *parser) parseRegion() (string, error) { func (p *parser) parseRegion() (string, error) {
tok, lit, err := p.shouldBe(IDENT, STRING, REGION) tok, lit, err := p.shouldBe(STRING, REGION)
if err != nil { if err != nil {
return string(""), &ParseError{Found: lit, Expected: []string{"string"}} return string(""), &ParseError{Found: lit, Expected: []string{"string"}}
} }
@ -251,6 +251,19 @@ func (p *parser) parseRegion() (string, error) {
} }
func (p *parser) parseBinary() ([]byte, error) {
tok, lit, err := p.shouldBe(STRING, REGION)
if err != nil {
return nil, &ParseError{Found: lit, Expected: []string{"string"}}
}
val, err := p.declare(tok, lit)
return []byte(val.(string)), err
}
func (p *parser) parseScript() (string, error) { func (p *parser) parseScript() (string, error) {
tok, lit, err := p.shouldBe(STRING, REGION) tok, lit, err := p.shouldBe(STRING, REGION)
@ -319,6 +332,35 @@ func (p *parser) parseBcrypt() ([]byte, error) {
} }
func (p *parser) parseAlgorithm() (string, error) {
expected := []string{
"ES256", "ES384", "ES512",
"HS256", "HS384", "HS512",
"PS256", "PS384", "PS512",
"RS256", "RS384", "RS512",
}
_, lit, err := p.shouldBe(IDENT, STRING)
if err != nil {
return string(""), &ParseError{Found: lit, Expected: expected}
}
switch lit {
case "ES256", "ES384", "ES512":
case "HS256", "HS384", "HS512":
case "PS256", "PS384", "PS512":
case "RS256", "RS384", "RS512":
default:
return string(""), &ParseError{Found: lit, Expected: expected}
}
val, err := p.declare(STRING, lit)
return val.(string), err
}
func (p *parser) parseExpr() (exp Expr, err error) { func (p *parser) parseExpr() (exp Expr, err error) {
// Create the root binary expression tree. // Create the root binary expression tree.

View file

@ -18,6 +18,10 @@ func (p *parser) parseDefineTokenStatement() (stmt *DefineTokenStatement, err er
stmt = &DefineTokenStatement{} stmt = &DefineTokenStatement{}
if stmt.Name, err = p.parseName(); err != nil {
return nil, err
}
if _, _, err = p.shouldBe(ON); err != nil { if _, _, err = p.shouldBe(ON); err != nil {
return nil, err return nil, err
} }
@ -44,9 +48,34 @@ func (p *parser) parseDefineTokenStatement() (stmt *DefineTokenStatement, err er
} }
} }
if stmt.Code, err = p.parseRand(); err != nil { for {
tok, _, exi := p.mightBe(TYPE, VALUE)
if !exi {
break
}
if p.is(tok, TYPE) {
if stmt.Type, err = p.parseAlgorithm(); err != nil {
return nil, err return nil, err
} }
}
if p.is(tok, VALUE) {
if stmt.Code, err = p.parseBinary(); err != nil {
return nil, err
}
}
}
if stmt.Type == "" {
return nil, &ParseError{Found: ";", Expected: []string{"TYPE"}}
}
if stmt.Code == nil {
return nil, &ParseError{Found: ";", Expected: []string{"VALUE"}}
}
if _, _, err = p.shouldBe(EOF, SEMICOLON); err != nil { if _, _, err = p.shouldBe(EOF, SEMICOLON); err != nil {
return nil, err return nil, err
@ -60,6 +89,10 @@ func (p *parser) parseRemoveTokenStatement() (stmt *RemoveTokenStatement, err er
stmt = &RemoveTokenStatement{} stmt = &RemoveTokenStatement{}
if stmt.Name, err = p.parseName(); err != nil {
return nil, err
}
if _, _, err = p.shouldBe(ON); err != nil { if _, _, err = p.shouldBe(ON); err != nil {
return nil, err return nil, err
} }

View file

@ -184,6 +184,7 @@ const (
UPSERT UPSERT
USE USE
VALIDATE VALIDATE
VALUE
VERSION VERSION
VIEW VIEW
VOID VOID
@ -346,6 +347,7 @@ var tokens = [...]string{
UPSERT: "UPSERT", UPSERT: "UPSERT",
USE: "USE", USE: "USE",
VALIDATE: "VALIDATE", VALIDATE: "VALIDATE",
VALUE: "VALUE",
VERSION: "VERSION", VERSION: "VERSION",
VIEW: "VIEW", VIEW: "VIEW",
VOID: "VOID", VOID: "VOID",