Skip to content

Commit 5babb64

Browse files
committed
organize parsers
1 parent c5b2c32 commit 5babb64

File tree

8 files changed

+265
-263
lines changed

8 files changed

+265
-263
lines changed

src/parsers/BindTargetParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ErrorLevel } from '../utils/errors/MetaBindErrors';
22
import { IPlugin } from '../IPlugin';
3-
import { BIND_TARGET } from './nomParsers/Parsers';
3+
import { BIND_TARGET } from './nomParsers/BindTargetParsers';
44
import { ParsingValidationError } from './ParsingError';
55
import { BindTargetDeclaration, UnvalidatedBindTargetDeclaration } from './inputFieldParser/InputFieldDeclaration';
66
import { BindTargetScope } from '../metadata/BindTargetScope';

src/parsers/ViewFieldDeclarationParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ErrorCollection } from '../utils/errors/ErrorCollection';
2-
import { JS_VIEW_FIELD_DECLARATION, VIEW_FIELD_FULL_DECLARATION } from './nomParsers/Parsers';
2+
import { JS_VIEW_FIELD_DECLARATION, VIEW_FIELD_FULL_DECLARATION } from './nomParsers/ViewFieldParsers';
33
import { IPlugin } from '../IPlugin';
44
import { BindTargetDeclaration, UnvalidatedBindTargetDeclaration } from './inputFieldParser/InputFieldDeclaration';
55
import { BindTargetScope } from '../metadata/BindTargetScope';

src/parsers/inputFieldParser/InputFieldParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { InputFieldTemplate } from '../../settings/Settings';
55
import { deepFreeze } from '../../utils/Utils';
66
import { InputFieldDeclarationValidator } from './InputFieldDeclarationValidator';
77
import { ITemplateSupplier, TemplateSupplierTemplate } from './ITemplateSupplier';
8-
import { INPUT_FIELD_FULL_DECLARATION, TEMPLATE_INPUT_FIELD_FULL_DECLARATION } from '../nomParsers/Parsers';
8+
import { INPUT_FIELD_FULL_DECLARATION, TEMPLATE_INPUT_FIELD_FULL_DECLARATION } from '../nomParsers/InputFieldParsers';
99
import { ParsingValidationError } from '../ParsingError';
1010
import { ErrorLevel } from '../../utils/errors/MetaBindErrors';
1111
import { InputFieldDeclaration, UnvalidatedInputFieldDeclaration } from './InputFieldDeclaration';
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { P } from '@lemons_dev/parsinom/lib/ParsiNOM';
2+
import { Parser } from '@lemons_dev/parsinom/lib/Parser';
3+
import { createResultNode, ParsingResultNode } from '../inputFieldParser/InputFieldParser';
4+
import { P_UTILS } from '@lemons_dev/parsinom/lib/ParserUtils';
5+
import { UnvalidatedBindTargetDeclaration } from '../inputFieldParser/InputFieldDeclaration';
6+
import { doubleQuotedString, ident } from './GeneralParsers';
7+
8+
const filePath: Parser<string> = P.noneOf('{}[]#^|:?')
9+
.many()
10+
.map(x => x.join(''))
11+
.box('file path');
12+
13+
const metadataPathPartIdent: Parser<string> = ident;
14+
15+
const bracketMetadataPathPart: Parser<ParsingResultNode> = P.or(P_UTILS.digits(), doubleQuotedString).wrap(P.string('['), P.string(']')).node(createResultNode);
16+
17+
const firstMetadataPathPart: Parser<UnvalidatedBindTargetDeclaration> = P.or(
18+
P.sequenceMap(
19+
(prefix, firstPart) => {
20+
return {
21+
file: undefined,
22+
boundToLocalScope: prefix !== undefined,
23+
path: firstPart,
24+
};
25+
},
26+
P.string('^').optional(),
27+
bracketMetadataPathPart.atLeast(1)
28+
),
29+
P.string('^.').result({
30+
file: undefined,
31+
boundToLocalScope: true,
32+
path: [],
33+
}),
34+
P.succeed({
35+
file: undefined,
36+
boundToLocalScope: false,
37+
path: [],
38+
})
39+
);
40+
41+
const metadataPathPart: Parser<ParsingResultNode[]> = P.sequenceMap(
42+
(ident, brackets) => {
43+
return [ident, ...brackets];
44+
},
45+
metadataPathPartIdent.node(createResultNode),
46+
bracketMetadataPathPart.many()
47+
);
48+
49+
const metadataPath: Parser<UnvalidatedBindTargetDeclaration> = P.sequenceMap(
50+
(declaration, others) => {
51+
declaration.path = declaration.path.concat(others.map(x => x[1]).reduce((x, acc) => acc.concat(x), []));
52+
return declaration;
53+
},
54+
firstMetadataPathPart,
55+
P.sequence(P.string('.'), metadataPathPart).many()
56+
);
57+
58+
export const BIND_TARGET: Parser<UnvalidatedBindTargetDeclaration> = P.sequenceMap(
59+
(a, b) => {
60+
b.file = a?.[0];
61+
return b;
62+
},
63+
P.sequence(filePath.node(createResultNode), P.string('#')).optional(),
64+
metadataPath
65+
);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { P } from '@lemons_dev/parsinom/lib/ParsiNOM';
2+
import { P_UTILS } from '@lemons_dev/parsinom/lib/ParserUtils';
3+
import { Parser } from '@lemons_dev/parsinom/lib/Parser';
4+
5+
export const ident = P.regexp(/^[a-z][a-z0-9_-]*/i)
6+
.map(x => {
7+
// console.log('ident', x);
8+
return x;
9+
})
10+
.describe('identifier');
11+
12+
export const identWithSpaces = P.sequenceMap(
13+
(a, b) => {
14+
return a + b.map(x => x[0] + x[1]).join('');
15+
},
16+
ident,
17+
P.sequence(P_UTILS.optionalWhitespace(), ident).many()
18+
).describe('identifier with spaces');
19+
20+
export const escapeCharacter = P.string('\\')
21+
.then(P_UTILS.any())
22+
.map(escaped => {
23+
if (escaped === "'") {
24+
return "'";
25+
} else if (escaped === '\\') {
26+
return '\\';
27+
} else {
28+
return '\\' + escaped;
29+
}
30+
});
31+
32+
function stringFactory(quotes: string): Parser<string> {
33+
return P.string(quotes)
34+
.then(
35+
P.or(escapeCharacter, P.noneOf(quotes + '\\'))
36+
.many()
37+
.map(x => x.join(''))
38+
)
39+
.skip(P.string(quotes));
40+
}
41+
42+
export const singleQuotedString = stringFactory("'");
43+
export const doubleQuotedString = stringFactory('"');
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { P } from '@lemons_dev/parsinom/lib/ParsiNOM';
2+
import { ident, identWithSpaces, singleQuotedString } from './GeneralParsers';
3+
import { Parser } from '@lemons_dev/parsinom/lib/Parser';
4+
import { createResultNode, ParsingResultNode } from '../inputFieldParser/InputFieldParser';
5+
import { P_UTILS } from '@lemons_dev/parsinom/lib/ParserUtils';
6+
import { PartialUnvalidatedInputFieldDeclaration, UnvalidatedInputFieldArgument } from '../inputFieldParser/InputFieldDeclaration';
7+
import { BIND_TARGET } from './BindTargetParsers';
8+
9+
const nonStringArgumentValue: Parser<string> = P.regexp(/^[^()',]+/).describe('any character except parentheses, single quotation marks and commas');
10+
11+
const argumentValue: Parser<ParsingResultNode> = P.or(singleQuotedString, nonStringArgumentValue).node(createResultNode);
12+
13+
const argumentValues: Parser<ParsingResultNode[]> = P.separateBy(argumentValue, P.string(',').trim(P_UTILS.optionalWhitespace()));
14+
15+
const inputFieldArgument: Parser<UnvalidatedInputFieldArgument> = P.sequenceMap(
16+
(name, value): UnvalidatedInputFieldArgument => {
17+
return {
18+
name: name,
19+
value: value,
20+
};
21+
},
22+
ident.node(createResultNode),
23+
argumentValues
24+
.trim(P_UTILS.optionalWhitespace())
25+
.wrap(P.string('('), P.string(')'))
26+
.optional([] as ParsingResultNode[])
27+
);
28+
29+
const inputFieldArguments: Parser<UnvalidatedInputFieldArgument[]> = P.separateBy(inputFieldArgument, P.string(',').trim(P_UTILS.optionalWhitespace()));
30+
31+
export const INPUT_FIELD_DECLARATION: Parser<PartialUnvalidatedInputFieldDeclaration> = P.sequenceMap(
32+
(type, args, b) => {
33+
const bindTarget = b === undefined ? undefined : b[1];
34+
return {
35+
inputFieldType: type,
36+
arguments: args,
37+
bindTarget: bindTarget,
38+
};
39+
},
40+
ident.node(createResultNode).describe('input field type'),
41+
inputFieldArguments
42+
.trim(P_UTILS.optionalWhitespace())
43+
.wrap(P.string('('), P.string(')'))
44+
.optional([] as UnvalidatedInputFieldArgument[]),
45+
P.sequence(P.string(':'), BIND_TARGET).optional()
46+
);
47+
48+
export const PARTIAL_INPUT_FIELD_DECLARATION: Parser<PartialUnvalidatedInputFieldDeclaration> = P.sequenceMap(
49+
(type, args, b) => {
50+
const bindTarget = b === undefined ? undefined : b[1];
51+
return {
52+
inputFieldType: type,
53+
arguments: args,
54+
bindTarget: bindTarget,
55+
};
56+
},
57+
ident.node(createResultNode).optional().describe('input field type'),
58+
inputFieldArguments
59+
.trim(P_UTILS.optionalWhitespace())
60+
.wrap(P.string('('), P.string(')'))
61+
.optional([] as UnvalidatedInputFieldArgument[]),
62+
P.sequence(P.string(':'), BIND_TARGET).optional()
63+
);
64+
65+
export const INPUT_FIELD_FULL_DECLARATION: Parser<PartialUnvalidatedInputFieldDeclaration> = P.or(
66+
P.sequenceMap(
67+
(_1, templateName, _2, declaration, _3) => {
68+
declaration.templateName = templateName;
69+
return declaration;
70+
},
71+
P.string('INPUT'),
72+
P.sequenceMap((_1, templateName, _2) => templateName, P.string('['), identWithSpaces.node(createResultNode).describe('template name'), P.string(']')),
73+
P.string('['),
74+
PARTIAL_INPUT_FIELD_DECLARATION,
75+
P.string(']'),
76+
P_UTILS.eof()
77+
),
78+
P.sequenceMap(
79+
(_1, _2, declaration, _3) => {
80+
return declaration;
81+
},
82+
P.string('INPUT'),
83+
P.string('['),
84+
INPUT_FIELD_DECLARATION,
85+
P.string(']'),
86+
P_UTILS.eof()
87+
)
88+
);
89+
90+
export const TEMPLATE_INPUT_FIELD_FULL_DECLARATION: Parser<PartialUnvalidatedInputFieldDeclaration> = P.sequenceMap(
91+
(_1, _2, declaration, _3) => {
92+
return declaration;
93+
},
94+
P.string('INPUT'),
95+
P.string('['),
96+
PARTIAL_INPUT_FIELD_DECLARATION,
97+
P.string(']'),
98+
P_UTILS.eof()
99+
);

0 commit comments

Comments
 (0)