|
| 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