// CodeMirror, copyright (c) by Marijn Haverbeke and others // Distributed under an MIT license: http://codemirror.net/LICENSE /** * Author: Cedric Joulain * Based on CodeMirror's Clojure from Hans Engel */ (function(mod) { if (typeof exports == "object" && typeof module == "object") // CommonJS mod(require("/js/dist/codemirror")); else if (typeof define == "function" && define.amd) // AMD define(["/js/dist/codemirror"], mod); else // Plain browser env mod(CodeMirror); })(function(CodeMirror) { "use strict"; var hintWords = []; function add(obj) { if (obj) for (var prop in obj) if (obj.hasOwnProperty(prop)) hintWords.push(prop); } function makeKeywords(str) { var obj = {}, words = str.split(" "); for (var i = 0; i < words.length; ++i) obj[words[i]] = true; return obj; } var atoms = makeKeywords("true false nil"); var keywords = makeKeywords( "all and assert cond close count defn def fn high let let* low open or sum " + "@ask-price @ask-volume @bid-price @bid-volume @sign @trade-price @trade-volume"); var builtins = makeKeywords(" > tanh maxpool < / mat/ cdr all year solve mod field? append atan make-date car bit-or close empty? set! hash delta cosh != history bit-not number? not sin sd skewness dropout + label range? time-truncate time-as-array map-reduce duration? list? prune range tensor sum not= del! sign time-as-value str = rest char? print array cos make-bd rand-g first temporal bit-xor cf? println slice acos keep cov-by-pair high now exp sqr map-reduce-arg kurtosis * backward asinh atanh value-as-array import concat get export atan2 svd-u timeserie? list abs acosh fields sync has-prefix hist len pow open float? tan weekday min bit-and null? zero? time? apply sigmoid solar-azimuth mat* timeserie reverse subsample forward int ln median max conv rts sqrt hour mean one clockref? rand-u symbol? array? make-array log graphsample nanosecond solar-altitude low covariance solar-radiation tget count sliding srl cons localize yearday contains sll has-suffix string? >= int? map symnum log10 vget transpose <= svd-s cbrt merge month perimeter cov-by-block svd-v last clockref round hash? day second version shape heartbeat cf asin sinh - minute uniq sra"); add(atoms); add(keywords); add(builtins); hintWords.sort(); CodeMirror.defineMode("lisptick", function (options) { var BUILTIN = "builtin", COMMENT = "comment", STRING = "string", CHARACTER = "string-2", ATOM = "atom", NUMBER = "number", DATETIME = "datetime", DURATION = "duration" , BRACKET = "bracket", KEYWORD = "keyword", VAR = "variable"; var INDENT_WORD_SKIP = options.indentUnit || 2; var NORMAL_INDENT_UNIT = options.indentUnit || 2; var indentKeys = makeKeywords( // Built-ins "ns fn def defn defmethod bound-fn if if-not case condp when while when-not when-first do future comment doto " + "locking proxy with-open with-precision reify deftype defrecord defprotocol extend extend-protocol extend-type " + "try catch " + // Binding forms "let letfn binding loop for doseq dotimes when-let if-let " + // Data structures "defstruct struct-map assoc " + // clojure.test "testing deftest " + // contrib "handler-case handle dotrace deftrace"); var tests = { digit: /\d/, digit_or_colon: /[\d:]/, hex: /[0-9a-f]/i, octal: /[0-7]/i, binary: /[0-1]/i, sign: /[+-]/, exponent: /e/i, keyword_char: /[^\s\(\[\;\)\]]/, symbol: /[\w*+!\-\._?:<>\/\xa1-\uffff]/, block_indent: /^(?:def|with)[^\/]+$|\/(?:def|with)/, duration: /[hmsunDMY]/ }; function stateStack(indent, type, prev) { // represents a state stack object this.indent = indent; this.type = type; this.prev = prev; } function pushStack(state, indent, type) { state.indentStack = new stateStack(indent, type, state.indentStack); } function popStack(state) { state.indentStack = state.indentStack.prev; } function isNumber(ch, stream){ // hex if ( ch === '0' && stream.eat(/x/i) ) { stream.eatWhile(tests.hex); return true; } // octal if ( ch === '0' && stream.eat(/o/i) ) { stream.eatWhile(tests.octal); return true; } // binary if ( ch === '0' && stream.eat(/b/i) ) { stream.eatWhile(tests.binary); return true; } // leading sign if ( ( ch == '+' || ch == '-' ) && ( tests.digit.test(stream.peek()) )) { stream.eat(tests.sign); ch = stream.next(); } if ( tests.digit.test(ch) ) { stream.eat(ch); stream.eatWhile(tests.digit); if ( '.' == stream.peek() ) { stream.eat('.'); stream.eatWhile(tests.digit); } else if ('/' == stream.peek() ) { stream.eat('/'); stream.eatWhile(tests.digit); } if ( stream.eat(tests.exponent) ) { stream.eat(tests.sign); stream.eatWhile(tests.digit); } return true; } return false; } function isDuration(ch, stream){ // if not duration reset input arguments var pos = stream.pos; var inCh = ch; // leading sign if ( ( ch == '+' || ch == '-') && ( tests.digit.test(stream.peek()) ) ) { stream.eat(tests.sign); ch = stream.next(); } if ( tests.digit.test(ch) ) { stream.eat(ch); stream.eatWhile(tests.digit); if ( '.' == stream.peek() ) { stream.eat('.'); stream.eatWhile(tests.digit); } ch = stream.next(); } else { ch = inCh; stream.pos = pos; return false } // Duration if (tests.duration.test(ch)) { stream.eat(ch); // ms us ns if('s' == stream.peek()) { stream.eat('s'); } return true; } ch = inCh; stream.pos = pos; return false; } function isDate(ch, stream){ // if not duration reset input arguments var pos = stream.pos; // Date is YYYY-MM-DD if ( tests.digit.test(ch) ) { // this is Year stream.eat(ch); stream.eatWhile(tests.digit); // must have Month if ( '-' == stream.peek() ) { stream.eat('-'); stream.eatWhile(tests.digit); } else { stream.pos = pos; return false } // must have Day if ( '-' == stream.peek() ) { stream.eat('-'); stream.eatWhile(tests.digit); } else { stream.pos = pos; return false } return true } return false } function isTime(ch, stream){ if (ch == 'T') { // Should be date and time stream.eatWhile(tests.digit_or_colon); if ( '.' == stream.peek() ) { stream.eat('.'); stream.eatWhile(tests.digit); } ch = stream.next(); // time zone if ( ( ch == '+' || ch == '-') && ( tests.digit.test(stream.peek()) ) ) { stream.eat(tests.sign); stream.eatWhile(tests.digit_or_colon); } return true; } return false; } // Eat character that starts after hash # function eatCharacter(stream) { var first = stream.next(); // Read special literals: backspace, newline, space, return. if (first === "\\" ) { first = stream.next(); } // Just read all lowercase letters. if (first && first.match(/[a-z]/) && stream.match(/[a-z]+/, true)) { return; } // Read unicode character: \u1000 \uA0a1 if (first === "u") { stream.match(/[0-9a-z]{4}/i, true); } } return { startState: function () { return { indentStack: null, indentation: 0, mode: false }; }, token: function (stream, state) { if (state.indentStack == null && stream.sol()) { // update indentation, but only if indentStack is empty state.indentation = stream.indentation(); } // skip spaces if (state.mode != "string" && stream.eatSpace()) { return null; } var returnType = null; switch(state.mode){ case "string": // multi-line string parsing mode var next, escaped = false; while ((next = stream.next()) != null) { if (next == "\"" && !escaped) { state.mode = false; break; } escaped = !escaped && next == "\\"; } returnType = STRING; // continue on in string mode break; default: // default parsing mode var ch = stream.next(); if (ch == "\"") { state.mode = "string"; returnType = STRING; } else if (ch == "#") { eatCharacter(stream); returnType = CHARACTER; } else if (ch == "'" && !( tests.digit_or_colon.test(stream.peek()) )) { returnType = ATOM; } else if (ch == ";") { // comment stream.skipToEnd(); // rest of the line is a comment returnType = COMMENT; } else if (ch == "@") { // timeserie type stream.eatWhile(tests.symbol) returnType = KEYWORD; } else if (isDate(ch,stream)){ returnType = DATETIME; } else if (isTime(ch,stream)){ returnType = DATETIME; } else if (isDuration(ch,stream)){ returnType = DURATION; } else if (isNumber(ch,stream)){ returnType = NUMBER; } else if (ch == "(" || ch == "[" || ch == "{" ) { var keyWord = '', indentTemp = stream.column(), letter; /** Either (indent-word .. (non-indent-word .. (;something else, bracket, etc. */ if (ch == "(") while ((letter = stream.eat(tests.keyword_char)) != null) { keyWord += letter; } if (keyWord.length > 0 && (indentKeys.propertyIsEnumerable(keyWord) || tests.block_indent.test(keyWord))) { // indent-word pushStack(state, indentTemp + INDENT_WORD_SKIP, ch); } else { // non-indent word // we continue eating the spaces stream.eatSpace(); if (stream.eol() || stream.peek() == ";") { // nothing significant after // we restart indentation the user defined spaces after pushStack(state, indentTemp + NORMAL_INDENT_UNIT, ch); } else { pushStack(state, indentTemp + stream.current().length, ch); // else we match } } stream.backUp(stream.current().length - 1); // undo all the eating returnType = BRACKET; } else if (ch == ")" || ch == "]" || ch == "}") { returnType = BRACKET; if (state.indentStack != null && state.indentStack.type == (ch == ")" ? "(" : (ch == "]" ? "[" :"{"))) { popStack(state); } } else if ( ch == ":" ) { stream.eatWhile(tests.symbol); return ATOM; } else { stream.eatWhile(tests.symbol); if (keywords && keywords.propertyIsEnumerable(stream.current())) { returnType = KEYWORD; } else if (builtins && builtins.propertyIsEnumerable(stream.current())) { returnType = BUILTIN; } else if (atoms && atoms.propertyIsEnumerable(stream.current())) { returnType = ATOM; } else { returnType = VAR; } } } return returnType; }, indent: function (state) { if (state.indentStack == null) return state.indentation; return state.indentStack.indent; }, closeBrackets: {pairs: "()[]{}\"\""}, lineComment: ";;" }; }); CodeMirror.registerHelper("hintWords", "lisptick", hintWords); CodeMirror.defineMIME("text/x-lisptick", "lisptick"); });