-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #315 from alephium/error-shift
Reposition and add file name to compilation error
- Loading branch information
Showing
3 changed files
with
194 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright 2018 - 2022 The Alephium Authors | ||
This file is part of the alephium project. | ||
The library is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
The library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with the library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { parseError } from './error' | ||
|
||
describe('error utils', function () { | ||
it('parse and reformat error', () => { | ||
function test(original: string, line: number, file: string, expected: string) { | ||
const error = parseError(original) | ||
if (error) { | ||
expect(error.reformat(line, file)).toEqual(expected) | ||
} else { | ||
throw new Error(`cannoot parse error ${original}`) | ||
} | ||
} | ||
|
||
const error = `-- error (256:3): Syntax error | ||
256 | event add(a: u256, b: u256) | ||
| ^^^^^^^^^^ | ||
| expected "}" | ||
|------------------------------------------------------------------------------------- | ||
|trace log: expected multicontract:1:1 / rawtxscript:2:1 / "}":3:3, found "event add("` | ||
|
||
const expected = `nft/nft.ral (3:3): Syntax error | ||
3 | event add(a: u256, b: u256) | ||
| ^^^^^^^^^^ | ||
| expected "}" | ||
|------------------------------------------------------------------------------------- | ||
|trace log: expected multicontract:1:1 / rawtxscript:2:1 / "}":3:3, found "event add("` | ||
|
||
const error2 = `-- error (7:3): Compilation error | ||
7 | event Add1(b: U256, a: U256) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| These events are defined multiple times: Add1, Add2` | ||
|
||
const expected2 = `foo.ral (123456:3): Compilation error | ||
123456 | event Add1(b: U256, a: U256) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| These events are defined multiple times: Add1, Add2` | ||
|
||
test(error, 3, 'nft/nft.ral', expected) | ||
test(error2, 123456, 'foo.ral', expected2) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2018 - 2022 The Alephium Authors | ||
This file is part of the alephium project. | ||
The library is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
The library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with the library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
class CompilationError { | ||
constructor( | ||
public lineStart: number, | ||
public column: number, | ||
public errorType: string, | ||
public line: number, | ||
public codeLine: string, | ||
public errorIndicator: string, | ||
public message: string, | ||
public additionalLine1?: string, | ||
public additionalLine2?: string | ||
) {} | ||
|
||
reformat(line: number, file: string): string { | ||
const spaces = `${line}`.replace(/\d/g, ' ') | ||
const newError = `${file} (${line}:${this.column}): ${this.errorType} | ||
${line} |${this.codeLine} | ||
${spaces} |${this.errorIndicator} | ||
${spaces} |${this.message}` | ||
|
||
if (this.additionalLine1 && this.additionalLine2) { | ||
return `${newError}\n${spaces} |${this.additionalLine1}\n${spaces} |${this.additionalLine2}` | ||
} else { | ||
return newError | ||
} | ||
} | ||
} | ||
|
||
const errorRegex = /error \((\d+):(\d+)\):\s*(.*)\n\s*(\d+)\s*\|(.*)\n.*\|(.*)\n\s*\|(.*)(?:\n\s*\|(.*)\n\s*\|(.*))?/ | ||
|
||
export function parseError(error: string): CompilationError | undefined { | ||
const match = error.match(errorRegex) | ||
|
||
if (match) { | ||
const lineStart = parseInt(match[1]) | ||
const column = parseInt(match[2]) | ||
const errorType = match[3] | ||
const line = parseInt(match[4]) | ||
const codeLine = match[5] | ||
const errorIndicator = match[6] | ||
const message = match[7] | ||
const additionalLine1 = match[8] | ||
const additionalLine2 = match[9] | ||
|
||
return new CompilationError( | ||
lineStart, | ||
column, | ||
errorType, | ||
line, | ||
codeLine, | ||
errorIndicator, | ||
message, | ||
additionalLine1, | ||
additionalLine2 | ||
) | ||
} else { | ||
undefined | ||
} | ||
} |