parser

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2020 License: Apache-2.0 Imports: 7 Imported by: 1

Documentation

Overview

nolint: govet

Index

Constants

This section is empty.

Variables

View Source
var (
	Lexer = lexer.Must(lexer.Regexp(`(\s+)` +
		`|\b(?P<Keyword>(?i)SELECT|FROM|WHERE|LIMIT|OFFSET|TRUE|FALSE|NULL|NOT|BETWEEN|AND|OR|USE|INDEX|ASC|DESC)\b` +
		"|(?P<QuotedIdent>`[^`]+`)" +
		`|(?P<Ident>[a-zA-Z_][a-zA-Z0-9_]*)` +
		`|(?P<Number>[-+]?\d*\.?\d+([eE][-+]?\d+)?)` +
		`|(?P<String>'[^']*'|"[^"]*")` +
		`|(?P<Operators><>|!=|<=|>=|[-+*/%:?,.()=<>\[\]])` +
		`|;`,
	))
	Parser = participle.MustBuild(
		&AST{},
		participle.Lexer(Lexer),
		participle.Unquote("String"),
		UnquoteIdent(),
		participle.CaseInsensitive("Keyword"),
		participle.UseLookahead(2),
	)
)

Functions

func IsReservedWord

func IsReservedWord(s string) bool

IsReservedWord returns true if the string is a reserved word in DynamoDB. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html

func UnquoteIdent

func UnquoteIdent() participle.Option

UnquoteIdent removes surrounding backticks (`) from quoted identifiers

func Visit

func Visit(node Node, visitor func(node Node, next func() error) error) error

Visit nodes in the AST

The visitor can call "next()" to continue traversal of child nodes.

Types

type AST

type AST struct {
	Select *Select `"SELECT" @@`
	End    string  `( ";" )?`
}

type AndExpression

type AndExpression struct {
	And []*Condition `@@ ( "AND" @@ )*`
}

type Between

type Between struct {
	Start *Operand `@@`
	End   *Operand `"AND" @@`
}

type Boolean

type Boolean bool

func (*Boolean) Capture

func (b *Boolean) Capture(values []string) error

type Compare

type Compare struct {
	Operator string   `@( "<>" | "<=" | ">=" | "=" | "<" | ">" | "!=" )`
	Operand  *Operand `@@`
}

type Condition

type Condition struct {
	Parenthesized *ParenthesizedExpression `  "(" @@ ")"`
	Not           *NotCondition            `| "NOT" @@`
	Operand       *ConditionOperand        `| @@`
	Function      *FunctionExpression      `| @@`
}

type ConditionExpression

type ConditionExpression struct {
	Or []*AndExpression `@@ ( "OR" @@ )*`
}

type ConditionOperand

type ConditionOperand struct {
	Operand      *DocumentPath `@@`
	ConditionRHS *ConditionRHS `@@`
}

type ConditionRHS

type ConditionRHS struct {
	Compare *Compare `  @@`
	Between *Between `| "BETWEEN" @@`
	In      *In      `| "IN" "(" @@ ")"`
}

type DocumentPath

type DocumentPath struct {
	Fragment []*PathFragment `@@ ( "." @@ )*`
}

func (DocumentPath) String

func (p DocumentPath) String() string

String marshals the DocumentPath into a human readable format. Do not use this function when marshaling to expressions, because substitutions need to be applied first for reserved words.

type FunctionArgument

type FunctionArgument struct {
	DocumentPath *DocumentPath `  @@`
	Value        *Value        `| @@`
}

func (FunctionArgument) String

func (a FunctionArgument) String() string

type FunctionExpression

type FunctionExpression struct {
	Function string              `@Ident`
	Args     []*FunctionArgument `"(" @@ ( "," @@ )* ")"`
}

func (*FunctionExpression) FirstArgIsRef

func (f *FunctionExpression) FirstArgIsRef() bool

func (*FunctionExpression) String

func (f *FunctionExpression) String() string

type In

type In struct {
	Values []*Value `@@ ( "," @@ )*`
}

type Node

type Node interface {
	// contains filtered or unexported methods
}

Node is an interface implemented by all AST nodes.

type NotCondition

type NotCondition struct {
	Condition *Condition `@@`
}

type Operand

type Operand struct {
	Value     *Value        `  @@`
	SymbolRef *DocumentPath `| @@`
}

type ParenthesizedExpression

type ParenthesizedExpression struct {
	ConditionExpression *ConditionExpression `@@`
}

type PathFragment

type PathFragment struct {
	Symbol  string `( @Ident | @QuotedIdent )`
	Indexes []int  `( "[" @Number "]" )*`
}

func (PathFragment) String

func (p PathFragment) String() string

type ProjectionColumn

type ProjectionColumn struct {
	Function     *FunctionExpression `  @@`
	DocumentPath *DocumentPath       `| @@`
}

func (ProjectionColumn) String

func (c ProjectionColumn) String() string

type ProjectionExpression

type ProjectionExpression struct {
	All     bool                `  ( @"*" | "document" "(" @"*" ")" )`
	Columns []*ProjectionColumn `| @@ ( "," @@ )*`
}

func (ProjectionExpression) String

func (e ProjectionExpression) String() string

type ScanDescending

type ScanDescending bool

func (*ScanDescending) Capture

func (b *ScanDescending) Capture(values []string) error

type Select

type Select struct {
	Projection *ProjectionExpression `@@`
	From       string                `"FROM" ( @Ident ( @"." @Ident )* | @QuotedIdent )`
	Index      *string               `( "USE" "INDEX" "(" @Ident ")" )?`
	Where      *AndExpression        `( "WHERE" @@ )?`
	Descending *ScanDescending       `( @"ASC" | @"DESC" )?`
	Limit      *int                  `( "LIMIT" @Number )?`
}

Select based on http://www.h2database.com/html/grammar.html

type Value

type Value struct {
	PlaceHolder           *string  `  @":" @Ident `
	PositionalPlaceholder *bool    `| @"?" `
	Number                *float64 `| @Number`
	String                *string  `| @String`
	Boolean               *Boolean `| @("TRUE" | "FALSE")`
	Null                  bool     `| @"NULL"`
}

func (Value) Literal

func (v Value) Literal() string

Jump to

Keyboard shortcuts

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