Skip to content

Commit 5a3578b

Browse files
committed
fix(template) Issue when rebuilding the parser #277
Signed-off-by: Jerome Simeon <[email protected]>
1 parent e86c42c commit 5a3578b

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

packages/markdown-template/lib/parsermanager.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,26 @@ class ParserManager {
6262
this.parser = null;
6363
this.templateKind = templateKind ? templateKind : 'clause';
6464
this.currentTime = datetimeutil.setCurrentTime(null); // Default setting to now
65+
this.userParsingTable = parsingTable;
66+
this.formulaEval = formulaEval ? formulaEval : defaultFormulaEval;
67+
68+
// Initialize parsing table
69+
this.initParsingTable();
70+
}
6571

72+
/**
73+
* Initialize parsing table
74+
*/
75+
initParsingTable() {
6676
// Mapping from types to parsers/drafters
6777
this.parserVisitor = new ToParserVisitor();
6878
const parserHook = function(ast,parameters) {
6979
return ToParserVisitor.toParserWithParameters(new ToParserVisitor(),ast,parameters);
7080
};
7181
this.parsingTable = new ParsingTable(this.modelManager,parserHook,draftVisitNodes);
72-
if (parsingTable) {
73-
this.parsingTable.addParsingTable(parsingTable);
82+
if (this.userParsingTable) {
83+
this.parsingTable.addParsingTable(this.userParsingTable);
7484
}
75-
this.formulaEval = formulaEval ? formulaEval : defaultFormulaEval;
7685
}
7786

7887
/**
@@ -154,17 +163,17 @@ class ParserManager {
154163
}
155164

156165
/**
157-
* Sets parsing table for variables
166+
* Sets parsing table extension
158167
* @param {object} table the parsing table
159168
*/
160169
setParsingTable(table) {
161-
this.parsingTable = table;
170+
this.userParsingTable = table;
162171
}
163172

164173
/**
165-
* Build the parser
174+
* Initialize the parser
166175
*/
167-
buildParser() {
176+
initParser() {
168177
if (!this.templateMark) {
169178
const tokenStream = templateToTokens(this.template);
170179
const template = tokensToUntypedTemplateMark(tokenStream, this.templateKind);
@@ -173,6 +182,32 @@ class ParserManager {
173182
this.parser = this.parserVisitor.toParser(this,this.templateMark,this.parsingTable);
174183
}
175184

185+
/**
186+
* Build the parser
187+
*/
188+
buildParser() {
189+
if (this.parser) {
190+
this.rebuildParser();
191+
} else {
192+
this.initParser();
193+
}
194+
}
195+
196+
/**
197+
* Rebuild the parser
198+
*/
199+
rebuildParser() {
200+
// Clear the parser
201+
this.parser = null;
202+
// Reinitialize the parsing table
203+
this.initParsingTable();
204+
// Clear templateMark if a template grammar exists
205+
if (this.template && this.templateMark) {
206+
this.templateMark = null;
207+
}
208+
this.initParser();
209+
}
210+
176211
/**
177212
* Get the execute function for a given formula
178213
* @param {string} name - the name of that formula

packages/markdown-template/test/parsermanager.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ describe('#constructor', () => {
5050
should.exist(parserManager.getTemplateMark());
5151
});
5252

53+
it('rebuild a parser', async () => {
54+
const model = './test/data/helloworld/model.cto';
55+
const template = normalizeNLs(fs.readFileSync('./test/data/helloworld/grammar.tem.md', 'utf8'));
56+
const modelManager = await ModelLoader.loadModelManager(null,[model]);
57+
const parserManager = new ParserManager(modelManager,'clause',null,null);
58+
parserManager.setTemplate(template);
59+
parserManager.getTemplate().should.equal('Name of the person to greet: {{name}}.\nThank you!');
60+
(() => parserManager.getTemplateMark()).should.throw('Must call buildParser before calling getTemplateMark');
61+
(() => parserManager.getParser()).should.throw('Must call buildParser before calling getParser');
62+
parserManager.buildParser();
63+
parserManager.buildParser();
64+
should.exist(parserManager.getTemplateMark());
65+
});
66+
5367
it('handle formulas', async () => {
5468
const model = './test/data/fixed-interests/model.cto';
5569
const template = normalizeNLs(fs.readFileSync('./test/data/fixed-interests/grammar.tem.md', 'utf8'));

0 commit comments

Comments
 (0)