### Defining tokens with multiple lexeme strings Source: https://github.com/anothersubatomo/moonslice/blob/main/README.md Shows how to define a token with multiple static lexeme strings. This applies to both static and pattern-based tokens. ```lua -- ... TRUE = { "true", "TRUE" }, -- ... ``` -------------------------------- ### Basic lexer construction with pattern and static tokens Source: https://github.com/anothersubatomo/moonslice/blob/main/README.md Constructs a lexer by passing pattern tokens (using Lua string patterns) and static tokens (exact strings) to the Moonslice function. Pattern tokens can use a single capture group to define the lexeme. ```lua local Moonslice = require(game.ReplicatedStorage:WaitForChild("Moonslice")) local Lexer = Moonslice ({ STRING = "\"([^\n\r]*)\"", BIN_NUMBER = "0[bB](%d+)", HEX_NUMBER = "0[xX](%d+)", SCI_NUMBER = "%d+e%d+", DEC_NUMBER = "(%d+)" }, { LOCAL = "local", FUNCTION = "function", IF = "if", THEN = "then", ELSE = "else", END = "end" }) ``` -------------------------------- ### Moonslice (Lexer Constructor) Source: https://github.com/anothersubatomo/moonslice/blob/main/README.md Constructs a lexer by providing pattern-based and static token definitions. Returns a Lexer object that can be used to tokenize input text. ```APIDOC ## Moonslice (Lexer Constructor) ### Description Constructs a lexer by providing pattern-based and static token definitions. Returns a Lexer object that can be used to tokenize input text. ### Method Function call ### Endpoint `Moonslice(patternTokens, staticTokens)` ### Parameters #### patternTokens (table) - Required A table mapping token types to Lua string patterns. Patterns can include a single capture group `()` to specify the lexeme portion. #### staticTokens (table) - Required A table mapping token types to static strings or tables of strings. Each entry can also include a `Drop` field (boolean) to indicate whether the token should be ignored. ### Request Example ```lua local Moonslice = require(game.ReplicatedStorage:WaitForChild("Moonslice")) local Lexer = Moonslice ({ STRING = "\"([^\n\r]*)\"", BIN_NUMBER = "0[bB](%d+)", HEX_NUMBER = "0[xX](%d+)", SCI_NUMBER = "%d+e%d+", DEC_NUMBER = "(%d+)" }, { LOCAL = "local", FUNCTION = "function", IF = "if", THEN = "then", ELSE = "else", END = "end" }) ``` ### Response #### Success Response Returns a Lexer object that can be used to tokenize input text. #### Response Example ```lua local Lexer = Moonslice({...}, {...}) ``` ``` -------------------------------- ### :Next() -> (Token) Source: https://github.com/anothersubatomo/moonslice/blob/main/README.md Advances the lexer to the next token and returns it. Updates the `TokenNow` property to the returned token, consuming peeked tokens if available. ```APIDOC ## :Next() -> (Token) ### Description Advances the lexer to the next token and returns it. Updates the `TokenNow` property to the returned token. If `TokensAhead` has peeked tokens, `:Next()` will consume the first one; otherwise, it parses a new token from the input. ### Method :Next ### Parameters None ### Returns - **Token** - The next token in the input. ``` -------------------------------- ### :Peek(Amount: number?) -> (Token) Source: https://github.com/anothersubatomo/moonslice/blob/main/README.md Returns the token a specified number of positions ahead without advancing the lexer. Caches the result in `TokensAhead` to avoid re-parsing. ```APIDOC ## :Peek(Amount: number?) -> (Token) ### Description Returns the token `Amount` positions ahead of the current one without advancing the lexer. If `Amount` is not specified, defaults to `1`. The returned token is cached in `TokensAhead` so repeated peeks do not reparse the input. ### Method :Peek ### Parameters #### Input Parameters - **Amount** (number) - Optional - The number of positions ahead to peek. Defaults to 1. ### Returns - **Token** - The token at the specified lookahead position. ``` -------------------------------- ### :Reset(Input: string, Source: string?) -> () Source: https://github.com/anothersubatomo/moonslice/blob/main/README.md Resets the lexer's internal state to prepare for tokenizing the given input text. Clears the current token and any peeked tokens. ```APIDOC ## :Reset(Input: string, Source: string?) -> () ### Description Resets the internal state of the lexer in preparation to lexically analyze the given `Input` text. This method also clears `TokenNow` and `TokensAhead`. ### Method :Reset ### Parameters #### Input Parameters - **Input** (string) - Required - The raw source code (or string) to tokenize. - **Source** (string) - Optional - A label or name for the input (e.g., filename). If omitted, defaults to "unknown". Used only for debugging and error messages. ### Returns - **()** - No return value. ``` -------------------------------- ### :GetCurrentLine() -> (number) Source: https://github.com/anothersubatomo/moonslice/blob/main/README.md Returns the current line number the lexer is reading from. Useful for error reporting and diagnostics. ```APIDOC ## :GetCurrentLine() -> (number) ### Description Returns the current line number the lexer is reading from. Starts at 1 and increments as the lexer encounters newline characters. ### Method :GetCurrentLine ### Parameters None ### Returns - **number** - The current line number. ``` -------------------------------- ### Dropping tokens with Drop flag Source: https://github.com/anothersubatomo/moonslice/blob/main/README.md Specifies that a token should be ignored by the lexer using the optional Drop field. Defaults to false when unspecified. ```lua -- ... WHITESPACE = { "%s+", Drop = true }, -- ... ``` -------------------------------- ### :LexicalError(Message: string, Token: Token?) -> () Source: https://github.com/anothersubatomo/moonslice/blob/main/README.md Raises a lexical error, typically during the tokenization phase. Includes line number and source label for debugging. ```APIDOC ## :LexicalError(Message: string, Token: Token?) -> () ### Description Raises a **lexical error**, typically during the tokenization phase. This function usually includes the line number and `Source` label (if set), making debugging easier. ### Method :LexicalError ### Parameters #### Input Parameters - **Message** (string) - Required - A description of what went wrong (e.g., "Unexpected character"). - **Token** (Token) - Optional - The specific token or position to attach to the error. If omitted, the lexer may fallback to the current position. ### Returns - **()** - No return value. ``` -------------------------------- ### :SyntaxError(Message: string) -> () Source: https://github.com/anothersubatomo/moonslice/blob/main/README.md Raises a syntax error, typically during parsing. Functionally equivalent to `:LexicalError(Message, TokenNow)`. ```APIDOC ## :SyntaxError(Message: string) -> () ### Description Raises a **syntax error**, typically during parsing. Functionally equivalent to `:LexicalError(Message, TokenNow)`, using the current token. Intended for situations where a valid token was produced, but it doesn't fit expected grammar. ### Method :SyntaxError ### Parameters #### Input Parameters - **Message** (string) - Required - A description of what went wrong (e.g., "Expected `)` after expression"). ### Returns - **()** - No return value. ``` -------------------------------- ### Token type definition Source: https://github.com/anothersubatomo/moonslice/blob/main/README.md Defines the structure of tokens produced by Moonslice lexers. The Lexeme field holds the matched character sequence, and Type holds the token type. ```lua type Token = { Lexeme: string, Type: string } ``` -------------------------------- ### Overriding identifier pattern with NAME field Source: https://github.com/anothersubatomo/moonslice/blob/main/README.md Overrides the default identifier pattern by assigning a NAME field in the pattern token table. The default pattern is "[%a_][%a%d_]*". ```lua -- ... NAME = "[%a_][%a%d_]*", -- ... ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.