summaryrefslogtreecommitdiff
path: root/com/jeremyfa/yaml/YamlParseException.js
blob: 04fddf3a48277bceb3a20c4c41c2b7af2cac52c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
 * Exception class thrown when an error occurs during parsing.
 *
 * @author Fabien Potencier <fabien@symfony.com>
 *
 * @api
 */
 
/**
 * Constructor.
 *
 * @param string	message	The error message
 * @param integer   parsedLine The line where the error occurred
 * @param integer   snippet	The snippet of code near the problem
 * @param string	parsedFile The file name where the error occurred
 */

var YamlParseException = function(message, parsedLine, snippet, parsedFile){

		this.rawMessage = message;
		this.parsedLine = (parsedLine !== undefined) ? parsedLine : -1;
		this.snippet = (snippet !== undefined) ? snippet : null;
		this.parsedFile = (parsedFile !== undefined) ? parsedFile : null;

		this.updateRepr();
		
		this.message = message;

};
YamlParseException.prototype =
{

	name: 'YamlParseException',
	message: null,
	
	parsedFile: null,
	parsedLine: -1,
	snippet: null,
	rawMessage: null,

	isDefined: function(input)
	{
		return input != undefined && input != null;
	},

	/**
	* Gets the snippet of code near the error.
	*
	* @return string The snippet of code
	*/
	getSnippet: function()
	{
		return this.snippet;
	},

	/**
	* Sets the snippet of code near the error.
	*
	* @param string snippet The code snippet
	*/
	setSnippet: function(snippet)
	{
		this.snippet = snippet;

		this.updateRepr();
	},

	/**
	* Gets the filename where the error occurred.
	*
	* This method returns null if a string is parsed.
	*
	* @return string The filename
	*/
	getParsedFile: function()
	{
		return this.parsedFile;
	},

	/**
	* Sets the filename where the error occurred.
	*
	* @param string parsedFile The filename
	*/
	setParsedFile: function(parsedFile)
	{
		this.parsedFile = parsedFile;

		this.updateRepr();
	},

	/**
	* Gets the line where the error occurred.
	*
	* @return integer The file line
	*/
	getParsedLine: function()
	{
		return this.parsedLine;
	},

	/**
	* Sets the line where the error occurred.
	*
	* @param integer parsedLine The file line
	*/
	setParsedLine: function(parsedLine)
	{
		this.parsedLine = parsedLine;

		this.updateRepr();
	},

	updateRepr: function()
	{
		this.message = this.rawMessage;

		dot = false;
		if ('.' === this.message.charAt(this.message.length - 1)) {
			this.message = this.message.substring(0, this.message.length - 1);
			dot = true;
		}

		if (null !== this.parsedFile) {
			this.message += ' in ' + JSON.stringify(this.parsedFile);
		}

		if (this.parsedLine >= 0) {
			this.message += ' at line ' + this.parsedLine;
		}

		if (this.snippet) {
			this.message += ' (near "' + this.snippet + '")';
		}

		if (dot) {
			this.message += '.';
		}
	}
}