parser

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package parser contains the definitions of the base tokens, the lexer that converts a query to a token stream, and the parser that converts a token stream into an AST.

Index

Constants

View Source
const (
	OPERATION  = "operation"
	EXPRESSION = "expression"
)

Variables

View Source
var TokenLookup = map[Token]string{
	OTHER:               "OTHER",
	EOF:                 "EOF",
	WS:                  "WS",
	STRING:              "STRING",
	EQUAL:               "EQUAL",
	GREATER_THAN:        "GREATER THAN",
	GREATHER_THAN_EQUAL: "GREATER THAN OR EQUAL",
	LESS_THAN:           "LESS THAN",
	LESS_THAN_EQUAL:     "LESS THAN OR EQUAL",
	NOT_EQUAL:           "NOT EQUAL",
	AND:                 "AND",
	OR:                  "OR",
	OPEN_BRACKET:        "(",
	CLOSED_BRACKET:      ")",
	PERCENT:             "%",
}

TokenLookup is a map, useful for printing readable names of the tokens.

Functions

This section is empty.

Types

type Expression

type Expression struct {
	Field      string
	Comparator string
	Value      string
}

Expression represents something like x=y or x>=y

func (Expression) String

func (e Expression) String() string

String returns the string representation of expression.

func (Expression) Type

func (e Expression) Type() string

Type returns the type for expression.

type Lexer

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

Lexer represents a lexical scanner.

func NewLexer

func NewLexer(r io.Reader) *Lexer

NewLexer returns a new instance of Lexer.

func NewLexerFromString

func NewLexerFromString(s string) *Lexer

NewLexerFromString returns a Lexer for the provided string.

func (*Lexer) Scan

func (s *Lexer) Scan() TokenInfo

Scan returns the next token and literal Value.

type Node

type Node interface {
	Type() string
	String() string
}

Node represents a node in the AST after the expression is parsed.

type Operation

type Operation struct {
	LeftNode  Node
	Gate      string
	RightNode Node
}

Operation represents a Node (Operation or Expression) compared with another Node using either `AND` or `OR`.

func (Operation) String

func (o Operation) String() string

String returns the string representation of operation.

func (Operation) Type

func (o Operation) Type() string

Type returns the type for operation.

type Parser

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

Parser represents a parser, including a scanner and the underlying raw input. It also contains a small buffer to allow for two unscans.

func NewParser

func NewParser(s string) *Parser

NewParser returns a new instance of Parser.

func (*Parser) Parse

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

Parse takes the raw string and returns the root node of the AST.

type Token

type Token int

Token represents a lexical token.

const (
	// Special tokens
	// Iota simply starts and integer count
	OTHER Token = iota
	EOF
	WS

	// Main literals
	STRING

	// Brackets
	OPEN_BRACKET
	CLOSED_BRACKET

	// Special characters
	GREATER_THAN
	GREATHER_THAN_EQUAL
	LESS_THAN
	LESS_THAN_EQUAL
	EQUAL
	NOT_EQUAL
	PERCENT

	// Keywords
	AND
	OR
)

Declare the tokens here.

func (Token) String

func (t Token) String() (print string)

String prints a human readable string name for a given token.

type TokenInfo

type TokenInfo struct {
	Token   Token
	Literal string
}

TokenInfo stores relevant information about the token during scanning.

type TokenStack

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

TokenStack is used as the buffer for the Parser.

func (*TokenStack) Len

func (s *TokenStack) Len() int

Len returns the current length of the TokenStack.

func (*TokenStack) Pop

func (s *TokenStack) Pop() (TokenInfo, error)

Pop removes and returns a token from the TokenStack.

func (*TokenStack) Push

func (s *TokenStack) Push(v TokenInfo)

Push pushes a token to the TokenStack.

Jump to

Keyboard shortcuts

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