1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | var ParseException = require('@accordproject/concerto-cto').ParseException;
|
18 | var TemplateException = require('./templateexception');
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function maxOfExpected(expected) {
|
26 | return Math.max.apply(null, expected.map(x => x.length));
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function cleanExpected(expected) {
|
35 | return expected.map(x => new RegExp(/'[^']*'/).test(x) ? x.substr(1, x.length - 2) : x);
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | function _throwParseException(markdown, result, fileName) {
|
45 |
|
46 | var fileLocation = {};
|
47 | var shortMessage;
|
48 | var longMessage;
|
49 | if (typeof result !== 'string') {
|
50 |
|
51 | shortMessage = "Parse error at line ".concat(result.index.line, " column ").concat(result.index.column);
|
52 |
|
53 |
|
54 | var start = result.index;
|
55 | var end = Object.assign({}, start);
|
56 | end.offset = end.offset + 1;
|
57 | end.column = end.column + 1;
|
58 | fileLocation.start = start;
|
59 | fileLocation.end = end;
|
60 | var lines = markdown.split('\n');
|
61 | var expected = result.expected;
|
62 | var underline = line => {
|
63 | var maxLength = line.length - (start.column - 1);
|
64 | var maxExpected = maxOfExpected(cleanExpected(expected));
|
65 | return '^'.repeat(maxLength < maxExpected ? maxLength : maxExpected);
|
66 | };
|
67 | var line = lines[start.line - 1];
|
68 | var snippet = line + '\n' + ' '.repeat(start.column - 1) + underline(line);
|
69 | var isEOF = x => {
|
70 | if (x[0] && x[0] === 'EOF') {
|
71 | return true;
|
72 | } else {
|
73 | return false;
|
74 | }
|
75 | };
|
76 |
|
77 |
|
78 | var expectedMessage = 'Expected: ' + (isEOF(expected) ? 'End of text' : expected.join(' or '));
|
79 | longMessage = shortMessage + '\n' + snippet + '\n' + expectedMessage;
|
80 | } else {
|
81 | shortMessage = result;
|
82 | longMessage = shortMessage;
|
83 | fileLocation.start = {
|
84 | offset: -1,
|
85 | column: -1
|
86 | };
|
87 | fileLocation.end = {
|
88 | offset: -1,
|
89 | column: -1
|
90 | };
|
91 | }
|
92 | throw new ParseException(shortMessage, fileLocation, fileName, longMessage, 'markdown-template');
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function _throwTemplateExceptionForElement(message, element) {
|
102 | var fileName = 'text/grammar.tem.md';
|
103 |
|
104 |
|
105 | var column = -1;
|
106 | var line = -1;
|
107 | var token = element && element.value ? element.value : ' ';
|
108 | var endColumn = column + token.length;
|
109 | var fileLocation = {
|
110 | start: {
|
111 | line,
|
112 | column
|
113 | },
|
114 | end: {
|
115 | line,
|
116 | endColumn
|
117 | }
|
118 | };
|
119 |
|
120 | throw new TemplateException(message, fileLocation, fileName, null, 'markdown-template');
|
121 | }
|
122 | module.exports._throwTemplateExceptionForElement = _throwTemplateExceptionForElement;
|
123 | module.exports._throwParseException = _throwParseException; |
\ | No newline at end of file |