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.

113 lines 6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const extractHeaderParts_1 = __importDefault(require("../parsers/extractHeaderParts")); const extractSignificantYiniLine_1 = require("../parsers/extractSignificantYiniLine"); const print_1 = require("../utils/print"); const string_1 = require("../utils/string"); const yiniHelpers_1 = require("../utils/yiniHelpers"); /** * Extract ... * @param rawLine Raw line with the section header. * @note Implemented without regexp to keep it less cryptic, etc. */ const parseSectionHeader = (rawLine, errorHandler, ctx) => { (0, print_1.debugPrint)('-> Entered parseSectionHeader(..)'); rawLine = rawLine.trim(); const line = (0, extractSignificantYiniLine_1.extractYiniLine)(rawLine); (0, print_1.debugPrint)(' rawLine: >>>' + rawLine + '<<<'); (0, print_1.debugPrint)('extractYiniLine(..), line: >>>' + line + '<<<'); let { strMarkerChars, strSectionName, strNumberPart, isBacktickedName } = (0, extractHeaderParts_1.default)(rawLine, errorHandler, ctx); (0, print_1.debugPrint)('In parseSectionHeader(..), after extractHeaderParts(..):'); (0, print_1.debugPrint)(' strMarkerChars: ' + strMarkerChars); (0, print_1.debugPrint)(' strSectionName: ' + strSectionName); (0, print_1.debugPrint)(' strNumberPart: ' + strNumberPart); (0, print_1.debugPrint)('isBacktickedName: ' + isBacktickedName); let headerMarkerType; let level = 0; if (strMarkerChars === '') { errorHandler.pushOrBail(ctx, 'Syntax-Error', 'Unknown section header marker type', 'Section header marker type could not be identified, header text: ' + rawLine); } // --- Determing level and headerMarkerType ------------------------ if (strNumberPart === '') { headerMarkerType = 'Classic-Header-Marker'; level = strMarkerChars.length; if (strNumberPart !== '') { errorHandler.pushOrBail(ctx, 'Syntax-Error', 'Invalid extra input in section header of a repeating marker characters: ' + strNumberPart, 'Classic section header markers may not include any numbers after, correct header markers looks like ^ Title, ^^ Title, ^^^ Title, etc.'); } } else { headerMarkerType = 'Numeric-Header-Marker'; try { level = Number.parseInt(strNumberPart); } catch (err) { errorHandler.pushOrBail(ctx, 'Syntax-Error', 'No number in this shorthand section header marker found', 'This shorthand section header marker could not be parse correctly, section header text: ' + line + ', raw: ' + rawLine); } } // --------------------------------------------------------------- // --- Check level contraints based on headerMarkerType ---------- if (headerMarkerType === 'Classic-Header-Marker') { if (level > 6) { errorHandler.pushOrBail(ctx, 'Syntax-Error', 'Invalid number of repeating marker characters: ' + strMarkerChars, 'It is invalid to use seven or more section marker characters in succession (e.g. ^^^^^^^). However, to represent nesting levels deeper than 6, you may switch to the numeric shorthand section header syntax, e.g. ^7.'); } } else { if (level < 1) { errorHandler.pushOrBail(ctx, 'Syntax-Error', 'Invalid number in numeric shorthand section marker: ' + strMarkerChars, 'The number in a numeric shorthand section marker must be 1 or higher, e.g. ^1, ^2, ^3, etc.'); } } // --------------------------------------------------------------- // --- Check naming contraints based on isBacktickedName ---------- const lenOfName = strSectionName.length; if (isBacktickedName) { if (!(0, yiniHelpers_1.isValidBacktickedIdent)(strSectionName)) { errorHandler.pushOrBail(ctx, 'Syntax-Error', 'Invalid name in this section header, section name: "' + strSectionName + '"', 'Section name should be backticked like e.g. `My section name`.'); } } else { (0, print_1.debugPrint)('Naming contraints: Is not a BacktickedName'); if (lenOfName <= 0) { errorHandler.pushOrBail(ctx, 'Syntax-Error', 'Invalid section name in repeating marker characters header, section name: "' + strSectionName + '"'); } if (!(0, yiniHelpers_1.isValidSimpleIdent)(strSectionName)) { errorHandler.pushOrBail(ctx, 'Syntax-Error', 'Invalid name in this section header, section name: "' + strSectionName + '"', 'Section name must start with: A-Z, a-z, or _, unless enclosed in backticks e.g. `' + strSectionName + '`, `My section name`.'); } strSectionName = (0, string_1.trimBackticks)(strSectionName); } // --------------------------------------------------------------- // strSectionName = trimBackticks(strSectionName) (0, print_1.debugPrint)(' --------------'); (0, print_1.debugPrint)('<- About to leave parseSectionHeader(..)'); (0, print_1.debugPrint)(` rawLine = >>>${rawLine}<<<`); (0, print_1.debugPrint)(` line = >>>${line}<<<`); (0, print_1.debugPrint)(); (0, print_1.debugPrint)('identified level: ' + level); (0, print_1.debugPrint)(' SectionName: ' + strSectionName); (0, print_1.debugPrint)('headerMarkerType: ' + headerMarkerType); (0, print_1.debugPrint)(' --------------'); return { markerType: headerMarkerType, sectionName: strSectionName, sectionLevel: level, }; }; exports.default = parseSectionHeader; //# sourceMappingURL=parseSectionHeader.js.map