UNPKG

2.75 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17const BaseFileException = require('../basefileexception');
18
19/**
20 * Exception throws when a Concerto file is syntactically invalid
21 * @extends BaseFileException
22 * @see See {@link BaseFileException}
23 * @class
24 * @memberof module:concerto-core
25 * @private
26 */
27class ParseException extends BaseFileException {
28
29 /**
30 * Create an ParseException
31 * @param {string} message - the message for the exception
32 * @param {string} fileLocation - the optional file location associated with the exception
33 * @param {string} fileName - the optional file name associated with the exception
34 * @param {string} fullMessageOverride - the optional pre-existing full message
35 * @param {string} component - the optional component which throws this error
36 */
37 constructor(message, fileLocation, fileName, fullMessageOverride, component) {
38 let fullMessage = message;
39 let suffix = '';
40
41 // Add the file name onto the message if it has been set.
42 if (fileName) {
43 suffix += ' File ' + fileName;
44 }
45
46 // The parser does not give us back the end location of an invalid token.
47 // Making the end column equal to the start column makes use of
48 // vscodes default behaviour of selecting an entire word
49 if (fileLocation) {
50 if (fileLocation.end && fileLocation.start) {
51 if (fileLocation.end.offset && fileLocation.start.offset) {
52 if (fileLocation.end.offset - fileLocation.start.offset === 1) {
53 fileLocation.end.column = fileLocation.start.column;
54 fileLocation.end.offset = fileLocation.start.offset;
55 }
56 }
57 }
58 if (suffix) {
59 suffix+= ' line ' + fileLocation.start.line + ' column ' + fileLocation.start.column;
60 } else {
61 suffix+= ' Line ' + fileLocation.start.line + ' column ' + fileLocation.start.column;
62 }
63 }
64
65 fullMessage += suffix;
66 super(message, fileLocation, fullMessageOverride || fullMessage, fileName, component);
67 }
68}
69
70module.exports = ParseException;