/** * Lexer State Machine for Bash Scripts * * The core Lexer class that tokenizes input into a stream of tokens. * It handles operators, words (with quoting rules), comments, * here-documents, and escape sequences. */ import { type LexerOptions, type Token } from "./token-definitions.js"; /** * Lexer class */ export declare class Lexer { private input; private pos; private line; private column; private tokens; private pendingHeredocs; private dparenDepth; private maxHeredocSize; constructor(input: string, options?: LexerOptions); /** * Tokenize the entire input */ tokenize(): Token[]; private skipWhitespace; private nextToken; /** * Look ahead from position after (( to determine if this is nested subshells * like ((cmd) || (cmd2)) rather than arithmetic like ((1+2)). * * Returns true if it looks like nested subshells (command invocation). */ private looksLikeNestedSubshells; private makeToken; private readComment; private readWord; private readHeredocContent; /** * Register a here-document to be read after the next newline */ addPendingHeredoc(delimiter: string, stripTabs: boolean, quoted: boolean): void; /** * Look ahead from current position to find the here-doc delimiter * and register it as a pending here-doc */ private registerHeredocFromLookahead; /** * Check if position is followed by word characters (not a word boundary). * Used to determine if } should be literal or RBRACE token. */ private isWordCharFollowing; /** * Read a word that starts with a brace expansion. * Includes the brace expansion plus any suffix characters and additional brace expansions. */ private readWordWithBraceExpansion; /** * Scan ahead to detect brace expansion pattern. * Returns the full brace expansion string if found, null otherwise. * Brace expansion must contain either: * - A comma (e.g., {a,b,c}) * - A range with .. (e.g., {1..10}) */ private scanBraceExpansion; /** * Scan a literal brace word like {foo} (no comma, no range). * Returns the literal string if found, null otherwise. * This is used when {} contains something but it's not a valid brace expansion. */ private scanLiteralBraceWord; /** * Scan an extglob pattern starting at the opening parenthesis. * Extglob patterns are: @(...), *(...), +(...), ?(...), !(...) * The operator (@, *, +, ?, !) is already consumed; we start at the (. * Returns the content including parentheses, or null if not a valid extglob. */ private scanExtglobPattern; /** * Scan for FD variable syntax: {varname} immediately followed by a redirect operator. * This is the bash 4.1+ feature where {fd}>file allocates an FD and stores it in variable. * Returns the variable name and end position if found, null otherwise. * * Valid patterns: * - {varname}>file, {varname}>>file, {varname}>|file * - {varname}file * - {varname}>&N, {varname}<&N * - {varname}>&-, {varname}<&- (close FD) */ private scanFdVariable; /** * Scan ahead from a $(( position to determine if it should be treated as * $( ( subshell ) ) instead of $(( arithmetic )). * This handles cases like: * echo $(( echo 1 * echo 2 * ) ) * which should be a command substitution containing a subshell, not arithmetic. * * @param startPos - position at the second ( (i.e., at input[startPos] === "(") * @returns true if this is a subshell (closes with ) )), false if arithmetic (closes with ))) */ private dollarDparenIsSubshell; /** * Scan ahead from a (( position to determine if it closes with ) ) (nested subshells) * or )) (arithmetic). We need to track paren depth and quotes to find the matching close. * @param startPos - position after the (( (i.e., at the first char of content) * @returns true if it closes with ) ) (space between parens), false otherwise */ private dparenClosesWithSpacedParens; }