[ Index ]

PHP Cross Reference of MyBB 1.8.30

title

Body

[close]

/admin/jscripts/codemirror/mode/javascript/ -> javascript.js (source)

   1  // CodeMirror, copyright (c) by Marijn Haverbeke and others
   2  // Distributed under an MIT license: http://codemirror.net/LICENSE
   3  
   4  (function(mod) {
   5    if (typeof exports == "object" && typeof module == "object") // CommonJS
   6      mod(require("../../lib/codemirror"));
   7    else if (typeof define == "function" && define.amd) // AMD
   8      define(["../../lib/codemirror"], mod);
   9    else // Plain browser env
  10      mod(CodeMirror);
  11  })(function(CodeMirror) {
  12  "use strict";
  13  
  14  CodeMirror.defineMode("javascript", function(config, parserConfig) {
  15    var indentUnit = config.indentUnit;
  16    var statementIndent = parserConfig.statementIndent;
  17    var jsonldMode = parserConfig.jsonld;
  18    var jsonMode = parserConfig.json || jsonldMode;
  19    var isTS = parserConfig.typescript;
  20    var wordRE = parserConfig.wordCharacters || /[\w$\xa1-\uffff]/;
  21  
  22    // Tokenizer
  23  
  24    var keywords = function(){
  25      function kw(type) {return {type: type, style: "keyword"};}
  26      var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
  27      var operator = kw("operator"), atom = {type: "atom", style: "atom"};
  28  
  29      var jsKeywords = {
  30        "if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
  31        "return": C, "break": C, "continue": C, "new": kw("new"), "delete": C, "void": C, "throw": C, "debugger": C,
  32        "var": kw("var"), "const": kw("var"), "let": kw("var"),
  33        "function": kw("function"), "catch": kw("catch"),
  34        "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
  35        "in": operator, "typeof": operator, "instanceof": operator,
  36        "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom,
  37        "this": kw("this"), "class": kw("class"), "super": kw("atom"),
  38        "yield": C, "export": kw("export"), "import": kw("import"), "extends": C,
  39        "await": C
  40      };
  41  
  42      // Extend the 'normal' keywords with the TypeScript language extensions
  43      if (isTS) {
  44        var type = {type: "variable", style: "type"};
  45        var tsKeywords = {
  46          // object-like things
  47          "interface": kw("class"),
  48          "implements": C,
  49          "namespace": C,
  50          "module": kw("module"),
  51          "enum": kw("module"),
  52  
  53          // scope modifiers
  54          "public": kw("modifier"),
  55          "private": kw("modifier"),
  56          "protected": kw("modifier"),
  57          "abstract": kw("modifier"),
  58          "readonly": kw("modifier"),
  59  
  60          // types
  61          "string": type, "number": type, "boolean": type, "any": type
  62        };
  63  
  64        for (var attr in tsKeywords) {
  65          jsKeywords[attr] = tsKeywords[attr];
  66        }
  67      }
  68  
  69      return jsKeywords;
  70    }();
  71  
  72    var isOperatorChar = /[+\-*&%=<>!?|~^@]/;
  73    var isJsonldKeyword = /^@(context|id|value|language|type|container|list|set|reverse|index|base|vocab|graph)"/;
  74  
  75    function readRegexp(stream) {
  76      var escaped = false, next, inSet = false;
  77      while ((next = stream.next()) != null) {
  78        if (!escaped) {
  79          if (next == "/" && !inSet) return;
  80          if (next == "[") inSet = true;
  81          else if (inSet && next == "]") inSet = false;
  82        }
  83        escaped = !escaped && next == "\\";
  84      }
  85    }
  86  
  87    // Used as scratch variables to communicate multiple values without
  88    // consing up tons of objects.
  89    var type, content;
  90    function ret(tp, style, cont) {
  91      type = tp; content = cont;
  92      return style;
  93    }
  94    function tokenBase(stream, state) {
  95      var ch = stream.next();
  96      if (ch == '"' || ch == "'") {
  97        state.tokenize = tokenString(ch);
  98        return state.tokenize(stream, state);
  99      } else if (ch == "." && stream.match(/^\d+(?:[eE][+\-]?\d+)?/)) {
 100        return ret("number", "number");
 101      } else if (ch == "." && stream.match("..")) {
 102        return ret("spread", "meta");
 103      } else if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
 104        return ret(ch);
 105      } else if (ch == "=" && stream.eat(">")) {
 106        return ret("=>", "operator");
 107      } else if (ch == "0" && stream.eat(/x/i)) {
 108        stream.eatWhile(/[\da-f]/i);
 109        return ret("number", "number");
 110      } else if (ch == "0" && stream.eat(/o/i)) {
 111        stream.eatWhile(/[0-7]/i);
 112        return ret("number", "number");
 113      } else if (ch == "0" && stream.eat(/b/i)) {
 114        stream.eatWhile(/[01]/i);
 115        return ret("number", "number");
 116      } else if (/\d/.test(ch)) {
 117        stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/);
 118        return ret("number", "number");
 119      } else if (ch == "/") {
 120        if (stream.eat("*")) {
 121          state.tokenize = tokenComment;
 122          return tokenComment(stream, state);
 123        } else if (stream.eat("/")) {
 124          stream.skipToEnd();
 125          return ret("comment", "comment");
 126        } else if (expressionAllowed(stream, state, 1)) {
 127          readRegexp(stream);
 128          stream.match(/^\b(([gimyu])(?![gimyu]*\2))+\b/);
 129          return ret("regexp", "string-2");
 130        } else {
 131          stream.eatWhile(isOperatorChar);
 132          return ret("operator", "operator", stream.current());
 133        }
 134      } else if (ch == "`") {
 135        state.tokenize = tokenQuasi;
 136        return tokenQuasi(stream, state);
 137      } else if (ch == "#") {
 138        stream.skipToEnd();
 139        return ret("error", "error");
 140      } else if (isOperatorChar.test(ch)) {
 141        if (ch != ">" || !state.lexical || state.lexical.type != ">")
 142          stream.eatWhile(isOperatorChar);
 143        return ret("operator", "operator", stream.current());
 144      } else if (wordRE.test(ch)) {
 145        stream.eatWhile(wordRE);
 146        var word = stream.current()
 147        if (state.lastType != ".") {
 148          if (keywords.propertyIsEnumerable(word)) {
 149            var kw = keywords[word]
 150            return ret(kw.type, kw.style, word)
 151          }
 152          if (word == "async" && stream.match(/^\s*[\(\w]/, false))
 153            return ret("async", "keyword", word)
 154        }
 155        return ret("variable", "variable", word)
 156      }
 157    }
 158  
 159    function tokenString(quote) {
 160      return function(stream, state) {
 161        var escaped = false, next;
 162        if (jsonldMode && stream.peek() == "@" && stream.match(isJsonldKeyword)){
 163          state.tokenize = tokenBase;
 164          return ret("jsonld-keyword", "meta");
 165        }
 166        while ((next = stream.next()) != null) {
 167          if (next == quote && !escaped) break;
 168          escaped = !escaped && next == "\\";
 169        }
 170        if (!escaped) state.tokenize = tokenBase;
 171        return ret("string", "string");
 172      };
 173    }
 174  
 175    function tokenComment(stream, state) {
 176      var maybeEnd = false, ch;
 177      while (ch = stream.next()) {
 178        if (ch == "/" && maybeEnd) {
 179          state.tokenize = tokenBase;
 180          break;
 181        }
 182        maybeEnd = (ch == "*");
 183      }
 184      return ret("comment", "comment");
 185    }
 186  
 187    function tokenQuasi(stream, state) {
 188      var escaped = false, next;
 189      while ((next = stream.next()) != null) {
 190        if (!escaped && (next == "`" || next == "$" && stream.eat("{"))) {
 191          state.tokenize = tokenBase;
 192          break;
 193        }
 194        escaped = !escaped && next == "\\";
 195      }
 196      return ret("quasi", "string-2", stream.current());
 197    }
 198  
 199    var brackets = "([{}])";
 200    // This is a crude lookahead trick to try and notice that we're
 201    // parsing the argument patterns for a fat-arrow function before we
 202    // actually hit the arrow token. It only works if the arrow is on
 203    // the same line as the arguments and there's no strange noise
 204    // (comments) in between. Fallback is to only notice when we hit the
 205    // arrow, and not declare the arguments as locals for the arrow
 206    // body.
 207    function findFatArrow(stream, state) {
 208      if (state.fatArrowAt) state.fatArrowAt = null;
 209      var arrow = stream.string.indexOf("=>", stream.start);
 210      if (arrow < 0) return;
 211  
 212      if (isTS) { // Try to skip TypeScript return type declarations after the arguments
 213        var m = /:\s*(?:\w+(?:<[^>]*>|\[\])?|\{[^}]*\})\s*$/.exec(stream.string.slice(stream.start, arrow))
 214        if (m) arrow = m.index
 215      }
 216  
 217      var depth = 0, sawSomething = false;
 218      for (var pos = arrow - 1; pos >= 0; --pos) {
 219        var ch = stream.string.charAt(pos);
 220        var bracket = brackets.indexOf(ch);
 221        if (bracket >= 0 && bracket < 3) {
 222          if (!depth) { ++pos; break; }
 223          if (--depth == 0) { if (ch == "(") sawSomething = true; break; }
 224        } else if (bracket >= 3 && bracket < 6) {
 225          ++depth;
 226        } else if (wordRE.test(ch)) {
 227          sawSomething = true;
 228        } else if (/["'\/]/.test(ch)) {
 229          return;
 230        } else if (sawSomething && !depth) {
 231          ++pos;
 232          break;
 233        }
 234      }
 235      if (sawSomething && !depth) state.fatArrowAt = pos;
 236    }
 237  
 238    // Parser
 239  
 240    var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true, "jsonld-keyword": true};
 241  
 242    function JSLexical(indented, column, type, align, prev, info) {
 243      this.indented = indented;
 244      this.column = column;
 245      this.type = type;
 246      this.prev = prev;
 247      this.info = info;
 248      if (align != null) this.align = align;
 249    }
 250  
 251    function inScope(state, varname) {
 252      for (var v = state.localVars; v; v = v.next)
 253        if (v.name == varname) return true;
 254      for (var cx = state.context; cx; cx = cx.prev) {
 255        for (var v = cx.vars; v; v = v.next)
 256          if (v.name == varname) return true;
 257      }
 258    }
 259  
 260    function parseJS(state, style, type, content, stream) {
 261      var cc = state.cc;
 262      // Communicate our context to the combinators.
 263      // (Less wasteful than consing up a hundred closures on every call.)
 264      cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc; cx.style = style;
 265  
 266      if (!state.lexical.hasOwnProperty("align"))
 267        state.lexical.align = true;
 268  
 269      while(true) {
 270        var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;
 271        if (combinator(type, content)) {
 272          while(cc.length && cc[cc.length - 1].lex)
 273            cc.pop()();
 274          if (cx.marked) return cx.marked;
 275          if (type == "variable" && inScope(state, content)) return "variable-2";
 276          return style;
 277        }
 278      }
 279    }
 280  
 281    // Combinator utils
 282  
 283    var cx = {state: null, column: null, marked: null, cc: null};
 284    function pass() {
 285      for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
 286    }
 287    function cont() {
 288      pass.apply(null, arguments);
 289      return true;
 290    }
 291    function register(varname) {
 292      function inList(list) {
 293        for (var v = list; v; v = v.next)
 294          if (v.name == varname) return true;
 295        return false;
 296      }
 297      var state = cx.state;
 298      cx.marked = "def";
 299      if (state.context) {
 300        if (inList(state.localVars)) return;
 301        state.localVars = {name: varname, next: state.localVars};
 302      } else {
 303        if (inList(state.globalVars)) return;
 304        if (parserConfig.globalVars)
 305          state.globalVars = {name: varname, next: state.globalVars};
 306      }
 307    }
 308  
 309    // Combinators
 310  
 311    var defaultVars = {name: "this", next: {name: "arguments"}};
 312    function pushcontext() {
 313      cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
 314      cx.state.localVars = defaultVars;
 315    }
 316    function popcontext() {
 317      cx.state.localVars = cx.state.context.vars;
 318      cx.state.context = cx.state.context.prev;
 319    }
 320    function pushlex(type, info) {
 321      var result = function() {
 322        var state = cx.state, indent = state.indented;
 323        if (state.lexical.type == "stat") indent = state.lexical.indented;
 324        else for (var outer = state.lexical; outer && outer.type == ")" && outer.align; outer = outer.prev)
 325          indent = outer.indented;
 326        state.lexical = new JSLexical(indent, cx.stream.column(), type, null, state.lexical, info);
 327      };
 328      result.lex = true;
 329      return result;
 330    }
 331    function poplex() {
 332      var state = cx.state;
 333      if (state.lexical.prev) {
 334        if (state.lexical.type == ")")
 335          state.indented = state.lexical.indented;
 336        state.lexical = state.lexical.prev;
 337      }
 338    }
 339    poplex.lex = true;
 340  
 341    function expect(wanted) {
 342      function exp(type) {
 343        if (type == wanted) return cont();
 344        else if (wanted == ";") return pass();
 345        else return cont(exp);
 346      };
 347      return exp;
 348    }
 349  
 350    function statement(type, value) {
 351      if (type == "var") return cont(pushlex("vardef", value.length), vardef, expect(";"), poplex);
 352      if (type == "keyword a") return cont(pushlex("form"), parenExpr, statement, poplex);
 353      if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
 354      if (type == "{") return cont(pushlex("}"), block, poplex);
 355      if (type == ";") return cont();
 356      if (type == "if") {
 357        if (cx.state.lexical.info == "else" && cx.state.cc[cx.state.cc.length - 1] == poplex)
 358          cx.state.cc.pop()();
 359        return cont(pushlex("form"), parenExpr, statement, poplex, maybeelse);
 360      }
 361      if (type == "function") return cont(functiondef);
 362      if (type == "for") return cont(pushlex("form"), forspec, statement, poplex);
 363      if (type == "variable") {
 364        if (isTS && value == "type") {
 365          cx.marked = "keyword"
 366          return cont(typeexpr, expect("operator"), typeexpr, expect(";"));
 367        } if (isTS && value == "declare") {
 368          cx.marked = "keyword"
 369          return cont(statement)
 370        } else {
 371          return cont(pushlex("stat"), maybelabel);
 372        }
 373      }
 374      if (type == "switch") return cont(pushlex("form"), parenExpr, expect("{"), pushlex("}", "switch"),
 375                                        block, poplex, poplex);
 376      if (type == "case") return cont(expression, expect(":"));
 377      if (type == "default") return cont(expect(":"));
 378      if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
 379                                       statement, poplex, popcontext);
 380      if (type == "class") return cont(pushlex("form"), className, poplex);
 381      if (type == "export") return cont(pushlex("stat"), afterExport, poplex);
 382      if (type == "import") return cont(pushlex("stat"), afterImport, poplex);
 383      if (type == "module") return cont(pushlex("form"), pattern, expect("{"), pushlex("}"), block, poplex, poplex)
 384      if (type == "async") return cont(statement)
 385      if (value == "@") return cont(expression, statement)
 386      return pass(pushlex("stat"), expression, expect(";"), poplex);
 387    }
 388    function expression(type) {
 389      return expressionInner(type, false);
 390    }
 391    function expressionNoComma(type) {
 392      return expressionInner(type, true);
 393    }
 394    function parenExpr(type) {
 395      if (type != "(") return pass()
 396      return cont(pushlex(")"), expression, expect(")"), poplex)
 397    }
 398    function expressionInner(type, noComma) {
 399      if (cx.state.fatArrowAt == cx.stream.start) {
 400        var body = noComma ? arrowBodyNoComma : arrowBody;
 401        if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, expect("=>"), body, popcontext);
 402        else if (type == "variable") return pass(pushcontext, pattern, expect("=>"), body, popcontext);
 403      }
 404  
 405      var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma;
 406      if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
 407      if (type == "function") return cont(functiondef, maybeop);
 408      if (type == "class") return cont(pushlex("form"), classExpression, poplex);
 409      if (type == "keyword c" || type == "async") return cont(noComma ? maybeexpressionNoComma : maybeexpression);
 410      if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeop);
 411      if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression);
 412      if (type == "[") return cont(pushlex("]"), arrayLiteral, poplex, maybeop);
 413      if (type == "{") return contCommasep(objprop, "}", null, maybeop);
 414      if (type == "quasi") return pass(quasi, maybeop);
 415      if (type == "new") return cont(maybeTarget(noComma));
 416      return cont();
 417    }
 418    function maybeexpression(type) {
 419      if (type.match(/[;\}\)\],]/)) return pass();
 420      return pass(expression);
 421    }
 422    function maybeexpressionNoComma(type) {
 423      if (type.match(/[;\}\)\],]/)) return pass();
 424      return pass(expressionNoComma);
 425    }
 426  
 427    function maybeoperatorComma(type, value) {
 428      if (type == ",") return cont(expression);
 429      return maybeoperatorNoComma(type, value, false);
 430    }
 431    function maybeoperatorNoComma(type, value, noComma) {
 432      var me = noComma == false ? maybeoperatorComma : maybeoperatorNoComma;
 433      var expr = noComma == false ? expression : expressionNoComma;
 434      if (type == "=>") return cont(pushcontext, noComma ? arrowBodyNoComma : arrowBody, popcontext);
 435      if (type == "operator") {
 436        if (/\+\+|--/.test(value) || isTS && value == "!") return cont(me);
 437        if (value == "?") return cont(expression, expect(":"), expr);
 438        return cont(expr);
 439      }
 440      if (type == "quasi") { return pass(quasi, me); }
 441      if (type == ";") return;
 442      if (type == "(") return contCommasep(expressionNoComma, ")", "call", me);
 443      if (type == ".") return cont(property, me);
 444      if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me);
 445      if (isTS && value == "as") { cx.marked = "keyword"; return cont(typeexpr, me) }
 446      if (type == "regexp") {
 447        cx.state.lastType = cx.marked = "operator"
 448        cx.stream.backUp(cx.stream.pos - cx.stream.start - 1)
 449        return cont(expr)
 450      }
 451    }
 452    function quasi(type, value) {
 453      if (type != "quasi") return pass();
 454      if (value.slice(value.length - 2) != "${") return cont(quasi);
 455      return cont(expression, continueQuasi);
 456    }
 457    function continueQuasi(type) {
 458      if (type == "}") {
 459        cx.marked = "string-2";
 460        cx.state.tokenize = tokenQuasi;
 461        return cont(quasi);
 462      }
 463    }
 464    function arrowBody(type) {
 465      findFatArrow(cx.stream, cx.state);
 466      return pass(type == "{" ? statement : expression);
 467    }
 468    function arrowBodyNoComma(type) {
 469      findFatArrow(cx.stream, cx.state);
 470      return pass(type == "{" ? statement : expressionNoComma);
 471    }
 472    function maybeTarget(noComma) {
 473      return function(type) {
 474        if (type == ".") return cont(noComma ? targetNoComma : target);
 475        else if (type == "variable" && isTS) return cont(maybeTypeArgs, noComma ? maybeoperatorNoComma : maybeoperatorComma)
 476        else return pass(noComma ? expressionNoComma : expression);
 477      };
 478    }
 479    function target(_, value) {
 480      if (value == "target") { cx.marked = "keyword"; return cont(maybeoperatorComma); }
 481    }
 482    function targetNoComma(_, value) {
 483      if (value == "target") { cx.marked = "keyword"; return cont(maybeoperatorNoComma); }
 484    }
 485    function maybelabel(type) {
 486      if (type == ":") return cont(poplex, statement);
 487      return pass(maybeoperatorComma, expect(";"), poplex);
 488    }
 489    function property(type) {
 490      if (type == "variable") {cx.marked = "property"; return cont();}
 491    }
 492    function objprop(type, value) {
 493      if (type == "async") {
 494        cx.marked = "property";
 495        return cont(objprop);
 496      } else if (type == "variable" || cx.style == "keyword") {
 497        cx.marked = "property";
 498        if (value == "get" || value == "set") return cont(getterSetter);
 499        var m // Work around fat-arrow-detection complication for detecting typescript typed arrow params
 500        if (isTS && cx.state.fatArrowAt == cx.stream.start && (m = cx.stream.match(/^\s*:\s*/, false)))
 501          cx.state.fatArrowAt = cx.stream.pos + m[0].length
 502        return cont(afterprop);
 503      } else if (type == "number" || type == "string") {
 504        cx.marked = jsonldMode ? "property" : (cx.style + " property");
 505        return cont(afterprop);
 506      } else if (type == "jsonld-keyword") {
 507        return cont(afterprop);
 508      } else if (type == "modifier") {
 509        return cont(objprop)
 510      } else if (type == "[") {
 511        return cont(expression, expect("]"), afterprop);
 512      } else if (type == "spread") {
 513        return cont(expression, afterprop);
 514      } else if (type == ":") {
 515        return pass(afterprop)
 516      }
 517    }
 518    function getterSetter(type) {
 519      if (type != "variable") return pass(afterprop);
 520      cx.marked = "property";
 521      return cont(functiondef);
 522    }
 523    function afterprop(type) {
 524      if (type == ":") return cont(expressionNoComma);
 525      if (type == "(") return pass(functiondef);
 526    }
 527    function commasep(what, end, sep) {
 528      function proceed(type, value) {
 529        if (sep ? sep.indexOf(type) > -1 : type == ",") {
 530          var lex = cx.state.lexical;
 531          if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;
 532          return cont(function(type, value) {
 533            if (type == end || value == end) return pass()
 534            return pass(what)
 535          }, proceed);
 536        }
 537        if (type == end || value == end) return cont();
 538        return cont(expect(end));
 539      }
 540      return function(type, value) {
 541        if (type == end || value == end) return cont();
 542        return pass(what, proceed);
 543      };
 544    }
 545    function contCommasep(what, end, info) {
 546      for (var i = 3; i < arguments.length; i++)
 547        cx.cc.push(arguments[i]);
 548      return cont(pushlex(end, info), commasep(what, end), poplex);
 549    }
 550    function block(type) {
 551      if (type == "}") return cont();
 552      return pass(statement, block);
 553    }
 554    function maybetype(type, value) {
 555      if (isTS) {
 556        if (type == ":") return cont(typeexpr);
 557        if (value == "?") return cont(maybetype);
 558      }
 559    }
 560    function typeexpr(type, value) {
 561      if (type == "variable") {
 562        if (value == "keyof") {
 563          cx.marked = "keyword"
 564          return cont(typeexpr)
 565        } else {
 566          cx.marked = "type"
 567          return cont(afterType)
 568        }
 569      }
 570      if (type == "string" || type == "number" || type == "atom") return cont(afterType);
 571      if (type == "[") return cont(pushlex("]"), commasep(typeexpr, "]", ","), poplex, afterType)
 572      if (type == "{") return cont(pushlex("}"), commasep(typeprop, "}", ",;"), poplex, afterType)
 573      if (type == "(") return cont(commasep(typearg, ")"), maybeReturnType)
 574    }
 575    function maybeReturnType(type) {
 576      if (type == "=>") return cont(typeexpr)
 577    }
 578    function typeprop(type, value) {
 579      if (type == "variable" || cx.style == "keyword") {
 580        cx.marked = "property"
 581        return cont(typeprop)
 582      } else if (value == "?") {
 583        return cont(typeprop)
 584      } else if (type == ":") {
 585        return cont(typeexpr)
 586      } else if (type == "[") {
 587        return cont(expression, maybetype, expect("]"), typeprop)
 588      }
 589    }
 590    function typearg(type) {
 591      if (type == "variable") return cont(typearg)
 592      else if (type == ":") return cont(typeexpr)
 593    }
 594    function afterType(type, value) {
 595      if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType)
 596      if (value == "|" || type == ".") return cont(typeexpr)
 597      if (type == "[") return cont(expect("]"), afterType)
 598      if (value == "extends") return cont(typeexpr)
 599    }
 600    function maybeTypeArgs(_, value) {
 601      if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType)
 602    }
 603    function vardef() {
 604      return pass(pattern, maybetype, maybeAssign, vardefCont);
 605    }
 606    function pattern(type, value) {
 607      if (type == "modifier") return cont(pattern)
 608      if (type == "variable") { register(value); return cont(); }
 609      if (type == "spread") return cont(pattern);
 610      if (type == "[") return contCommasep(pattern, "]");
 611      if (type == "{") return contCommasep(proppattern, "}");
 612    }
 613    function proppattern(type, value) {
 614      if (type == "variable" && !cx.stream.match(/^\s*:/, false)) {
 615        register(value);
 616        return cont(maybeAssign);
 617      }
 618      if (type == "variable") cx.marked = "property";
 619      if (type == "spread") return cont(pattern);
 620      if (type == "}") return pass();
 621      return cont(expect(":"), pattern, maybeAssign);
 622    }
 623    function maybeAssign(_type, value) {
 624      if (value == "=") return cont(expressionNoComma);
 625    }
 626    function vardefCont(type) {
 627      if (type == ",") return cont(vardef);
 628    }
 629    function maybeelse(type, value) {
 630      if (type == "keyword b" && value == "else") return cont(pushlex("form", "else"), statement, poplex);
 631    }
 632    function forspec(type) {
 633      if (type == "(") return cont(pushlex(")"), forspec1, expect(")"), poplex);
 634    }
 635    function forspec1(type) {
 636      if (type == "var") return cont(vardef, expect(";"), forspec2);
 637      if (type == ";") return cont(forspec2);
 638      if (type == "variable") return cont(formaybeinof);
 639      return pass(expression, expect(";"), forspec2);
 640    }
 641    function formaybeinof(_type, value) {
 642      if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }
 643      return cont(maybeoperatorComma, forspec2);
 644    }
 645    function forspec2(type, value) {
 646      if (type == ";") return cont(forspec3);
 647      if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }
 648      return pass(expression, expect(";"), forspec3);
 649    }
 650    function forspec3(type) {
 651      if (type != ")") cont(expression);
 652    }
 653    function functiondef(type, value) {
 654      if (value == "*") {cx.marked = "keyword"; return cont(functiondef);}
 655      if (type == "variable") {register(value); return cont(functiondef);}
 656      if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, maybetype, statement, popcontext);
 657      if (isTS && value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, functiondef)
 658    }
 659    function funarg(type, value) {
 660      if (value == "@") cont(expression, funarg)
 661      if (type == "spread" || type == "modifier") return cont(funarg);
 662      return pass(pattern, maybetype, maybeAssign);
 663    }
 664    function classExpression(type, value) {
 665      // Class expressions may have an optional name.
 666      if (type == "variable") return className(type, value);
 667      return classNameAfter(type, value);
 668    }
 669    function className(type, value) {
 670      if (type == "variable") {register(value); return cont(classNameAfter);}
 671    }
 672    function classNameAfter(type, value) {
 673      if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, classNameAfter)
 674      if (value == "extends" || value == "implements" || (isTS && type == ","))
 675        return cont(isTS ? typeexpr : expression, classNameAfter);
 676      if (type == "{") return cont(pushlex("}"), classBody, poplex);
 677    }
 678    function classBody(type, value) {
 679      if (type == "modifier" || type == "async" ||
 680          (type == "variable" &&
 681           (value == "static" || value == "get" || value == "set") &&
 682           cx.stream.match(/^\s+[\w$\xa1-\uffff]/, false))) {
 683        cx.marked = "keyword";
 684        return cont(classBody);
 685      }
 686      if (type == "variable" || cx.style == "keyword") {
 687        cx.marked = "property";
 688        return cont(isTS ? classfield : functiondef, classBody);
 689      }
 690      if (type == "[")
 691        return cont(expression, expect("]"), isTS ? classfield : functiondef, classBody)
 692      if (value == "*") {
 693        cx.marked = "keyword";
 694        return cont(classBody);
 695      }
 696      if (type == ";") return cont(classBody);
 697      if (type == "}") return cont();
 698      if (value == "@") return cont(expression, classBody)
 699    }
 700    function classfield(type, value) {
 701      if (value == "?") return cont(classfield)
 702      if (type == ":") return cont(typeexpr, maybeAssign)
 703      if (value == "=") return cont(expressionNoComma)
 704      return pass(functiondef)
 705    }
 706    function afterExport(type, value) {
 707      if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); }
 708      if (value == "default") { cx.marked = "keyword"; return cont(expression, expect(";")); }
 709      if (type == "{") return cont(commasep(exportField, "}"), maybeFrom, expect(";"));
 710      return pass(statement);
 711    }
 712    function exportField(type, value) {
 713      if (value == "as") { cx.marked = "keyword"; return cont(expect("variable")); }
 714      if (type == "variable") return pass(expressionNoComma, exportField);
 715    }
 716    function afterImport(type) {
 717      if (type == "string") return cont();
 718      return pass(importSpec, maybeMoreImports, maybeFrom);
 719    }
 720    function importSpec(type, value) {
 721      if (type == "{") return contCommasep(importSpec, "}");
 722      if (type == "variable") register(value);
 723      if (value == "*") cx.marked = "keyword";
 724      return cont(maybeAs);
 725    }
 726    function maybeMoreImports(type) {
 727      if (type == ",") return cont(importSpec, maybeMoreImports)
 728    }
 729    function maybeAs(_type, value) {
 730      if (value == "as") { cx.marked = "keyword"; return cont(importSpec); }
 731    }
 732    function maybeFrom(_type, value) {
 733      if (value == "from") { cx.marked = "keyword"; return cont(expression); }
 734    }
 735    function arrayLiteral(type) {
 736      if (type == "]") return cont();
 737      return pass(commasep(expressionNoComma, "]"));
 738    }
 739  
 740    function isContinuedStatement(state, textAfter) {
 741      return state.lastType == "operator" || state.lastType == "," ||
 742        isOperatorChar.test(textAfter.charAt(0)) ||
 743        /[,.]/.test(textAfter.charAt(0));
 744    }
 745  
 746    function expressionAllowed(stream, state, backUp) {
 747      return state.tokenize == tokenBase &&
 748        /^(?:operator|sof|keyword [bc]|case|new|export|default|spread|[\[{}\(,;:]|=>)$/.test(state.lastType) ||
 749        (state.lastType == "quasi" && /\{\s*$/.test(stream.string.slice(0, stream.pos - (backUp || 0))))
 750    }
 751  
 752    // Interface
 753  
 754    return {
 755      startState: function(basecolumn) {
 756        var state = {
 757          tokenize: tokenBase,
 758          lastType: "sof",
 759          cc: [],
 760          lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
 761          localVars: parserConfig.localVars,
 762          context: parserConfig.localVars && {vars: parserConfig.localVars},
 763          indented: basecolumn || 0
 764        };
 765        if (parserConfig.globalVars && typeof parserConfig.globalVars == "object")
 766          state.globalVars = parserConfig.globalVars;
 767        return state;
 768      },
 769  
 770      token: function(stream, state) {
 771        if (stream.sol()) {
 772          if (!state.lexical.hasOwnProperty("align"))
 773            state.lexical.align = false;
 774          state.indented = stream.indentation();
 775          findFatArrow(stream, state);
 776        }
 777        if (state.tokenize != tokenComment && stream.eatSpace()) return null;
 778        var style = state.tokenize(stream, state);
 779        if (type == "comment") return style;
 780        state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type;
 781        return parseJS(state, style, type, content, stream);
 782      },
 783  
 784      indent: function(state, textAfter) {
 785        if (state.tokenize == tokenComment) return CodeMirror.Pass;
 786        if (state.tokenize != tokenBase) return 0;
 787        var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical, top
 788        // Kludge to prevent 'maybelse' from blocking lexical scope pops
 789        if (!/^\s*else\b/.test(textAfter)) for (var i = state.cc.length - 1; i >= 0; --i) {
 790          var c = state.cc[i];
 791          if (c == poplex) lexical = lexical.prev;
 792          else if (c != maybeelse) break;
 793        }
 794        while ((lexical.type == "stat" || lexical.type == "form") &&
 795               (firstChar == "}" || ((top = state.cc[state.cc.length - 1]) &&
 796                                     (top == maybeoperatorComma || top == maybeoperatorNoComma) &&
 797                                     !/^[,\.=+\-*:?[\(]/.test(textAfter))))
 798          lexical = lexical.prev;
 799        if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat")
 800          lexical = lexical.prev;
 801        var type = lexical.type, closing = firstChar == type;
 802  
 803        if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info + 1 : 0);
 804        else if (type == "form" && firstChar == "{") return lexical.indented;
 805        else if (type == "form") return lexical.indented + indentUnit;
 806        else if (type == "stat")
 807          return lexical.indented + (isContinuedStatement(state, textAfter) ? statementIndent || indentUnit : 0);
 808        else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false)
 809          return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
 810        else if (lexical.align) return lexical.column + (closing ? 0 : 1);
 811        else return lexical.indented + (closing ? 0 : indentUnit);
 812      },
 813  
 814      electricInput: /^\s*(?:case .*?:|default:|\{|\})$/,
 815      blockCommentStart: jsonMode ? null : "/*",
 816      blockCommentEnd: jsonMode ? null : "*/",
 817      lineComment: jsonMode ? null : "//",
 818      fold: "brace",
 819      closeBrackets: "()[]{}''\"\"``",
 820  
 821      helperType: jsonMode ? "json" : "javascript",
 822      jsonldMode: jsonldMode,
 823      jsonMode: jsonMode,
 824  
 825      expressionAllowed: expressionAllowed,
 826  
 827      skipExpression: function(state) {
 828        var top = state.cc[state.cc.length - 1]
 829        if (top == expression || top == expressionNoComma) state.cc.pop()
 830      }
 831    };
 832  });
 833  
 834  CodeMirror.registerHelper("wordChars", "javascript", /[\w$]/);
 835  
 836  CodeMirror.defineMIME("text/javascript", "javascript");
 837  CodeMirror.defineMIME("text/ecmascript", "javascript");
 838  CodeMirror.defineMIME("application/javascript", "javascript");
 839  CodeMirror.defineMIME("application/x-javascript", "javascript");
 840  CodeMirror.defineMIME("application/ecmascript", "javascript");
 841  CodeMirror.defineMIME("application/json", {name: "javascript", json: true});
 842  CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true});
 843  CodeMirror.defineMIME("application/ld+json", {name: "javascript", jsonld: true});
 844  CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });
 845  CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });
 846  
 847  });


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref