UNPKG

yini-parser

Version:

Readable configuration without YAML foot-guns or JSON noise. The official Node.js parser for YINI config format — An INI-inspired configuration format with clear nesting, explicit types, and predictable parsing.

192 lines (191 loc) 7.76 kB
import { AnnotationContext, AssignmentContext, Bad_memberContext, Bad_meta_textContext, Boolean_literalContext, Colon_list_declContext, DirectiveContext, ElementsContext, EolContext, List_literalContext, MemberContext, Meta_stmtContext, Null_literalContext, Number_literalContext, Object_literalContext, Object_memberContext, Object_membersContext, PrologContext, StmtContext, String_concatContext, String_literalContext, Terminal_stmtContext, ValueContext, YiniContext } from '../grammar/generated/YiniParser.js'; import YiniParserVisitor from '../grammar/generated/YiniParserVisitor'; import { ErrorDataHandler } from './errorDataHandler'; import { IParseCoreOptions, IYiniAST, TSourceType } from './internalTypes'; /** Parse SECTION_HEAD token text → {level, name}. * Supports repeated markers (^^^^) and shorthand (^7) (Spec 5.2–5.3.1). :contentReference[oaicite:5]{index=5}:contentReference[oaicite:6]{index=6} */ /** * This interface defines a complete generic visitor for a parse tree produced * by `YiniParser`. * * @param <Result> The return type of the visit operation. Use `void` for * operations with no return type. */ export default class ASTBuilder<Result> extends YiniParserVisitor<Result> { private errorHandler; private readonly options; private readonly isStrict; private readonly onDuplicateKey; private ast; private sectionStack; private meta_hasYiniMarker; private _numOfMembers; private meta_maxLevel; mapSectionNamePaths: Map<string, number>; /** * @param metaFileName If parsing from a file, provide the file name here so the meta information can be updated accordingly. * @param metaLineCount Provide the line-count here so the meta information can be updated accordingly. */ constructor(errorHandler: ErrorDataHandler, options: IParseCoreOptions, sourceType: TSourceType, metaFileName: string | null); private hasDefinedSectionTitle; private setDefineSectionTitle; /** Attach a section to the stack respecting up/down moves (Spec 5.3). :contentReference[oaicite:7]{index=7} */ private attachSection; /** Insert a key/value into current section (duplicate handling per options). */ private putMember; buildAST(ctx: YiniContext): IYiniAST; /** * Visit a parse tree produced by `YiniParser.yini`. * @param ctx the parse tree * @return the visitor result */ visitYini: (ctx: YiniContext) => any; /** * Visit a parse tree produced by `YiniParser.prolog`. * @param ctx the parse tree * @return the visitor result */ visitProlog: (ctx: PrologContext) => any; /** * Visit a parse tree produced by `YiniParser.terminal`. * @param ctx the parse tree * @return the visitor result */ visitTerminal_stmt: (ctx: Terminal_stmtContext) => any; /** * Visit a parse tree produced by `YiniParser.stmt`. * @param ctx the parse tree * @grammarRule eol | SECTION_HEAD | assignment | colon_list_decl | marker_stmt | bad_member * @return the visitor result */ visitStmt: (ctx: StmtContext) => any; /** * Visit a parse tree produced by `YiniParser.meta_stmt`. * @param ctx the parse tree */ visitMeta_stmt: (ctx: Meta_stmtContext) => any; /** * Visit a parse tree produced by `YiniParser.directive`. * @param ctx the parse tree * @note Directive statements in YINI are special top-level commands that * appear only at the beginning of a document, before any sections * or members. Each directive may occur at most once per file. */ visitDirective: (ctx: DirectiveContext) => any; /** * Visit a parse tree produced by `YiniParser.annotation`. * @param ctx the parse tree * @return the visitor result */ visitAnnotation: (ctx: AnnotationContext) => any; /** * Visit a parse tree produced by `YiniParser.eol`. * @param ctx the parse tree * @return the visitor result */ visitEol: (ctx: EolContext) => any; /** * Visit a parse tree produced by `YiniParser.assignment`. * @param ctx the parse tree * @return the visitor result */ visitAssignment: (ctx: AssignmentContext) => any; /** * Visit a parse tree produced by `YiniParser.member`. * @param ctx the parse tree * @grammarRule KEY WS? EQ WS? value? * @return the visitor result */ visitMember: (ctx: MemberContext) => any; /** * Visit a parse tree produced by `YiniParser.value`. * @param ctx the parse tree * @return the visitor result */ visitValue: (ctx: ValueContext) => any; /** * Visit a parse tree produced by `YiniParser.string_literal`. * @param ctx the parse tree * @return the visitor result */ visitString_literal: (ctx: String_literalContext) => any; /** * Visit a parse tree produced by `YiniParser.number_literal`. * @param ctx the parse tree * @return the visitor result */ visitNumber_literal: (ctx: Number_literalContext) => any; /** * Visit a parse tree produced by `YiniParser.boolean_literal`. * @param ctx the parse tree * @return the visitor result */ visitBoolean_literal: (ctx: Boolean_literalContext) => any; /** * Visit a parse tree produced by `YiniParser.null_literal`. * @param ctx the parse tree * @return the visitor result */ visitNull_literal: (ctx: Null_literalContext) => any; /** * Visit a parse tree produced by `YiniParser.list_literal`. * @param ctx the parse tree * @grammarRule OB NL* elements? NL* CB NL* | EMPTY_LIST NL* * @return the visitor result */ visitList_literal: (ctx: List_literalContext) => any; /** * Visit a parse tree produced by `YiniParser.elements`. * @param ctx the parse tree * @grammarRule value (NL* COMMA NL* value)* COMMA? * @return the visitor result */ visitElements: (ctx: ElementsContext) => any; /** * Visit a parse tree produced by `YiniParser.object_literal`. * @param ctx the parse tree * @grammarRule OC NL* object_members? NL* CC NL* | EMPTY_OBJECT NL* * @return the visitor result */ visitObject_literal: (ctx: Object_literalContext) => any; /** * Visit a parse tree produced by `YiniParser.object_members`. * @param ctx the parse tree * @grammarRule object_member (COMMA NL* object_member)* COMMA? * @return the visitor result */ visitObject_members: (ctx: Object_membersContext) => any; /** * Visit a parse tree produced by `YiniParser.object_member`. * @param ctx the parse tree * @grammarRule KEY WS? COLON NL* value * @return the visitor result */ visitObject_member: (ctx: Object_memberContext) => any; /** * Visit a parse tree produced by `YiniParser.colon_list_decl`. * @param ctx the parse tree * @grammarRule KEY WS? COLON (eol | WS+)* elements (eol | WS+)* eol * @return the visitor result */ visitColon_list_decl: (ctx: Colon_list_declContext) => any; /** * Visit a parse tree produced by `YiniParser.string_concat`. * @param ctx the parse tree * @return the visitor result */ visitString_concat: (ctx: String_concatContext) => any; /** * Visit a parse tree produced by `YiniParser.bad_member`. * @param ctx the parse tree * @return the visitor result */ visitBad_member: (ctx: Bad_memberContext) => any; /** * Visit a parse tree produced by `YiniParser.bad_meta_text`. * @param ctx the parse tree * @return the visitor result */ visitBad_meta_text: (ctx: Bad_meta_textContext) => any; }