parser

package
v2.1.1+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2018 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TokenKeyWords map[TokenKind][]string = map[TokenKind][]string{
	ILLEGAL:            []string{"ILLEGAL"},
	EOF:                []string{"EOF"},
	WS:                 []string{"any whitespace"},
	COMMA:              []string{","},
	OPEN_PARAN:         []string{"("},
	CLOSE_PARAN:        []string{")"},
	EQUAL_OP:           []string{"="},
	GREATER_OP:         []string{">"},
	LESS_OP:            []string{"<"},
	GREATER_EQUAL_OP:   []string{">="},
	LESS_EQUAL_OP:      []string{"<="},
	IDENT_STRING:       []string{"'abc 123'", "\"abc_123\""},
	IDENT_FLOAT:        []string{"1.00", "0.5"},
	IDENT_INT:          []string{"123"},
	IDENT_FIELD:        []string{"@fieldName", "@field_name"},
	IDENT_BOOL:         []string{"true", "false"},
	IDENT_TABLE_OR_COL: []string{"table_name", "columnName", "(notice lack of quotes)"},
	IDENT_DIRECTIVE:    []string{"@{FORCE_INDEX=some_index}", "@{...}"},
	INSERT:             []string{"INSERT", "insert"},
	UPDATE:             []string{"UPDATE", "update"},
	DELETE:             []string{"DELETE", "delete"},
	SELECT:             []string{"SELECT", "select"},
	FROM:               []string{"FROM", "from"},
	INTO:               []string{"INTO", "into"},
	VALUES:             []string{"VALUES", "values"},
	SET:                []string{"SET", "set"},
	AND:                []string{"AND", "and"},
	OR:                 []string{"OR", "or"},
	START:              []string{"START", "start"},
	END:                []string{"END", "end"},
	KIND:               []string{"KIND", "kind"},
	CLOSED_OPEN_KIND: []string{
		"CLOSED_OPEN", "closed_open", "ClosedOpen",
		"closedOpen", "CO", "co",
	},
	CLOSED_CLOSED_KIND: []string{
		"CLOSED_CLOSED", "closed_closed", "ClosedClosed",
		"closedClosed", "CC", "cc",
	},
	OPEN_OPEN_KIND: []string{
		"OPEN_OPEN", "open_open", "OpenOpen",
		"openOpen", "OO", "oo",
	},
	OPEN_CLOSED_KIND: []string{
		"OPEN_CLOSED", "open_closed", "OpenClosed",
		"openClosed", "OC", "oc",
	},
	PRIMARY_KEY: []string{
		"PRIMARY_KEY", "primary_key", "PrimaryKey",
		"primaryKey", "PK", "pk",
	},
}
View Source
var TokenNames map[TokenKind]string = map[TokenKind]string{
	ILLEGAL:            "ILLEGAL",
	EOF:                "EOF",
	WS:                 "WS",
	COMMA:              "COMMA",
	OPEN_PARAN:         "OPEN_PARAN",
	CLOSE_PARAN:        "CLOSE_PARAN",
	EQUAL_OP:           "EQUAL_OP",
	GREATER_OP:         "GREATER_OP",
	LESS_OP:            "LESS_OP",
	GREATER_EQUAL_OP:   "GREATER_EQUAL_OP",
	LESS_EQUAL_OP:      "LESS_EQUAL_OP",
	IDENT_STRING:       "IDENT_STRING",
	IDENT_FLOAT:        "IDENT_FLOAT",
	IDENT_INT:          "IDENT_INT",
	IDENT_FIELD:        "IDENT_FIELD",
	IDENT_BOOL:         "IDENT_BOOL",
	IDENT_TABLE_OR_COL: "IDENT_TABLE_OR_COL",
	IDENT_DIRECTIVE:    "IDENT_DIRECTIVE",
	INSERT:             "INSERT",
	UPDATE:             "UPDATE",
	DELETE:             "DELETE",
	SELECT:             "SELECT",
	FROM:               "FROM",
	INTO:               "INTO",
	VALUES:             "VALUES",
	SET:                "SET",
	AND:                "AND",
	OR:                 "OR",
	START:              "START",
	END:                "END",
	KIND:               "KIND",
	CLOSED_OPEN_KIND:   "CLOSED_OPEN_KIND",
	CLOSED_CLOSED_KIND: "CLOSED_CLOSED_KIND",
	OPEN_OPEN_KIND:     "OPEN_OPEN_KIND",
	OPEN_CLOSED_KIND:   "OPEN_CLOSED_KIND",
	PRIMARY_KEY:        "PRIMARY_KEY",
}

Functions

func NewEater

func NewEater(scanner *Scanner) *eater

func SyntaxStringFromIdent

func SyntaxStringFromIdent(tkn *Token) string

Types

type DeleteMode

type DeleteMode struct {
}

func NewDeleteMode

func NewDeleteMode() *DeleteMode

func (*DeleteMode) Parse

func (m *DeleteMode) Parse(scanner *Scanner) (Query, error)

supports two formats: delete key range: DELETE FROM table_name START(...) END(...) KIND(...) delete record pk: DELETE FROM table_name (...) VALUES (...) PRIMARY_KEY(...)

type DeleteQuery

type DeleteQuery struct {
	// contains filtered or unexported fields
}

func (*DeleteQuery) AddParam

func (q *DeleteQuery) AddParam(key, val string)

func (*DeleteQuery) Args

func (q *DeleteQuery) Args() []*Token

func (*DeleteQuery) Fields

func (q *DeleteQuery) Fields() (out []string)

func (*DeleteQuery) KindSyntaxString

func (q *DeleteQuery) KindSyntaxString() string

func (*DeleteQuery) SetParams

func (q *DeleteQuery) SetParams(p map[string]string)

func (*DeleteQuery) String

func (q *DeleteQuery) String() string

func (*DeleteQuery) StringKR

func (q *DeleteQuery) StringKR() string

func (*DeleteQuery) StringSingle

func (q *DeleteQuery) StringSingle() string

func (*DeleteQuery) Table

func (q *DeleteQuery) Table() string

func (*DeleteQuery) Tokens

func (q *DeleteQuery) Tokens() []*Token

func (*DeleteQuery) Type

func (q *DeleteQuery) Type() QueryType

type InsertMode

type InsertMode struct{}

func NewInsertMode

func NewInsertMode() *InsertMode

func (*InsertMode) Parse

func (m *InsertMode) Parse(scanner *Scanner) (Query, error)

INSERT INTO tablename <optional> (colname, colname) VALUES(...)

type InsertQuery

type InsertQuery struct {
	// contains filtered or unexported fields
}

func (*InsertQuery) AddParam

func (q *InsertQuery) AddParam(key, val string)

func (*InsertQuery) Args

func (q *InsertQuery) Args() []*Token

func (*InsertQuery) Fields

func (q *InsertQuery) Fields() []string

func (*InsertQuery) SetParams

func (q *InsertQuery) SetParams(p map[string]string)

func (*InsertQuery) String

func (q *InsertQuery) String() string

func (*InsertQuery) Table

func (q *InsertQuery) Table() string

func (*InsertQuery) Tokens

func (q *InsertQuery) Tokens() []*Token

func (*InsertQuery) Type

func (q *InsertQuery) Type() QueryType

type Mode

type Mode interface {
	Parse(s *Scanner) (Query, error)
}

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

func NewParser

func NewParser(r io.Reader) *Parser

func (*Parser) Parse

func (p *Parser) Parse() (Query, error)

type Query

type Query interface {
	String() string // golang syntax of the query
	Tokens() []*Token
	Type() QueryType
	Table() string
	Fields() []string
	Args() []*Token
	// map of field, to go syntax string
	SetParams(map[string]string)
	AddParam(key, val string)
}

type QueryType

type QueryType int
const (
	SELECT_QUERY QueryType = iota
	UPDATE_QUERY
	DELETE_QUERY
	INSERT_QUERY
)

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

func NewScanner

func NewScanner(r *bufio.Reader) *Scanner

func (*Scanner) Peek

func (s *Scanner) Peek(num int) []*Token

peek the number ahead of tokens since we cant unscan tokens, set these peeked tokens as next to be scanned

func (*Scanner) Read

func (s *Scanner) Read() (rune, bool)

func (*Scanner) Scan

func (s *Scanner) Scan() *Token

if there is peeked tokens, return those first. otherwise if there is an error, return that as an illegal token, otherwise perform scan

func (*Scanner) ScanDirective

func (s *Scanner) ScanDirective() *Token

func (*Scanner) ScanIdentifier

func (s *Scanner) ScanIdentifier() *Token

func (*Scanner) ScanNumber

func (s *Scanner) ScanNumber() *Token

func (*Scanner) ScanSpecial

func (s *Scanner) ScanSpecial() *Token

func (*Scanner) ScanString

func (s *Scanner) ScanString() *Token

func (*Scanner) ScanWhitespace

func (s *Scanner) ScanWhitespace() *Token

func (*Scanner) Unread

func (s *Scanner) Unread()

type SelectMode

type SelectMode struct {
	// contains filtered or unexported fields
}

we don't parse anything for select mode

func NewSelectMode

func NewSelectMode() *SelectMode

func (*SelectMode) Parse

func (m *SelectMode) Parse(scanner *Scanner) (Query, error)

type SelectQuery

type SelectQuery struct {
	// contains filtered or unexported fields
}

func NewSelectQuery

func NewSelectQuery(q string, fields []*Token) *SelectQuery

func (*SelectQuery) AddParam

func (q *SelectQuery) AddParam(key, val string)

func (*SelectQuery) Args

func (q *SelectQuery) Args() []*Token

func (*SelectQuery) Fields

func (q *SelectQuery) Fields() (out []string)

func (*SelectQuery) SetParams

func (q *SelectQuery) SetParams(p map[string]string)

func (*SelectQuery) String

func (q *SelectQuery) String() string

func (*SelectQuery) Table

func (q *SelectQuery) Table() string

func (*SelectQuery) Tokens

func (q *SelectQuery) Tokens() []*Token

func (*SelectQuery) Type

func (q *SelectQuery) Type() QueryType

type StartMode

type StartMode struct{}

func (*StartMode) Parse

func (m *StartMode) Parse(scanner *Scanner) (Query, error)

type Token

type Token struct {
	// contains filtered or unexported fields
}

func (Token) Name

func (t Token) Name() string

func (Token) Raw

func (t Token) Raw() string

type TokenGroup

type TokenGroup []hasable

func Group

func Group(tkns ...hasable) TokenGroup

func (TokenGroup) Has

func (tkns TokenGroup) Has(in TokenKind) bool

func (TokenGroup) Values

func (tkns TokenGroup) Values() (ret []TokenKind)

type TokenKind

type TokenKind int
const (
	// special
	ILLEGAL TokenKind = iota
	EOF
	WS

	COMMA
	OPEN_PARAN
	CLOSE_PARAN

	//Operators
	EQUAL_OP
	GREATER_OP
	LESS_OP
	GREATER_EQUAL_OP
	LESS_EQUAL_OP

	IDENT_STRING
	IDENT_FLOAT
	IDENT_INT
	IDENT_FIELD
	IDENT_BOOL
	// kind of its own thing, it represents the literal column or table name
	IDENT_TABLE_OR_COL
	// not used yet, but exists and looks like @{...}
	IDENT_DIRECTIVE

	// Keywords
	INSERT
	UPDATE
	DELETE
	SELECT
	FROM
	INTO
	VALUES
	SET
	AND
	OR
	START
	END
	KIND
	CLOSED_OPEN_KIND
	CLOSED_CLOSED_KIND
	OPEN_OPEN_KIND
	OPEN_CLOSED_KIND
	PRIMARY_KEY
)

func (TokenKind) Has

func (t TokenKind) Has(in TokenKind) bool

func (TokenKind) Values

func (t TokenKind) Values() []TokenKind

type UpdateMode

type UpdateMode struct{}

func NewUpdateMode

func NewUpdateMode() *UpdateMode

func (*UpdateMode) Parse

func (m *UpdateMode) Parse(scanner *Scanner) (Query, error)

Normal Update sql query syntax does not really make a ton of sense here for now, just support: Update table <set loop> | <(col), VALUES (values)> PK(IDENT)

type UpdateQuery

type UpdateQuery struct {
	// contains filtered or unexported fields
}

func (*UpdateQuery) AddParam

func (q *UpdateQuery) AddParam(key, val string)

func (*UpdateQuery) Args

func (q *UpdateQuery) Args() []*Token

func (*UpdateQuery) Fields

func (q *UpdateQuery) Fields() (out []string)

func (*UpdateQuery) SetParams

func (q *UpdateQuery) SetParams(p map[string]string)

func (*UpdateQuery) String

func (q *UpdateQuery) String() string

func (*UpdateQuery) Table

func (q *UpdateQuery) Table() string

func (*UpdateQuery) Tokens

func (q *UpdateQuery) Tokens() []*Token

func (*UpdateQuery) Type

func (q *UpdateQuery) Type() QueryType

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL