Browse Source

`assert`高亮

mxd 4 years ago
parent
commit
fd5dd49a74

+ 1 - 1
magic-editor/src/console/src/scripts/editor/high-light.js

@@ -19,7 +19,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)[\\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)[\\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 - 0
magic-editor/src/console/src/scripts/parsing/ast.js

@@ -237,6 +237,18 @@ class Exit extends Node {
     }
 }
 
+class Assert extends Node {
+    constructor(span, condition, values) {
+        super(span)
+        this.condition = condition
+        this.values = values
+    }
+
+    expressions() {
+        return [this.condition, ...this.values]
+    }
+}
+
 class NewStatement extends Node {
     constructor(span, identifier, parameters) {
         super(span)
@@ -582,6 +594,7 @@ export {
     Node,
     Expression,
     Literal,
+    Assert,
     MethodCall,
     FunctionCall,
     MemberAccess,

+ 21 - 1
magic-editor/src/console/src/scripts/parsing/parser.js

@@ -1,6 +1,7 @@
 import {ParseException, Span, TokenType} from './index.js'
 import JavaClass from '../editor/java-class.js'
 import {
+    Assert,
     AsyncCall,
     BinaryOperation,
     Break,
@@ -35,7 +36,7 @@ import {
     LanguageExpression
 } from './ast.js'
 
-export const keywords = ["import", "as", "var", "return", "break", "continue", "if", "for", "in", "new", "true", "false", "null", "else", "try", "catch", "finally", "async", "while", "exit", "and", "or"];
+export const keywords = ["import", "as", "var", "return", "break", "continue", "if", "for", "in", "new", "true", "false", "null", "else", "try", "catch", "finally", "async", "while", "exit", "and", "or", /*"assert"*/];
 export const linqKeywords = ["from", "join", "left", "group", "by", "as", "having", "and", "or", "in", "where", "on"];
 const binaryOperatorPrecedence = [
     [TokenType.Assignment],
@@ -111,6 +112,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("assert", false)) {
+            result = this.parseAssert();
         } else {
             result = this.parseExpression(expectRightCurly);
         }
@@ -154,6 +157,23 @@ export class Parser {
         return new Exit(new Span(opening, this.stream.getPrev().getSpan()), expressionList);
     }
 
+    parseAssert() {
+        let index = this.stream.makeIndex()
+        try {
+            let opening = this.stream.expect("assert").getSpan();
+            let condition = this.parseExpression();
+            this.stream.expect(TokenType.Colon);
+            let expressionList = [];
+            do {
+                expressionList.push(this.parseExpression());
+            } while (this.stream.match(TokenType.Comma, true));
+            return new Assert(new Span(opening, this.stream.getPrev().getSpan()), condition, expressionList);
+        } catch (e) {
+            this.stream.resetIndex(index)
+            return this.parseExpression();
+        }
+    }
+
     parseImport() {
         let opening = this.stream.expect("import").getSpan();
         if (this.stream.hasMore()) {