-
Notifications
You must be signed in to change notification settings - Fork 120
/
parsermanager.js
526 lines (468 loc) · 20 KB
/
parsermanager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const fsPath = require('path');
const TemplateException = require('./templateexception');
const RelationshipDeclaration = require('@accordproject/concerto-core').RelationshipDeclaration;
const Writer = require('@accordproject/concerto-core').Writer;
const Logger = require('@accordproject/concerto-core').Logger;
const nearley = require('nearley');
const compile = require('nearley/lib/compile');
const generate = require('nearley/lib/generate');
const nearleyGrammar = require('nearley/lib/nearley-language-bootstrapped');
const templateGrammar = require('./tdl.js');
const GrammarVisitor = require('./grammarvisitor');
const uuid = require('uuid');
const nunjucks = require('nunjucks');
const DateTimeFormatParser = require('./datetimeformatparser');
const CommonMarkTransformer = require('@accordproject/markdown-common').CommonMarkTransformer;
// This required because only compiled nunjucks templates are supported browser-side
// https://mozilla.github.io/nunjucks/api.html#browser-usage
// We can't always import it in Cicero because precompiling is not supported server-side!
// https://github.com/mozilla/nunjucks/issues/1065
if(process.browser){
require('./compiled_template');
}
/**
* Generates and manages a Nearley parser for a template.
* @class
*/
class ParserManager {
/**
* Create the ParserManager.
* @param {object} template - the template instance
*/
constructor(template) {
this.template = template;
this.grammar = null;
this.grammarAst = null;
this.templatizedGrammar = null;
this.templateAst = null;
this.ergoExpression = false;
}
/**
* Gets a parser object for this template
* @return {object} the parser for this template
*/
getParser() {
if (!this.grammarAst) {
throw new Error('Must call setGrammar or buildGrammar before calling getParser');
}
return new nearley.Parser(nearley.Grammar.fromCompiled(this.grammarAst));
}
/**
* Gets the AST for the template
* @return {object} the AST for the template
*/
getTemplateAst() {
if (!this.grammarAst) {
throw new Error('Must call setGrammar or buildGrammar before calling getTemplateAst');
}
return this.templateAst;
}
/**
* Set the grammar for the template
* @param {String} grammar - the grammar for the template
*/
setGrammar(grammar) {
this.grammarAst = ParserManager.compileGrammar(grammar);
this.grammar = grammar;
}
/**
* Adjust the template for list blocks
* @param {object} x - The current template AST node
* @param {String} separator - The list separator
* @return {object} the new template AST node
*/
static adjustListBlock(x,separator) {
if (x.data[0] && x.data[0].type === 'Chunk') {
x.data[0].value = separator + x.data[0].value;
return x;
} else {
throw new Error('List block in template should contain text');
}
}
/**
* Build a grammar from a template
* @param {String} templatizedGrammar - the annotated template
* using the markdown parser
*/
buildGrammar(templatizedGrammar) {
// Set ergoExpression flag to false
this.ergoExpression = false;
// Roundtrip the grammar through the Commonmark parser
templatizedGrammar = this.roundtripMarkdown(templatizedGrammar);
// console.log(templatizedGrammar);
Logger.debug('buildGrammar', templatizedGrammar);
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(templateGrammar));
parser.feed(templatizedGrammar);
if (parser.results.length !== 1) {
throw new Error('Ambiguous parse!');
}
// parse the template grammar to generate a dynamic grammar
const ast = parser.results[0];
this.templateAst = ast;
const parts = {
textRules: [],
modelRules: [],
grammars : {}
};
parts.grammars.base = require('./grammars/base');
this.buildGrammarRules(ast, this.template.getTemplateModel(), 'rule', parts);
// generate the grammar for the model
const parameters = {
writer: new Writer(),
rules : []
};
const gv = new GrammarVisitor();
this.template.getModelManager().accept(gv, parameters);
parts.modelRules.push(...parameters.rules);
// combine the results
nunjucks.configure(fsPath.resolve(__dirname), {
tags: {
blockStart: '<%',
blockEnd: '%>'
},
autoescape: false // Required to allow nearley syntax strings
});
const combined = nunjucks.render('template.ne', parts);
Logger.debug('Generated template grammar' + combined);
// console.log(combined);
this.setGrammar(combined);
this.templatizedGrammar = templatizedGrammar;
}
/**
* Build grammar rules from a template
* @param {object} ast - the AST from which to build the grammar
* @param {ClassDeclaration} templateModel - the type of the parent class for this AST
* @param {String} prefix - A unique prefix for the grammar rules
* @param {Object} parts - Result object to acculumate rules and required sub-grammars
*/
buildGrammarRules(ast, templateModel, prefix, parts) {
// these are the rules for variables
const rules = {};
// these are the rules for static text
let textRules = {};
// generate all the rules for the static text
textRules.prefix = prefix;
textRules.symbols = [];
ast.data.forEach((element, index) => {
// ignore empty chunks (issue #1) and missing optional last chunks
if (element && (element.type !== 'Chunk' || element.value.length > 0)) {
Logger.debug(`element ${prefix}${index} ${JSON.stringify(element)}`);
rules[prefix + index] = element;
textRules.symbols.push(prefix + index);
}
}, this);
// the result of parsing is an instance of the template model
textRules.class = templateModel.getFullyQualifiedName();
const identifier = templateModel.getIdentifierFieldName();
if (identifier !== null) {
textRules.identifier = `${identifier} : "${uuid.v4()}"`;
}
// we then bind each variable in the template model
// to the first occurence of the variable in the template grammar
textRules.properties = [];
templateModel.getProperties().forEach((property, index) => {
const sep = index < templateModel.getProperties().length - 1 ? ',' : '';
const bindingIndex = this.findFirstBinding(property.getName(), ast.data);
if (bindingIndex !== -1) { // ignore things like transactionId
textRules.properties.push(`${property.getName()} : ${prefix}${bindingIndex}${sep}`);
}
});
parts.textRules.push(textRules);
// Now create the child rules for each symbol in the root rule
for (let rule in rules) {
const element = rules[rule];
switch (element.type) {
case 'Chunk':
case 'LastChunk':
parts.modelRules.push({
prefix: rule,
symbols: [this.cleanChunk(element.value)],
});
break;
case 'IfBinding': {
const property = ParserManager.getProperty(templateModel, element);
if(property.getType() !== 'Boolean') {
ParserManager._throwTemplateExceptionForElement(`An if block can only be used with a boolean property. Property ${element.fieldName.value} has type ${property.getType()}`, element);
}
parts.modelRules.push({
prefix: rule,
symbols: [`"${element.stringIf.value}":? {% (d) => {return d[0] !== null;}%} # ${element.fieldName.value}`],
});
}
break;
case 'IfElseBinding': {
const property = ParserManager.getProperty(templateModel, element);
if(property.getType() !== 'Boolean') {
ParserManager._throwTemplateExceptionForElement(`An if block can only be used with a boolean property. Property ${element.fieldName.value} has type ${property.getType()}`, element);
}
parts.modelRules.push({
prefix: rule,
symbols: [`("${element.stringIf.value}"|"${element.stringElse.value}") {% (d) => {return d[0][0] === "${element.stringIf.value}";}%} # ${element.fieldName.value}`],
});
}
break;
case 'FormattedBinding':
case 'Binding':
case 'ClauseBinding':
case 'WithBinding':
case 'UListBinding':
case 'OListBinding':
case 'JoinBinding':
this.handleBinding(templateModel, parts, rule, element);
break;
case 'Expr':
this.ergoExpression = true;
parts.modelRules.push({
prefix: rule,
symbols: ['Any'],
});
break;
default:
ParserManager._throwTemplateExceptionForElement(`Unrecognized type ${element.type}`, element);
}
}
}
/**
* Throws an error if a template variable doesn't exist on the model.
* @param {*} templateModel - the model for the template
* @param {*} element - the current element in the AST
* @returns {*} the property
*/
static getProperty(templateModel, element) {
const propertyName = element.fieldName.value;
const property = templateModel.getProperty(propertyName);
if (!property) {
ParserManager._throwTemplateExceptionForElement(`Template references a property '${propertyName}' that is not declared in the template model '${templateModel.getFullyQualifiedName()}'`, element);
}
return property;
}
/**
* Throw a template exception for the element
* @param {string} message - the error message
* @param {object} element the AST
* @throws {TemplateException}
*/
static _throwTemplateExceptionForElement(message, element) {
const fileName = 'text/grammar.tem.md';
let column = element.fieldName.col;
let line = element.fieldName.line;
let token = element.value ? element.value : ' ';
const endColumn = column + token.length;
const fileLocation = {
start: {
line,
column,
},
end: {
line,
endColumn,//XXX
},
};
throw new TemplateException(message, fileLocation, fileName, null, 'cicero-core');
}
/**
* Utility method to generate a grammar rule for a variable binding
* @param {ClassDeclaration} templateModel - the current template model
* @param {*} parts - the parts, where the rule will be added
* @param {*} inputRule - the rule we are processing in the AST
* @param {*} element - the current element in the AST
*/
handleBinding(templateModel, parts, inputRule, element) {
const propertyName = element.fieldName.value;
const property = ParserManager.getProperty(templateModel, element);
let action = null;
let suffix = ':';
let type = property.getType();
let firstType = null;
// if the type/action have not been set explicity, then we infer them
if(!action) {
action = '{% id %}';
if(property.getType() === 'DateTime' || element.type === 'FormattedBinding' ) {
if(property.getType() !== 'DateTime') {
ParserManager._throwTemplateExceptionForElement('Formatted types are currently only supported for DateTime properties.', element);
}
// we only include the datetime grammar if custom formats are used
if(!parts.grammars.dateTime) {
parts.grammars.dateTime = require('./grammars/datetime');
parts.grammars.dateTimeEn = require('./grammars/datetime-en');
}
// push the formatting rule, iff it has not been already declared
const format = element.format ? element.format.value : '"MM/DD/YYYY"';
const formatRule = DateTimeFormatParser.buildDateTimeFormatRule(format);
type = formatRule.name;
const ruleExists = parts.modelRules.some(rule => (rule.prefix === formatRule.name));
if(!ruleExists) {
parts.modelRules.push({
prefix: formatRule.name,
symbols: [`${formatRule.tokens} ${formatRule.action} # ${propertyName} as ${format}`],
});
}
} else if(element.type === 'ClauseBinding' || element.type === 'WithBinding') {
const nestedTemplate = element.template;
const nestedTemplateModel = this.template.getIntrospector().getClassDeclaration(property.getFullyQualifiedTypeName());
this.buildGrammarRules(nestedTemplate, nestedTemplateModel, propertyName, parts);
type = element.fieldName.value;
} else if(element.type === 'UListBinding' || element.type === 'OListBinding' || element.type === 'JoinBinding') {
const nestedTemplateModel = this.template.getIntrospector().getClassDeclaration(property.getFullyQualifiedTypeName());
// What separates elements in the list?
let separator;
if (element.type === 'JoinBinding') {
separator = element.separator;
} else {
separator = element.type === 'UListBinding' ? '- ' : '1. ';
}
// Rule for first item in the list
let firstNestedTemplate;
if (element.type === 'JoinBinding') {
firstNestedTemplate = element.template;
} else {
firstNestedTemplate = ParserManager.adjustListBlock(element.template, separator);
}
this.buildGrammarRules(firstNestedTemplate, nestedTemplateModel, propertyName + 'First', parts);
firstType = element.fieldName.value + 'First';
// Rule for all other items in the list
let nestedTemplate;
if (element.type === 'JoinBinding') {
nestedTemplate = ParserManager.adjustListBlock(element.template, separator);
} else {
nestedTemplate = ParserManager.adjustListBlock(element.template, '\n');
}
this.buildGrammarRules(nestedTemplate, nestedTemplateModel, propertyName, parts);
type = element.fieldName.value;
action = `
{%
([ ${propertyName + 'First'}, ${propertyName} ]) => {
return [${propertyName + 'First'}].concat(${propertyName});
}
%}`;
} else {
// relationships need to be transformed into strings
if (property instanceof RelationshipDeclaration) {
type = 'String';
}
}
}
if (property.isArray()) {
suffix += '*';
}
if (property.isOptional()) {
suffix += '?';
}
if (suffix === ':') {
suffix = '';
}
// console.log(`${inputRule} => ${type}${suffix} ${action} # ${propertyName}`);
if(element.type === 'UListBinding' || element.type === 'OListBinding' || element.type === 'JoinBinding') {
parts.modelRules.push({
prefix: inputRule,
//symbols: [`"[{" ${type}${suffix} "}]" ${action} # ${propertyName}`],
symbols: [`${firstType} ${type}${suffix} ${action} # ${propertyName}`],
});
} else {
parts.modelRules.push({
prefix: inputRule,
//symbols: [`"[{" ${type}${suffix} "}]" ${action} # ${propertyName}`],
symbols: [`${type}${suffix} ${action} # ${propertyName}`],
});
}
}
/**
* Cleans a chunk of text to make it safe to include
* as a grammar rule. We need to remove linefeeds and
* escape any '"' characters.
*
* @param {string} input - the input text from the template
* @return {string} cleaned text
*/
cleanChunk(input) {
// we replace all \n with \\n
let text = input.replace(/\n/gm,'\\n');
// replace all " with \"
text = text.replace(/"/gm, '\\"');
return `"${text}"`;
}
/**
* Finds the first binding for the given property
*
* @param {string} propertyName the name of the property
* @param {object[]} elements the result of parsing the template_txt.
* @return {int} the index of the element or -1
*/
findFirstBinding(propertyName, elements) {
for(let n=0; n < elements.length; n++) {
const element = elements[n];
if(element !== null && ['Binding','FormattedBinding','IfBinding','IfElseBinding','UListBinding','OListBinding','JoinBinding','ClauseBinding','WithBinding'].includes(element.type)) {
if(element.fieldName.value === propertyName) {
return n;
}
}
}
return -1;
}
/**
* Get the (compiled) grammar for the template
* @return {String} - the grammar for the template
*/
getGrammar() {
return this.grammar;
}
/**
* Returns the templatized grammar
* @return {String} the contents of the templatized grammar
*/
getTemplatizedGrammar() {
return this.templatizedGrammar;
}
/**
* Compiles a Nearley grammar to its AST
* @param {string} sourceCode - the source text for the grammar
* @return {object} the AST for the grammar
*/
static compileGrammar(sourceCode) {
try {
// Parse the grammar source into an AST
const grammarParser = new nearley.Parser(nearleyGrammar);
grammarParser.feed(sourceCode);
const grammarAst = grammarParser.results[0]; // TODO check for errors
// Compile the AST into a set of rules
const grammarInfoObject = compile(grammarAst, {});
// Generate JavaScript code from the rules
const grammarJs = generate(grammarInfoObject, 'grammar');
// Pretend this is a CommonJS environment to catch exports from the grammar.
const module = {
exports: {}
};
eval(grammarJs);
return module.exports;
} catch (err) {
Logger.error(err);
throw err;
}
}
/**
* Round-trip markdown
* @param {string} text - the markdown text
* @return {string} the result of parsing and printing back the text
*/
roundtripMarkdown(text) {
// Roundtrip the grammar through the Commonmark parser
const commonMarkTransformer = new CommonMarkTransformer({ noIndex: true });
const concertoAst = commonMarkTransformer.fromMarkdown(text);
return commonMarkTransformer.toMarkdown(concertoAst);
}
}
module.exports = ParserManager;