Selaa lähdekoodia

支持throw语法

mxd 3 vuotta sitten
vanhempi
sitoutus
424fea20c3

+ 1 - 1
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"
                 }

+ 13 - 1
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
 }

+ 11 - 2
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 = [];