UNPKG

1.64 kBJavaScriptView Raw
1#!/usr/bin/env node
2const peg = require('pegjs');
3const tspegjs = require('ts-pegjs');
4const fs = require('fs');
5const { outputFileSync } = require('fs-extra');
6const grammar = fs.readFileSync('./src/parser.pegjs', 'utf-8');
7
8// TS
9const srcString = peg.generate(grammar, {
10 plugins: [tspegjs],
11 output: 'source',
12 tspegjs: {
13 customHeader: `
14import {
15 MessageFormatElement,
16 LiteralElement,
17 ArgumentElement,
18 NumberElement,
19 DateElement,
20 TimeElement,
21 SelectElement,
22 PluralElement,
23 PluralOrSelectOption,
24 NumberSkeleton,
25 DateSkeleton,
26 SKELETON_TYPE,
27 TYPE,
28} from './types'`
29 },
30 returnTypes: {
31 argument: 'string',
32 ws: 'string',
33 digit: 'string',
34 hexDigit: 'string',
35 quoteEscapedChar: 'string',
36 apostrophe: 'string',
37 escape: 'string',
38 char: 'string',
39 chars: 'string',
40 varName: 'string',
41 number: 'number',
42 start: 'MessageFormatElement[]',
43 message: 'MessageFormatElement[]',
44 literalElement: 'LiteralElement',
45 argumentElement: 'ArgumentElement',
46 selectElement: 'SelectElement',
47 pluralElement: 'PluralElement',
48 selectOption: 'PluralOrSelectOption',
49 pluralOption: 'PluralOrSelectOption',
50 numberSkeleton: 'NumberSkeleton',
51 dateOrTimeSkeleton: 'DateSkeleton',
52 numberArgStyle: 'string | NumberSkeleton',
53 dateOrTimeArgStyle: 'string | DateSkeleton',
54 simpleFormatElement: `
55| NumberElement
56| DateElement
57| TimeElement
58`
59 }
60});
61
62const REGEX = /ParseFunction = \((.*?)\) => (any);/g;
63
64outputFileSync(
65 'src/parser.ts',
66 srcString.replace(REGEX, 'ParseFunction = ($1) => MessageFormatElement[];')
67);