yini-parser 1.0.1-beta → 1.1.0-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/README.md +131 -328
- package/dist/YINI.d.ts +34 -11
- package/dist/YINI.js +206 -121
- package/dist/core/ASTBuilder.d.ts +191 -0
- package/dist/core/ASTBuilder.js +827 -0
- package/dist/core/ErrorDataHandler.d.ts +19 -19
- package/dist/core/ErrorDataHandler.js +258 -150
- package/dist/core/objectBuilder.d.ts +9 -3
- package/dist/core/objectBuilder.js +126 -163
- package/dist/core/types.d.ts +234 -44
- package/dist/core/types.js +7 -33
- package/dist/grammar/YiniLexer.d.ts +54 -48
- package/dist/grammar/YiniLexer.js +330 -293
- package/dist/grammar/YiniParser.d.ts +167 -150
- package/dist/grammar/YiniParser.js +1241 -1202
- package/dist/grammar/YiniParserVisitor.d.ts +59 -45
- package/dist/grammar/YiniParserVisitor.js +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.js +298 -120
- package/dist/parseEntry.d.ts +3 -2
- package/dist/parseEntry.js +352 -81
- package/dist/parsers/extractHeaderParts.d.ts +3 -2
- package/dist/parsers/extractHeaderParts.js +1 -0
- package/dist/parsers/parseBoolean.d.ts +1 -1
- package/dist/parsers/parseBoolean.js +2 -1
- package/dist/parsers/parseNumber.d.ts +8 -3
- package/dist/parsers/parseNumber.js +50 -16
- package/dist/parsers/parseSectionHeader.d.ts +3 -2
- package/dist/parsers/parseSectionHeader.js +1 -0
- package/dist/utils/number.d.ts +3 -0
- package/dist/utils/number.js +18 -0
- package/dist/utils/object.d.ts +55 -0
- package/dist/utils/object.js +85 -0
- package/dist/utils/string.d.ts +21 -1
- package/dist/utils/string.js +39 -4
- package/dist/utils/system.d.ts +15 -0
- package/dist/utils/system.js +21 -0
- package/dist/yiniHelpers.d.ts +3 -0
- package/dist/yiniHelpers.js +43 -7
- package/package.json +3 -3
- package/dist/core/YINIVisitor.d.ts +0 -158
- package/dist/core/YINIVisitor.js +0 -1010
package/dist/YINI.js
CHANGED
|
@@ -4,139 +4,224 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const perf_hooks_1 = require("perf_hooks");
|
|
7
8
|
const env_1 = require("./config/env");
|
|
9
|
+
const ErrorDataHandler_1 = require("./core/ErrorDataHandler");
|
|
8
10
|
const parseEntry_1 = require("./parseEntry");
|
|
9
11
|
const pathAndFileName_1 = require("./utils/pathAndFileName");
|
|
10
12
|
const print_1 = require("./utils/print");
|
|
13
|
+
const string_1 = require("./utils/string");
|
|
14
|
+
const DEFAULT_TAB_SIZE = 4; // De facto "modern default" (even though traditionally/historically it's 8).
|
|
15
|
+
let _runtimeInfo = {
|
|
16
|
+
sourceType: 'Inline',
|
|
17
|
+
fileName: undefined,
|
|
18
|
+
fileByteSize: null,
|
|
19
|
+
lineCount: null,
|
|
20
|
+
timeIoMs: null,
|
|
21
|
+
preferredBailSensitivity: null,
|
|
22
|
+
sha256: null,
|
|
23
|
+
};
|
|
24
|
+
// Type guard: did the caller use the options-object form?
|
|
25
|
+
const isOptionsObjectForm = (v) => {
|
|
26
|
+
return (v != null &&
|
|
27
|
+
typeof v === 'object' &&
|
|
28
|
+
// Note: If one wants, this can be relax to "typeof v === 'object'"
|
|
29
|
+
// but this keeps accidental booleans/strings out.
|
|
30
|
+
('strictMode' in v ||
|
|
31
|
+
'failLevel' in v ||
|
|
32
|
+
'includeMetaData' in v ||
|
|
33
|
+
'includeDiagnostics' in v ||
|
|
34
|
+
'includeTiming' in v ||
|
|
35
|
+
'preserveUndefinedInMeta' in v ||
|
|
36
|
+
'requireDocTerminator' in v));
|
|
37
|
+
};
|
|
38
|
+
// Initial default values.
|
|
39
|
+
const DEFAULT_OPTS = {
|
|
40
|
+
strictMode: false,
|
|
41
|
+
failLevel: 'auto',
|
|
42
|
+
includeMetaData: false,
|
|
43
|
+
includeDiagnostics: false,
|
|
44
|
+
includeTiming: false,
|
|
45
|
+
preserveUndefinedInMeta: false,
|
|
46
|
+
requireDocTerminator: false,
|
|
47
|
+
};
|
|
11
48
|
/**
|
|
12
49
|
* This class is the public API, which exposes only parse(..) and
|
|
13
50
|
* parseFile(..), rest of the implementation details are hidden.
|
|
14
51
|
* @note Only parse and parseFile are public.
|
|
15
52
|
*/
|
|
16
53
|
class YINI {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
* @param yiniContent YINI code as a string (multi‑line content supported).
|
|
23
|
-
* @param strictMode If `true`, enforce strict parsing rules (e.g. require `/END`, disallow trailing commas).
|
|
24
|
-
* @param bailSensitivity Controls how errors and warnings are handled:
|
|
25
|
-
* - `'auto'` : Auto‑select level (strict→1, lenient→0)
|
|
26
|
-
* - `0` / `'Ignore-Errors'` : Continue parsing despite errors; log them and attempt recovery.
|
|
27
|
-
* - `1` / `'Abort-on-Errors'` : Stop parsing on the first error.
|
|
28
|
-
* - `2` / `'Abort-Even-on-Warnings'`: Stop parsing on the first warning **or** error.
|
|
29
|
-
* @param includeMetaData If `true`, return additional metadata (e.g. warnings, statistics) alongside the parsed object.
|
|
30
|
-
*
|
|
31
|
-
* @note The order of properties in each output object may differ from their order in the YINI source.
|
|
32
|
-
*
|
|
33
|
-
* @returns A JavaScript object representing the parsed YINI content.
|
|
34
|
-
*/
|
|
35
|
-
YINI.parse = (yiniContent, strictMode = false, bailSensitivity = 'auto', includeMetaData = false) => {
|
|
36
|
-
(0, print_1.debugPrint)('-> Entered static parse(..) in class YINI\n');
|
|
37
|
-
// Important: First, before anything, trim beginning and trailing whitespaces!
|
|
38
|
-
yiniContent = yiniContent.trim();
|
|
39
|
-
if (!yiniContent) {
|
|
40
|
-
throw new Error('Syntax-Error: Unexpected blank YINI input');
|
|
41
|
-
}
|
|
42
|
-
if (!yiniContent.endsWith('\n')) {
|
|
43
|
-
yiniContent += '\n';
|
|
54
|
+
/**
|
|
55
|
+
* @returns The number of spaces per tab character used in error messages.
|
|
56
|
+
*/
|
|
57
|
+
static getTabSize() {
|
|
58
|
+
return this.g_tabSize;
|
|
44
59
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Overrides the number of spaces per tab character used in error messages.
|
|
62
|
+
* Allowed range: 1-32.
|
|
63
|
+
*/
|
|
64
|
+
static setTabSize(spaces) {
|
|
65
|
+
if (spaces < 1 || spaces > 32) {
|
|
66
|
+
new ErrorDataHandler_1.ErrorDataHandler('None/Ignore').pushOrBail(null, 'Fatal-Error', `Invalid tab size ${spaces} is out of range.`, 'Tab size must be between 1 and 32 spaces.');
|
|
67
|
+
}
|
|
68
|
+
this.g_tabSize = spaces;
|
|
54
69
|
}
|
|
55
|
-
|
|
56
|
-
|
|
70
|
+
// --- Single implementation --------------------------------------------
|
|
71
|
+
// Implementation method (not declared with arrow function) for both method overload signatures.
|
|
72
|
+
// NOTE: Must be method declaration with NO =, arrow functions not (currently) supported for this type of method overloading.
|
|
73
|
+
static parse(yiniContent, arg2, // strictMode | options
|
|
74
|
+
failLevel = DEFAULT_OPTS.failLevel, includeMetaData = DEFAULT_OPTS.includeMetaData) {
|
|
75
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
76
|
+
(0, print_1.debugPrint)('-> Entered static parse(..) in class YINI\n');
|
|
77
|
+
// Runtime guard to catch illegal/ambiguous calls coming from JS or any-cast code
|
|
78
|
+
if (isOptionsObjectForm(arg2) &&
|
|
79
|
+
(failLevel !== 'auto' || includeMetaData !== false)) {
|
|
80
|
+
throw new TypeError('Invalid call: when providing an options object, do not also pass positional parameters.');
|
|
81
|
+
}
|
|
82
|
+
// Normalize to a fully-required options object.
|
|
83
|
+
let userOpts;
|
|
84
|
+
// Required, makes all properties in T required, no undefined.
|
|
85
|
+
// const userOpts: Required<IAllUserOptions> = isOptionsObjectForm(arg2)
|
|
86
|
+
if (isOptionsObjectForm(arg2)) {
|
|
87
|
+
// Options-object Form.
|
|
88
|
+
/* parse = (
|
|
89
|
+
yiniContent: string,
|
|
90
|
+
options: IAllUserOptions,
|
|
91
|
+
)
|
|
92
|
+
*/
|
|
93
|
+
userOpts = Object.assign(Object.assign({}, DEFAULT_OPTS), { strictMode: (_a = arg2.strictMode) !== null && _a !== void 0 ? _a : DEFAULT_OPTS.strictMode, failLevel: (_b = arg2.failLevel) !== null && _b !== void 0 ? _b : DEFAULT_OPTS.failLevel, includeMetaData: (_c = arg2.includeMetaData) !== null && _c !== void 0 ? _c : DEFAULT_OPTS.includeMetaData, includeDiagnostics: (_d = arg2.includeDiagnostics) !== null && _d !== void 0 ? _d : DEFAULT_OPTS.includeDiagnostics, includeTiming: (_e = arg2.includeTiming) !== null && _e !== void 0 ? _e : DEFAULT_OPTS.includeTiming, preserveUndefinedInMeta: (_f = arg2.preserveUndefinedInMeta) !== null && _f !== void 0 ? _f : DEFAULT_OPTS.preserveUndefinedInMeta, requireDocTerminator: (_g = arg2.requireDocTerminator) !== null && _g !== void 0 ? _g : DEFAULT_OPTS.requireDocTerminator });
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
// Positional form.
|
|
97
|
+
/* parse = (
|
|
98
|
+
yiniContent: string,
|
|
99
|
+
strictMode?: boolean,
|
|
100
|
+
failLevel?: TPreferredFailLevel,
|
|
101
|
+
includeMetaData?: boolean,
|
|
102
|
+
)
|
|
103
|
+
*/
|
|
104
|
+
userOpts = Object.assign(Object.assign({}, DEFAULT_OPTS), { strictMode: (_h = arg2) !== null && _h !== void 0 ? _h : DEFAULT_OPTS.strictMode, failLevel,
|
|
105
|
+
includeMetaData });
|
|
106
|
+
}
|
|
107
|
+
if (userOpts.includeMetaData && _runtimeInfo.sourceType === 'Inline') {
|
|
108
|
+
const lineCount = yiniContent.split(/\r?\n/).length; // Counts the lines.
|
|
109
|
+
const sha256 = (0, string_1.computeSha256)(yiniContent); // NOTE: Compute BEFORE any possible tampering of content.
|
|
110
|
+
_runtimeInfo.lineCount = lineCount;
|
|
111
|
+
_runtimeInfo.preferredBailSensitivity = userOpts.failLevel;
|
|
112
|
+
_runtimeInfo.sha256 = sha256;
|
|
113
|
+
}
|
|
114
|
+
// NOTE: Important: Do not trim or mutate the yiniContent here, due
|
|
115
|
+
// to it will mess up the line numbers in error reporting.
|
|
116
|
+
if (!yiniContent) {
|
|
117
|
+
throw new Error('Syntax-Error: Unexpected blank YINI input');
|
|
118
|
+
}
|
|
119
|
+
if (!yiniContent.endsWith('\n')) {
|
|
120
|
+
yiniContent += '\n';
|
|
121
|
+
}
|
|
122
|
+
let level = 0;
|
|
123
|
+
if (userOpts.failLevel === 'auto') {
|
|
124
|
+
if (!userOpts.strictMode)
|
|
125
|
+
level = 0;
|
|
126
|
+
if (userOpts.strictMode)
|
|
127
|
+
level = 1;
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
level = userOpts.failLevel;
|
|
131
|
+
}
|
|
132
|
+
const coreOpts = {
|
|
133
|
+
isStrict: userOpts.strictMode,
|
|
134
|
+
bailSensitivity: level,
|
|
135
|
+
isIncludeMeta: userOpts.includeMetaData,
|
|
136
|
+
isWithDiagnostics: (0, env_1.isDev)() || (0, env_1.isDebug)() || userOpts.includeDiagnostics,
|
|
137
|
+
isWithTiming: (0, env_1.isDev)() || (0, env_1.isDebug)() || userOpts.includeTiming,
|
|
138
|
+
isKeepUndefinedInMeta: (0, env_1.isDebug)() || userOpts.preserveUndefinedInMeta,
|
|
139
|
+
isRequireDocTerminator: userOpts.requireDocTerminator,
|
|
140
|
+
};
|
|
141
|
+
(0, print_1.debugPrint)();
|
|
142
|
+
(0, print_1.debugPrint)('==== Call parse ==========================');
|
|
143
|
+
const result = (0, parseEntry_1._parseMain)(yiniContent, coreOpts, _runtimeInfo);
|
|
144
|
+
(0, print_1.debugPrint)('==== End call parse ==========================\n');
|
|
145
|
+
if ((0, env_1.isDev)()) {
|
|
146
|
+
console.log();
|
|
147
|
+
(0, print_1.devPrint)('YINI.parse(..): result:');
|
|
148
|
+
console.log(result);
|
|
149
|
+
(0, print_1.devPrint)('Complete result:');
|
|
150
|
+
(0, print_1.printObject)(result);
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
57
153
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
154
|
+
// --- Single implementation --------------------------------------------
|
|
155
|
+
// Implementation method (not declared with arrow function) for both method overload signatures.
|
|
156
|
+
// NOTE: Must be method declaration with NO =, arrow functions not (currently) supported for this type of method overloading.
|
|
157
|
+
static parseFile(filePath, arg2, // strictMode | options
|
|
158
|
+
failLevel = DEFAULT_OPTS.failLevel, includeMetaData = DEFAULT_OPTS.includeMetaData) {
|
|
159
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
160
|
+
(0, print_1.debugPrint)('-> Entered static parseFile(..) in class YINI\n');
|
|
161
|
+
(0, print_1.debugPrint)('Current directory = ' + process.cwd());
|
|
162
|
+
// Runtime guard to catch illegal/ambiguous calls coming from JS or any-cast code
|
|
163
|
+
if (isOptionsObjectForm(arg2) &&
|
|
164
|
+
(failLevel !== 'auto' || includeMetaData !== false)) {
|
|
165
|
+
throw new TypeError('Invalid call: when providing an options object, do not also pass positional parameters.');
|
|
166
|
+
}
|
|
167
|
+
// Normalize to a fully-required options object.
|
|
168
|
+
let userOpts;
|
|
169
|
+
// Required, makes all properties in T required, no undefined.
|
|
170
|
+
// const userOpts: Required<IAllUserOptions> = isOptionsObjectForm(arg2)
|
|
171
|
+
if (isOptionsObjectForm(arg2)) {
|
|
172
|
+
// Options-object Form.
|
|
173
|
+
/* parse = (
|
|
174
|
+
yiniContent: string,
|
|
175
|
+
options: IAllUserOptions,
|
|
176
|
+
)
|
|
177
|
+
*/
|
|
178
|
+
userOpts = Object.assign(Object.assign({}, DEFAULT_OPTS), { strictMode: (_a = arg2.strictMode) !== null && _a !== void 0 ? _a : DEFAULT_OPTS.strictMode, failLevel: (_b = arg2.failLevel) !== null && _b !== void 0 ? _b : DEFAULT_OPTS.failLevel, includeMetaData: (_c = arg2.includeMetaData) !== null && _c !== void 0 ? _c : DEFAULT_OPTS.includeMetaData, includeDiagnostics: (_d = arg2.includeDiagnostics) !== null && _d !== void 0 ? _d : DEFAULT_OPTS.includeDiagnostics, includeTiming: (_e = arg2.includeTiming) !== null && _e !== void 0 ? _e : DEFAULT_OPTS.includeTiming, preserveUndefinedInMeta: (_f = arg2.preserveUndefinedInMeta) !== null && _f !== void 0 ? _f : DEFAULT_OPTS.preserveUndefinedInMeta, requireDocTerminator: (_g = arg2.requireDocTerminator) !== null && _g !== void 0 ? _g : DEFAULT_OPTS.requireDocTerminator });
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
// Positional form.
|
|
182
|
+
/* parse = (
|
|
183
|
+
yiniContent: string,
|
|
184
|
+
strictMode?: boolean,
|
|
185
|
+
failLevel?: TPreferredFailLevel,
|
|
186
|
+
includeMetaData?: boolean,
|
|
187
|
+
)
|
|
188
|
+
*/
|
|
189
|
+
userOpts = Object.assign(Object.assign({}, DEFAULT_OPTS), { strictMode: (_h = arg2) !== null && _h !== void 0 ? _h : DEFAULT_OPTS.strictMode, failLevel,
|
|
190
|
+
includeMetaData });
|
|
191
|
+
}
|
|
192
|
+
if ((0, pathAndFileName_1.getFileNameExtension)(filePath).toLowerCase() !== '.yini') {
|
|
193
|
+
console.error('Invalid file extension for YINI file:');
|
|
194
|
+
console.error(`"${filePath}"`);
|
|
195
|
+
console.log('File does not have a valid ".yini" extension (case-insensitive).');
|
|
196
|
+
throw new Error('Error: Unexpected file extension for YINI file');
|
|
197
|
+
}
|
|
198
|
+
// ---- Phase 0: I/O ----
|
|
199
|
+
const timeStartMs = perf_hooks_1.performance.now();
|
|
200
|
+
// let content = fs.readFileSync(filePath, 'utf8')
|
|
201
|
+
const rawBuffer = fs_1.default.readFileSync(filePath); // Raw buffer for size.
|
|
202
|
+
const fileByteSize = rawBuffer.byteLength; // Byte size in UTF-8.
|
|
203
|
+
let content = rawBuffer.toString('utf8');
|
|
204
|
+
const timeEndMs = perf_hooks_1.performance.now();
|
|
205
|
+
_runtimeInfo.sourceType = 'File';
|
|
206
|
+
_runtimeInfo.fileName = filePath;
|
|
207
|
+
if (userOpts.includeMetaData) {
|
|
208
|
+
_runtimeInfo.lineCount = content.split(/\r?\n/).length; // Counts the lines.
|
|
209
|
+
_runtimeInfo.fileByteSize = fileByteSize;
|
|
210
|
+
_runtimeInfo.timeIoMs = +(timeEndMs - timeStartMs);
|
|
211
|
+
_runtimeInfo.preferredBailSensitivity = userOpts.failLevel;
|
|
212
|
+
_runtimeInfo.sha256 = (0, string_1.computeSha256)(content); // NOTE: Compute BEFORE any possible tampering of content.
|
|
213
|
+
}
|
|
214
|
+
let hasNoNewlineAtEOF = false;
|
|
215
|
+
if (!content.endsWith('\n')) {
|
|
216
|
+
content += '\n';
|
|
217
|
+
hasNoNewlineAtEOF = true;
|
|
218
|
+
}
|
|
219
|
+
const result = this.parse(content, userOpts.strictMode, userOpts.failLevel, userOpts.includeMetaData);
|
|
220
|
+
if (hasNoNewlineAtEOF) {
|
|
221
|
+
console.warn(`No newline at end of file, it's recommended to end a file with a newline. File:\n"${filePath}"`);
|
|
222
|
+
}
|
|
223
|
+
return result;
|
|
75
224
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Parse a YINI file into a JavaScript object.
|
|
80
|
-
*
|
|
81
|
-
* @param yiniFile Path to the YINI file.
|
|
82
|
-
* @param strictMode If `true`, enforce strict parsing rules (e.g. require `/END`, disallow trailing commas).
|
|
83
|
-
* @param bailSensitivity Controls how errors and warnings are handled:
|
|
84
|
-
* - `'auto'` : Auto‑select level (strict→1, lenient→0)
|
|
85
|
-
* - `0` / `'Ignore-Errors'` : Continue parsing despite errors; log them and attempt recovery.
|
|
86
|
-
* - `1` / `'Abort-on-Errors'` : Stop parsing on the first error.
|
|
87
|
-
* - `2` / `'Abort-Even-on-Warnings'`: Stop parsing on the first warning **or** error.
|
|
88
|
-
* @param includeMetaData If `true`, return additional metadata (e.g. warnings, statistics) alongside the parsed object.
|
|
89
|
-
*
|
|
90
|
-
* @note The order of properties in each output object may differ from their order in the YINI source.
|
|
91
|
-
*
|
|
92
|
-
* @returns A JavaScript object representing the parsed YINI content.
|
|
93
|
-
*/
|
|
94
|
-
YINI.parseFile = (filePath, strictMode = false, bailSensitivity = 'auto', includeMetaData = false) => {
|
|
95
|
-
(0, print_1.debugPrint)('Current directory = ' + process.cwd());
|
|
96
|
-
if ((0, pathAndFileName_1.getFileNameExtension)(filePath).toLowerCase() !== '.yini') {
|
|
97
|
-
console.error('Invalid file extension for YINI file:');
|
|
98
|
-
console.error(`"${filePath}"`);
|
|
99
|
-
console.log('File does not have a valid ".yini" extension (case-insensitive).');
|
|
100
|
-
throw new Error('Error: Unexpected file extension for YINI file');
|
|
101
|
-
}
|
|
102
|
-
let content = fs_1.default.readFileSync(filePath, 'utf8');
|
|
103
|
-
let hasNoNewlineAtEOF = false;
|
|
104
|
-
if (!content.endsWith('\n')) {
|
|
105
|
-
content += '\n';
|
|
106
|
-
hasNoNewlineAtEOF = true;
|
|
107
|
-
}
|
|
108
|
-
YINI.filePath = filePath;
|
|
109
|
-
let level = 0;
|
|
110
|
-
// if (bailSensitivity === 'auto' && !strictMode) level = 0
|
|
111
|
-
// if (bailSensitivity === 'auto' && strictMode) level = 1
|
|
112
|
-
if (bailSensitivity === 'auto') {
|
|
113
|
-
if (!strictMode)
|
|
114
|
-
level = 0;
|
|
115
|
-
if (strictMode)
|
|
116
|
-
level = 1;
|
|
117
|
-
// if (process.env.NODE_ENV === 'test') level = 1
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
level = bailSensitivity;
|
|
121
|
-
}
|
|
122
|
-
const options = {
|
|
123
|
-
isStrict: strictMode,
|
|
124
|
-
bailSensitivityLevel: level,
|
|
125
|
-
isIncludeMeta: includeMetaData,
|
|
126
|
-
isWithDiagnostics: (0, env_1.isDev)() || (0, env_1.isDebug)(),
|
|
127
|
-
isWithTiming: (0, env_1.isDebug)(),
|
|
128
|
-
};
|
|
129
|
-
(0, print_1.debugPrint)();
|
|
130
|
-
(0, print_1.debugPrint)('==== Call parse ==========================');
|
|
131
|
-
const result = (0, parseEntry_1.parseMain)(content, options);
|
|
132
|
-
(0, print_1.debugPrint)('==== End call parse ==========================\n');
|
|
133
|
-
if ((0, env_1.isDev)()) {
|
|
134
|
-
console.log();
|
|
135
|
-
(0, print_1.devPrint)('YINI.parse(..): result:');
|
|
136
|
-
console.log(result);
|
|
137
|
-
(0, print_1.devPrint)('Complete result:');
|
|
138
|
-
(0, print_1.printObject)(result);
|
|
139
|
-
}
|
|
140
|
-
return result;
|
|
141
|
-
};
|
|
225
|
+
}
|
|
226
|
+
YINI.g_tabSize = DEFAULT_TAB_SIZE; // Global tab size used in error messages.
|
|
142
227
|
exports.default = YINI;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
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/YiniParser.js';
|
|
2
|
+
import YiniParserVisitor from '../grammar/YiniParserVisitor';
|
|
3
|
+
import { ErrorDataHandler } from './ErrorDataHandler';
|
|
4
|
+
import { IParseCoreOptions, IYiniAST, TSourceType } from './types';
|
|
5
|
+
/** Parse SECTION_HEAD token text → {level, name}.
|
|
6
|
+
* Supports repeated markers (^^^^) and shorthand (^7) (Spec 5.2–5.3.1). :contentReference[oaicite:5]{index=5}:contentReference[oaicite:6]{index=6}
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* This interface defines a complete generic visitor for a parse tree produced
|
|
10
|
+
* by `YiniParser`.
|
|
11
|
+
*
|
|
12
|
+
* @param <Result> The return type of the visit operation. Use `void` for
|
|
13
|
+
* operations with no return type.
|
|
14
|
+
*/
|
|
15
|
+
export default class ASTBuilder<Result> extends YiniParserVisitor<Result> {
|
|
16
|
+
private errorHandler;
|
|
17
|
+
private readonly options;
|
|
18
|
+
private readonly isStrict;
|
|
19
|
+
private readonly onDuplicateKey;
|
|
20
|
+
private ast;
|
|
21
|
+
private sectionStack;
|
|
22
|
+
private meta_hasYiniMarker;
|
|
23
|
+
private _numOfMembers;
|
|
24
|
+
private meta_maxLevel;
|
|
25
|
+
mapSectionNamePaths: Map<string, number>;
|
|
26
|
+
/**
|
|
27
|
+
* @param metaFileName If parsing from a file, provide the file name here so the meta information can be updated accordingly.
|
|
28
|
+
* @param metaLineCount Provide the line-count here so the meta information can be updated accordingly.
|
|
29
|
+
*/
|
|
30
|
+
constructor(errorHandler: ErrorDataHandler, options: IParseCoreOptions, sourceType: TSourceType, metaFileName: string | null);
|
|
31
|
+
private hasDefinedSectionTitle;
|
|
32
|
+
private setDefineSectionTitle;
|
|
33
|
+
/** Attach a section to the stack respecting up/down moves (Spec 5.3). :contentReference[oaicite:7]{index=7} */
|
|
34
|
+
private attachSection;
|
|
35
|
+
/** Insert a key/value into current section (duplicate handling per options). */
|
|
36
|
+
private putMember;
|
|
37
|
+
buildAST(ctx: YiniContext): IYiniAST;
|
|
38
|
+
/**
|
|
39
|
+
* Visit a parse tree produced by `YiniParser.yini`.
|
|
40
|
+
* @param ctx the parse tree
|
|
41
|
+
* @return the visitor result
|
|
42
|
+
*/
|
|
43
|
+
visitYini: (ctx: YiniContext) => any;
|
|
44
|
+
/**
|
|
45
|
+
* Visit a parse tree produced by `YiniParser.prolog`.
|
|
46
|
+
* @param ctx the parse tree
|
|
47
|
+
* @return the visitor result
|
|
48
|
+
*/
|
|
49
|
+
visitProlog: (ctx: PrologContext) => any;
|
|
50
|
+
/**
|
|
51
|
+
* Visit a parse tree produced by `YiniParser.terminal`.
|
|
52
|
+
* @param ctx the parse tree
|
|
53
|
+
* @return the visitor result
|
|
54
|
+
*/
|
|
55
|
+
visitTerminal_stmt: (ctx: Terminal_stmtContext) => any;
|
|
56
|
+
/**
|
|
57
|
+
* Visit a parse tree produced by `YiniParser.stmt`.
|
|
58
|
+
* @param ctx the parse tree
|
|
59
|
+
* @grammarRule eol | SECTION_HEAD | assignment | colon_list_decl | marker_stmt | bad_member
|
|
60
|
+
* @return the visitor result
|
|
61
|
+
*/
|
|
62
|
+
visitStmt: (ctx: StmtContext) => any;
|
|
63
|
+
/**
|
|
64
|
+
* Visit a parse tree produced by `YiniParser.meta_stmt`.
|
|
65
|
+
* @param ctx the parse tree
|
|
66
|
+
*/
|
|
67
|
+
visitMeta_stmt: (ctx: Meta_stmtContext) => any;
|
|
68
|
+
/**
|
|
69
|
+
* Visit a parse tree produced by `YiniParser.directive`.
|
|
70
|
+
* @param ctx the parse tree
|
|
71
|
+
* @note Directive statements in YINI are special top-level commands that
|
|
72
|
+
* appear only at the beginning of a document, before any sections
|
|
73
|
+
* or members. Each directive may occur at most once per file.
|
|
74
|
+
*/
|
|
75
|
+
visitDirective: (ctx: DirectiveContext) => any;
|
|
76
|
+
/**
|
|
77
|
+
* Visit a parse tree produced by `YiniParser.annotation`.
|
|
78
|
+
* @param ctx the parse tree
|
|
79
|
+
* @return the visitor result
|
|
80
|
+
*/
|
|
81
|
+
visitAnnotation: (ctx: AnnotationContext) => any;
|
|
82
|
+
/**
|
|
83
|
+
* Visit a parse tree produced by `YiniParser.eol`.
|
|
84
|
+
* @param ctx the parse tree
|
|
85
|
+
* @return the visitor result
|
|
86
|
+
*/
|
|
87
|
+
visitEol: (ctx: EolContext) => any;
|
|
88
|
+
/**
|
|
89
|
+
* Visit a parse tree produced by `YiniParser.assignment`.
|
|
90
|
+
* @param ctx the parse tree
|
|
91
|
+
* @return the visitor result
|
|
92
|
+
*/
|
|
93
|
+
visitAssignment: (ctx: AssignmentContext) => any;
|
|
94
|
+
/**
|
|
95
|
+
* Visit a parse tree produced by `YiniParser.member`.
|
|
96
|
+
* @param ctx the parse tree
|
|
97
|
+
* @grammarRule KEY WS? EQ WS? value?
|
|
98
|
+
* @return the visitor result
|
|
99
|
+
*/
|
|
100
|
+
visitMember: (ctx: MemberContext) => any;
|
|
101
|
+
/**
|
|
102
|
+
* Visit a parse tree produced by `YiniParser.value`.
|
|
103
|
+
* @param ctx the parse tree
|
|
104
|
+
* @return the visitor result
|
|
105
|
+
*/
|
|
106
|
+
visitValue: (ctx: ValueContext) => any;
|
|
107
|
+
/**
|
|
108
|
+
* Visit a parse tree produced by `YiniParser.string_literal`.
|
|
109
|
+
* @param ctx the parse tree
|
|
110
|
+
* @return the visitor result
|
|
111
|
+
*/
|
|
112
|
+
visitString_literal: (ctx: String_literalContext) => any;
|
|
113
|
+
/**
|
|
114
|
+
* Visit a parse tree produced by `YiniParser.number_literal`.
|
|
115
|
+
* @param ctx the parse tree
|
|
116
|
+
* @return the visitor result
|
|
117
|
+
*/
|
|
118
|
+
visitNumber_literal: (ctx: Number_literalContext) => any;
|
|
119
|
+
/**
|
|
120
|
+
* Visit a parse tree produced by `YiniParser.boolean_literal`.
|
|
121
|
+
* @param ctx the parse tree
|
|
122
|
+
* @return the visitor result
|
|
123
|
+
*/
|
|
124
|
+
visitBoolean_literal: (ctx: Boolean_literalContext) => any;
|
|
125
|
+
/**
|
|
126
|
+
* Visit a parse tree produced by `YiniParser.null_literal`.
|
|
127
|
+
* @param ctx the parse tree
|
|
128
|
+
* @return the visitor result
|
|
129
|
+
*/
|
|
130
|
+
visitNull_literal: (ctx: Null_literalContext) => any;
|
|
131
|
+
/**
|
|
132
|
+
* Visit a parse tree produced by `YiniParser.list_literal`.
|
|
133
|
+
* @param ctx the parse tree
|
|
134
|
+
* @grammarRule OB NL* elements? NL* CB NL* | EMPTY_LIST NL*
|
|
135
|
+
* @return the visitor result
|
|
136
|
+
*/
|
|
137
|
+
visitList_literal: (ctx: List_literalContext) => any;
|
|
138
|
+
/**
|
|
139
|
+
* Visit a parse tree produced by `YiniParser.elements`.
|
|
140
|
+
* @param ctx the parse tree
|
|
141
|
+
* @grammarRule value (NL* COMMA NL* value)* COMMA?
|
|
142
|
+
* @return the visitor result
|
|
143
|
+
*/
|
|
144
|
+
visitElements: (ctx: ElementsContext) => any;
|
|
145
|
+
/**
|
|
146
|
+
* Visit a parse tree produced by `YiniParser.object_literal`.
|
|
147
|
+
* @param ctx the parse tree
|
|
148
|
+
* @grammarRule OC NL* object_members? NL* CC NL* | EMPTY_OBJECT NL*
|
|
149
|
+
* @return the visitor result
|
|
150
|
+
*/
|
|
151
|
+
visitObject_literal: (ctx: Object_literalContext) => any;
|
|
152
|
+
/**
|
|
153
|
+
* Visit a parse tree produced by `YiniParser.object_members`.
|
|
154
|
+
* @param ctx the parse tree
|
|
155
|
+
* @grammarRule object_member (COMMA NL* object_member)* COMMA?
|
|
156
|
+
* @return the visitor result
|
|
157
|
+
*/
|
|
158
|
+
visitObject_members: (ctx: Object_membersContext) => any;
|
|
159
|
+
/**
|
|
160
|
+
* Visit a parse tree produced by `YiniParser.object_member`.
|
|
161
|
+
* @param ctx the parse tree
|
|
162
|
+
* @grammarRule KEY WS? COLON NL* value
|
|
163
|
+
* @return the visitor result
|
|
164
|
+
*/
|
|
165
|
+
visitObject_member: (ctx: Object_memberContext) => any;
|
|
166
|
+
/**
|
|
167
|
+
* Visit a parse tree produced by `YiniParser.colon_list_decl`.
|
|
168
|
+
* @param ctx the parse tree
|
|
169
|
+
* @grammarRule KEY WS? COLON (eol | WS+)* elements (eol | WS+)* eol
|
|
170
|
+
* @return the visitor result
|
|
171
|
+
*/
|
|
172
|
+
visitColon_list_decl: (ctx: Colon_list_declContext) => any;
|
|
173
|
+
/**
|
|
174
|
+
* Visit a parse tree produced by `YiniParser.string_concat`.
|
|
175
|
+
* @param ctx the parse tree
|
|
176
|
+
* @return the visitor result
|
|
177
|
+
*/
|
|
178
|
+
visitString_concat: (ctx: String_concatContext) => any;
|
|
179
|
+
/**
|
|
180
|
+
* Visit a parse tree produced by `YiniParser.bad_member`.
|
|
181
|
+
* @param ctx the parse tree
|
|
182
|
+
* @return the visitor result
|
|
183
|
+
*/
|
|
184
|
+
visitBad_member: (ctx: Bad_memberContext) => any;
|
|
185
|
+
/**
|
|
186
|
+
* Visit a parse tree produced by `YiniParser.bad_meta_text`.
|
|
187
|
+
* @param ctx the parse tree
|
|
188
|
+
* @return the visitor result
|
|
189
|
+
*/
|
|
190
|
+
visitBad_meta_text: (ctx: Bad_meta_textContext) => any;
|
|
191
|
+
}
|