| 1 |
/** @license |
| 2 |
* |
| 3 |
* Copyright 2017 Google Inc. All rights reserved. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
|
| 19 |
(function(mod) { |
| 20 |
if (typeof exports == "object" && typeof module == "object") // CommonJS |
| 21 |
mod(require("../../lib/codemirror"), require("../../addon/mode/simple")); |
| 22 |
else if (typeof define == "function" && define.amd) // AMD |
| 23 |
define(["../../lib/codemirror", "../../addon/mode/simple"], mod); |
| 24 |
else // Plain browser env |
| 25 |
mod(CodeMirror); |
| 26 |
})(function(CodeMirror) { |
| 27 |
"use strict"; |
| 28 |
|
| 29 |
let keywords = { |
| 30 |
"local": "keyword", |
| 31 |
"self": "keyword", |
| 32 |
"super": "keyword", |
| 33 |
"assert": "keyword", |
| 34 |
"function": "keyword", |
| 35 |
"if": "keyword", |
| 36 |
"then": "keyword", |
| 37 |
"else": "keyword", |
| 38 |
"for": "keyword", |
| 39 |
"in": "keyword", |
| 40 |
"tailstrict": "keyword", |
| 41 |
"error": "keyword", |
| 42 |
"true": "atom", |
| 43 |
"false": "atom", |
| 44 |
"null": "atom", |
| 45 |
}; |
| 46 |
|
| 47 |
CodeMirror.defineMode("jsonnet", function() { |
| 48 |
return { |
| 49 |
token: function(stream, state) { |
| 50 |
// Handle special states: |
| 51 |
|
| 52 |
// In a C-style comment |
| 53 |
if (state.cComment) { |
| 54 |
if (stream.match(/\*\//)) { |
| 55 |
state.cComment = false; |
| 56 |
return "comment"; |
| 57 |
} |
| 58 |
stream.next(); |
| 59 |
return "comment"; |
| 60 |
} |
| 61 |
|
| 62 |
// In a text block (|||) |
| 63 |
if (state.textBlock) { |
| 64 |
if (state.textBlockIndent == null) { |
| 65 |
state.textBlockIndent = stream.indentation(); |
| 66 |
} |
| 67 |
if (stream.indentation() >= state.textBlockIndent) { |
| 68 |
stream.skipToEnd(); |
| 69 |
return "string"; |
| 70 |
} |
| 71 |
if (stream.match(/\s*\|\|\|/)) { |
| 72 |
state.textBlock = false; |
| 73 |
return "string"; |
| 74 |
} |
| 75 |
stream.next(); |
| 76 |
return "error"; |
| 77 |
} |
| 78 |
|
| 79 |
// In a string (all 4 variants) |
| 80 |
if (state.string || state.importString) { |
| 81 |
let mode = state.string ? "string" : "meta"; |
| 82 |
if (state.stringRaw) { |
| 83 |
if (stream.match(state.stringSingle ? /''/ : /""/)) { |
| 84 |
return "string-2"; |
| 85 |
} |
| 86 |
} else { |
| 87 |
if (stream.match(/\\[\\"'\/bfnrt0]/)) { |
| 88 |
return "string-2"; |
| 89 |
} |
| 90 |
if (stream.match(/\\u[0-9a-fA-F]{4}/)) { |
| 91 |
return "string-2"; |
| 92 |
} |
| 93 |
if (stream.match(/\\/)) { |
| 94 |
return "error"; |
| 95 |
} |
| 96 |
} |
| 97 |
if (stream.match(state.stringSingle ? /'/ : /"/)) { |
| 98 |
state.string = false; |
| 99 |
state.importString = false; |
| 100 |
state.stringRaw = false; |
| 101 |
state.stringDouble = false; |
| 102 |
return mode; |
| 103 |
} |
| 104 |
stream.next(); |
| 105 |
return mode; |
| 106 |
} |
| 107 |
|
| 108 |
// Regular (whole token at a time) processing: |
| 109 |
|
| 110 |
// Comments. |
| 111 |
if (stream.match(/\/\//) || stream.match(/#/)) { |
| 112 |
stream.skipToEnd(); |
| 113 |
return "comment"; |
| 114 |
} |
| 115 |
if (stream.match(/\/\*/)) { |
| 116 |
state.cComment = true; |
| 117 |
return "comment"; |
| 118 |
} |
| 119 |
|
| 120 |
// Imports (including the strings after them). |
| 121 |
if (stream.match(/import(?:str|bin)?\s*"/)) { |
| 122 |
state.importString = true; |
| 123 |
state.stringSingle = false; |
| 124 |
state.stringRaw = false; |
| 125 |
return "meta"; |
| 126 |
} |
| 127 |
|
| 128 |
if (stream.match(/import(?:str|bin)?\s*'/)) { |
| 129 |
state.importString = true; |
| 130 |
state.stringSingle = true; |
| 131 |
state.stringRaw = false; |
| 132 |
return "meta"; |
| 133 |
} |
| 134 |
|
| 135 |
if (stream.match(/import(?:str|bin)?\s*@"/)) { |
| 136 |
state.importString = true; |
| 137 |
state.stringSingle = false; |
| 138 |
state.stringRaw = true; |
| 139 |
return "meta"; |
| 140 |
} |
| 141 |
|
| 142 |
if (stream.match(/import(?:str|bin)?\s*@'/)) { |
| 143 |
state.importString = true; |
| 144 |
state.stringSingle = true; |
| 145 |
state.stringRaw = true; |
| 146 |
return "meta"; |
| 147 |
} |
| 148 |
|
| 149 |
// Strings (without imports) |
| 150 |
if (stream.match(/"/)) { |
| 151 |
state.string = true; |
| 152 |
state.stringSingle = false; |
| 153 |
state.stringRaw = false; |
| 154 |
return "string"; |
| 155 |
} |
| 156 |
|
| 157 |
if (stream.match(/'/)) { |
| 158 |
state.string = true; |
| 159 |
state.stringSingle = true; |
| 160 |
state.stringRaw = false; |
| 161 |
return "string"; |
| 162 |
} |
| 163 |
|
| 164 |
if (stream.match(/@"/)) { |
| 165 |
state.string = true; |
| 166 |
state.stringSingle = false; |
| 167 |
state.stringRaw = true; |
| 168 |
return "string"; |
| 169 |
} |
| 170 |
|
| 171 |
if (stream.match(/@'/)) { |
| 172 |
state.string = true; |
| 173 |
state.stringSingle = true; |
| 174 |
state.stringRaw = true; |
| 175 |
return "string"; |
| 176 |
} |
| 177 |
|
| 178 |
// Enter text block. |
| 179 |
if (stream.match(/\|\|\|-?/)) { |
| 180 |
state.textBlock = true; |
| 181 |
state.textBlockIndent = null; |
| 182 |
return "string"; |
| 183 |
} |
| 184 |
|
| 185 |
if (stream.match(/\$/)) return "keyword"; |
| 186 |
if (stream.match(/(?:0|[1-9]\d*(?:_\d+)*)(?:\.\d+(?:_\d+)*)?(?:e[+-]?\d+(?:_\d+)*)?/i)) return "number"; |
| 187 |
if (stream.match(/[-+\/*=<>!&~^|$%]+/)) return "operator"; |
| 188 |
|
| 189 |
// Identifiers and keywords that look like identifiers. |
| 190 |
let identifier = stream.match(/[a-zA-Z_][a-zA-Z0-9_]*/); |
| 191 |
if (identifier) { |
| 192 |
identifier = identifier[0]; |
| 193 |
// If it's not in the dict, we return null to indicate no special |
| 194 |
// syntax highlighting. |
| 195 |
return keywords.hasOwnProperty(identifier) ? keywords[identifier] : undefined; |
| 196 |
} |
| 197 |
|
| 198 |
stream.next(); |
| 199 |
|
| 200 |
return null; |
| 201 |
}, |
| 202 |
startState: function() { |
| 203 |
return { |
| 204 |
cComment: false, |
| 205 |
textBlock: false, |
| 206 |
importString: false, |
| 207 |
string: false, |
| 208 |
stringSingle: false, |
| 209 |
stringRaw: false, |
| 210 |
}; |
| 211 |
}, |
| 212 |
}; |
| 213 |
}); |
| 214 |
}); |
| 215 |
|
| 216 |
CodeMirror.defineMIME("text/x-jsonnet", "jsonnet"); |