Documentation ¶
Index ¶
- func IsDecimalDigit(r rune) bool
- func IsKeyword(r []rune) bool
- func ScanNext(input []rune, k TokenKind) (token []rune, err error)
- type ScanState
- func (s ScanState) CountWhile(begin int, satisfy func(rune) bool) int
- func (s ScanState) FindFirst(begin int, patternSize int, pattern func([]rune) bool) (int, bool)
- func (s ScanState) Len() int
- func (s ScanState) PeekAt(offset int) rune
- func (s ScanState) PeekSlice(begin int, endExclusive int) []rune
- type Token
- type TokenKind
- func Comment(s *ScanState) (int, TokenKind, error)
- func IdentifierOrKeyword(s *ScanState) (int, TokenKind, error)
- func IdentifierQuoted(s *ScanState) (int, TokenKind, error)
- func LiteralQuoted(s *ScanState) (int, TokenKind, error)
- func NumberOrDot(s *ScanState) (int, TokenKind, error)
- func Spaces(s *ScanState) (int, TokenKind, error)
- func SpecialChar(s *ScanState) (int, TokenKind, error)
- type TokenScanner
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsDecimalDigit ¶
Types ¶
type ScanState ¶
ScanState represents a state for scanning and processing a sequence of runes. It contains the Input slice, which holds the runes to be scanned, and the Cursor indicating the current position in the Input slice.
func (ScanState) CountWhile ¶
CountWhile counts the number of runes that satisfy the given function 'satisfy', starting from the 'begin' position from the current Cursor position. The counting stops as soon as a rune that does not satisfy the condition is encountered. 'begin' must be in [0, s.Len()).
func (ScanState) FindFirst ¶
FindFirst searches for the first occurrence of a pattern with the given 'patternSize' using the 'pattern' function, starting from the 'begin' position from the current Cursor position. It returns the index of the first occurrence and a boolean indicating if the pattern was found. If the pattern is not found, it returns the index 's.Len()' and 'false'. 'begin' must be in [0, s.Len()).
func (ScanState) Len ¶
Len returns the remaining number of runes in the Input slice from the current Cursor position.
type Token ¶
Token represents a single token identified by the token scanner. The Token struct is used to represent identified tokens during the tokenization process. It contains information about the type and location of the token in the source code. A valid token has its TokenKind set to a specific type (not TokenUnspecified). It contains the following fields: - Kind: The TokenKind representing the type of the token. - Content: The content or value of the token. - Begin: The starting position (index) of the token in the input sequence. - End: The ending position (index) of the token in the input sequence. - Line: The line number where the token starts in the input source. - Column: The column number where the token starts in the input source.
type TokenKind ¶
type TokenKind int
TokenKind represents the type of token identified by the token scanner.
const ( // TokenUnspecified represents an unspecified or unknown token. TokenUnspecified TokenKind = iota // TokenEOF represents the end of the file (EOF) token. TokenEOF // TokenSpace represents a space token. TokenSpace // TokenComment represents a comment token. TokenComment // TokenIdentifier represents an identifier token. TokenIdentifier // TokenIdentifierQuoted represents a quoted identifier token. TokenIdentifierQuoted // TokenLiteralQuoted represents a quoted literal (string) token. TokenLiteralQuoted // TokenLiteralInteger represents an integer literal token. TokenLiteralInteger // TokenLiteralFloat represents a floating-point literal token. TokenLiteralFloat // TokenKeyword represents a keyword token. TokenKeyword // TokenSpecialChar represents a special character token. TokenSpecialChar )
func Comment ¶
Comment scans the input sequence represented by the ScanState 's' to identify and handle comments. It returns the count of runes in the scanned comment token and the corresponding TokenKind. If no comments are found at the current Cursor position, the function returns 0 for the count and TokenUnspecified for the TokenKind. If the comment starts with '#' and extends to the end of the line, the function returns the count of runes up to the newline character. If the comment starts with '--' and extends to the end of the line, the function returns the count of runes up to the newline character. If the comment starts with '/*' and ends with '*/', the function returns the count of runes up to the closing '*/' sequence. If the comment is not properly terminated with '*/', the function returns an error with a message indicating an incomplete comment.
func IdentifierOrKeyword ¶
IdentifierOrKeyword scans the input sequence represented by the ScanState 's' to identify and handle identifiers or keywords. It returns the count of runes in the scanned identifier or keyword, the corresponding TokenKind, and an error if any occurs during processing. If no identifier or keyword is found at the current Cursor position, the function returns 0 for the count, TokenUnspecified for the TokenKind, and nil for the error. If the scanned token is a keyword, the function returns the count of runes in the scanned keyword and TokenKind TokenKeyword. If the scanned token is an identifier, the function returns the count of runes in the scanned identifier and TokenKind TokenIdentifier.
func IdentifierQuoted ¶
IdentifierQuoted scans the input sequence represented by the ScanState 's' to identify and handle quoted identifiers enclosed within back quotes (`). It returns the count of runes in the scanned quoted identifier token and the corresponding TokenKind. If no quoted identifier is found at the current Cursor position, the function returns 0 for the count and TokenUnspecified for the TokenKind. If the quoted identifier is empty (two consecutive backticks), the function returns an error indicating an empty quoted identifier. If the quoted identifier is not properly enclosed within backticks, the function returns an error with a message indicating an invalid quoted identifier.
func LiteralQuoted ¶
LiteralQuoted scans the input sequence represented by the ScanState 's' to identify and handle quoted literals (strings or bytes) with optional prefixes. It returns the count of runes in the scanned quoted literal, the corresponding TokenKind, and an error if any occurs during processing. If no quoted literal is found at the current Cursor position, the function returns 0 for the count, TokenUnspecified for the TokenKind, and nil for the error.
func NumberOrDot ¶
NumberOrDot scans the input sequence represented by the ScanState 's' to identify and handle numbers or the dot (.) operator. It returns the count of runes in the scanned number or dot operator, the corresponding TokenKind, and an error if any occurs during processing. If no number or dot operator is found at the current Cursor position, the function returns 0 for the count, TokenUnspecified for the TokenKind, and nil for the error. The function recognizes hexadecimal integers (starting with "0x"), decimals (with or without a decimal point), and floating-point numbers (with or without an exponent using 'e' or 'E'). The function does not allow sign (- or +) prefix for numbers. If the scanned token is a dot (.) operator, the function returns 1 for the count and TokenKind TokenSpecialChar. If the scanned token is an integer (either decimal or hexadecimal), the function returns the count of runes in the scanned integer and TokenKind TokenLiteralInteger. If the scanned token is a floating-point number, the function returns the count of runes in the scanned number and TokenKind TokenLiteralFloat.
func Spaces ¶
Spaces scans the input sequence represented by the ScanState 's' to find the number of consecutive space runes at the current Cursor position. It returns the count of runes in the scanned space token and the corresponding TokenKind. If no spaces are found at the current Cursor position, the function returns 0 for the count and TokenUnspecified for the TokenKind. If an error occurs during processing, it will be returned as the third value, which will be nil in this implementation.
func SpecialChar ¶
SpecialChar scans the input sequence represented by the ScanState 's' to identify and handle special characters. It returns the count of runes in the scanned special character, the corresponding TokenKind, and an error if any occurs during processing. If no special character is found at the current Cursor position, the function returns 0 for the count, TokenUnspecified for the TokenKind, and nil for the error. If the scanned token is a dot (.) character followed by a decimal digit, the function returns 0 for the count, TokenUnspecified for the TokenKind, and nil. For all other cases, where the current rune represents a standalone special character, the function returns 1 for the count and TokenKind TokenSpecialChar.
type TokenScanner ¶
type TokenScanner struct { ScanState // contains filtered or unexported fields }
TokenScanner provides a tokenizer for processing a sequence of runes and identifying different types of tokens.
func (*TokenScanner) Init ¶
func (s *TokenScanner) Init(input []rune)
func (*TokenScanner) ScanNext ¶
func (s *TokenScanner) ScanNext() (Token, error)
ScanNext scans the next token in the input sequence and returns the Token and an error if any occurs during processing. If the end of the input sequence is reached, the method returns a special Token with TokenKind TokenEOF to indicate the end of the file.