UNPKG

yini-parser

Version:

Node.js parser for YINI — a clean, structured INI alternative with types, simple section nesting, comments, and strict mode.

159 lines (158 loc) 5.94 kB
import { Bad_memberContext, Boolean_literalContext, ElementContext, ElementsContext, List_in_bracketsContext, ListContext, Member_colon_listContext, MemberContext, Null_literalContext, Number_literalContext, Object_literalContext, ObjectMemberContext, ObjectMemberListContext, Section_membersContext, SectionContext, String_concatContext, String_literalContext, Terminal_lineContext, ValueContext, YiniContext } from '../grammar/YiniParser.js'; import YiniParserVisitor from '../grammar/YiniParserVisitor'; import { ErrorDataHandler } from './ErrorDataHandler'; export type TDataType = undefined | 'String' | 'Number-Integer' | 'Number-Float' | 'Boolean' | 'Null' | 'Object' | 'List'; /** * This interface defines a complete generic visitor for a parse tree produced * by `YiniParser`. * * @param <IResult> The return type of the visit operation. Use `void` for * operations with no return type. */ export default class YINIVisitor<IResult> extends YiniParserVisitor<IResult> { private reversedTree; private errorHandler; private isStrict; private lastActiveSectionAtLevels; private lastActiveSectionNameAtLevels; private numOfLevelOnes; private level; private prevLevel; private prevSectionName; private meta_numOfSections; private meta_numOfMembers; private meta_numOfChains; private meta_maxLevelSection; private existingSectionTitlesAtLevels; private hasDefinedSectionTitle; private setDefineSectionTitle; constructor(errorHandler: ErrorDataHandler, isStrict: boolean); private pushOnTree; private getDepthOfLevels; private setLastActiveSection; /** * Visit a parse tree produced by `YiniParser.yini`. * @param ctx the parse tree * @return the visitor result */ visitYini: (ctx: YiniContext, isStrict?: boolean) => any; /** * Will visit here on EVERY section. * @param ctx the parse tree * @returns { [sectionName]: sectionObj } */ visitSection: (ctx: SectionContext) => 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.terminal_line`. * @param ctx the parse tree * @return the visitor result */ visitTerminal_line?: (ctx: Terminal_lineContext) => IResult; /** * Visit a parse tree produced by `YiniParser.section_members`. * In here will mount object onto members object. * @param ctx the parse tree * @returns { key: value, ... } */ visitSection_members: (ctx: Section_membersContext) => any; /** * Visit every single section or member (any key-value pair such as * key=value or key=[...] etc.). * @returns { type: resultType, key: resultKey, value: resultValue, }: IResult */ visitMember: (ctx: MemberContext) => IResult; /** * Visit a parse tree produced by `YiniParser.member_colon_list`. * @param ctx the parse tree * @return the visitor result */ visitMember_colon_list: (ctx: Member_colon_listContext) => IResult; /** * 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) => IResult; /** * Visit a parse tree produced by `YiniParser.number_literal`. * @param ctx the parse tree * @return the visitor result */ visitNumber_literal: (ctx: Number_literalContext) => IResult; /** * Visit a parse tree produced by `YiniParser.boolean_literal`. * @param ctx the parse tree * @return the visitor result */ visitBoolean_literal: (ctx: Boolean_literalContext) => IResult; /** * Visit a parse tree produced by `YiniParser.null_literal`. * @param ctx the parse tree * @return the visitor result */ visitNull_literal: (ctx: Null_literalContext) => IResult; /** * Visit a parse tree produced by `YiniParser.object_literal`. * @param ctx the parse tree * @return the visitor result */ visitObject_literal: (ctx: Object_literalContext) => IResult; /** * Visit a parse tree produced by `YiniParser.objectMemberList`. * @param ctx the parse tree * @return the visitor result */ visitObjectMemberList: (ctx: ObjectMemberListContext) => IResult; /** * Visit a parse tree produced by `YiniParser.objectMember`. * @param ctx the parse tree * @return the visitor result */ visitObjectMember: (ctx: ObjectMemberContext) => IResult; /** * Visit a parse tree produced by `YiniParser.list`. * @param ctx the parse tree * @return the visitor result */ visitList: (ctx: ListContext) => IResult; /** * Visit a parse tree produced by `YiniParser.list_in_brackets`. * @param ctx the parse tree * @return the visitor result */ visitList_in_brackets: (ctx: List_in_bracketsContext) => IResult; /** * Visit a parse tree produced by `YiniParser.elements`. * @param ctx the parse tree * @return the visitor result */ visitElements: (ctx: ElementsContext) => IResult; /** * Visit a parse tree produced by `YiniParser.element`. * @param ctx the parse tree * @return the visitor result */ visitElement: (ctx: ElementContext) => any; /** * Visit a parse tree produced by `YiniParser.string_concat`. * @param ctx the parse tree * @return the visitor result */ visitString_concat?: (ctx: String_concatContext) => IResult; }