From 424fea20c3cccbc48a01061e4ebc8e501f669e3f Mon Sep 17 00:00:00 2001 From: mxd <838425805@qq.com> Date: Fri, 13 Aug 2021 22:09:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81throw=E8=AF=AD=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/console/src/scripts/editor/high-light.js | 2 +- .../src/console/src/scripts/parsing/ast.js | 14 +++++++++++++- .../src/console/src/scripts/parsing/parser.js | 13 +++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/magic-editor/src/console/src/scripts/editor/high-light.js b/magic-editor/src/console/src/scripts/editor/high-light.js index 50ec0045..393ff9ff 100644 --- a/magic-editor/src/console/src/scripts/editor/high-light.js +++ b/magic-editor/src/console/src/scripts/editor/high-light.js @@ -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" } diff --git a/magic-editor/src/console/src/scripts/parsing/ast.js b/magic-editor/src/console/src/scripts/parsing/ast.js index deef90cd..622b3751 100644 --- a/magic-editor/src/console/src/scripts/parsing/ast.js +++ b/magic-editor/src/console/src/scripts/parsing/ast.js @@ -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 } \ No newline at end of file diff --git a/magic-editor/src/console/src/scripts/parsing/parser.js b/magic-editor/src/console/src/scripts/parsing/parser.js index 4f2e0d20..70f3f716 100644 --- a/magic-editor/src/console/src/scripts/parsing/parser.js +++ b/magic-editor/src/console/src/scripts/parsing/parser.js @@ -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 = [];