lexer

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsRegexOp

func IsRegexOp(t Token) bool

IsRegexOp returns true if the operator accepts a regex operand.

func LoadTokenMap

func LoadTokenMap(keywordTokens map[Token]string)

LoadTokenMap allows for extra keywords to be added to the lexer

func ScanBareIdent

func ScanBareIdent(r io.RuneScanner) string

ScanBareIdent reads bare identifier from a rune reader.

func ScanDelimited

func ScanDelimited(r io.RuneScanner, start, end rune, escapes map[rune]rune, escapesPassThru bool) ([]byte, error)

func ScanString

func ScanString(r io.RuneScanner) (string, error)

ScanString reads a quoted string from a rune reader.

Types

type Pos

type Pos struct {
	Line int
	Char int
}

Pos specifies the line and character position of a token. The Char and Line are both zero-based indexes.

type Scanner

type Scanner struct {
	OneRuneOperators bool // only scan one rune operators (+, not ++)
	// contains filtered or unexported fields
}

Scanner represents a lexical scanner for InfluxQL.

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner returns a new instance of Scanner.

func (*Scanner) Peek

func (s *Scanner) Peek() rune

Peek returns the next rune without advancing the scanner

func (*Scanner) Scan

func (s *Scanner) Scan() (tok Token, pos Pos, lit string)

Scan returns the next token and position from the underlying reader. Also returns the literal text read for strings and numbers tokens since these token types can have different literal representations.

func (*Scanner) ScanRegex

func (s *Scanner) ScanRegex() (tok Token, pos Pos, lit string)

ScanRegex tokenizes a regex expression

type Token

type Token int

Token represents each lexer symbol

const (
	ILLEGAL Token = iota
	EOF
	WS

	LPAREN      // (
	RPAREN      // )
	LBRACKET    // [
	RBRACKET    // ]
	LCURLY      // {
	RCURLY      // }
	COMMA       // ,
	SEMICOLON   // ;
	COLON       // :
	DOT         // .
	SINGLEQUOTE // '
	DOUBLEQUOTE // "
	PERCENT     // %
	DOLLAR      // $
	HASH        // #
	ATSIGN      // @
	ARROW       // ->
	EQARROW     // =>

	IDENT
	INTEGER
	DECIMAL
	STRING
	BADSTRING
	BADESCAPE
	TRUE
	FALSE
	REGEX
	BADREGEX
	DURATION

	PLUS       // +
	PLUSPLUS   // ++
	MINUS      // -
	MINUSMINUS // --
	MUL        // *
	DIV        // /
	AMPERSAND  // &
	XOR        // ^
	PIPE       // |
	LSHIFT     // <<
	RSHIFT     // >>
	POW        // **

	AND // AND
	OR  // OR

	EQ       // =
	NEQ      // !=
	EQEQ     // ==
	EQREGEX  // =~
	NEQREGEX // !~
	LT       // <
	LTE      // <=
	GT       // >
	GTE      // >=

	VARARG // ...

)

Token enums

func Lookup

func Lookup(ident string) Token

Lookup returns the token associated with a given string.

func (Token) IsOperator

func (tok Token) IsOperator() bool

isOperator returns true for operator tokens.

func (Token) Precedence

func (tok Token) Precedence() int

Precedence returns the operator precedence of the binary operator token.

func (Token) String

func (tok Token) String() string

String returns the string representation of the token.

type TokenBuffer

type TokenBuffer struct {
	IgnoreWhitespace bool
	// contains filtered or unexported fields
}

TokenBuffer represents a wrapper for scanner to add a buffer. It provides a fixed-length circular buffer that can be unread.

func NewTokenBuffer

func NewTokenBuffer(r io.Reader) *TokenBuffer

NewTokenBuffer returns a new buffered scanner for a reader.

func (*TokenBuffer) Current

func (s *TokenBuffer) Current() (tok Token, pos Pos, lit string)

curr returns the last read token.

func (*TokenBuffer) OneRuneOperators

func (s *TokenBuffer) OneRuneOperators(b bool)

OneRuneOperators sets the scanner option to ignore multi rune operators.

func (*TokenBuffer) Peek

func (s *TokenBuffer) Peek() (tok Token, pos Pos, lit string)

Peek reads the next token and then unscans.

func (*TokenBuffer) PeekRune

func (s *TokenBuffer) PeekRune() rune

PeekRune returns the next rune from the scanner.

func (*TokenBuffer) Scan

func (s *TokenBuffer) Scan() (tok Token, pos Pos, lit string)

Scan reads the next token from the scanner.

func (*TokenBuffer) ScanFunc

func (s *TokenBuffer) ScanFunc(scan func() (Token, Pos, string)) (tok Token, pos Pos, lit string)

ScanFunc uses the provided function to scan the next token.

func (*TokenBuffer) ScanRegex

func (s *TokenBuffer) ScanRegex() (tok Token, pos Pos, lit string)

ScanRegex reads a regex token from the scanner.

func (*TokenBuffer) Unscan

func (s *TokenBuffer) Unscan()

Unscan pushes the previously token back onto the buffer.

Jump to

Keyboard shortcuts

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