-
Notifications
You must be signed in to change notification settings - Fork 0
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 #22 from Solo-steven/steven/issu-10/enhance-web-pa…
…rser [Feat]: Refactor Parser structure with different scope manager
- Loading branch information
Showing
1,667 changed files
with
10,414 additions
and
1,406 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
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,54 @@ | ||
/** | ||
* Error handler to record a recoverable error and print it | ||
* as a format message | ||
*/ | ||
import { SourcePosition } from "web-infra-common"; | ||
import { SyntaxError, SyntaxErrorHandler } from "./type"; | ||
|
||
export function createErrorHandler(code: string): SyntaxErrorHandler { | ||
const errors: Array<SyntaxError> = []; | ||
function pushSyntaxErrors(...syntaxErrors: Array<SyntaxError>) { | ||
errors.push(...syntaxErrors); | ||
} | ||
function haveError() { | ||
return errors.length > 0; | ||
} | ||
function formatErrorString() { | ||
let formatString = ""; | ||
for (const err of errors) { | ||
formatString += printError(err); | ||
} | ||
return formatString; | ||
} | ||
function printError(error: SyntaxError) { | ||
const [lineStart, lineEnd] = getLineStartAndEnd(error.position); | ||
const arrayLen = error.position.index - lineStart - 1 >= 0 ? error.position.index - lineStart - 1 : 0; | ||
return `[SyntaxError]: ${error.message} (${error.position.row},${error.position.col})\n${error.position.row}|${code.slice(lineStart, lineEnd)}\n |${printSpace(arrayLen)}^\n`; | ||
} | ||
function getLineStartAndEnd(position: SourcePosition) { | ||
let lineStart = 0; | ||
for (lineStart = position.index - 1; lineStart > 0; --lineStart) { | ||
if (code[lineStart] === "\n") { | ||
break; | ||
} | ||
} | ||
let lineEnd = 0; | ||
for (lineEnd = position.index + 1; lineEnd < code.length; ++lineEnd) { | ||
if (code[lineEnd] === "\n") { | ||
break; | ||
} | ||
} | ||
return [lineStart === 0 ? lineStart : lineStart + 1, lineEnd]; | ||
} | ||
function printSpace(len: number): string { | ||
return new Array(len) | ||
.fill(0) | ||
.map(() => " ") | ||
.join(""); | ||
} | ||
return { | ||
pushSyntaxErrors, | ||
haveError, | ||
formatErrorString, | ||
}; | ||
} |
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,11 @@ | ||
import { SourcePosition } from "web-infra-common"; | ||
export interface SyntaxError { | ||
message: string; | ||
position: SourcePosition; | ||
} | ||
|
||
export type SyntaxErrorHandler = { | ||
pushSyntaxErrors: (...syntaxErrors: Array<SyntaxError>) => void; | ||
haveError: () => boolean; | ||
formatErrorString: () => string; | ||
}; |
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
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
Oops, something went wrong.