Вход Регистрация
Файл: mg-core/script/codemirror/mode/javascript/javascript.js
Строк: 764
<?php
// TODO actually recognize syntax of TypeScript constructs

CodeMirror.defineMode("javascript", function(configparserConfig) {
  var 
indentUnit config.indentUnit;
  var 
statementIndent parserConfig.statementIndent;
  var 
jsonMode parserConfig.json;
  var 
isTS parserConfig.typescript;

  
// Tokenizer

  
var keywords = function(){
    function 
kw(type) {return {typetypestyle"keyword"};}
    var 
kw("keyword a"), kw("keyword b"), kw("keyword c");
    var 
operator kw("operator"), atom = {type"atom"style"atom"};

    var 
jsKeywords = {
      
"if"kw("if"), "while"A"with"A"else"B"do"B"try"B"finally"B,
      
"return"C"break"C"continue"C"new"C"delete"C"throw"C,
      
"var"kw("var"), "const"kw("var"), "let"kw("var"),
      
"function"kw("function"), "catch"kw("catch"),
      
"for"kw("for"), "switch"kw("switch"), "case"kw("case"), "default"kw("default"),
      
"in"operator"typeof"operator"instanceof"operator,
      
"true"atom"false"atom"null"atom"undefined"atom"NaN"atom"Infinity"atom,
      
"this"kw("this")
    };

    
// Extend the 'normal' keywords with the TypeScript language extensions
    
if (isTS) {
      var 
type = {type"variable"style"variable-3"};
      var 
tsKeywords = {
        
// object-like things
        
"interface"kw("interface"),
        
"class"kw("class"),
        
"extends"kw("extends"),
        
"constructor"kw("constructor"),

        
// scope modifiers
        
"public"kw("public"),
        
"private"kw("private"),
        
"protected"kw("protected"),
        
"static"kw("static"),

        
"super"kw("super"),

        
// types
        
"string"type"number"type"bool"type"any"type
      
};

      for (var 
attr in tsKeywords) {
        
jsKeywords[attr] = tsKeywords[attr];
      }
    }

    return 
jsKeywords;
  }();

  var 
isOperatorChar = /[+-*&%=<>!?|~^]/;

  function 
chain(streamstatef) {
    
state.tokenize f;
    return 
f(streamstate);
  }

  function 
nextUntilUnescaped(streamend) {
    var 
escaped falsenext;
    while ((
next stream.next()) != null) {
      if (
next == end && !escaped)
        return 
false;
      
escaped = !escaped && next == "\";
    }
    return escaped;
  }

  // Used as scratch variables to communicate multiple values without
  // consing up tons of objects.
  var type, content;
  function ret(tp, style, cont) {
    type = tp; content = cont;
    return style;
  }
  function jsTokenBase(stream, state) {
    var ch = stream.next();
    if (ch == '"' || ch == "'")
      return chain(stream, state, jsTokenString(ch));
    else if (ch == "
." && stream.match(/^d+(?:[eE][+-]?d+)?/))
      return ret("
number", "number");
    else if (/[[]{}(),;:.]/.test(ch))
      return ret(ch);
    else if (ch == "
0" && stream.eat(/x/i)) {
      stream.eatWhile(/[da-f]/i);
      return ret("
number", "number");
    }
    else if (/d/.test(ch)) {
      stream.match(/^d*(?:.d*)?(?:[eE][+-]?d+)?/);
      return ret("
number", "number");
    }
    else if (ch == "
/") {
      if (stream.eat("
*")) {
        return chain(stream, state, jsTokenComment);
      }
      else if (stream.eat("
/")) {
        stream.skipToEnd();
        return ret("
comment", "comment");
      }
      else if (state.lastType == "
operator" || state.lastType == "keyword c" ||
               /^[[{}(,;:]$/.test(state.lastType)) {
        nextUntilUnescaped(stream, "
/");
        stream.eatWhile(/[gimy]/); // 'y' is "
sticky" option in Mozilla
        return ret("
regexp", "string-2");
      }
      else {
        stream.eatWhile(isOperatorChar);
        return ret("
operator", null, stream.current());
      }
    }
    else if (ch == "
#") {
      
stream.skipToEnd();
      return 
ret("error""error");
    }
    else if (
isOperatorChar.test(ch)) {
      
stream.eatWhile(isOperatorChar);
      return 
ret("operator"nullstream.current());
    }
    else {
      
stream.eatWhile(/[w$_]/);
      var 
word stream.current(), known keywords.propertyIsEnumerable(word) && keywords[word];
      return (
known && state.lastType != ".") ? ret(known.typeknown.styleword) :
                     
ret("variable""variable"word);
    }
  }

  function 
jsTokenString(quote) {
    return function(
streamstate) {
      if (!
nextUntilUnescaped(streamquote))
        
state.tokenize jsTokenBase;
      return 
ret("string""string");
    };
  }

  function 
jsTokenComment(streamstate) {
    var 
maybeEnd falsech;
    while (
ch stream.next()) {
      if (
ch == "/" && maybeEnd) {
        
state.tokenize jsTokenBase;
        break;
      }
      
maybeEnd = (ch == "*");
    }
    return 
ret("comment""comment");
  }

  
// Parser

  
var atomicTypes = {"atom"true"number"true"variable"true"string"true"regexp"true"this"true};

  function 
JSLexical(indentedcolumntypealignprevinfo) {
    
this.indented indented;
    
this.column column;
    
this.type type;
    
this.prev prev;
    
this.info info;
    if (
align != nullthis.align align;
  }

  function 
inScope(statevarname) {
    for (var 
state.localVarsvv.next)
      if (
v.name == varname) return true;
  }

  function 
parseJS(statestyletypecontentstream) {
    var 
cc state.cc;
    
// Communicate our context to the combinators.
    // (Less wasteful than consing up a hundred closures on every call.)
    
cx.state statecx.stream streamcx.marked nullcx.cc cc;

    if (!
state.lexical.hasOwnProperty("align"))
      
state.lexical.align true;

    while(
true) {
      var 
combinator cc.length cc.pop() : jsonMode expression statement;
      if (
combinator(typecontent)) {
        while(
cc.length && cc[cc.length 1].lex)
          
cc.pop()();
        if (
cx.marked) return cx.marked;
        if (
type == "variable" && inScope(statecontent)) return "variable-2";
        return 
style;
      }
    }
  }

  
// Combinator utils

  
var cx = {statenullcolumnnullmarkednullccnull};
  function 
pass() {
    for (var 
arguments.length 1>= 0i--) cx.cc.push(arguments[i]);
  }
  function 
cont() {
    
pass.apply(nullarguments);
    return 
true;
  }
  function 
register(varname) {
    function 
inList(list) {
      for (var 
= list; vv.next)
        if (
v.name == varname) return true;
      return 
false;
    }
    var 
state cx.state;
    if (
state.context) {
      
cx.marked "def";
      if (
inList(state.localVars)) return;
      
state.localVars = {namevarnamenextstate.localVars};
    } else {
      if (
inList(state.globalVars)) return;
      
state.globalVars = {namevarnamenextstate.globalVars};
    }
  }

  
// Combinators

  
var defaultVars = {name"this"next: {name"arguments"}};
  function 
pushcontext() {
    
cx.state.context = {prevcx.state.contextvarscx.state.localVars};
    
cx.state.localVars defaultVars;
  }
  function 
popcontext() {
    
cx.state.localVars cx.state.context.vars;
    
cx.state.context cx.state.context.prev;
  }
  function 
pushlex(typeinfo) {
    var 
result = function() {
      var 
state cx.stateindent state.indented;
      if (
state.lexical.type == "stat"indent state.lexical.indented;
      
state.lexical = new JSLexical(indentcx.stream.column(), typenullstate.lexicalinfo);
    };
    
result.lex true;
    return 
result;
  }
  function 
poplex() {
    var 
state cx.state;
    if (
state.lexical.prev) {
      if (
state.lexical.type == ")")
        
state.indented state.lexical.indented;
      
state.lexical state.lexical.prev;
    }
  }
  
poplex.lex true;

  function 
expect(wanted) {
    return function(
type) {
      if (
type == wanted) return cont();
      else if (
wanted == ";") return pass();
      else return 
cont(arguments.callee);
    };
  }

  function 
statement(type) {
    if (
type == "var") return cont(pushlex("vardef"), vardef1expect(";"), poplex);
    if (
type == "keyword a") return cont(pushlex("form"), expressionstatementpoplex);
    if (
type == "keyword b") return cont(pushlex("form"), statementpoplex);
    if (
type == "{") return cont(pushlex("}"), blockpoplex);
    if (
type == ";") return cont();
    if (
type == "if") return cont(pushlex("form"), expressionstatementpoplexmaybeelse);
    if (
type == "function") return cont(functiondef);
    if (
type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1expect(")"),
                                   
poplexstatementpoplex);
    if (
type == "variable") return cont(pushlex("stat"), maybelabel);
    if (
type == "switch") return cont(pushlex("form"), expressionpushlex("}""switch"), expect("{"),
                                      
blockpoplexpoplex);
    if (
type == "case") return cont(expressionexpect(":"));
    if (
type == "default") return cont(expect(":"));
    if (
type == "catch") return cont(pushlex("form"), pushcontextexpect("("), funargexpect(")"),
                                     
statementpoplexpopcontext);
    return 
pass(pushlex("stat"), expressionexpect(";"), poplex);
  }
  function 
expression(type) {
    return 
expressionInner(typefalse);
  }
  function 
expressionNoComma(type) {
    return 
expressionInner(typetrue);
  }
  function 
expressionInner(typenoComma) {
    var 
maybeop noComma maybeoperatorNoComma maybeoperatorComma;
    if (
atomicTypes.hasOwnProperty(type)) return cont(maybeop);
    if (
type == "function") return cont(functiondef);
    if (
type == "keyword c") return cont(noComma maybeexpressionNoComma maybeexpression);
    if (
type == "(") return cont(pushlex(")"), maybeexpressionexpect(")"), poplexmaybeop);
    if (
type == "operator") return cont(noComma expressionNoComma expression);
    if (
type == "[") return cont(pushlex("]"), commasep(expressionNoComma"]"), poplexmaybeop);
    if (
type == "{") return cont(pushlex("}"), commasep(objprop"}"), poplexmaybeop);
    return 
cont();
  }
  function 
maybeexpression(type) {
    if (
type.match(/[;})],]/)) return pass();
    return 
pass(expression);
  }
  function 
maybeexpressionNoComma(type) {
    if (
type.match(/[;})],]/)) return pass();
    return 
pass(expressionNoComma);
  }

  function 
maybeoperatorComma(typevalue) {
    if (
type == ",") return cont(expression);
    return 
maybeoperatorNoComma(typevaluefalse);
  }
  function 
maybeoperatorNoComma(typevaluenoComma) {
    var 
me noComma == false maybeoperatorComma maybeoperatorNoComma;
    var 
expr noComma == false expression expressionNoComma;
    if (
type == "operator") {
      if (/++|--/.
test(value)) return cont(me);
      if (
value == "?") return cont(expressionexpect(":"), expr);
      return 
cont(expr);
    }
    if (
type == ";") return;
    if (
type == "(") return cont(pushlex(")""call"), commasep(expressionNoComma")"), poplexme);
    if (
type == ".") return cont(propertyme);
    if (
type == "[") return cont(pushlex("]"), maybeexpressionexpect("]"), poplexme);
  }
  function 
maybelabel(type) {
    if (
type == ":") return cont(poplexstatement);
    return 
pass(maybeoperatorCommaexpect(";"), poplex);
  }
  function 
property(type) {
    if (
type == "variable") {cx.marked "property"; return cont();}
  }
  function 
objprop(typevalue) {
    if (
type == "variable") {
      
cx.marked "property";
      if (
value == "get" || value == "set") return cont(getterSetter);
    } else if (
type == "number" || type == "string") {
      
cx.marked type " property";
    }
    if (
atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expressionNoComma);
  }
  function 
getterSetter(type) {
    if (
type == ":") return cont(expression);
    if (
type != "variable") return cont(expect(":"), expression);
    
cx.marked "property";
    return 
cont(functiondef);
  }
  function 
commasep(whatend) {
    function 
proceed(type) {
      if (
type == ",") {
        var 
lex cx.state.lexical;
        if (
lex.info == "call"lex.pos = (lex.pos || 0) + 1;
        return 
cont(whatproceed);
      }
      if (
type == end) return cont();
      return 
cont(expect(end));
    }
    return function(
type) {
      if (
type == end) return cont();
      else return 
pass(whatproceed);
    };
  }
  function 
block(type) {
    if (
type == "}") return cont();
    return 
pass(statementblock);
  }
  function 
maybetype(type) {
    if (
type == ":") return cont(typedef);
    return 
pass();
  }
  function 
typedef(type) {
    if (
type == "variable"){cx.marked "variable-3"; return cont();}
    return 
pass();
  }
  function 
vardef1(typevalue) {
    if (
type == "variable") {
      
register(value);
      return 
isTS cont(maybetypevardef2) : cont(vardef2);
    }
    return 
pass();
  }
  function 
vardef2(typevalue) {
    if (
value == "=") return cont(expressionNoCommavardef2);
    if (
type == ",") return cont(vardef1);
  }
  function 
maybeelse(typevalue) {
    if (
type == "keyword b" && value == "else") return cont(pushlex("form"), statementpoplex);
  }
  function 
forspec1(type) {
    if (
type == "var") return cont(vardef1expect(";"), forspec2);
    if (
type == ";") return cont(forspec2);
    if (
type == "variable") return cont(formaybein);
    return 
pass(expressionexpect(";"), forspec2);
  }
  function 
formaybein(_typevalue) {
    if (
value == "in") return cont(expression);
    return 
cont(maybeoperatorCommaforspec2);
  }
  function 
forspec2(typevalue) {
    if (
type == ";") return cont(forspec3);
    if (
value == "in") return cont(expression);
    return 
pass(expressionexpect(";"), forspec3);
  }
  function 
forspec3(type) {
    if (
type != ")"cont(expression);
  }
  function 
functiondef(typevalue) {
    if (
type == "variable") {register(value); return cont(functiondef);}
    if (
type == "(") return cont(pushlex(")"), pushcontextcommasep(funarg")"), poplexstatementpopcontext);
  }
  function 
funarg(typevalue) {
    if (
type == "variable") {register(value); return isTS cont(maybetype) : cont();}
  }

  
// Interface

  
return {
    
startState: function(basecolumn) {
      return {
        
tokenizejsTokenBase,
        
lastTypenull,
        
cc: [],
        
lexical: new JSLexical((basecolumn || 0) - indentUnit0"block"false),
        
localVarsparserConfig.localVars,
        
globalVarsparserConfig.globalVars,
        
contextparserConfig.localVars && {varsparserConfig.localVars},
        
indented0
      
};
    },

    
token: function(streamstate) {
      if (
stream.sol()) {
        if (!
state.lexical.hasOwnProperty("align"))
          
state.lexical.align false;
        
state.indented stream.indentation();
      }
      if (
state.tokenize != jsTokenComment && stream.eatSpace()) return null;
      var 
style state.tokenize(streamstate);
      if (
type == "comment") return style;
      
state.lastType type == "operator" && (content == "++" || content == "--") ? "incdec" type;
      return 
parseJS(statestyletypecontentstream);
    },

    
indent: function(statetextAfter) {
      if (
state.tokenize == jsTokenComment) return CodeMirror.Pass;
      if (
state.tokenize != jsTokenBase) return 0;
      var 
firstChar textAfter && textAfter.charAt(0), lexical state.lexical;
      
// Kludge to prevent 'maybelse' from blocking lexical scope pops
      
for (var state.cc.length 1>= 0; --i) {
        var 
state.cc[i];
        if (
== poplexlexical lexical.prev;
        else if (
!= maybeelse || /^elseb/.test(textAfter)) break;
      }
      if (
lexical.type == "stat" && firstChar == "}"lexical lexical.prev;
      if (
statementIndent && lexical.type == ")" && lexical.prev.type == "stat")
        
lexical lexical.prev;
      var 
type lexical.typeclosing firstChar == type;

      if (
type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," 0);
      else if (
type == "form" && firstChar == "{") return lexical.indented;
      else if (
type == "form") return lexical.indented indentUnit;
      else if (
type == "stat")
        return 
lexical.indented + (state.lastType == "operator" || state.lastType == "," statementIndent || indentUnit 0);
      else if (
lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false)
        return 
lexical.indented + (/^(?:case|default)b/.test(textAfter) ? indentUnit indentUnit);
      else if (
lexical.align) return lexical.column + (closing 1);
      else return 
lexical.indented + (closing indentUnit);
    },

    
electricChars":{}",
    
blockCommentStartjsonMode null "/*",
    
blockCommentEndjsonMode null "*/",
    
lineCommentjsonMode null "//",
    
fold"brace",

    
helperTypejsonMode "json" "javascript",
    
jsonModejsonMode
  
};
});

CodeMirror.defineMIME("text/javascript""javascript");
CodeMirror.defineMIME("text/ecmascript""javascript");
CodeMirror.defineMIME("application/javascript""javascript");
CodeMirror.defineMIME("application/ecmascript""javascript");
CodeMirror.defineMIME("application/json", {name"javascript"jsontrue});
CodeMirror.defineMIME("application/x-json", {name"javascript"jsontrue});
CodeMirror.defineMIME("text/typescript", { name"javascript"typescripttrue });
CodeMirror.defineMIME("application/typescript", { name"javascript"typescripttrue });
?>
Онлайн: 0
Реклама