Documentation
¶
Index ¶
- Variables
- type BooleanTokenizer
- type BuildInType
- type CommentSyntax
- type CommentTokenizer
- type Diff
- type Differ
- type Lexer
- func (l *Lexer) CharAtCursor() rune
- func (l *Lexer) CharAtPosition(pos int) rune
- func (l *Lexer) CharAtRelativePosition(pos int) rune
- func (l *Lexer) CollectAnyTokenDelimited(delimiter TokenType) (Tokens, error)
- func (l *Lexer) CollectTokensBetween(open TokenType, close TokenType) (Tokens, int, int, error)
- func (l *Lexer) CollectTokensBetweenCurlyBraces() (Tokens, int, int, error)
- func (l *Lexer) CollectTokensBetweenParentheses() (Tokens, int, int, error)
- func (l *Lexer) CollectTokensDelimited(tokenType TokenType, delimiter TokenType) (Tokens, error)
- func (l Lexer) CurrentToken() Token
- func (l *Lexer) CursorIsOutOfBounds() bool
- func (l Lexer) GetCurrentLine() (int, int)
- func (l *Lexer) GetCursor() int
- func (l *Lexer) GetPosition() Position
- func (l *Lexer) GetSourceSubsString(start int, end int) string
- func (l *Lexer) GetState() State
- func (l *Lexer) IncrementCursor(amount int)
- func (l *Lexer) Iterate(content string) iter.Seq2[Token, error]
- func (l *Lexer) IterateAnyTokenDelimited(delimiter TokenType) iter.Seq2[Token, error]
- func (l *Lexer) IterateTokensBetween(open TokenType, close TokenType) (iter.Seq2[Token, error], *int, *int, error)
- func (l *Lexer) IterateTokensBetweenCurlyBraces() (iter.Seq2[Token, error], *int, *int, error)
- func (l *Lexer) IterateTokensBetweenParentheses() (iter.Seq2[Token, error], *int, *int, error)
- func (l *Lexer) IterateTokensDelimited(tokenType TokenType, delimiter TokenType) iter.Seq2[Token, error]
- func (l *Lexer) Lookahead(offset int) Token
- func (l *Lexer) LookaheadIterator(count int) iter.Seq[Token]
- func (l *Lexer) NextCharsAre(chars []rune) bool
- func (l *Lexer) NextToken() (Token, error)
- func (l *Lexer) NextTokenIs(token Token) bool
- func (l *Lexer) NextTokenIsAnyOf(tokens ...Token) bool
- func (l *Lexer) NextTokenSequenceIs(tokens ...Token) bool
- func (l Lexer) ReachedEOF() bool
- func (l *Lexer) RemoveTokenizer(tokenizerType TokenizerType)
- func (l *Lexer) SetCursor(cursor int)
- func (l *Lexer) SetState(state State)
- func (l *Lexer) SkipWhitespace()
- func (l *Lexer) TokenizeManual(content string)
- func (l *Lexer) TokenizeToSlice(content string) ([]Token, error)
- type LexerError
- type LexerOptionFunc
- func DebugPrintTokens() LexerOptionFunc
- func IgnoreTokens(types ...TokenType) LexerOptionFunc
- func OmitTokenPosition() LexerOptionFunc
- func RetainWhitespace() LexerOptionFunc
- func SymbolCharacterMap(startCharMap, continueCharMap string) LexerOptionFunc
- func WithCommentSyntax(syntaxes ...CommentSyntax) LexerOptionFunc
- func WithKeywords(keywords ...string) LexerOptionFunc
- func WithLiteralTokens(literalTokens ...LiteralToken) LexerOptionFunc
- func WithStringEnclosure(enclosures ...StringEnclosure) LexerOptionFunc
- func WithTokenizer(inserter TokenizerInserter) LexerOptionFunc
- func WithoutCommentSyntax(syntaxes ...CommentSyntax) LexerOptionFunc
- func WithoutLiteralTokens(literalTokens ...TokenType) LexerOptionFunc
- func WithoutStringEnclosure(enclosures ...string) LexerOptionFunc
- type LiteralToken
- type LiteralTokenizer
- type LiteralTokenizerCacheKey
- type LookaheadCache
- type NumberTokenizer
- type Position
- type State
- type StringEnclosure
- type StringTokenizer
- type SymbolTokenizer
- type Token
- func (t *Token) AppendChar(char ...rune)
- func (t Token) Dump()
- func (t Token) Is(token Token) bool
- func (t Token) IsAnyOf(tokens ...Token) bool
- func (t Token) LiteralIs(literal string) bool
- func (t Token) LiteralIsAnyOf(literals ...string) bool
- func (t Token) TypeIs(tt TokenType) bool
- func (t Token) TypeIsAnyOf(tokenTypes ...TokenType) bool
- type TokenCollection
- func (t *TokenCollection) CollectAnyTokensDelimited(delimiter TokenType) ([]Token, error)
- func (t *TokenCollection) CollectTokensBetween(open TokenType, close TokenType) (Tokens, int, int, error)
- func (t *TokenCollection) CollectTokensBetweenCurlyBraces() (Tokens, int, int, error)
- func (t *TokenCollection) CollectTokensBetweenParentheses() (Tokens, int, int, error)
- func (t *TokenCollection) CollectTokensDelimited(tokenType TokenType, delimiter TokenType) (Tokens, error)
- func (t *TokenCollection) CollectTokensUntil(delimiter TokenType) ([]Token, error)
- func (t TokenCollection) CursorIsOutOfBounds() bool
- func (t *TokenCollection) IncrementCursor(amount int)
- func (ti *TokenCollection) Iter() iter.Seq2[int, Token]
- func (t *TokenCollection) NextToken() Token
- func (t TokenCollection) ReachedEOF() bool
- func (t *TokenCollection) TokenAtCursor() Token
- func (t *TokenCollection) TokenAtPosition(pos int) Token
- func (t *TokenCollection) TokenAtRelativePosition(pos int) Token
- type TokenType
- type Tokenizer
- type TokenizerInserter
- type TokenizerType
- type Tokens
Constants ¶
This section is empty.
Variables ¶
var ( EOF rune = rune(byte(0x03)) ErrNoEOFTokenType error = errors.New("no eof token type specified. Specify an EOF token type or use build-in types") ErrNoInvalidTokenType error = errors.New("no eof token type specified. Specify an EOF token type or use build-in types") )
var ( SlashSingleLineCommentSyntax = CommentSyntax{Opener: "//"} SlashMultilineCommentSyntax = CommentSyntax{Opener: "/*", Closer: "*/"} HashtagSingleLineCommentSyntax = CommentSyntax{Opener: "#"} )
Functions ¶
This section is empty.
Types ¶
type BooleanTokenizer ¶
type BooleanTokenizer struct{}
func (BooleanTokenizer) CanTokenize ¶
func (b BooleanTokenizer) CanTokenize(l *Lexer) bool
type BuildInType ¶
type BuildInType string
const ( TypeSof BuildInType = "StartOfFile" TypeEof BuildInType = "EndOfFile" TypeInvalid BuildInType = "Invalid" TypeString BuildInType = "String" TypeDoubleQuoteString BuildInType = "DoubleQuoteString" TypeSingleQuoteString BuildInType = "SingleQuoteString" TypeBacktickString BuildInType = "BacktickString" TypeTripleBacktickString BuildInType = "TripleBacktickString" TypeNumber BuildInType = "Number" TypeInteger BuildInType = "Integer" TypeFloat BuildInType = "Float" TypeBool BuildInType = "Boolean" TypeNull BuildInType = "Null" TypeNil BuildInType = "Nil" TypeComment BuildInType = "Comment" TypeKeyword BuildInType = "Keyword" TypeIdentifier BuildInType = "Identifier" TypeSymbol BuildInType = "Symbol" TypePlus BuildInType = "Plus" // + TypeMinus BuildInType = "Minus" // - TypeMultiply BuildInType = "Multiply" // * TypeDivide BuildInType = "Divide" // / TypeModulo BuildInType = "Modulo" // % TypeAssign BuildInType = "Assign" // = TypeEqual BuildInType = "Equal" // == TypeNotEqual BuildInType = "NotEqual" // != TypeLessThan BuildInType = "LessThan" // < TypeGreaterThan BuildInType = "GreaterThan" // > TypeLessThanOrEqual BuildInType = "LessThanOrEqual" // <= TypeGreaterThanOrEqual BuildInType = "GreaterThanOrEqual" // >= TypeAnd BuildInType = "And" // && TypeOr BuildInType = "Or" // || TypeNot BuildInType = "Not" // ! TypeOpenParen BuildInType = "OpenParenthesis" // ( TypeCloseParen BuildInType = "CloseParenthesis" // ) TypeOpenCurly BuildInType = "OpenCurlyBracket" // { TypeCloseCurly BuildInType = "CloseCurlyBracket" // } TypeOpenSquare BuildInType = "OpenSquareBracket" // [ TypeCloseSquare BuildInType = "CloseSquareBracket" // ] TypeComma BuildInType = "Comma" // , TypeDot BuildInType = "Dot" // . TypeColon BuildInType = "Colon" // : TypeSemicolon BuildInType = "Semicolon" // ; TypeArrowRight BuildInType = "ArrowRight" // -> TypeArrowLeft BuildInType = "ArrowLeft" // <- TypeQuestionMark BuildInType = "QuestionMark" // ? TypeTilde BuildInType = "Tilde" // ~ TypeAmpersand BuildInType = "Ampersand" // & TypePipe BuildInType = "Pipe" // | TypeCaret BuildInType = "Caret" // ^ TypeDollar BuildInType = "Dollar" // $ TypeHash BuildInType = "Hash" // # TypeAt BuildInType = "At" // @ TypeEllipses BuildInType = "Ellipses" //... TypeSpace BuildInType = "Space" TypeTab BuildInType = "Tab" TypeNewline BuildInType = "Newline" TypeCarriageReturn BuildInType = "CarriageReturn" TypeFormFeed BuildInType = "FormFeed" // AnyTokenType represents a wildcard for // token comparison using Token.Is() // and should never be returned by the lexer. AnyTokenType BuildInType = "AnyTokenType" )
func (BuildInType) String ¶
func (bit BuildInType) String() string
type CommentSyntax ¶
type CommentTokenizer ¶
type CommentTokenizer struct{}
func (CommentTokenizer) CanTokenize ¶
func (c CommentTokenizer) CanTokenize(l *Lexer) bool
type Diff ¶
type Diff struct { Field string Expect interface{} Got interface{} }
Diff stores the differences between two values
type Differ ¶
type Differ struct {
Diffs []Diff
}
Differ is the main diff engine
func (*Differ) HasDifference ¶
type Lexer ¶
type Lexer struct { CommentTokenizer CommentTokenizer LiteralTokenizer LiteralTokenizer NumberTokenizer NumberTokenizer BooleanTokenizer BooleanTokenizer SymbolTokenizer SymbolTokenizer StringTokenizer StringTokenizer // options LiteralTokens []LiteralToken StringEnclosures []StringEnclosure CommentSyntaxes []CommentSyntax Keywords []string IgnoreTokens []TokenType IgnoreWhitespace bool IgnoreComments bool UseBuiltinTypes bool CheckForKeywords bool SymbolStartCharacterMap string SymbolContinueCharacterMap string DebugPrintTokens bool OmitTokenPosition bool // contains filtered or unexported fields }
func NewLexer ¶
func NewLexer(options ...LexerOptionFunc) *Lexer
func (*Lexer) CharAtCursor ¶
CharAtCursor returns the rune at the current cursor position
func (*Lexer) CharAtPosition ¶
CharAtPosition returns the rune at the provided absolute position
func (*Lexer) CharAtRelativePosition ¶
CharAtRelativePosition returns the rune at the relative position to the cursor
func (*Lexer) CollectAnyTokenDelimited ¶
func (*Lexer) CollectTokensBetween ¶
CollectTokensBetween collects all the tokens between the open and close type starting at the offset start it assumes that the offset start contains the opening token. Nested openers and closers will be contained in the output tokens until the matching closer is found. In addition it returns the start and end cursor position for the collected portion
func (*Lexer) CollectTokensBetweenCurlyBraces ¶
CollectTokensBetweenCurlyBraced collects all the tokens between the opening and closing curly braces starting at the offset start It assumes that the offset start contains the opening curly brace. Nested curly braces will be contained in the output tokens until the matching closing curly brace to the start is found. In addition it returns the start and end cursor position for the collected portion
func (*Lexer) CollectTokensBetweenParentheses ¶
CollectTokensBetweenParentheses collects all the tokens between the opening and closing parentheses starting at the offset start It assumes that the offset start contains the opening parenthesis. Nested parenthesis will be contained in the output tokens until the matching closing parenthesis to the start is found. In addition it returns the start and end cursor position for the collected portion
func (*Lexer) CollectTokensDelimited ¶
func (Lexer) CurrentToken ¶
func (*Lexer) CursorIsOutOfBounds ¶
func (Lexer) GetCurrentLine ¶
func (*Lexer) GetPosition ¶
func (*Lexer) IncrementCursor ¶
func (*Lexer) IterateAnyTokenDelimited ¶
func (*Lexer) IterateTokensBetween ¶
func (l *Lexer) IterateTokensBetween(open TokenType, close TokenType) (iter.Seq2[Token, error], *int, *int, error)
IterateTokensBetween returns an iterator that iterates over all the tokens between the open and close type starting at the offset start it assumes that the offset start contains the opening token. Nested openers and closers will be contained in the output tokens until the matching closer is found. In addition it returns the start and end cursor position for the iterated portion
func (*Lexer) IterateTokensBetweenCurlyBraces ¶
IterateTokensBetweenCurlyBraced returns an iterator that iterates over all the tokens between the opening and closing curly braces starting at the offset start It assumes that the offset start contains the opening curly brace. Nested curly braces will be contained in the output tokens until the matching closing curly brace to the start is found. In addition it returns the start and end cursor position for the iterated portion
func (*Lexer) IterateTokensBetweenParentheses ¶
IterateTokensBetweenParentheses returns an iterator that iterates over all the tokens between the opening and closing parentheses starting at the offset start It assumes that the offset start contains the opening parenthesis. Nested parenthesis will be contained in the output tokens until the matching closing parenthesis to the start is found. In addition it returns the start and end cursor position for the iterated portion
func (*Lexer) IterateTokensDelimited ¶
func (*Lexer) Lookahead ¶
Lookahead returns the token at count offset from the cursor without consuming it
func (*Lexer) LookaheadIterator ¶
LookaheadIterator returns an iterator that iterates over the count number of tokens without consuming them
func (*Lexer) NextCharsAre ¶
NextCharsAre checks if the next chars from the cursor on match the provided chars without consuming them
func (*Lexer) NextTokenIs ¶
NextTokenIs checks if the next token is the same as the provided token without consuming the token
func (*Lexer) NextTokenIsAnyOf ¶
NextTokenIsAnyOf checks if the next token is of any of the provided tokens without consuming the token
func (*Lexer) NextTokenSequenceIs ¶
NextTokenSequenceIs checks if the next sequence of tokens in the lexer matches the provided token sequence without consuming the tokens
func (Lexer) ReachedEOF ¶
func (*Lexer) RemoveTokenizer ¶
func (l *Lexer) RemoveTokenizer(tokenizerType TokenizerType)
func (*Lexer) SkipWhitespace ¶
func (l *Lexer) SkipWhitespace()
func (*Lexer) TokenizeManual ¶
type LexerError ¶
LexerError represents an error that occurred during lexical analysis.
func NewLexerError ¶
func NewLexerError(message string, position Position, cursor int, input []rune) *LexerError
Utility to create a new LexerError with a snippet from the input.
func (*LexerError) Error ¶
func (e *LexerError) Error() string
Error implements the error interface for LexerError
type LexerOptionFunc ¶
type LexerOptionFunc func(*Lexer)
func DebugPrintTokens ¶
func DebugPrintTokens() LexerOptionFunc
func IgnoreTokens ¶
func IgnoreTokens(types ...TokenType) LexerOptionFunc
func OmitTokenPosition ¶
func OmitTokenPosition() LexerOptionFunc
func RetainWhitespace ¶
func RetainWhitespace() LexerOptionFunc
func SymbolCharacterMap ¶
func SymbolCharacterMap(startCharMap, continueCharMap string) LexerOptionFunc
func WithCommentSyntax ¶
func WithCommentSyntax(syntaxes ...CommentSyntax) LexerOptionFunc
func WithKeywords ¶
func WithKeywords(keywords ...string) LexerOptionFunc
func WithLiteralTokens ¶
func WithLiteralTokens(literalTokens ...LiteralToken) LexerOptionFunc
func WithStringEnclosure ¶
func WithStringEnclosure(enclosures ...StringEnclosure) LexerOptionFunc
func WithTokenizer ¶
func WithTokenizer(inserter TokenizerInserter) LexerOptionFunc
func WithoutCommentSyntax ¶
func WithoutCommentSyntax(syntaxes ...CommentSyntax) LexerOptionFunc
func WithoutLiteralTokens ¶
func WithoutLiteralTokens(literalTokens ...TokenType) LexerOptionFunc
func WithoutStringEnclosure ¶
func WithoutStringEnclosure(enclosures ...string) LexerOptionFunc
type LiteralToken ¶
func SortLiteralTokens ¶
func SortLiteralTokens(tokens []LiteralToken) []LiteralToken
type LiteralTokenizer ¶
type LiteralTokenizer struct{}
func (LiteralTokenizer) CanTokenize ¶
func (t LiteralTokenizer) CanTokenize(l *Lexer) bool
type LiteralTokenizerCacheKey ¶
type LiteralTokenizerCacheKey string
type LookaheadCache ¶
type LookaheadCache struct {
// contains filtered or unexported fields
}
func (*LookaheadCache) AddItem ¶
func (lc *LookaheadCache) AddItem(token Token) bool
func (*LookaheadCache) ContainsItems ¶
func (lc *LookaheadCache) ContainsItems() bool
func (*LookaheadCache) GetFirstItem ¶
func (lc *LookaheadCache) GetFirstItem() Token
func (*LookaheadCache) GetItem ¶
func (lc *LookaheadCache) GetItem(pos int) Token
func (*LookaheadCache) ItemCount ¶
func (lc *LookaheadCache) ItemCount() int
func (*LookaheadCache) PluckItem ¶
func (lc *LookaheadCache) PluckItem() Token
TODO: this does not check for out of bounds stuff.. TODO: Probably not what we want to do...
type NumberTokenizer ¶
type NumberTokenizer struct{}
func (NumberTokenizer) CanTokenize ¶
func (n NumberTokenizer) CanTokenize(l *Lexer) bool
type Position ¶
################################################### # Position ###################################################
type State ¶
type StringEnclosure ¶
var ( DoubleQuoteStringEnclosure StringEnclosure = StringEnclosure{ Type: TypeDoubleQuoteString, Enclosure: "\"", Escapable: true, } SingleQuoteStringEnclosure StringEnclosure = StringEnclosure{ Type: TypeSingleQuoteString, Enclosure: "'", } BacktickStringEnclosure StringEnclosure = StringEnclosure{ Type: TypeBacktickString, Enclosure: "`", } TripleBacktickStringEnclosure StringEnclosure = StringEnclosure{ Type: TypeTripleBacktickString, Enclosure: "```", } )
func (StringEnclosure) TokenizeEscapable ¶
func (se StringEnclosure) TokenizeEscapable(l *Lexer) (Token, error)
func (StringEnclosure) TokenizeNotEscapableMultiChar ¶
func (se StringEnclosure) TokenizeNotEscapableMultiChar(l *Lexer) (Token, error)
func (StringEnclosure) TokenizeNotEscapableSingleChar ¶
func (se StringEnclosure) TokenizeNotEscapableSingleChar(l *Lexer) (Token, error)
type StringTokenizer ¶
type StringTokenizer struct{}
func (StringTokenizer) CanTokenize ¶
func (s StringTokenizer) CanTokenize(l *Lexer) bool
type SymbolTokenizer ¶
type SymbolTokenizer struct{}
func (SymbolTokenizer) CanTokenize ¶
func (s SymbolTokenizer) CanTokenize(l *Lexer) bool
type Token ¶
################################################### # Token ###################################################
func (*Token) AppendChar ¶
func (Token) LiteralIsAnyOf ¶
func (Token) TypeIsAnyOf ¶
type TokenCollection ¶
type TokenCollection struct {
// contains filtered or unexported fields
}
TokenCollection represents an iterable collection of tokens
func NewTokenCollection ¶
func NewTokenCollection(tokens Tokens) TokenCollection
func (*TokenCollection) CollectAnyTokensDelimited ¶
func (t *TokenCollection) CollectAnyTokensDelimited(delimiter TokenType) ([]Token, error)
func (*TokenCollection) CollectTokensBetween ¶
func (t *TokenCollection) CollectTokensBetween(open TokenType, close TokenType) (Tokens, int, int, error)
CollectTokensBetween collects all the tokens between the open and close type starting at the offset start it assumes that the offset start contains the opening token. Nested openers and closers will be contained in the output tokens until the matching closer is found. In addition it returns the start and end cursor position for the collected portion
func (*TokenCollection) CollectTokensBetweenCurlyBraces ¶
func (t *TokenCollection) CollectTokensBetweenCurlyBraces() (Tokens, int, int, error)
CollectTokensBetweenCurlyBraced collects all the tokens between the opening and closing curly braces starting at the offset start It assumes that the offset start contains the opening curly brace. Nested curly braces will be contained in the output tokens until the matching closing curly brace to the start is found. In addition it returns the start and end cursor position for the collected portion
func (*TokenCollection) CollectTokensBetweenParentheses ¶
func (t *TokenCollection) CollectTokensBetweenParentheses() (Tokens, int, int, error)
CollectTokensBetweenParentheses collects all the tokens between the opening and closing parentheses starting at the offset start It assumes that the offset start contains the opening parenthesis. Nested parenthesis will be contained in the output tokens until the matching closing parenthesis to the start is found. In addition it returns the start and end cursor position for the collected portion
func (*TokenCollection) CollectTokensDelimited ¶
func (t *TokenCollection) CollectTokensDelimited(tokenType TokenType, delimiter TokenType) (Tokens, error)
func (*TokenCollection) CollectTokensUntil ¶
func (t *TokenCollection) CollectTokensUntil(delimiter TokenType) ([]Token, error)
func (TokenCollection) CursorIsOutOfBounds ¶
func (t TokenCollection) CursorIsOutOfBounds() bool
func (*TokenCollection) IncrementCursor ¶
func (t *TokenCollection) IncrementCursor(amount int)
IncrementCursor increments the cursor by the amount
func (*TokenCollection) NextToken ¶
func (t *TokenCollection) NextToken() Token
NextToken increments the cursor position by 1 and returns the token at that position
func (TokenCollection) ReachedEOF ¶
func (t TokenCollection) ReachedEOF() bool
func (*TokenCollection) TokenAtCursor ¶
func (t *TokenCollection) TokenAtCursor() Token
TokenAtCursor returns the token at the current cursor position
func (*TokenCollection) TokenAtPosition ¶
func (t *TokenCollection) TokenAtPosition(pos int) Token
TokenAtPosition returns the token at the absolute position
func (*TokenCollection) TokenAtRelativePosition ¶
func (t *TokenCollection) TokenAtRelativePosition(pos int) Token
TokenAtRelativePosition returns the token at the position relative to the cursor
type TokenType ¶
type TokenType interface {
String() string
}
################################################### # TokenType ###################################################
type TokenizerInserter ¶
type TokenizerInserter struct { Before TokenizerType After TokenizerType // contains filtered or unexported fields }
func InsertAfter ¶
func InsertAfter(after TokenizerType, tokenizerType TokenizerType, tokenizer Tokenizer) TokenizerInserter
func InsertBefore ¶
func InsertBefore(before TokenizerType, tokenizerType TokenizerType, tokenizer Tokenizer) TokenizerInserter
func (TokenizerInserter) Insert ¶
func (ti TokenizerInserter) Insert(tokenizers map[TokenizerType]Tokenizer, tokenizationOrder []TokenizerType) (map[TokenizerType]Tokenizer, []TokenizerType)
type TokenizerType ¶
type TokenizerType string
const ( TypeNoTokenizer TokenizerType = "" TypeCommentTokenizer TokenizerType = "BuildInCommentTokenizer" TypeStringTokenizer TokenizerType = "BuildInStringTokenizer" TypeNumberTokenizer TokenizerType = "BuildInNumberTokenizer" TypeLiteralTokenizer TokenizerType = "BuildInLiteralTokenizer" TypeSymbolTokenizer TokenizerType = "BuildInSymbolTokenizer" TypeBooleanTokenizer TokenizerType = "BuildInBooleanTokenizer" )