1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | "use strict";
|
17 | var __extends = (this && this.__extends) || (function () {
|
18 | var extendStatics = Object.setPrototypeOf ||
|
19 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
20 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
21 | return function (d, b) {
|
22 | extendStatics(d, b);
|
23 | function __() { this.constructor = d; }
|
24 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
25 | };
|
26 | })();
|
27 | Object.defineProperty(exports, "__esModule", { value: true });
|
28 |
|
29 | var Line = (function () {
|
30 | function Line() {
|
31 | }
|
32 | return Line;
|
33 | }());
|
34 | exports.Line = Line;
|
35 | var CodeLine = (function (_super) {
|
36 | __extends(CodeLine, _super);
|
37 | function CodeLine(contents) {
|
38 | var _this = _super.call(this) || this;
|
39 | _this.contents = contents;
|
40 | return _this;
|
41 | }
|
42 | return CodeLine;
|
43 | }(Line));
|
44 | exports.CodeLine = CodeLine;
|
45 | var MessageSubstitutionLine = (function (_super) {
|
46 | __extends(MessageSubstitutionLine, _super);
|
47 | function MessageSubstitutionLine(key, message) {
|
48 | var _this = _super.call(this) || this;
|
49 | _this.key = key;
|
50 | _this.message = message;
|
51 | return _this;
|
52 | }
|
53 | return MessageSubstitutionLine;
|
54 | }(Line));
|
55 | exports.MessageSubstitutionLine = MessageSubstitutionLine;
|
56 | var ErrorLine = (function (_super) {
|
57 | __extends(ErrorLine, _super);
|
58 | function ErrorLine(startCol) {
|
59 | var _this = _super.call(this) || this;
|
60 | _this.startCol = startCol;
|
61 | return _this;
|
62 | }
|
63 | return ErrorLine;
|
64 | }(Line));
|
65 | exports.ErrorLine = ErrorLine;
|
66 | var MultilineErrorLine = (function (_super) {
|
67 | __extends(MultilineErrorLine, _super);
|
68 | function MultilineErrorLine(startCol) {
|
69 | return _super.call(this, startCol) || this;
|
70 | }
|
71 | return MultilineErrorLine;
|
72 | }(ErrorLine));
|
73 | exports.MultilineErrorLine = MultilineErrorLine;
|
74 | var EndErrorLine = (function (_super) {
|
75 | __extends(EndErrorLine, _super);
|
76 | function EndErrorLine(startCol, endCol, message) {
|
77 | var _this = _super.call(this, startCol) || this;
|
78 | _this.endCol = endCol;
|
79 | _this.message = message;
|
80 | return _this;
|
81 | }
|
82 | return EndErrorLine;
|
83 | }(ErrorLine));
|
84 | exports.EndErrorLine = EndErrorLine;
|
85 |
|
86 |
|
87 | var multilineErrorRegex = /^\s*(~+|~nil)$/;
|
88 |
|
89 | var endErrorRegex = /^\s*(~+|~nil)\s*\[(.+)\]\s*$/;
|
90 |
|
91 | var messageSubstitutionRegex = /^\[([\w\-\_]+?)]: \s*(.+?)\s*$/;
|
92 | exports.ZERO_LENGTH_ERROR = "~nil";
|
93 |
|
94 |
|
95 |
|
96 | function parseLine(text) {
|
97 | var multilineErrorMatch = text.match(multilineErrorRegex);
|
98 | if (multilineErrorMatch != null) {
|
99 | var startErrorCol = text.indexOf("~");
|
100 | return new MultilineErrorLine(startErrorCol);
|
101 | }
|
102 | var endErrorMatch = text.match(endErrorRegex);
|
103 | if (endErrorMatch != null) {
|
104 | var squiggles = endErrorMatch[1], message = endErrorMatch[2];
|
105 | var startErrorCol = text.indexOf("~");
|
106 | var zeroLengthError = (squiggles === exports.ZERO_LENGTH_ERROR);
|
107 | var endErrorCol = zeroLengthError ? startErrorCol : text.lastIndexOf("~") + 1;
|
108 | return new EndErrorLine(startErrorCol, endErrorCol, message);
|
109 | }
|
110 | var messageSubstitutionMatch = text.match(messageSubstitutionRegex);
|
111 | if (messageSubstitutionMatch != null) {
|
112 | var key = messageSubstitutionMatch[1], message = messageSubstitutionMatch[2];
|
113 | return new MessageSubstitutionLine(key, message);
|
114 | }
|
115 |
|
116 | return new CodeLine(text);
|
117 | }
|
118 | exports.parseLine = parseLine;
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function printLine(line, code) {
|
128 | if (line instanceof ErrorLine) {
|
129 | if (code == null) {
|
130 | throw new Error("Must supply argument for code parameter when line is an ErrorLine");
|
131 | }
|
132 | var leadingSpaces = " ".repeat(line.startCol);
|
133 | if (line instanceof MultilineErrorLine) {
|
134 |
|
135 |
|
136 | if (code.length === 0 && line.startCol === 0) {
|
137 | return exports.ZERO_LENGTH_ERROR;
|
138 | }
|
139 | var tildes = "~".repeat(code.length - leadingSpaces.length);
|
140 | return "" + leadingSpaces + tildes;
|
141 | }
|
142 | else if (line instanceof EndErrorLine) {
|
143 | var tildes = "~".repeat(line.endCol - line.startCol);
|
144 | var endSpaces = " ".repeat(code.length - line.endCol);
|
145 | if (tildes.length === 0) {
|
146 | tildes = exports.ZERO_LENGTH_ERROR;
|
147 |
|
148 |
|
149 | endSpaces = endSpaces.substring(0, Math.max(endSpaces.length - 4, 1));
|
150 | }
|
151 | return "" + leadingSpaces + tildes + endSpaces + " [" + line.message + "]";
|
152 | }
|
153 | }
|
154 | else if (line instanceof MessageSubstitutionLine) {
|
155 | return "[" + line.key + "]: " + line.message;
|
156 | }
|
157 | else if (line instanceof CodeLine) {
|
158 | return line.contents;
|
159 | }
|
160 | return null;
|
161 | }
|
162 | exports.printLine = printLine;
|