Skip to content

Commit

Permalink
fix(template) Issue when rebuilding the parser #277
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Aug 7, 2020
1 parent e86c42c commit 5a3578b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
49 changes: 42 additions & 7 deletions packages/markdown-template/lib/parsermanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,26 @@ class ParserManager {
this.parser = null;
this.templateKind = templateKind ? templateKind : 'clause';
this.currentTime = datetimeutil.setCurrentTime(null); // Default setting to now
this.userParsingTable = parsingTable;
this.formulaEval = formulaEval ? formulaEval : defaultFormulaEval;

// Initialize parsing table
this.initParsingTable();
}

/**
* Initialize parsing table
*/
initParsingTable() {
// Mapping from types to parsers/drafters
this.parserVisitor = new ToParserVisitor();
const parserHook = function(ast,parameters) {
return ToParserVisitor.toParserWithParameters(new ToParserVisitor(),ast,parameters);
};
this.parsingTable = new ParsingTable(this.modelManager,parserHook,draftVisitNodes);
if (parsingTable) {
this.parsingTable.addParsingTable(parsingTable);
if (this.userParsingTable) {
this.parsingTable.addParsingTable(this.userParsingTable);
}
this.formulaEval = formulaEval ? formulaEval : defaultFormulaEval;
}

/**
Expand Down Expand Up @@ -154,17 +163,17 @@ class ParserManager {
}

/**
* Sets parsing table for variables
* Sets parsing table extension
* @param {object} table the parsing table
*/
setParsingTable(table) {
this.parsingTable = table;
this.userParsingTable = table;
}

/**
* Build the parser
* Initialize the parser
*/
buildParser() {
initParser() {
if (!this.templateMark) {
const tokenStream = templateToTokens(this.template);
const template = tokensToUntypedTemplateMark(tokenStream, this.templateKind);
Expand All @@ -173,6 +182,32 @@ class ParserManager {
this.parser = this.parserVisitor.toParser(this,this.templateMark,this.parsingTable);
}

/**
* Build the parser
*/
buildParser() {
if (this.parser) {
this.rebuildParser();
} else {
this.initParser();
}
}

/**
* Rebuild the parser
*/
rebuildParser() {
// Clear the parser
this.parser = null;
// Reinitialize the parsing table
this.initParsingTable();
// Clear templateMark if a template grammar exists
if (this.template && this.templateMark) {
this.templateMark = null;
}
this.initParser();
}

/**
* Get the execute function for a given formula
* @param {string} name - the name of that formula
Expand Down
14 changes: 14 additions & 0 deletions packages/markdown-template/test/parsermanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ describe('#constructor', () => {
should.exist(parserManager.getTemplateMark());
});

it('rebuild a parser', async () => {
const model = './test/data/helloworld/model.cto';
const template = normalizeNLs(fs.readFileSync('./test/data/helloworld/grammar.tem.md', 'utf8'));
const modelManager = await ModelLoader.loadModelManager(null,[model]);
const parserManager = new ParserManager(modelManager,'clause',null,null);
parserManager.setTemplate(template);
parserManager.getTemplate().should.equal('Name of the person to greet: {{name}}.\nThank you!');
(() => parserManager.getTemplateMark()).should.throw('Must call buildParser before calling getTemplateMark');
(() => parserManager.getParser()).should.throw('Must call buildParser before calling getParser');
parserManager.buildParser();
parserManager.buildParser();
should.exist(parserManager.getTemplateMark());
});

it('handle formulas', async () => {
const model = './test/data/fixed-interests/model.cto';
const template = normalizeNLs(fs.readFileSync('./test/data/fixed-interests/grammar.tem.md', 'utf8'));
Expand Down

0 comments on commit 5a3578b

Please sign in to comment.