Debian Codegraph @

codemirror-legacy-modes/mode/yaml.js

raw
1 var cons = ['true', 'false', 'on', 'off', 'yes', 'no'];
2 var keywordRegex = new RegExp("\\b(("+cons.join(")|(")+"))$", 'i');
3
4 export const yaml = {
5 name: "yaml",
6 token: function(stream, state) {
7 var ch = stream.peek();
8 var esc = state.escaped;
9 state.escaped = false;
10 /* comments */
11 if (ch == "#" && (stream.pos == 0 || /\s/.test(stream.string.charAt(stream.pos - 1)))) {
12 stream.skipToEnd();
13 return "comment";
14 }
15
16 if (stream.match(/^('([^']|\\.)*'?|"([^"]|\\.)*"?)/))
17 return "string";
18
19 if (state.literal && stream.indentation() > state.keyCol) {
20 stream.skipToEnd(); return "string";
21 } else if (state.literal) { state.literal = false; }
22 if (stream.sol()) {
23 state.keyCol = 0;
24 state.pair = false;
25 state.pairStart = false;
26 /* document start */
27 if(stream.match('---')) { return "def"; }
28 /* document end */
29 if (stream.match('...')) { return "def"; }
30 /* array list item */
31 if (stream.match(/^\s*-\s+/)) { return 'meta'; }
32 }
33 /* inline pairs/lists */
34 if (stream.match(/^(\{|\}|\[|\])/)) {
35 if (ch == '{')
36 state.inlinePairs++;
37 else if (ch == '}')
38 state.inlinePairs--;
39 else if (ch == '[')
40 state.inlineList++;
41 else
42 state.inlineList--;
43 return 'meta';
44 }
45
46 /* list separator */
47 if (state.inlineList > 0 && !esc && ch == ',') {
48 stream.next();
49 return 'meta';
50 }
51 /* pairs separator */
52 if (state.inlinePairs > 0 && !esc && ch == ',') {
53 state.keyCol = 0;
54 state.pair = false;
55 state.pairStart = false;
56 stream.next();
57 return 'meta';
58 }
59
60 /* start of value of a pair */
61 if (state.pairStart) {
62 /* block literals */
63 if (stream.match(/^\s*(\||\>)\s*/)) { state.literal = true; return 'meta'; };
64 /* references */
65 if (stream.match(/^\s*(\&|\*)[a-z0-9\._-]+\b/i)) { return 'variable'; }
66 /* numbers */
67 if (state.inlinePairs == 0 && stream.match(/^\s*-?[0-9\.\,]+\s?$/)) { return 'number'; }
68 if (state.inlinePairs > 0 && stream.match(/^\s*-?[0-9\.\,]+\s?(?=(,|}))/)) { return 'number'; }
69 /* keywords */
70 if (stream.match(keywordRegex)) { return 'keyword'; }
71 }
72
73 /* pairs (associative arrays) -> key */
74 if (!state.pair && stream.match(/^\s*(?:[,\[\]{}&*!|>'"%@`][^\s'":]|[^,\[\]{}#&*!|>'"%@`])[^#]*?(?=\s*:($|\s))/)) {
75 state.pair = true;
76 state.keyCol = stream.indentation();
77 return "atom";
78 }
79 if (state.pair && stream.match(/^:\s*/)) { state.pairStart = true; return 'meta'; }
80
81 /* nothing found, continue */
82 state.pairStart = false;
83 state.escaped = (ch == '\\');
84 stream.next();
85 return null;
86 },
87 startState: function() {
88 return {
89 pair: false,
90 pairStart: false,
91 keyCol: 0,
92 inlinePairs: 0,
93 inlineList: 0,
94 literal: false,
95 escaped: false
96 };
97 },
98 languageData: {
99 commentTokens: {line: "#"}
100 }
101 };