Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/codemirror/mode/coffeescript2.js
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45// Based on https://github.com/giovannicalo/brackets-coffeescript/blob/master/main.js6// modified by William Stein.78(function(mod) {9if (typeof exports == "object" && typeof module == "object") // CommonJS10mod(require("codemirror"));11else if (typeof define == "function" && define.amd) // AMD12define(["../../lib/codemirror"], mod);13else // Plain browser env14mod(CodeMirror);15})(function(CodeMirror) {16"use strict";17CodeMirror.defineMode("coffeescript2", function(config, parser_config) {18var constant_list = [19"false",20"no",21"null",22"off",23"on",24"true",25"undefined",26"Infinity",27"NaN"28];29var keyword_list = [30"and",31"break",32"by",33"catch",34"class",35"continue",36"debugger",37"delete",38"do",39"else",40"extends",41"finally",42"for",43"if",44"in",45"instanceof",46"is",47"isnt",48"loop",49"new",50"not",51"of",52"or",53"return",54"super",55"switch",56"then",57"this",58"throw",59"try",60"typeof",61"unless",62"until",63"when",64"while",65"yield"66];67var constant = constant_list.join("|");68var identifier = "[a-zA-Z\\$_][\\w\\$]*";69var keyword = keyword_list.join("|");70var number = "((?:0(?:(?:[bB][01]+)|(?:[oO][0-7]+)|(?:[xX][0-9a-fA-F]+)))|(?:[\\d]*\\.?[\\d]+(?:e[\\+\\-]\\d+)?))";71var regexp = "\\/((?![*+?\\s])(?:[^\\r\\n\\[/\\\\]|\\\\.|\\[(?:[^\\r\\n\\]\\\\]|\\\\.)*\\])+)\\/";72var regexp_flag = "\\b(([gimuy])(?![gimuy]*\\2))+\\b";73var not_identifier = "[^\\w\\$]";74var not_keyword = "[^a-z]";75var not_number = "([^0-9a-fA-FoxOX\\+\\-\\.]|\\.{2,})";76var whitespace = "[\\t ]*";77var xml_identifier = "[a-zA-Z:_][a-zA-Z0-9:_\\-\\.]*";78var xml_string = "(?:\"(?:(?:\\\")|[^\"])*\")|(?:'(?:(?:\\\')|[^'])*')";79var xml_value = "(?:\\{[\\s\\S]*?\\})";80var xml_element = "<\\/?(" + xml_identifier + ")(?: (?:" + xml_string + "|" + xml_value + "|[^<>\"'])*?)?(?:\\/)?" + whitespace + ">";81return {82token: function(stream, state) {83var highlight = "";84if (!state.isolated) {85if (stream.sol()) {86state.isolated = true;87} else {88stream.backUp(1);89if (stream.match(new RegExp("^" + not_identifier), false)) {90state.isolated = true;91}92stream.next();93}94} else if (!stream.sol()) {95stream.backUp(1);96if (!stream.match(new RegExp("^" + not_identifier), false)) {97state.isolated = false;98}99stream.next();100}101if (parser_config.cjsx) {102if (state.xml_element) {103if (stream.match(/^\/?>/)) {104state.xml_attribute = false;105state.xml_element = false;106state.xml_string = false;107state.xml_value = false;108highlight = "keyword";109}110} else if ((!state.string_interpolated) && (!state.string_literal) && (!state.regexp) && (!state.regexp_block) && (stream.match(new RegExp("^" + xml_element), false))) {111state.xml_element = true;112stream.match(new RegExp("<\\/?" + xml_identifier));113return "keyword";114}115if (state.xml_element) {116if (state.xml_attribute) {117if (stream.match(/^(?:\/>)|[\t=> ]/)) {118state.xml_attribute = false;119return highlight;120} else {121highlight = "number";122}123} else if ((state.isolated) && (!state.xml_string) && (!state.xml_value) && (stream.match(new RegExp("^" + identifier), false))) {124state.xml_attribute = true;125highlight = "number";126}127if (stream.match(new RegExp("^" + xml_string))) {128return "string";129}130if (state.xml_value) {131if (stream.match(/^\}/)) {132state.xml_value = false;133return "minus";134}135} else if (stream.match(new RegExp("^" + xml_value), false)) {136state.xml_value = true;137highlight = "minus";138}139if (!state.xml_value) {140stream.next();141return highlight;142}143}144}145if (state.parameter_list) {146if (stream.match(/^\)/, false)) {147state.parameter_list = false;148}149} else if (stream.match(/^\([^\n\r\(\)]*\)[\t ]*(->|=>)/, false)) {150state.parameter_list = true;151}152if (state.parameter) {153if ((stream.sol()) || (stream.match(new RegExp("^" + not_identifier), false))) {154state.parameter = false;155} else {156highlight = "def";157}158}159if ((state.parameter_list) && (stream.match(new RegExp("^" + identifier), false))) {160state.parameter = true;161highlight = "def";162}163if ((state.isolated) && (!state.string_interpolated) && (!state.string_literal) && (!state.comment_block) && (!state.comment_line) && (stream.match(new RegExp("^@")))) {164state.method = true;165return "keyword";166}167if (state.keyword) {168if ((stream.sol()) || (stream.match(new RegExp("^" + not_keyword), false))) {169state.keyword = false;170} else {171highlight = "keyword";172}173}174if ((state.isolated) && (stream.match(new RegExp("^(" + keyword + ")(" + not_identifier + "|$)"), false))) {175state.keyword = true;176highlight = "keyword";177}178if (state.constant) {179if ((stream.sol()) || (stream.match(new RegExp("^" + not_keyword), false))) {180state.constant = false;181} else {182highlight = "string";183}184}185if ((state.isolated) && (stream.match(new RegExp("^(" + constant + ")(" + not_identifier + "|$)"), false))) {186state.constant = true;187highlight = "string";188}189if (state.function) {190if ((stream.sol()) || (stream.match(/^(:|=)/, false))) {191state.function = false;192} else {193highlight = "def";194}195}196if (stream.match(new RegExp("^" + identifier + whitespace + "(:|=)" + whitespace + "(\\([^\\n\\r]+\\))?" + whitespace + "(->|=>)"), false)) {197state.function = true;198highlight = "def";199}200if (state.property) {201if ((stream.sol()) || (stream.match(/^:/, false))) {202state.property = false;203} else {204highlight = "def";205}206} else if ((!state.regexp) && (!state.regexp_block) && (!state.string_interpolated) && (!state.string_literal) && (stream.match(new RegExp("^(" + identifier + "|((\"|')?(?:(?:(?!\\3).)|\\\\\\3)*\\3))" + whitespace + ":"), false))) {207state.property = true;208highlight = "def";209}210if (state.variable) {211if ((stream.sol()) || (stream.match(/^[=\[]/, false))) {212state.variable = false;213} else {214highlight = "def";215}216}217if (stream.match(new RegExp("^" + identifier + "(\\[.*\\])*" + whitespace + "=([^=]|$)"), false)) {218state.variable = true;219highlight = "def";220}221if (state.method) {222if ((stream.sol()) || (stream.match(new RegExp("^" + not_identifier), false))) {223state.method = false;224} else {225highlight = "def";226}227}228if ((stream.match(new RegExp("^\\." + identifier), false))) {229state.method = true;230}231if (state.number) {232if ((stream.sol()) || (stream.match(new RegExp("^" + not_number), false))) {233state.number = false;234} else {235highlight = "number";236}237}238if ((state.isolated) && (stream.match(new RegExp("^" + number + "(" + not_identifier + "|$)"), false))) {239stream.backUp(1);240if (!stream.match(/^\.{2,}/, false)) {241state.number = true;242highlight = "number";243}244stream.next();245}246if (state.string_interpolated) {247if ((stream.match(/^\\{2}/, false)) || (stream.match(/^\\"/, false))) {248highlight = "string";249stream.next();250} else if (stream.match(/^"/, false)) {251state.string_interpolated = false;252highlight = "string";253} else {254highlight = "string";255}256} else if ((!state.comment_block) && (!state.comment_line) && (!state.regexp) && (!state.regexp_block) && (!state.property) && (!state.string_literal) && (stream.match(/^"/, false))) {257state.string_interpolated = true;258highlight = "string";259}260if (state.string_literal) {261if ((stream.match(/^\\{2}/, false)) || (stream.match(/^\\'/, false))) {262highlight = "string";263stream.next();264} else if (stream.match(/^'/, false)) {265state.string_literal = false;266highlight = "string";267} else {268highlight = "string";269}270} else if ((!state.comment_block) && (!state.comment_line) && (!state.regexp) && (!state.regexp_block) && (!state.property) && (!state.string_interpolated) && (stream.match(/^'/, false))) {271state.string_literal = true;272highlight = "string";273}274if (state.regexp_block) {275if (stream.match(/^\/{3}/, false)) {276state.regexp_block = false;277highlight = "string";278stream.next();279stream.next();280stream.next();281stream.match(new RegExp("^" + regexp_flag));282stream.backUp(1);283} else {284highlight = "string";285}286} else if ((!state.string_interpolated) && (!state.string_literal) && (stream.match(/^\/{3}/, false))) {287state.regexp_block = true;288highlight = "string";289}290if (state.regexp) {291if (stream.match(/^\\\\\//, false)) {292state.regexp = false;293highlight = "string";294stream.next();295stream.next();296stream.next();297stream.match(new RegExp("^" + regexp_flag));298stream.backUp(1);299} else if (stream.match(/^\\\//, false)) {300highlight = "string";301stream.next();302} else if ((stream.sol()) || (stream.match(/^\//, false))) {303state.regexp = false;304highlight = "string";305stream.next();306stream.match(new RegExp("^" + regexp_flag));307stream.backUp(1);308} else {309highlight = "string";310}311} else if ((!state.regexp_block) && (!state.string_interpolated) && (!state.string_literal) && (stream.match(new RegExp("^" + regexp), false))) {312state.regexp = true;313highlight = "string";314}315if (state.comment_block) {316if (stream.match(/^#{3}/, false)) {317state.comment_block = false;318highlight = "comment";319stream.next();320stream.next();321} else {322highlight = "comment";323}324} else if ((!state.regexp) && (!state.regexp_block) && (!state.string_interpolated) && (!state.string_literal) && (stream.match(/^#{3}/, false))) {325state.comment_block = true;326highlight = "comment";327stream.next();328stream.next();329}330if (stream.sol()) {331state.comment_line = false;332}333if (state.comment_line) {334highlight = "comment";335} else if ((!state.comment_block) && (!state.regexp) && (!state.regexp_block) && (!state.string_interpolated) && (!state.string_literal) && (stream.match(/^#/, false))) {336if (stream.column() > 1) {337stream.backUp(2);338if (!stream.match(/^#{3}/, false)) {339state.comment_line = true;340highlight = "comment";341}342stream.next();343stream.next();344} else {345state.comment_line = true;346highlight = "comment";347}348} else if ((state.regexp_block) && (stream.match(/^[\t ]+#/, false))) {349state.comment_line = true;350highlight = "comment";351}352if (state.string_interpolation) {353if ((!state.comment_block) && (!state.regexp) && (!state.regexp_block) && (!state.string_interpolated) && (!state.string_literal) && (stream.match(/^\}/, false))) {354state.string_interpolation = false;355state.string_interpolated = true;356highlight = "minus";357}358} else if ((state.string_interpolated) && (stream.match(/^#\{/, false))) {359state.string_interpolation = true;360state.string_interpolated = false;361highlight = "minus";362stream.next();363}364stream.next();365return highlight;366},367startState: function() {368return {369comment_block: false,370comment_line: false,371constant: false,372function: false,373isolated: false,374keyword: false,375method: false,376number: false,377parameter: false,378parameter_list: false,379property: false,380regexp: false,381regexp_block: false,382string_interpolated: false,383string_interpolation: false,384string_literal: false,385variable: false,386xml_attribute: false,387xml_element: false,388xml_string: false,389xml_value: false390};391},392lineComment: "#",393fold: "indent"394};395});396CodeMirror.defineMIME("text/cjsx", {397cjsx: true,398name: "coffeescript2"399});400CodeMirror.defineMIME("text/coffeescript2", {401cjsx: false,402name: "coffeescript2"403});404});405406407