Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/mode/coffeescript/coffeescript.js
1293 views
/**1* Link to the project's GitHub page:2* https://github.com/pickhardt/coffeescript-codemirror-mode3*/4CodeMirror.defineMode("coffeescript", function(conf) {5var ERRORCLASS = "error";67function wordRegexp(words) {8return new RegExp("^((" + words.join(")|(") + "))\\b");9}1011var operators = /^(?:->|=>|\+[+=]?|-[\-=]?|\*[\*=]?|\/[\/=]?|[=!]=|<[><]?=?|>>?=?|%=?|&=?|\|=?|\^=?|\~|!|\?)/;12var delimiters = /^(?:[()\[\]{},:`=;]|\.\.?\.?)/;13var identifiers = /^[_A-Za-z$][_A-Za-z$0-9]*/;14var properties = /^(@|this\.)[_A-Za-z$][_A-Za-z$0-9]*/;1516var wordOperators = wordRegexp(["and", "or", "not",17"is", "isnt", "in",18"instanceof", "typeof"]);19var indentKeywords = ["for", "while", "loop", "if", "unless", "else",20"switch", "try", "catch", "finally", "class"];21var commonKeywords = ["break", "by", "continue", "debugger", "delete",22"do", "in", "of", "new", "return", "then",23"this", "throw", "when", "until"];2425var keywords = wordRegexp(indentKeywords.concat(commonKeywords));2627indentKeywords = wordRegexp(indentKeywords);282930var stringPrefixes = /^('{3}|\"{3}|['\"])/;31var regexPrefixes = /^(\/{3}|\/)/;32var commonConstants = ["Infinity", "NaN", "undefined", "null", "true", "false", "on", "off", "yes", "no"];33var constants = wordRegexp(commonConstants);3435// Tokenizers36function tokenBase(stream, state) {37// Handle scope changes38if (stream.sol()) {39if (state.scope.align === null) state.scope.align = false;40var scopeOffset = state.scope.offset;41if (stream.eatSpace()) {42var lineOffset = stream.indentation();43if (lineOffset > scopeOffset && state.scope.type == "coffee") {44return "indent";45} else if (lineOffset < scopeOffset) {46return "dedent";47}48return null;49} else {50if (scopeOffset > 0) {51dedent(stream, state);52}53}54}55if (stream.eatSpace()) {56return null;57}5859var ch = stream.peek();6061// Handle docco title comment (single line)62if (stream.match("####")) {63stream.skipToEnd();64return "comment";65}6667// Handle multi line comments68if (stream.match("###")) {69state.tokenize = longComment;70return state.tokenize(stream, state);71}7273// Single line comment74if (ch === "#") {75stream.skipToEnd();76return "comment";77}7879// Handle number literals80if (stream.match(/^-?[0-9\.]/, false)) {81var floatLiteral = false;82// Floats83if (stream.match(/^-?\d*\.\d+(e[\+\-]?\d+)?/i)) {84floatLiteral = true;85}86if (stream.match(/^-?\d+\.\d*/)) {87floatLiteral = true;88}89if (stream.match(/^-?\.\d+/)) {90floatLiteral = true;91}9293if (floatLiteral) {94// prevent from getting extra . on 1..95if (stream.peek() == "."){96stream.backUp(1);97}98return "number";99}100// Integers101var intLiteral = false;102// Hex103if (stream.match(/^-?0x[0-9a-f]+/i)) {104intLiteral = true;105}106// Decimal107if (stream.match(/^-?[1-9]\d*(e[\+\-]?\d+)?/)) {108intLiteral = true;109}110// Zero by itself with no other piece of number.111if (stream.match(/^-?0(?![\dx])/i)) {112intLiteral = true;113}114if (intLiteral) {115return "number";116}117}118119// Handle strings120if (stream.match(stringPrefixes)) {121state.tokenize = tokenFactory(stream.current(), false, "string");122return state.tokenize(stream, state);123}124// Handle regex literals125if (stream.match(regexPrefixes)) {126if (stream.current() != "/" || stream.match(/^.*\//, false)) { // prevent highlight of division127state.tokenize = tokenFactory(stream.current(), true, "string-2");128return state.tokenize(stream, state);129} else {130stream.backUp(1);131}132}133134// Handle operators and delimiters135if (stream.match(operators) || stream.match(wordOperators)) {136return "operator";137}138if (stream.match(delimiters)) {139return "punctuation";140}141142if (stream.match(constants)) {143return "atom";144}145146if (stream.match(keywords)) {147return "keyword";148}149150if (stream.match(identifiers)) {151return "variable";152}153154if (stream.match(properties)) {155return "property";156}157158// Handle non-detected items159stream.next();160return ERRORCLASS;161}162163function tokenFactory(delimiter, singleline, outclass) {164return function(stream, state) {165while (!stream.eol()) {166stream.eatWhile(/[^'"\/\\]/);167if (stream.eat("\\")) {168stream.next();169if (singleline && stream.eol()) {170return outclass;171}172} else if (stream.match(delimiter)) {173state.tokenize = tokenBase;174return outclass;175} else {176stream.eat(/['"\/]/);177}178}179if (singleline) {180if (conf.mode.singleLineStringErrors) {181outclass = ERRORCLASS;182} else {183state.tokenize = tokenBase;184}185}186return outclass;187};188}189190function longComment(stream, state) {191while (!stream.eol()) {192stream.eatWhile(/[^#]/);193if (stream.match("###")) {194state.tokenize = tokenBase;195break;196}197stream.eatWhile("#");198}199return "comment";200}201202function indent(stream, state, type) {203type = type || "coffee";204var offset = 0, align = false, alignOffset = null;205for (var scope = state.scope; scope; scope = scope.prev) {206if (scope.type === "coffee") {207offset = scope.offset + conf.indentUnit;208break;209}210}211if (type !== "coffee") {212align = null;213alignOffset = stream.column() + stream.current().length;214} else if (state.scope.align) {215state.scope.align = false;216}217state.scope = {218offset: offset,219type: type,220prev: state.scope,221align: align,222alignOffset: alignOffset223};224}225226function dedent(stream, state) {227if (!state.scope.prev) return;228if (state.scope.type === "coffee") {229var _indent = stream.indentation();230var matched = false;231for (var scope = state.scope; scope; scope = scope.prev) {232if (_indent === scope.offset) {233matched = true;234break;235}236}237if (!matched) {238return true;239}240while (state.scope.prev && state.scope.offset !== _indent) {241state.scope = state.scope.prev;242}243return false;244} else {245state.scope = state.scope.prev;246return false;247}248}249250function tokenLexer(stream, state) {251var style = state.tokenize(stream, state);252var current = stream.current();253254// Handle "." connected identifiers255if (current === ".") {256style = state.tokenize(stream, state);257current = stream.current();258if (/^\.[\w$]+$/.test(current)) {259return "variable";260} else {261return ERRORCLASS;262}263}264265// Handle scope changes.266if (current === "return") {267state.dedent += 1;268}269if (((current === "->" || current === "=>") &&270!state.lambda &&271!stream.peek())272|| style === "indent") {273indent(stream, state);274}275var delimiter_index = "[({".indexOf(current);276if (delimiter_index !== -1) {277indent(stream, state, "])}".slice(delimiter_index, delimiter_index+1));278}279if (indentKeywords.exec(current)){280indent(stream, state);281}282if (current == "then"){283dedent(stream, state);284}285286287if (style === "dedent") {288if (dedent(stream, state)) {289return ERRORCLASS;290}291}292delimiter_index = "])}".indexOf(current);293if (delimiter_index !== -1) {294while (state.scope.type == "coffee" && state.scope.prev)295state.scope = state.scope.prev;296if (state.scope.type == current)297state.scope = state.scope.prev;298}299if (state.dedent > 0 && stream.eol() && state.scope.type == "coffee") {300if (state.scope.prev) state.scope = state.scope.prev;301state.dedent -= 1;302}303304return style;305}306307var external = {308startState: function(basecolumn) {309return {310tokenize: tokenBase,311scope: {offset:basecolumn || 0, type:"coffee", prev: null, align: false},312lastToken: null,313lambda: false,314dedent: 0315};316},317318token: function(stream, state) {319var fillAlign = state.scope.align === null && state.scope;320if (fillAlign && stream.sol()) fillAlign.align = false;321322var style = tokenLexer(stream, state);323if (fillAlign && style && style != "comment") fillAlign.align = true;324325state.lastToken = {style:style, content: stream.current()};326327if (stream.eol() && stream.lambda) {328state.lambda = false;329}330331return style;332},333334indent: function(state, text) {335if (state.tokenize != tokenBase) return 0;336var scope = state.scope;337var closer = text && "])}".indexOf(text.charAt(0)) > -1;338if (closer) while (scope.type == "coffee" && scope.prev) scope = scope.prev;339var closes = closer && scope.type === text.charAt(0);340if (scope.align)341return scope.alignOffset - (closes ? 1 : 0);342else343return (closes ? scope.prev : scope).offset;344},345346lineComment: "#",347fold: "indent"348};349return external;350});351352CodeMirror.defineMIME("text/x-coffeescript", "coffeescript");353354355