Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
parse(input, currentTime, fileName) {
let text = TemplateLoader.normalizeText(input);
// Roundtrip the sample through the Commonmark parser
text = this.getTemplate().getParserManager().roundtripMarkdown(text);
// Set the current time and UTC Offset
const now = Util.setCurrentTime(currentTime);
const utcOffset = now.utcOffset();
let parser = this.getTemplate().getParserManager().getParser();
try {
parser.feed(text);
} catch(err) {
const fileLocation = ErrorUtil.locationOfError(err);
throw new ParseException(err.message, fileLocation, fileName, err.message, 'cicero-core');
}
if (parser.results.length !== 1) {
const head = JSON.stringify(parser.results[0]);
parser.results.forEach(function (element) {
if (head !== JSON.stringify(element)) {
const err = `Ambiguous text. Got ${parser.results.length} ASTs for text: ${text}`;
throw new ParseException(err, null, fileName, err, 'cicero-core' );
}
}, this);
}
let ast = parser.results[0];
Logger.debug('Result of parsing: ' + JSON.stringify(ast));
if(!ast) {
const err = 'Parsing clause text returned a null AST. This may mean the text is valid, but not complete.';
if (parser.results.length !== 1) {
const head = JSON.stringify(parser.results[0]);
parser.results.forEach(function (element) {
if (head !== JSON.stringify(element)) {
const err = `Ambiguous text. Got ${parser.results.length} ASTs for text: ${text}`;
throw new ParseException(err, null, fileName, err, 'cicero-core' );
}
}, this);
}
let ast = parser.results[0];
Logger.debug('Result of parsing: ' + JSON.stringify(ast));
if(!ast) {
const err = 'Parsing clause text returned a null AST. This may mean the text is valid, but not complete.';
throw new ParseException(err, null, fileName, err, 'cicero-core' );
}
ast = TemplateInstance.convertDateTimes(ast, utcOffset);
this.setData(ast);
}
parser.results.forEach(function (element) {
if (head !== JSON.stringify(element)) {
const err = `Ambiguous text. Got ${parser.results.length} ASTs for text: ${text}`;
throw new ParseException(err, null, fileName, err, 'cicero-core' );
}
}, this);
}