Skip to content

Commit 47adcf3

Browse files
author
Builder
committed
Merge branch '4390-stub-parser-warning-line-number' into 'master'
[FIX][SDK] adding line number to parse errors See merge request codingame/game-engine!152
2 parents 13f1210 + dcbcad2 commit 47adcf3

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

runner/src/main/resources/view/lib/Parser.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const OCCURENCE_TOKEN = '@>'
22

33
function ParseError (name, params) {
4+
this.message = name + ': ' + JSON.stringify(params)
45
this.name = name
56
this.params = params
67
}
@@ -106,12 +107,14 @@ Parser.prototype.read = function (input, n) {
106107
const type = typeValue[1]
107108
if (!/^[a-zA-Z0-9]+$/.test(name)) {
108109
throw new ParseError('InvalidVariable', {
109-
variable: name
110+
variable: name,
111+
line: this.getLineNumber()
110112
})
111113
}
112114
if (!type) {
113115
throw new ParseError('MissingType', {
114-
variable: name
116+
variable: name,
117+
line: this.getLineNumber()
115118
})
116119
}
117120
const nType = new Node(type, n)
@@ -125,12 +128,14 @@ Parser.prototype.read = function (input, n) {
125128
} else if (type === 'word' || type === 'string') {
126129
throw new ParseError('MissingLength', {
127130
variable: name,
128-
type: type
131+
type: type,
132+
line: this.getLineNumber()
129133
})
130134
} else if (this.allowType.indexOf(type) < 0) {
131135
throw new ParseError('InvalidType', {
132136
variable: name,
133-
type: type
137+
type: type,
138+
line: this.getLineNumber()
134139
})
135140
}
136141

@@ -155,7 +160,8 @@ Parser.prototype.parseTree = function (node, input, loopCnt) {
155160

156161
if (typeof param !== 'undefined') {
157162
throw new ParseError('InvalidKeyword', {
158-
param: cmd + ':' + param
163+
param: cmd + ':' + param,
164+
line: this.getLineNumber()
159165
})
160166
}
161167

@@ -214,10 +220,10 @@ Parser.prototype.parseTree = function (node, input, loopCnt) {
214220
const name = vari.trim()
215221
match = name.match(/"(.*)"/)
216222
if (match) {
217-
return {type: 'CONST', value: match[1]}
223+
return { type: 'CONST', value: match[1] }
218224
} else {
219225
const codeName = parser.getFirstCodeName(input.vars, name)
220-
return {type: 'VAR', node: input.getVar(codeName)}
226+
return { type: 'VAR', node: input.getVar(codeName) }
221227
}
222228
})
223229
write.param.separator = separator
@@ -300,7 +306,8 @@ Parser.prototype.parseTree = function (node, input, loopCnt) {
300306
}
301307
default:
302308
throw new ParseError('InvalidKeyword', {
303-
param: cmd
309+
param: cmd,
310+
line: this.getLineNumber()
304311
})
305312
}
306313
}
@@ -336,5 +343,8 @@ Parser.prototype.extractParam = function (type) {
336343
const iclose = type.indexOf(')')
337344
return type.substring(iopen + 1, iclose)
338345
}
346+
Parser.prototype.getLineNumber = function () {
347+
return this.code.substring(0, this.cursor - 1).split('\n').length
348+
}
339349

340350
export default Parser

0 commit comments

Comments
 (0)