| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| // CodeMirror, copyright (c) by Marijn Haverbeke and others | ||
| // Distributed under an MIT license: http://codemirror.net/LICENSE | ||
|
|
||
| (function(mod) { | ||
| if (typeof exports == "object" && typeof module == "object") // CommonJS | ||
| mod(require("../../lib/codemirror")); | ||
| else if (typeof define == "function" && define.amd) // AMD | ||
| define(["../../lib/codemirror"], mod); | ||
| else // Plain browser env | ||
| mod(CodeMirror); | ||
| })(function(CodeMirror) { | ||
| "use strict"; | ||
|
|
||
| CodeMirror.defineMode("elm", function() { | ||
|
|
||
| function switchState(source, setState, f) { | ||
| setState(f); | ||
| return f(source, setState); | ||
| } | ||
|
|
||
| // These should all be Unicode extended, as per the Haskell 2010 report | ||
| var smallRE = /[a-z_]/; | ||
| var largeRE = /[A-Z]/; | ||
| var digitRE = /[0-9]/; | ||
| var hexitRE = /[0-9A-Fa-f]/; | ||
| var octitRE = /[0-7]/; | ||
| var idRE = /[a-z_A-Z0-9\']/; | ||
| var symbolRE = /[-!#$%&*+.\/<=>?@\\^|~:\u03BB\u2192]/; | ||
| var specialRE = /[(),;[\]`{}]/; | ||
| var whiteCharRE = /[ \t\v\f]/; // newlines are handled in tokenizer | ||
|
|
||
| function normal() { | ||
| return function (source, setState) { | ||
| if (source.eatWhile(whiteCharRE)) { | ||
| return null; | ||
| } | ||
|
|
||
| var ch = source.next(); | ||
| if (specialRE.test(ch)) { | ||
| if (ch == '{' && source.eat('-')) { | ||
| var t = "comment"; | ||
| if (source.eat('#')) t = "meta"; | ||
| return switchState(source, setState, ncomment(t, 1)); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| if (ch == '\'') { | ||
| if (source.eat('\\')) | ||
| source.next(); // should handle other escapes here | ||
| else | ||
| source.next(); | ||
|
|
||
| if (source.eat('\'')) | ||
| return "string"; | ||
| return "error"; | ||
| } | ||
|
|
||
| if (ch == '"') { | ||
| return switchState(source, setState, stringLiteral); | ||
| } | ||
|
|
||
| if (largeRE.test(ch)) { | ||
| source.eatWhile(idRE); | ||
| if (source.eat('.')) | ||
| return "qualifier"; | ||
| return "variable-2"; | ||
| } | ||
|
|
||
| if (smallRE.test(ch)) { | ||
| var isDef = source.pos === 1; | ||
| source.eatWhile(idRE); | ||
| return isDef ? "variable-3" : "variable"; | ||
| } | ||
|
|
||
| if (digitRE.test(ch)) { | ||
| if (ch == '0') { | ||
| if (source.eat(/[xX]/)) { | ||
| source.eatWhile(hexitRE); // should require at least 1 | ||
| return "integer"; | ||
| } | ||
| if (source.eat(/[oO]/)) { | ||
| source.eatWhile(octitRE); // should require at least 1 | ||
| return "number"; | ||
| } | ||
| } | ||
| source.eatWhile(digitRE); | ||
| var t = "number"; | ||
| if (source.eat('.')) { | ||
| t = "number"; | ||
| source.eatWhile(digitRE); // should require at least 1 | ||
| } | ||
| if (source.eat(/[eE]/)) { | ||
| t = "number"; | ||
| source.eat(/[-+]/); | ||
| source.eatWhile(digitRE); // should require at least 1 | ||
| } | ||
| return t; | ||
| } | ||
|
|
||
| if (symbolRE.test(ch)) { | ||
| if (ch == '-' && source.eat(/-/)) { | ||
| source.eatWhile(/-/); | ||
| if (!source.eat(symbolRE)) { | ||
| source.skipToEnd(); | ||
| return "comment"; | ||
| } | ||
| } | ||
| source.eatWhile(symbolRE); | ||
| return "builtin"; | ||
| } | ||
|
|
||
| return "error"; | ||
| } | ||
| } | ||
|
|
||
| function ncomment(type, nest) { | ||
| if (nest == 0) { | ||
| return normal(); | ||
| } | ||
| return function(source, setState) { | ||
| var currNest = nest; | ||
| while (!source.eol()) { | ||
| var ch = source.next(); | ||
| if (ch == '{' && source.eat('-')) { | ||
| ++currNest; | ||
| } else if (ch == '-' && source.eat('}')) { | ||
| --currNest; | ||
| if (currNest == 0) { | ||
| setState(normal()); | ||
| return type; | ||
| } | ||
| } | ||
| } | ||
| setState(ncomment(type, currNest)); | ||
| return type; | ||
| } | ||
| } | ||
|
|
||
| function stringLiteral(source, setState) { | ||
| while (!source.eol()) { | ||
| var ch = source.next(); | ||
| if (ch == '"') { | ||
| setState(normal()); | ||
| return "string"; | ||
| } | ||
| if (ch == '\\') { | ||
| if (source.eol() || source.eat(whiteCharRE)) { | ||
| setState(stringGap); | ||
| return "string"; | ||
| } | ||
| if (!source.eat('&')) source.next(); // should handle other escapes here | ||
| } | ||
| } | ||
| setState(normal()); | ||
| return "error"; | ||
| } | ||
|
|
||
| function stringGap(source, setState) { | ||
| if (source.eat('\\')) { | ||
| return switchState(source, setState, stringLiteral); | ||
| } | ||
| source.next(); | ||
| setState(normal()); | ||
| return "error"; | ||
| } | ||
|
|
||
|
|
||
| var wellKnownWords = (function() { | ||
| var wkw = {}; | ||
|
|
||
| var keywords = [ | ||
| "case", "of", "as", | ||
| "if", "then", "else", | ||
| "let", "in", | ||
| "infix", "infixl", "infixr", | ||
| "type", "alias", | ||
| "input", "output", "foreign", "loopback", | ||
| "module", "where", "import", "exposing", | ||
| "_", "..", "|", ":", "=", "\\", "\"", "->", "<-" | ||
| ]; | ||
|
|
||
| for (var i = keywords.length; i--;) | ||
| wkw[keywords[i]] = "keyword"; | ||
|
|
||
| return wkw; | ||
| })(); | ||
|
|
||
|
|
||
|
|
||
| return { | ||
| startState: function () { return { f: normal() }; }, | ||
| copyState: function (s) { return { f: s.f }; }, | ||
|
|
||
| token: function(stream, state) { | ||
| var t = state.f(stream, function(s) { state.f = s; }); | ||
| var w = stream.current(); | ||
| return (wellKnownWords.hasOwnProperty(w)) ? wellKnownWords[w] : t; | ||
| } | ||
| }; | ||
|
|
||
| }); | ||
|
|
||
| CodeMirror.defineMIME("text/x-elm", "elm"); | ||
| })(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <!doctype html> | ||
|
|
||
| <title>CodeMirror: Elm mode</title> | ||
| <meta charset="utf-8"/> | ||
| <link rel=stylesheet href="../../doc/docs.css"> | ||
|
|
||
| <link rel="stylesheet" href="../../lib/codemirror.css"> | ||
| <script src="../../lib/codemirror.js"></script> | ||
| <script src="elm.js"></script> | ||
| <style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style> | ||
| <div id=nav> | ||
| <a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a> | ||
|
|
||
| <ul> | ||
| <li><a href="../../index.html">Home</a> | ||
| <li><a href="../../doc/manual.html">Manual</a> | ||
| <li><a href="https://github.com/codemirror/codemirror">Code</a> | ||
| </ul> | ||
| <ul> | ||
| <li><a href="../index.html">Language modes</a> | ||
| <li><a class=active href="#">Elm</a> | ||
| </ul> | ||
| </div> | ||
|
|
||
| <article> | ||
| <h2>Elm mode</h2> | ||
|
|
||
| <div><textarea id="code" name="code"> | ||
| import Color exposing (..) | ||
| import Graphics.Collage exposing (..) | ||
| import Graphics.Element exposing (..) | ||
| import Time exposing (..) | ||
|
|
||
| main = | ||
| Signal.map clock (every second) | ||
|
|
||
| clock t = | ||
| collage 400 400 | ||
| [ filled lightGrey (ngon 12 110) | ||
| , outlined (solid grey) (ngon 12 110) | ||
| , hand orange 100 t | ||
| , hand charcoal 100 (t/60) | ||
| , hand charcoal 60 (t/720) | ||
| ] | ||
|
|
||
| hand clr len time = | ||
| let angle = degrees (90 - 6 * inSeconds time) | ||
| in | ||
| segment (0,0) (fromPolar (len,angle)) | ||
| |> traced (solid clr) | ||
| </textarea></div> | ||
|
|
||
| <script> | ||
| var editor = CodeMirror.fromTextArea(document.getElementById("code"), { | ||
| lineNumbers: true, | ||
| mode: "text/x-elm" | ||
| }); | ||
| </script> | ||
|
|
||
| <p><strong>MIME types defined:</strong> <code>text/x-elm</code>.</p> | ||
| </article> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // CodeMirror, copyright (c) by Marijn Haverbeke and others | ||
| // Distributed under an MIT license: http://codemirror.net/LICENSE | ||
|
|
||
| // Factor syntax highlight - simple mode | ||
| // | ||
| // by Dimage Sapelkin (https://github.com/kerabromsmu) | ||
|
|
||
| (function(mod) { | ||
| if (typeof exports == "object" && typeof module == "object") // CommonJS | ||
| mod(require("../../lib/codemirror"), require("../../addon/mode/simple")); | ||
| else if (typeof define == "function" && define.amd) // AMD | ||
| define(["../../lib/codemirror", "../../addon/mode/simple"], mod); | ||
| else // Plain browser env | ||
| mod(CodeMirror); | ||
| })(function(CodeMirror) { | ||
| "use strict"; | ||
|
|
||
| CodeMirror.defineSimpleMode("factor", { | ||
| // The start state contains the rules that are intially used | ||
| start: [ | ||
| // comments | ||
| {regex: /#?!.*/, token: "comment"}, | ||
| // strings """, multiline --> state | ||
| {regex: /"""/, token: "string", next: "string3"}, | ||
| {regex: /"/, token: "string", next: "string"}, | ||
| // numbers: dec, hex, unicode, bin, fractional, complex | ||
| {regex: /(?:[+-]?)(?:0x[\d,a-f]+)|(?:0o[0-7]+)|(?:0b[0,1]+)|(?:\d+.?\d*)/, token: "number"}, | ||
| //{regex: /[+-]?/} //fractional | ||
| // definition: defining word, defined word, etc | ||
| {regex: /(\:)(\s+)(\S+)(\s+)(\()/, token: ["keyword", null, "def", null, "keyword"], next: "stack"}, | ||
| // vocabulary using --> state | ||
| {regex: /USING\:/, token: "keyword", next: "vocabulary"}, | ||
| // vocabulary definition/use | ||
| {regex: /(USE\:|IN\:)(\s+)(\S+)/, token: ["keyword", null, "variable-2"]}, | ||
| // <constructors> | ||
| {regex: /<\S+>/, token: "builtin"}, | ||
| // "keywords", incl. ; t f . [ ] { } defining words | ||
| {regex: /;|t|f|if|\.|\[|\]|\{|\}|MAIN:/, token: "keyword"}, | ||
| // any id (?) | ||
| {regex: /\S+/, token: "variable"}, | ||
|
|
||
| { | ||
| regex: /./, | ||
| token: null | ||
| } | ||
| ], | ||
| vocabulary: [ | ||
| {regex: /;/, token: "keyword", next: "start"}, | ||
| {regex: /\S+/, token: "variable-2"}, | ||
| { | ||
| regex: /./, | ||
| token: null | ||
| } | ||
| ], | ||
| string: [ | ||
| {regex: /(?:[^\\]|\\.)*?"/, token: "string", next: "start"}, | ||
| {regex: /.*/, token: "string"} | ||
| ], | ||
| string3: [ | ||
| {regex: /(?:[^\\]|\\.)*?"""/, token: "string", next: "start"}, | ||
| {regex: /.*/, token: "string"} | ||
| ], | ||
| stack: [ | ||
| {regex: /\)/, token: "meta", next: "start"}, | ||
| {regex: /--/, token: "meta"}, | ||
| {regex: /\S+/, token: "variable-3"}, | ||
| { | ||
| regex: /./, | ||
| token: null | ||
| } | ||
| ], | ||
| // The meta property contains global information about the mode. It | ||
| // can contain properties like lineComment, which are supported by | ||
| // all modes, and also directives like dontIndentStates, which are | ||
| // specific to simple modes. | ||
| meta: { | ||
| dontIndentStates: ["start", "vocabulary", "string", "string3", "stack"], | ||
| lineComment: [ "!", "#!" ] | ||
| } | ||
| }); | ||
|
|
||
| CodeMirror.defineMIME("text/x-factor", "factor"); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <!doctype html> | ||
|
|
||
| <title>CodeMirror: Factor mode</title> | ||
| <meta charset="utf-8"/> | ||
| <link rel=stylesheet href="../../doc/docs.css"> | ||
|
|
||
| <link href='http://fonts.googleapis.com/css?family=Droid+Sans+Mono' rel='stylesheet' type='text/css'> | ||
| <link rel="stylesheet" href="../../lib/codemirror.css"> | ||
| <script src="../../lib/codemirror.js"></script> | ||
| <script src="../../addon/mode/simple.js"></script> | ||
| <script src="factor.js"></script> | ||
| <style> | ||
| .CodeMirror { | ||
| font-family: 'Droid Sans Mono', monospace; | ||
| font-size: 14px; | ||
| } | ||
| </style> | ||
| <div id=nav> | ||
| <a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a> | ||
|
|
||
| <ul> | ||
| <li><a href="../../index.html">Home</a> | ||
| <li><a href="../../doc/manual.html">Manual</a> | ||
| <li><a href="https://github.com/codemirror/codemirror">Code</a> | ||
| </ul> | ||
| <ul> | ||
| <li><a href="../index.html">Language modes</a> | ||
| <li><a class=active href="#">Factor</a> | ||
| </ul> | ||
| </div> | ||
|
|
||
| <article> | ||
|
|
||
| <h2>Factor mode</h2> | ||
|
|
||
| <form><textarea id="code" name="code"> | ||
| ! Copyright (C) 2008 Slava Pestov. | ||
| ! See http://factorcode.org/license.txt for BSD license. | ||
|
|
||
| ! A simple time server | ||
|
|
||
| USING: accessors calendar calendar.format io io.encodings.ascii | ||
| io.servers kernel threads ; | ||
| IN: time-server | ||
|
|
||
| : handle-time-client ( -- ) | ||
| now timestamp>rfc822 print ; | ||
|
|
||
| : <time-server> ( -- threaded-server ) | ||
| ascii <threaded-server> | ||
| "time-server" >>name | ||
| 1234 >>insecure | ||
| [ handle-time-client ] >>handler ; | ||
|
|
||
| : start-time-server ( -- ) | ||
| <time-server> start-server drop ; | ||
|
|
||
| MAIN: start-time-server | ||
| </textarea> | ||
| </form> | ||
|
|
||
| <script> | ||
| var editor = CodeMirror.fromTextArea(document.getElementById("code"), { | ||
| lineNumbers: true, | ||
| lineWrapping: true, | ||
| indentUnit: 2, | ||
| tabSize: 2, | ||
| autofocus: true, | ||
| mode: "text/x-factor" | ||
| }); | ||
| </script> | ||
| <p/> | ||
| <p>Simple mode that handles Factor Syntax (<a href="http://en.wikipedia.org/wiki/Factor_(programming_language)">Factor on WikiPedia</a>).</p> | ||
|
|
||
| <p><strong>MIME types defined:</strong> <code>text/x-factor</code>.</p> | ||
|
|
||
| </article> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <!doctype html> | ||
|
|
||
| <title>CodeMirror: Swift mode</title> | ||
| <meta charset="utf-8"/> | ||
| <link rel=stylesheet href="../../doc/docs.css"> | ||
|
|
||
| <link rel="stylesheet" href="../../lib/codemirror.css"> | ||
| <script src="../../lib/codemirror.js"></script> | ||
| <script src="../../addon/edit/matchbrackets.js"></script> | ||
| <script src="./swift.js"></script> | ||
| <style> | ||
| .CodeMirror { border: 2px inset #dee; } | ||
| </style> | ||
| <div id=nav> | ||
| <a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a> | ||
|
|
||
| <ul> | ||
| <li><a href="../../index.html">Home</a> | ||
| <li><a href="../../doc/manual.html">Manual</a> | ||
| <li><a href="https://github.com/codemirror/codemirror">Code</a> | ||
| </ul> | ||
| <ul> | ||
| <li><a href="../index.html">Language modes</a> | ||
| <li><a class=active href="#">Swift</a> | ||
| </ul> | ||
| </div> | ||
|
|
||
| <article> | ||
| <h2>Swift mode</h2> | ||
| <form><textarea id="code" name="code"> | ||
| // | ||
| // TipCalculatorModel.swift | ||
| // TipCalculator | ||
| // | ||
| // Created by Main Account on 12/18/14. | ||
| // Copyright (c) 2014 Razeware LLC. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| class TipCalculatorModel { | ||
|
|
||
| var total: Double | ||
| var taxPct: Double | ||
| var subtotal: Double { | ||
| get { | ||
| return total / (taxPct + 1) | ||
| } | ||
| } | ||
|
|
||
| init(total: Double, taxPct: Double) { | ||
| self.total = total | ||
| self.taxPct = taxPct | ||
| } | ||
|
|
||
| func calcTipWithTipPct(tipPct: Double) -> Double { | ||
| return subtotal * tipPct | ||
| } | ||
|
|
||
| func returnPossibleTips() -> [Int: Double] { | ||
|
|
||
| let possibleTipsInferred = [0.15, 0.18, 0.20] | ||
| let possibleTipsExplicit:[Double] = [0.15, 0.18, 0.20] | ||
|
|
||
| var retval = [Int: Double]() | ||
| for possibleTip in possibleTipsInferred { | ||
| let intPct = Int(possibleTip*100) | ||
| retval[intPct] = calcTipWithTipPct(possibleTip) | ||
| } | ||
| return retval | ||
|
|
||
| } | ||
|
|
||
| } | ||
| </textarea></form> | ||
|
|
||
| <script> | ||
| var editor = CodeMirror.fromTextArea(document.getElementById("code"), { | ||
| lineNumbers: true, | ||
| matchBrackets: true, | ||
| mode: "text/x-swift" | ||
| }); | ||
| </script> | ||
|
|
||
| <p>A simple mode for Swift</p> | ||
|
|
||
| <p><strong>MIME types defined:</strong> <code>text/x-swift</code> (Swift code)</p> | ||
| </article> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| // CodeMirror, copyright (c) by Marijn Haverbeke and others | ||
| // Distributed under an MIT license: http://codemirror.net/LICENSE | ||
|
|
||
| // Swift mode created by Michael Kaminsky https://github.com/mkaminsky11 | ||
|
|
||
| (function(mod) { | ||
| if (typeof exports == "object" && typeof module == "object") | ||
| mod(require("../../lib/codemirror")) | ||
| else if (typeof define == "function" && define.amd) | ||
| define(["../../lib/codemirror"], mod) | ||
| else | ||
| mod(CodeMirror) | ||
| })(function(CodeMirror) { | ||
| "use strict" | ||
|
|
||
| function trim(str) { return /^\s*(.*?)\s*$/.exec(str)[1] } | ||
|
|
||
| var separators = [" ","\\\+","\\\-","\\\(","\\\)","\\\*","/",":","\\\?","\\\<","\\\>"," ","\\\."] | ||
| var tokens = new RegExp(separators.join("|"),"g") | ||
|
|
||
| function getWord(string, pos) { | ||
| var index = -1, count = 1 | ||
| var words = string.split(tokens) | ||
| for (var i = 0; i < words.length; i++) { | ||
| for(var j = 1; j <= words[i].length; j++) { | ||
| if (count==pos) index = i | ||
| count++ | ||
| } | ||
| count++ | ||
| } | ||
| var ret = ["", ""] | ||
| if (pos == 0) { | ||
| ret[1] = words[0] | ||
| ret[0] = null | ||
| } else { | ||
| ret[1] = words[index] | ||
| ret[0] = words[index-1] | ||
| } | ||
| return ret | ||
| } | ||
|
|
||
| CodeMirror.defineMode("swift", function() { | ||
| var keywords=["var","let","class","deinit","enum","extension","func","import","init","let","protocol","static","struct","subscript","typealias","var","as","dynamicType","is","new","super","self","Self","Type","__COLUMN__","__FILE__","__FUNCTION__","__LINE__","break","case","continue","default","do","else","fallthrough","if","in","for","return","switch","where","while","associativity","didSet","get","infix","inout","left","mutating","none","nonmutating","operator","override","postfix","precedence","prefix","right","set","unowned","unowned(safe)","unowned(unsafe)","weak","willSet"] | ||
| var commonConstants=["Infinity","NaN","undefined","null","true","false","on","off","yes","no","nil","null","this","super"] | ||
| var types=["String","bool","int","string","double","Double","Int","Float","float","public","private","extension"] | ||
| var numbers=["0","1","2","3","4","5","6","7","8","9"] | ||
| var operators=["+","-","/","*","%","=","|","&","<",">"] | ||
| var punc=[";",",",".","(",")","{","}","[","]"] | ||
| var delimiters=/^(?:[()\[\]{},:`=;]|\.\.?\.?)/ | ||
| var identifiers=/^[_A-Za-z$][_A-Za-z$0-9]*/ | ||
| var properties=/^(@|this\.)[_A-Za-z$][_A-Za-z$0-9]*/ | ||
| var regexPrefixes=/^(\/{3}|\/)/ | ||
|
|
||
| return { | ||
| startState: function() { | ||
| return { | ||
| prev: false, | ||
| string: false, | ||
| escape: false, | ||
| inner: false, | ||
| comment: false, | ||
| num_left: 0, | ||
| num_right: 0, | ||
| doubleString: false, | ||
| singleString: false | ||
| } | ||
| }, | ||
| token: function(stream, state) { | ||
| if (stream.eatSpace()) return null | ||
|
|
||
| var ch = stream.next() | ||
| if (state.string) { | ||
| if (state.escape) { | ||
| state.escape = false | ||
| return "string" | ||
| } else { | ||
| if ((ch == "\"" && (state.doubleString && !state.singleString) || | ||
| (ch == "'" && (!state.doubleString && state.singleString))) && | ||
| !state.escape) { | ||
| state.string = false | ||
| state.doubleString = false | ||
| state.singleString = false | ||
| return "string" | ||
| } else if (ch == "\\" && stream.peek() == "(") { | ||
| state.inner = true | ||
| state.string = false | ||
| return "keyword" | ||
| } else if (ch == "\\" && stream.peek() != "(") { | ||
| state.escape = true | ||
| state.string = true | ||
| return "string" | ||
| } else { | ||
| return "string" | ||
| } | ||
| } | ||
| } else if (state.comment) { | ||
| if (ch == "*" && stream.peek() == "/") { | ||
| state.prev = "*" | ||
| return "comment" | ||
| } else if (ch == "/" && state.prev == "*") { | ||
| state.prev = false | ||
| state.comment = false | ||
| return "comment" | ||
| } | ||
| return "comment" | ||
| } else { | ||
| if (ch == "/") { | ||
| if (stream.peek() == "/") { | ||
| stream.skipToEnd() | ||
| return "comment" | ||
| } | ||
| if (stream.peek() == "*") { | ||
| state.comment = true | ||
| return "comment" | ||
| } | ||
| } | ||
| if (ch == "(" && state.inner) { | ||
| state.num_left++ | ||
| return null | ||
| } | ||
| if (ch == ")" && state.inner) { | ||
| state.num_right++ | ||
| if (state.num_left == state.num_right) { | ||
| state.inner=false | ||
| state.string=true | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| var ret = getWord(stream.string, stream.pos) | ||
| var the_word = ret[1] | ||
| var prev_word = ret[0] | ||
|
|
||
| if (operators.indexOf(ch + "") > -1) return "operator" | ||
| if (punc.indexOf(ch) > -1) return "punctuation" | ||
|
|
||
| if (typeof the_word != "undefined") { | ||
| the_word = trim(the_word) | ||
| if (typeof prev_word != "undefined") prev_word = trim(prev_word) | ||
| if (the_word.charAt(0) == "#") return null | ||
|
|
||
| if (types.indexOf(the_word) > -1) return "def" | ||
| if (commonConstants.indexOf(the_word) > -1) return "atom" | ||
| if (numbers.indexOf(the_word) > -1) return "number" | ||
|
|
||
| if ((numbers.indexOf(the_word.charAt(0) + "") > -1 || | ||
| operators.indexOf(the_word.charAt(0) + "") > -1) && | ||
| numbers.indexOf(ch) > -1) { | ||
| return "number" | ||
| } | ||
|
|
||
| if (keywords.indexOf(the_word) > -1 || | ||
| keywords.indexOf(the_word.split(tokens)[0]) > -1) | ||
| return "keyword" | ||
| if (keywords.indexOf(prev_word) > -1) return "def" | ||
| } | ||
| if (ch == '"' && !state.doubleString) { | ||
| state.string = true | ||
| state.doubleString = true | ||
| return "string" | ||
| } | ||
| if (ch == "'" && !state.singleString) { | ||
| state.string = true | ||
| state.singleString = true | ||
| return "string" | ||
| } | ||
| if (ch == "(" && state.inner) | ||
| state.num_left++ | ||
| if (ch == ")" && state.inner) { | ||
| state.num_right++ | ||
| if (state.num_left == state.num_right) { | ||
| state.inner = false | ||
| state.string = true | ||
| } | ||
| return null | ||
| } | ||
| if (stream.match(/^-?[0-9\.]/, false)) { | ||
| if (stream.match(/^-?\d*\.\d+(e[\+\-]?\d+)?/i) || | ||
| stream.match(/^-?\d+\.\d*/) || | ||
| stream.match(/^-?\.\d+/)) { | ||
| if (stream.peek() == ".") stream.backUp(1) | ||
| return "number" | ||
| } | ||
| if (stream.match(/^-?0x[0-9a-f]+/i) || | ||
| stream.match(/^-?[1-9]\d*(e[\+\-]?\d+)?/) || | ||
| stream.match(/^-?0(?![\dx])/i)) | ||
| return "number" | ||
| } | ||
| if (stream.match(regexPrefixes)) { | ||
| if (stream.current()!="/" || stream.match(/^.*\//,false)) return "string" | ||
| else stream.backUp(1) | ||
| } | ||
| if (stream.match(delimiters)) return "punctuation" | ||
| if (stream.match(identifiers)) return "variable" | ||
| if (stream.match(properties)) return "property" | ||
| return "variable" | ||
| } | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| CodeMirror.defineMIME("text/x-swift","swift") | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <!doctype html> | ||
|
|
||
| <title>CodeMirror: Twig mode</title> | ||
| <meta charset="utf-8"/> | ||
| <link rel=stylesheet href="../../doc/docs.css"> | ||
|
|
||
| <link rel="stylesheet" href="../../lib/codemirror.css"> | ||
| <script src="../../lib/codemirror.js"></script> | ||
| <script src="twig.js"></script> | ||
| <style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style> | ||
| <div id=nav> | ||
| <a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a> | ||
|
|
||
| <ul> | ||
| <li><a href="../../index.html">Home</a> | ||
| <li><a href="../../doc/manual.html">Manual</a> | ||
| <li><a href="https://github.com/codemirror/codemirror">Code</a> | ||
| </ul> | ||
| <ul> | ||
| <li><a href="../index.html">Language modes</a> | ||
| <li><a class=active href="#">Twig</a> | ||
| </ul> | ||
| </div> | ||
|
|
||
| <article> | ||
| <h2>Twig mode</h2> | ||
| <form><textarea id="code" name="code"> | ||
| {% extends "layout.twig" %} | ||
| {% block title %}CodeMirror: Twig mode{% endblock %} | ||
| {# this is a comment #} | ||
| {% block content %} | ||
| {% for foo in bar if foo.baz is divisible by(3) %} | ||
| Hello {{ foo.world }} | ||
| {% else %} | ||
| {% set msg = "Result not found" %} | ||
| {% include "empty.twig" with { message: msg } %} | ||
| {% endfor %} | ||
| {% endblock %} | ||
| </textarea></form> | ||
| <script> | ||
| var editor = | ||
| CodeMirror.fromTextArea(document.getElementById("code"), {mode: | ||
| {name: "twig", htmlMode: true}}); | ||
| </script> | ||
| </article> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // CodeMirror, copyright (c) by Marijn Haverbeke and others | ||
| // Distributed under an MIT license: http://codemirror.net/LICENSE | ||
|
|
||
| (function(mod) { | ||
| if (typeof exports == "object" && typeof module == "object") // CommonJS | ||
| mod(require("../../lib/codemirror")); | ||
| else if (typeof define == "function" && define.amd) // AMD | ||
| define(["../../lib/codemirror"], mod); | ||
| else // Plain browser env | ||
| mod(CodeMirror); | ||
| })(function(CodeMirror) { | ||
| "use strict"; | ||
|
|
||
| CodeMirror.defineMode("twig", function() { | ||
| var keywords = ["and", "as", "autoescape", "endautoescape", "block", "do", "endblock", "else", "elseif", "extends", "for", "endfor", "embed", "endembed", "filter", "endfilter", "flush", "from", "if", "endif", "in", "is", "include", "import", "not", "or", "set", "spaceless", "endspaceless", "with", "endwith", "trans", "endtrans", "blocktrans", "endblocktrans", "macro", "endmacro", "use", "verbatim", "endverbatim"], | ||
| operator = /^[+\-*&%=<>!?|~^]/, | ||
| sign = /^[:\[\(\{]/, | ||
| atom = ["true", "false", "null", "empty", "defined", "divisibleby", "divisible by", "even", "odd", "iterable", "sameas", "same as"], | ||
| number = /^(\d[+\-\*\/])?\d+(\.\d+)?/; | ||
|
|
||
| keywords = new RegExp("((" + keywords.join(")|(") + "))\\b"); | ||
| atom = new RegExp("((" + atom.join(")|(") + "))\\b"); | ||
|
|
||
| function tokenBase (stream, state) { | ||
| var ch = stream.peek(); | ||
|
|
||
| //Comment | ||
| if (state.incomment) { | ||
| if (!stream.skipTo("#}")) { | ||
| stream.skipToEnd(); | ||
| } else { | ||
| stream.eatWhile(/\#|}/); | ||
| state.incomment = false; | ||
| } | ||
| return "comment"; | ||
| //Tag | ||
| } else if (state.intag) { | ||
| //After operator | ||
| if (state.operator) { | ||
| state.operator = false; | ||
| if (stream.match(atom)) { | ||
| return "atom"; | ||
| } | ||
| if (stream.match(number)) { | ||
| return "number"; | ||
| } | ||
| } | ||
| //After sign | ||
| if (state.sign) { | ||
| state.sign = false; | ||
| if (stream.match(atom)) { | ||
| return "atom"; | ||
| } | ||
| if (stream.match(number)) { | ||
| return "number"; | ||
| } | ||
| } | ||
|
|
||
| if (state.instring) { | ||
| if (ch == state.instring) { | ||
| state.instring = false; | ||
| } | ||
| stream.next(); | ||
| return "string"; | ||
| } else if (ch == "'" || ch == '"') { | ||
| state.instring = ch; | ||
| stream.next(); | ||
| return "string"; | ||
| } else if (stream.match(state.intag + "}") || stream.eat("-") && stream.match(state.intag + "}")) { | ||
| state.intag = false; | ||
| return "tag"; | ||
| } else if (stream.match(operator)) { | ||
| state.operator = true; | ||
| return "operator"; | ||
| } else if (stream.match(sign)) { | ||
| state.sign = true; | ||
| } else { | ||
| if (stream.eat(" ") || stream.sol()) { | ||
| if (stream.match(keywords)) { | ||
| return "keyword"; | ||
| } | ||
| if (stream.match(atom)) { | ||
| return "atom"; | ||
| } | ||
| if (stream.match(number)) { | ||
| return "number"; | ||
| } | ||
| if (stream.sol()) { | ||
| stream.next(); | ||
| } | ||
| } else { | ||
| stream.next(); | ||
| } | ||
|
|
||
| } | ||
| return "variable"; | ||
| } else if (stream.eat("{")) { | ||
| if (ch = stream.eat("#")) { | ||
| state.incomment = true; | ||
| if (!stream.skipTo("#}")) { | ||
| stream.skipToEnd(); | ||
| } else { | ||
| stream.eatWhile(/\#|}/); | ||
| state.incomment = false; | ||
| } | ||
| return "comment"; | ||
| //Open tag | ||
| } else if (ch = stream.eat(/\{|%/)) { | ||
| //Cache close tag | ||
| state.intag = ch; | ||
| if (ch == "{") { | ||
| state.intag = "}"; | ||
| } | ||
| stream.eat("-"); | ||
| return "tag"; | ||
| } | ||
| } | ||
| stream.next(); | ||
| }; | ||
|
|
||
| return { | ||
| startState: function () { | ||
| return {}; | ||
| }, | ||
| token: function (stream, state) { | ||
| return tokenBase(stream, state); | ||
| } | ||
| }; | ||
| }); | ||
|
|
||
| CodeMirror.defineMIME("text/x-twig", "twig"); | ||
| }); |