import Token from './tokenizer/tokens.cjs'; import CollectionNode from './nodes/collections.cjs'; import DocumentNode from './nodes/document.cjs'; import ObjectNode from './nodes/objects.cjs'; import '../core/decimal/decimal.cjs'; import '../core/positions.cjs'; import '../errors/io-error.cjs'; import './tokenizer/token-types.cjs'; import '../core/definitions.cjs'; import '../schema/schema.cjs'; import '../schema/types/memberdef.cjs'; import '../schema/schema-types.cjs'; import './nodes/containers.cjs'; import './nodes/nodes.cjs'; import './nodes/section.cjs'; import './nodes/tokens.cjs'; import '../core/internet-object.cjs'; import './nodes/members.cjs'; /** * ASTParser transforms a token stream into an Abstract Syntax Tree (AST). * * @remarks * This is a recursive descent parser with error recovery capabilities. * It implements a three-tier error recovery strategy: * * 1. **Token-level**: Handles individual token errors (via ERROR tokens from tokenizer) * 2. **Collection-level**: `skipToNextCollectionItem()` - recovers at `~` boundaries * 3. **Section-level**: `skipToNextSyncPoint()` - recovers at structural terminators * * The parser accumulates errors in the `errors` array and continues parsing, * allowing IDEs to show all diagnostics in a single pass. * * @example * ```ts * const tokenizer = new Tokenizer(input); * const tokens = tokenizer.tokenize(); * const parser = new ASTParser(tokens); * const doc = parser.parse(); * * // Check for errors * const errors = doc.getErrors(); * if (errors.length > 0) { * errors.forEach(e => console.error(e.message)); * } * ``` */ declare class ASTParser { /** Array of tokens produced by the tokenizer */ private readonly tokens; /** Tracks section names for duplicate detection */ private readonly sectionNames; /** Accumulated errors during parsing for IDE diagnostics */ private readonly errors; /** Current token index in the token stream */ private current; private static readonly CURLY_OPEN_ARRAY; private static readonly CURLY_CLOSE_ARRAY; private static readonly BRACKET_OPEN_ARRAY; private static readonly BRACKET_CLOSE_ARRAY; private static readonly COLLECTION_START_ARRAY; private static readonly SECTION_SEP_ARRAY; private static readonly COMMA_ARRAY; private static readonly COLON_ARRAY; private static readonly COLLECTION_OR_SECTION_ARRAY; private static readonly VALID_KEY_TYPES; /** * Creates a new ASTParser instance. * @param tokens - Read-only array of tokens from the tokenizer */ constructor(tokens: readonly Token[]); /** * Parses the token stream and produces a DocumentNode AST. * * @returns DocumentNode containing the parsed AST and accumulated errors */ parse(): DocumentNode; /** * Creates a syntax error with proper range spanning the entire construct. * * @remarks * Industry standard (TypeScript, Roslyn): Error should highlight from * the opening token to the last valid token, not just a single point. * This provides better IDE experience for unclosed constructs. * * @param errorCode - The error code from ErrorCodes * @param message - Human-readable error message * @param startToken - Opening token of the construct (e.g., '{' or '[') * @param members - Array of parsed members/elements to find the last valid token * @returns SyntaxError with position range spanning the entire construct */ private createUnclosedConstructError; private processDocument; private processSection; private parseSectionAndSchemaNames; parseSectionContent(): ObjectNode | CollectionNode | null; private processCollection; /** * Skips tokens until the next collection item boundary. * * @remarks * This is a **collection-level error recovery** mechanism. * When an error occurs while parsing a collection item, this method * advances the token stream to the next `~` (COLLECTION_START) or * section separator (`---`), allowing parsing to continue with the * next collection item. * * Recovery pattern: * ``` * ~ valid, item ← parsed successfully * ~ { unclosed object ← error here, skipToNextCollectionItem called * ~ another, valid ← parsing resumes here * ``` */ private skipToNextCollectionItem; /** * Skips tokens to the next synchronization point after a parsing error. * * @remarks * This is a **section-level error recovery** mechanism. * Synchronization points are structural terminator tokens (comma, colon, * brackets, etc.) that serve as safe resumption points after an error. * * This method is more aggressive than `skipToNextCollectionItem()` and * is used when errors occur in non-collection contexts or when finer-grained * recovery is needed. * * The method uses `isTerminator()` to check for token types that represent * structural boundaries in the Internet Object format. */ private skipToNextSyncPoint; private processObject; private checkForPendingTokens; private parseObject; private parseMember; private parseArray; private parseValue; private pushUndefinedMember; /** * Type guard to check if a token is valid (not null) */ private isValidToken; /** * Returns the current token without advancing the current index * @returns {Token} the current token or null if eof is reached */ private peek; /** * Advances the current token index by the given number of steps */ private advance; /** * Checks if the current token matches any of the given types. * If a current token is not available, returns false. * @param types - Array of token types to match against * @returns true if current token matches any of the given types */ private match; private matchPrev; /** * Match the next token in the stream without advancing the current index. * If the next token matches any of the given types, returns true, otherwise returns false. * If the next token is not available, returns false. * @param types - Array of token types to match against * @returns true if next token matches any of the given types */ private matchNext; private advanceIfMatch; } export { ASTParser as default };