支持throw语法

This commit is contained in:
mxd
2021-08-13 22:09:41 +08:00
parent 8718446141
commit 424fea20c3
3 changed files with 25 additions and 4 deletions

View File

@@ -17,7 +17,7 @@ export const HighLightOptions = {
[/[a-zA-Z_$][\w$]*[\s]?/, {
cases: {
'@builtinFunctions': 'predefined',
"~(new|var|if|else|for|in|return|import|break|continue|as|null|true|false|try|catch|finally|async|while|exit|asc|desc|ASC|DESC|assert|let|const)[\\s]?": {token: "keywords"},
"~(new|var|if|else|for|in|return|import|break|continue|as|null|true|false|try|catch|finally|async|while|exit|asc|desc|ASC|DESC|assert|let|const|throw)[\\s]?": {token: "keywords"},
"~(select|from|left|join|on|and|or|order|by|where|group|having|SELECT|FROM|LEFT|JOIN|ON|AND|OR|ORDER|BY|WHERE|GROUP|HAVING)[\\s]{1}": {token: "keywords"},
"@default": "identifier"
}

View File

@@ -253,6 +253,17 @@ class Exit extends Node {
}
}
class Throw extends Node {
constructor(span, value) {
super(span)
this.value = value
}
expressions() {
return [this.value]
}
}
class Assert extends Node {
constructor(span, condition, values) {
super(span)
@@ -646,5 +657,6 @@ export {
LinqSelect,
WholeLiteral,
ClassConverter,
LanguageExpression
LanguageExpression,
Throw
}

View File

@@ -34,10 +34,11 @@ import {
VarDefine,
VariableAccess,
WhileStatement,
WholeLiteral
WholeLiteral,
Throw
} from './ast.js'
export const keywords = ["import", "as", "var", "let", "const", "return", "break", "continue", "if", "for", "in", "new", "true", "false", "null", "else", "try", "catch", "finally", "async", "while", "exit", "and", "or", /*"assert"*/];
export const keywords = ["import", "as", "var", "let", "const", "return", "break", "continue", "if", "for", "in", "new", "true", "false", "null", "else", "try", "catch", "finally", "async", "while", "exit", "and", "or", "throw"/*"assert"*/];
export const linqKeywords = ["from", "join", "left", "group", "by", "as", "having", "and", "or", "in", "where", "on"];
const binaryOperatorPrecedence = [
[TokenType.Assignment],
@@ -121,6 +122,8 @@ export class Parser {
result = new Break(this.stream.consume().getSpan());
} else if (this.stream.match("exit", false)) {
result = this.parseExit();
} else if (this.stream.match("throw", false)) {
result = this.parseThrow();
} else if (this.stream.match("assert", false)) {
result = this.parseAssert();
} else {
@@ -165,6 +168,12 @@ export class Parser {
}
}
parseThrow() {
let opening = this.stream.consume().getSpan();
let expression = this.parseExpression();
return new Throw(new Span(opening, this.stream.getPrev().getSpan()), expression);
}
parseExit() {
let opening = this.stream.expect("exit").getSpan();
let expressionList = [];