LaravelTest
223 строки · 7.3 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4// Swift mode created by Michael Kaminsky https://github.com/mkaminsky11
5
6(function(mod) {7if (typeof exports == "object" && typeof module == "object")8mod(require("../../lib/codemirror"))9else if (typeof define == "function" && define.amd)10define(["../../lib/codemirror"], mod)11else12mod(CodeMirror)13})(function(CodeMirror) {14"use strict"15
16function wordSet(words) {17var set = {}18for (var i = 0; i < words.length; i++) set[words[i]] = true19return set20}21
22var keywords = wordSet(["_","var","let","class","enum","extension","import","protocol","struct","func","typealias","associatedtype",23"open","public","internal","fileprivate","private","deinit","init","new","override","self","subscript","super",24"convenience","dynamic","final","indirect","lazy","required","static","unowned","unowned(safe)","unowned(unsafe)","weak","as","is",25"break","case","continue","default","else","fallthrough","for","guard","if","in","repeat","switch","where","while",26"defer","return","inout","mutating","nonmutating","catch","do","rethrows","throw","throws","try","didSet","get","set","willSet",27"assignment","associativity","infix","left","none","operator","postfix","precedence","precedencegroup","prefix","right",28"Any","AnyObject","Type","dynamicType","Self","Protocol","__COLUMN__","__FILE__","__FUNCTION__","__LINE__"])29var definingKeywords = wordSet(["var","let","class","enum","extension","import","protocol","struct","func","typealias","associatedtype","for"])30var atoms = wordSet(["true","false","nil","self","super","_"])31var types = wordSet(["Array","Bool","Character","Dictionary","Double","Float","Int","Int8","Int16","Int32","Int64","Never","Optional","Set","String",32"UInt8","UInt16","UInt32","UInt64","Void"])33var operators = "+-/*%=|&<>~^?!"34var punc = ":;,.(){}[]"35var binary = /^\-?0b[01][01_]*/36var octal = /^\-?0o[0-7][0-7_]*/37var hexadecimal = /^\-?0x[\dA-Fa-f][\dA-Fa-f_]*(?:(?:\.[\dA-Fa-f][\dA-Fa-f_]*)?[Pp]\-?\d[\d_]*)?/38var decimal = /^\-?\d[\d_]*(?:\.\d[\d_]*)?(?:[Ee]\-?\d[\d_]*)?/39var identifier = /^\$\d+|(`?)[_A-Za-z][_A-Za-z$0-9]*\1/40var property = /^\.(?:\$\d+|(`?)[_A-Za-z][_A-Za-z$0-9]*\1)/41var instruction = /^\#[A-Za-z]+/42var attribute = /^@(?:\$\d+|(`?)[_A-Za-z][_A-Za-z$0-9]*\1)/43//var regexp = /^\/(?!\s)(?:\/\/)?(?:\\.|[^\/])+\//44
45function tokenBase(stream, state, prev) {46if (stream.sol()) state.indented = stream.indentation()47if (stream.eatSpace()) return null48
49var ch = stream.peek()50if (ch == "/") {51if (stream.match("//")) {52stream.skipToEnd()53return "comment"54}55if (stream.match("/*")) {56state.tokenize.push(tokenComment)57return tokenComment(stream, state)58}59}60if (stream.match(instruction)) return "builtin"61if (stream.match(attribute)) return "attribute"62if (stream.match(binary)) return "number"63if (stream.match(octal)) return "number"64if (stream.match(hexadecimal)) return "number"65if (stream.match(decimal)) return "number"66if (stream.match(property)) return "property"67if (operators.indexOf(ch) > -1) {68stream.next()69return "operator"70}71if (punc.indexOf(ch) > -1) {72stream.next()73stream.match("..")74return "punctuation"75}76var stringMatch77if (stringMatch = stream.match(/("""|"|')/)) {78var tokenize = tokenString.bind(null, stringMatch[0])79state.tokenize.push(tokenize)80return tokenize(stream, state)81}82
83if (stream.match(identifier)) {84var ident = stream.current()85if (types.hasOwnProperty(ident)) return "variable-2"86if (atoms.hasOwnProperty(ident)) return "atom"87if (keywords.hasOwnProperty(ident)) {88if (definingKeywords.hasOwnProperty(ident))89state.prev = "define"90return "keyword"91}92if (prev == "define") return "def"93return "variable"94}95
96stream.next()97return null98}99
100function tokenUntilClosingParen() {101var depth = 0102return function(stream, state, prev) {103var inner = tokenBase(stream, state, prev)104if (inner == "punctuation") {105if (stream.current() == "(") ++depth106else if (stream.current() == ")") {107if (depth == 0) {108stream.backUp(1)109state.tokenize.pop()110return state.tokenize[state.tokenize.length - 1](stream, state)111}112else --depth113}114}115return inner116}117}118
119function tokenString(openQuote, stream, state) {120var singleLine = openQuote.length == 1121var ch, escaped = false122while (ch = stream.peek()) {123if (escaped) {124stream.next()125if (ch == "(") {126state.tokenize.push(tokenUntilClosingParen())127return "string"128}129escaped = false130} else if (stream.match(openQuote)) {131state.tokenize.pop()132return "string"133} else {134stream.next()135escaped = ch == "\\"136}137}138if (singleLine) {139state.tokenize.pop()140}141return "string"142}143
144function tokenComment(stream, state) {145var ch146while (true) {147stream.match(/^[^/*]+/, true)148ch = stream.next()149if (!ch) break150if (ch === "/" && stream.eat("*")) {151state.tokenize.push(tokenComment)152} else if (ch === "*" && stream.eat("/")) {153state.tokenize.pop()154}155}156return "comment"157}158
159function Context(prev, align, indented) {160this.prev = prev161this.align = align162this.indented = indented163}164
165function pushContext(state, stream) {166var align = stream.match(/^\s*($|\/[\/\*])/, false) ? null : stream.column() + 1167state.context = new Context(state.context, align, state.indented)168}169
170function popContext(state) {171if (state.context) {172state.indented = state.context.indented173state.context = state.context.prev174}175}176
177CodeMirror.defineMode("swift", function(config) {178return {179startState: function() {180return {181prev: null,182context: null,183indented: 0,184tokenize: []185}186},187
188token: function(stream, state) {189var prev = state.prev190state.prev = null191var tokenize = state.tokenize[state.tokenize.length - 1] || tokenBase192var style = tokenize(stream, state, prev)193if (!style || style == "comment") state.prev = prev194else if (!state.prev) state.prev = style195
196if (style == "punctuation") {197var bracket = /[\(\[\{]|([\]\)\}])/.exec(stream.current())198if (bracket) (bracket[1] ? popContext : pushContext)(state, stream)199}200
201return style202},203
204indent: function(state, textAfter) {205var cx = state.context206if (!cx) return 0207var closing = /^[\]\}\)]/.test(textAfter)208if (cx.align != null) return cx.align - (closing ? 1 : 0)209return cx.indented + (closing ? 0 : config.indentUnit)210},211
212electricInput: /^\s*[\)\}\]]$/,213
214lineComment: "//",215blockCommentStart: "/*",216blockCommentEnd: "*/",217fold: "brace",218closeBrackets: "()[]{}''\"\"``"219}220})221
222CodeMirror.defineMIME("text/x-swift","swift")223});224