Skip to content

Commit dc7c668

Browse files
committed
bump parsinom and better bind target error messages
1 parent 1e323b8 commit dc7c668

File tree

10 files changed

+88
-69
lines changed

10 files changed

+88
-69
lines changed

exampleVault/examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
slider1: 6
2+
slider1: 4
33
suggest: test
44
toggle1: false
55
Domestic_tasks:

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
},
4646
"dependencies": {
4747
"@codemirror/language": "https://github.com/lishid/cm-language",
48-
"@lemons_dev/parsinom": "^0.0.4",
48+
"@lemons_dev/parsinom": "^0.0.6",
4949
"@opd-libs/opd-metadata-lib": "0.0.4",
5050
"@opd-libs/opd-utils-lib": "0.0.2",
5151
"@popperjs/core": "^2.11.8",

src/parsers/BindTargetParser.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { parsePath } from '@opd-libs/opd-utils-lib/lib/ObjectTraversalUtils';
33
import { IPlugin } from '../IPlugin';
44
import { UnvalidatedBindTargetDeclaration } from './newInputFieldParser/InputFieldDeclarationValidator';
55
import { BIND_TARGET } from './nomParsers/Parsers';
6+
import { ParsingValidationError } from './ParsingError';
67

78
export interface BindTargetDeclaration {
89
filePath: string | undefined;
@@ -17,29 +18,54 @@ export class BindTargetParser {
1718
}
1819

1920
parseAndValidateBindTarget(bindTargetString: string): BindTargetDeclaration {
20-
return this.validateBindTarget(this.parseBindTarget(bindTargetString));
21+
return this.validateBindTarget(bindTargetString, this.parseBindTarget(bindTargetString));
2122
}
2223

2324
parseBindTarget(bindTargetString: string): UnvalidatedBindTargetDeclaration {
2425
return BIND_TARGET.parse(bindTargetString) as UnvalidatedBindTargetDeclaration;
2526
}
2627

27-
validateBindTarget(unvalidatedBindTargetDeclaration: UnvalidatedBindTargetDeclaration): BindTargetDeclaration {
28+
validateBindTarget(fullDeclaration: string, unvalidatedBindTargetDeclaration: UnvalidatedBindTargetDeclaration): BindTargetDeclaration {
2829
const bindTargetDeclaration: BindTargetDeclaration = {} as BindTargetDeclaration;
2930

3031
const filePath = unvalidatedBindTargetDeclaration.file?.value;
31-
if (filePath) {
32+
if (filePath !== undefined) {
3233
const filePaths: string[] = this.plugin.getFilePathsByName(filePath);
34+
3335
if (filePaths.length === 0) {
34-
throw new MetaBindBindTargetError(ErrorLevel.CRITICAL, 'failed to parse bind target', 'bind target file not found');
36+
if (unvalidatedBindTargetDeclaration.file?.position) {
37+
throw new ParsingValidationError(
38+
ErrorLevel.CRITICAL,
39+
'Bind Target Validator',
40+
`Failed to parse bind target. Bind target file path '${unvalidatedBindTargetDeclaration.file.value}' not found.`,
41+
fullDeclaration,
42+
unvalidatedBindTargetDeclaration.file.position
43+
);
44+
} else {
45+
throw new ParsingValidationError(
46+
ErrorLevel.CRITICAL,
47+
'Bind Target Validator',
48+
`Failed to parse bind target. Bind target file path '${unvalidatedBindTargetDeclaration.file?.value}' not found.`
49+
);
50+
}
3551
} else if (filePaths.length === 1) {
3652
bindTargetDeclaration.filePath = filePaths[0];
3753
} else {
38-
throw new MetaBindBindTargetError(
39-
ErrorLevel.CRITICAL,
40-
'failed to parse bind target',
41-
'bind target resolves to multiple files, please also specify the file path'
42-
);
54+
if (unvalidatedBindTargetDeclaration.file?.position) {
55+
throw new ParsingValidationError(
56+
ErrorLevel.CRITICAL,
57+
'Bind Target Validator',
58+
`Failed to parse bind target. Bind target file path '${unvalidatedBindTargetDeclaration.file.value}' resolves to multiple files, please also specify the file path.`,
59+
fullDeclaration,
60+
unvalidatedBindTargetDeclaration.file.position
61+
);
62+
} else {
63+
throw new ParsingValidationError(
64+
ErrorLevel.CRITICAL,
65+
'Bind Target Validator',
66+
`Failed to parse bind target. Bind target file path '${unvalidatedBindTargetDeclaration.file?.value}' resolves to multiple files, please also specify the file path.`
67+
);
68+
}
4369
}
4470
}
4571

src/parsers/ParsingError.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ErrorLevel, ErrorType, MetaBindError } from '../utils/errors/MetaBindErrors';
2-
import { ParseFailure } from '@lemons_dev/parsinom/lib/HelperTypes';
3-
import { ParsingRange } from './newInputFieldParser/InputFieldDeclarationValidator';
2+
import { ParseFailure, ParsingRange } from '@lemons_dev/parsinom/lib/HelperTypes';
43

54
export class ParsingError extends MetaBindError {
65
str: string;
@@ -61,7 +60,7 @@ export class ParsingValidationError extends MetaBindError {
6160

6261
if (this.str && this.position) {
6362
this.message += this.str + '\n';
64-
this.message += ' '.repeat(this.position.start.index) + '^'.repeat(this.position.end.index - this.position.start.index) + '\n';
63+
this.message += ' '.repeat(this.position.from.index) + '^'.repeat(this.position.to.index - this.position.from.index) + '\n';
6564
}
6665
}
6766
}

src/parsers/ViewFieldDeclarationParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class ViewFieldDeclarationParser {
5959
if (typeof x === 'string') {
6060
return x;
6161
} else {
62-
return this.plugin.api.bindTargetParser.validateBindTarget(x);
62+
return this.plugin.api.bindTargetParser.validateBindTarget(fullDeclaration, x);
6363
}
6464
});
6565
} catch (e) {

src/parsers/newInputFieldParser/InputFieldDeclarationValidator.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,19 @@ import { ErrorLevel } from '../../utils/errors/MetaBindErrors';
55
import { InputFieldArgumentContainer } from '../../inputFieldArguments/InputFieldArgumentContainer';
66
import { AbstractInputFieldArgument } from '../../inputFieldArguments/AbstractInputFieldArgument';
77
import { InputFieldArgumentFactory } from '../../inputFieldArguments/InputFieldArgumentFactory';
8-
import { ParsingMarker, ParsingPosition } from '@lemons_dev/parsinom/lib/HelperTypes';
8+
import { ParsingMarker, ParsingPosition, ParsingRange } from '@lemons_dev/parsinom/lib/HelperTypes';
99
import { IPlugin } from '../../IPlugin';
1010
import { BindTargetDeclaration } from '../BindTargetParser';
1111

12-
export interface ParsingRange {
13-
start: ParsingPosition;
14-
end: ParsingPosition;
15-
}
16-
1712
export interface ParsingResultNode {
1813
value: string;
1914
position?: ParsingRange;
2015
}
2116

22-
export function markerToResultNode(marker: ParsingMarker<string>): ParsingResultNode {
17+
export function markerToResultNode(value: string, range: ParsingRange): ParsingResultNode {
2318
return {
24-
value: marker.value,
25-
position: {
26-
start: marker.start,
27-
end: marker.end,
28-
},
19+
value: value,
20+
position: range,
2921
};
3022
}
3123

@@ -91,7 +83,7 @@ export class InputFieldDeclarationValidator {
9183
new ParsingValidationError(
9284
ErrorLevel.ERROR,
9385
'Declaration Validator',
94-
`Encountered invalid identifier. Expected token to be an input field type but received '${inputFieldType}'.`,
86+
`Encountered invalid identifier. Expected token to be an input field type but received '${inputFieldType.value}'.`,
9587
this.unvalidatedDeclaration.fullDeclaration,
9688
inputFieldType.position
9789
)
@@ -101,7 +93,7 @@ export class InputFieldDeclarationValidator {
10193
new ParsingValidationError(
10294
ErrorLevel.ERROR,
10395
'Declaration Validator',
104-
`Encountered invalid identifier. Expected token to be an input field type but received '${inputFieldType}'.`
96+
`Encountered invalid identifier. Expected token to be an input field type but received '${inputFieldType?.value}'.`
10597
)
10698
);
10799
}
@@ -110,7 +102,11 @@ export class InputFieldDeclarationValidator {
110102
}
111103

112104
private validateBindTarget(): BindTargetDeclaration | undefined {
113-
return this.unvalidatedDeclaration.bindTarget ? this.plugin.api.bindTargetParser.validateBindTarget(this.unvalidatedDeclaration.bindTarget) : undefined;
105+
if (this.unvalidatedDeclaration.bindTarget !== undefined) {
106+
return this.plugin.api.bindTargetParser.validateBindTarget(this.unvalidatedDeclaration.fullDeclaration, this.unvalidatedDeclaration.bindTarget);
107+
} else {
108+
return undefined;
109+
}
114110
}
115111

116112
private validateArguments(inputFieldType: InputFieldType): InputFieldArgumentContainer {

src/parsers/nomParsers/Parsers.ts

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,19 @@ const filePath = P.noneOf('[]#^|:?')
5454
.map(x => x.join(''))
5555
.describe('file path');
5656

57-
const bracketMetadataPath: Parser<ParsingResultNode[]> = P.or(P_UTILS.digits(), createStr('"'))
58-
.wrap(P.string('['), P.string(']'))
59-
.mark()
60-
.map(x => markerToResultNode(x))
61-
.many();
57+
const bracketMetadataPathPart: Parser<ParsingResultNode> = P.or(P_UTILS.digits(), createStr('"')).wrap(P.string('['), P.string(']')).node(markerToResultNode);
6258

63-
const firstMetadataPathPart: Parser<ParsingResultNode[]> = P.sequenceMap(
64-
(ident, brackets) => {
65-
if (ident === undefined) {
66-
return brackets;
67-
} else {
68-
return [markerToResultNode(ident), ...brackets];
69-
}
70-
},
71-
ident.mark().optional(),
72-
bracketMetadataPath
59+
const firstMetadataPathPart: Parser<ParsingResultNode[]> = P.or(
60+
bracketMetadataPathPart.atLeast(1),
61+
P.sequenceMap((ident, brackets) => [ident, ...brackets], ident.node(markerToResultNode), bracketMetadataPathPart.many())
7362
);
7463

7564
const metadataPathPart: Parser<ParsingResultNode[]> = P.sequenceMap(
7665
(ident, brackets) => {
77-
return [markerToResultNode(ident), ...brackets];
66+
return [ident, ...brackets];
7867
},
79-
ident.mark(),
80-
bracketMetadataPath
68+
ident.node(markerToResultNode),
69+
bracketMetadataPathPart.many()
8170
);
8271

8372
const metadataPath: Parser<ParsingResultNode[]> = P.sequenceMap(
@@ -97,28 +86,25 @@ export const BIND_TARGET: Parser<UnvalidatedBindTargetDeclaration> = P.sequenceM
9786
};
9887
} else {
9988
return {
100-
file: markerToResultNode(a[0]),
89+
file: a[0],
10190
path: b,
10291
};
10392
}
10493
},
105-
P.sequence(filePath.mark(), P.string('#')).optional(),
94+
P.sequence(filePath.node(markerToResultNode), P.string('#')).optional(),
10695
metadataPath
10796
);
10897

109-
const inputFieldArgumentValue: Parser<ParsingResultNode[]> = P.separateBy(
110-
value.mark().map(x => markerToResultNode(x)),
111-
P.string(',').trim(P_UTILS.optionalWhitespace())
112-
);
98+
const inputFieldArgumentValue: Parser<ParsingResultNode[]> = P.separateBy(value.node(markerToResultNode), P.string(',').trim(P_UTILS.optionalWhitespace()));
11399

114100
const inputFieldArgument: Parser<UnvalidatedInputFieldArgument> = P.sequenceMap(
115101
(name, value): UnvalidatedInputFieldArgument => {
116102
return {
117-
name: markerToResultNode(name),
103+
name: name,
118104
value: value,
119105
};
120106
},
121-
ident.mark(),
107+
ident.node(markerToResultNode),
122108
inputFieldArgumentValue
123109
.trim(P_UTILS.optionalWhitespace())
124110
.wrap(P.string('('), P.string(')'))
@@ -130,13 +116,14 @@ const inputFieldArguments: Parser<UnvalidatedInputFieldArgument[]> = P.separateB
130116
export const INPUT_FIELD_DECLARATION: Parser<PartialUnvalidatedInputFieldDeclaration> = P.sequenceMap(
131117
(type, args, b) => {
132118
const bindTarget = b === undefined ? undefined : b[1];
119+
console.warn(type, args, b);
133120
return {
134-
inputFieldType: markerToResultNode(type),
121+
inputFieldType: type,
135122
arguments: args,
136123
bindTarget: bindTarget,
137124
};
138125
},
139-
ident.mark().describe('input field type'),
126+
ident.node(markerToResultNode).describe('input field type'),
140127
inputFieldArguments
141128
.trim(P_UTILS.optionalWhitespace())
142129
.wrap(P.string('('), P.string(')'))
@@ -147,35 +134,39 @@ export const INPUT_FIELD_DECLARATION: Parser<PartialUnvalidatedInputFieldDeclara
147134
export const INPUT_FIELD_FULL_DECLARATION: Parser<PartialUnvalidatedInputFieldDeclaration> = P.or(
148135
P.sequenceMap(
149136
(_1, templateName, _2, declaration, _3) => {
137+
console.warn('first');
150138
return declaration;
151139
},
152140
P.string('INPUT'),
153-
P.sequenceMap((_1, templateName, _2) => templateName, P.string('['), spaceIdent, P.string(']')),
141+
P.sequenceMap((_1, templateName, _2) => templateName, P.string('['), spaceIdent.describe('template name'), P.string(']')),
154142
P.string('['),
155143
INPUT_FIELD_DECLARATION,
156-
P.string(']')
144+
P.string(']'),
145+
P_UTILS.eof()
157146
),
158147
P.sequenceMap(
159148
(_1, _2, declaration, _3) => {
149+
console.warn('second');
160150
return declaration;
161151
},
162152
P.string('INPUT'),
163153
P.string('['),
164154
INPUT_FIELD_DECLARATION,
165-
P.string(']')
155+
P.string(']'),
156+
P_UTILS.eof()
166157
)
167158
);
168159

169160
export const PARTIAL_INPUT_FIELD_DECLARATION: Parser<PartialUnvalidatedInputFieldDeclaration> = P.sequenceMap(
170161
(type, args, b) => {
171162
const bindTarget = b === undefined ? undefined : b[1];
172163
return {
173-
inputFieldType: markerToResultNode(type),
164+
inputFieldType: type,
174165
arguments: args,
175166
bindTarget: bindTarget,
176167
};
177168
},
178-
ident.mark().describe('input field type'),
169+
ident.node(markerToResultNode).describe('input field type'),
179170
inputFieldArguments
180171
.trim(P_UTILS.optionalWhitespace())
181172
.wrap(P.string('('), P.string(')'))
@@ -190,7 +181,8 @@ export const TEMPLATE_INPUT_FIELD_FULL_DECLARATION: Parser<PartialUnvalidatedInp
190181
P.string('INPUT'),
191182
P.string('['),
192183
PARTIAL_INPUT_FIELD_DECLARATION,
193-
P.string(']')
184+
P.string(']'),
185+
P_UTILS.eof()
194186
);
195187

196188
const viewFieldMathJS = P.noneOf('{}[]')
@@ -213,5 +205,6 @@ export const VIEW_FIELD_FULL_DECLARATION: Parser<(string | UnvalidatedBindTarget
213205
P.string('VIEW'),
214206
P.string('['),
215207
VIEW_FIELD_DECLARATION,
216-
P.string(']')
208+
P.string(']'),
209+
P_UTILS.eof()
217210
);

src/utils/errors/ErrorCollectionViewModal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class ErrorCollectionViewModal extends Modal {
1414
}
1515

1616
public onOpen(): void {
17+
this.modalEl.addClass('meta-bind-error-collection-modal');
1718
this.titleEl.innerText = 'Meta Bind Field';
1819

1920
this.component = new ErrorCollectionComponent({

styles.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,7 @@ code.meta-bind-plugin-error {
215215
.meta-bind-pre > code {
216216
white-space: pre;
217217
}
218+
219+
.meta-bind-error-collection-modal {
220+
width: 80%;
221+
}

0 commit comments

Comments
 (0)