1 |
|
2 |
|
3 |
|
4 | (function(mod) {
|
5 | if (typeof exports == "object" && typeof module == "object")
|
6 | mod(require("../../lib/codemirror"));
|
7 | else if (typeof define == "function" && define.amd)
|
8 | define(["../../lib/codemirror"], mod);
|
9 | else
|
10 | mod(CodeMirror);
|
11 | })(function(CodeMirror) {
|
12 | "use strict";
|
13 |
|
14 | CodeMirror.defineMode("yaml", function() {
|
15 |
|
16 | var cons = ['true', 'false', 'on', 'off', 'yes', 'no'];
|
17 | var keywordRegex = new RegExp("\\b(("+cons.join(")|(")+"))$", 'i');
|
18 |
|
19 | return {
|
20 | token: function(stream, state) {
|
21 | var ch = stream.peek();
|
22 | var esc = state.escaped;
|
23 | state.escaped = false;
|
24 |
|
25 | if (ch == "#" && (stream.pos == 0 || /\s/.test(stream.string.charAt(stream.pos - 1)))) {
|
26 | stream.skipToEnd();
|
27 | return "comment";
|
28 | }
|
29 |
|
30 | if (stream.match(/^('([^']|\\.)*'?|"([^"]|\\.)*"?)/))
|
31 | return "string";
|
32 |
|
33 | if (state.literal && stream.indentation() > state.keyCol) {
|
34 | stream.skipToEnd(); return "string";
|
35 | } else if (state.literal) { state.literal = false; }
|
36 | if (stream.sol()) {
|
37 | state.keyCol = 0;
|
38 | state.pair = false;
|
39 | state.pairStart = false;
|
40 |
|
41 | if(stream.match(/---/)) { return "def"; }
|
42 |
|
43 | if (stream.match(/\.\.\./)) { return "def"; }
|
44 |
|
45 | if (stream.match(/\s*-\s+/)) { return 'meta'; }
|
46 | }
|
47 |
|
48 | if (stream.match(/^(\{|\}|\[|\])/)) {
|
49 | if (ch == '{')
|
50 | state.inlinePairs++;
|
51 | else if (ch == '}')
|
52 | state.inlinePairs--;
|
53 | else if (ch == '[')
|
54 | state.inlineList++;
|
55 | else
|
56 | state.inlineList--;
|
57 | return 'meta';
|
58 | }
|
59 |
|
60 |
|
61 | if (state.inlineList > 0 && !esc && ch == ',') {
|
62 | stream.next();
|
63 | return 'meta';
|
64 | }
|
65 |
|
66 | if (state.inlinePairs > 0 && !esc && ch == ',') {
|
67 | state.keyCol = 0;
|
68 | state.pair = false;
|
69 | state.pairStart = false;
|
70 | stream.next();
|
71 | return 'meta';
|
72 | }
|
73 |
|
74 |
|
75 | if (state.pairStart) {
|
76 |
|
77 | if (stream.match(/^\s*(\||\>)\s*/)) { state.literal = true; return 'meta'; };
|
78 |
|
79 | if (stream.match(/^\s*(\&|\*)[a-z0-9\._-]+\b/i)) { return 'variable-2'; }
|
80 |
|
81 | if (state.inlinePairs == 0 && stream.match(/^\s*-?[0-9\.\,]+\s?$/)) { return 'number'; }
|
82 | if (state.inlinePairs > 0 && stream.match(/^\s*-?[0-9\.\,]+\s?(?=(,|}))/)) { return 'number'; }
|
83 |
|
84 | if (stream.match(keywordRegex)) { return 'keyword'; }
|
85 | }
|
86 |
|
87 |
|
88 | if (!state.pair && stream.match(/^\s*(?:[,\[\]{}&*!|>'"%@`][^\s'":]|[^,\[\]{}#&*!|>'"%@`])[^#]*?(?=\s*:($|\s))/)) {
|
89 | state.pair = true;
|
90 | state.keyCol = stream.indentation();
|
91 | return "atom";
|
92 | }
|
93 | if (state.pair && stream.match(/^:\s*/)) { state.pairStart = true; return 'meta'; }
|
94 |
|
95 |
|
96 | state.pairStart = false;
|
97 | state.escaped = (ch == '\\');
|
98 | stream.next();
|
99 | return null;
|
100 | },
|
101 | startState: function() {
|
102 | return {
|
103 | pair: false,
|
104 | pairStart: false,
|
105 | keyCol: 0,
|
106 | inlinePairs: 0,
|
107 | inlineList: 0,
|
108 | literal: false,
|
109 | escaped: false
|
110 | };
|
111 | },
|
112 | lineComment: "#",
|
113 | fold: "indent"
|
114 | };
|
115 | });
|
116 |
|
117 | CodeMirror.defineMIME("text/x-yaml", "yaml");
|
118 | CodeMirror.defineMIME("text/yaml", "yaml");
|
119 |
|
120 | });
|