Debian Codegraph @

codemirror-legacy-modes/mode/dtd.js

raw
1 var type;
2 function ret(style, tp) {type = tp; return style;}
3
4 function tokenBase(stream, state) {
5 var ch = stream.next();
6
7 if (ch == "<" && stream.eat("!") ) {
8 if (stream.eatWhile(/[\-]/)) {
9 state.tokenize = tokenSGMLComment;
10 return tokenSGMLComment(stream, state);
11 } else if (stream.eatWhile(/[\w]/)) return ret("keyword", "doindent");
12 } else if (ch == "<" && stream.eat("?")) { //xml declaration
13 state.tokenize = inBlock("meta", "?>");
14 return ret("meta", ch);
15 } else if (ch == "#" && stream.eatWhile(/[\w]/)) return ret("atom", "tag");
16 else if (ch == "|") return ret("keyword", "separator");
17 else if (ch.match(/[\(\)\[\]\-\.,\+\?>]/)) return ret(null, ch);//if(ch === ">") return ret(null, "endtag"); else
18 else if (ch.match(/[\[\]]/)) return ret("rule", ch);
19 else if (ch == "\"" || ch == "'") {
20 state.tokenize = tokenString(ch);
21 return state.tokenize(stream, state);
22 } else if (stream.eatWhile(/[a-zA-Z\?\+\d]/)) {
23 var sc = stream.current();
24 if( sc.substr(sc.length-1,sc.length).match(/\?|\+/) !== null )stream.backUp(1);
25 return ret("tag", "tag");
26 } else if (ch == "%" || ch == "*" ) return ret("number", "number");
27 else {
28 stream.eatWhile(/[\w\\\-_%.{,]/);
29 return ret(null, null);
30 }
31 }
32
33 function tokenSGMLComment(stream, state) {
34 var dashes = 0, ch;
35 while ((ch = stream.next()) != null) {
36 if (dashes >= 2 && ch == ">") {
37 state.tokenize = tokenBase;
38 break;
39 }
40 dashes = (ch == "-") ? dashes + 1 : 0;
41 }
42 return ret("comment", "comment");
43 }
44
45 function tokenString(quote) {
46 return function(stream, state) {
47 var escaped = false, ch;
48 while ((ch = stream.next()) != null) {
49 if (ch == quote && !escaped) {
50 state.tokenize = tokenBase;
51 break;
52 }
53 escaped = !escaped && ch == "\\";
54 }
55 return ret("string", "tag");
56 };
57 }
58
59 function inBlock(style, terminator) {
60 return function(stream, state) {
61 while (!stream.eol()) {
62 if (stream.match(terminator)) {
63 state.tokenize = tokenBase;
64 break;
65 }
66 stream.next();
67 }
68 return style;
69 };
70 }
71
72 export const dtd = {
73 name: "dtd",
74 startState: function() {
75 return {tokenize: tokenBase,
76 baseIndent: 0,
77 stack: []};
78 },
79
80 token: function(stream, state) {
81 if (stream.eatSpace()) return null;
82 var style = state.tokenize(stream, state);
83
84 var context = state.stack[state.stack.length-1];
85 if (stream.current() == "[" || type === "doindent" || type == "[") state.stack.push("rule");
86 else if (type === "endtag") state.stack[state.stack.length-1] = "endtag";
87 else if (stream.current() == "]" || type == "]" || (type == ">" && context == "rule")) state.stack.pop();
88 else if (type == "[") state.stack.push("[");
89 return style;
90 },
91
92 indent: function(state, textAfter, cx) {
93 var n = state.stack.length;
94
95 if( textAfter.charAt(0) === ']' )n--;
96 else if(textAfter.substr(textAfter.length-1, textAfter.length) === ">"){
97 if(textAfter.substr(0,1) === "<") {}
98 else if( type == "doindent" && textAfter.length > 1 ) {}
99 else if( type == "doindent")n--;
100 else if( type == ">" && textAfter.length > 1) {}
101 else if( type == "tag" && textAfter !== ">") {}
102 else if( type == "tag" && state.stack[state.stack.length-1] == "rule")n--;
103 else if( type == "tag")n++;
104 else if( textAfter === ">" && state.stack[state.stack.length-1] == "rule" && type === ">")n--;
105 else if( textAfter === ">" && state.stack[state.stack.length-1] == "rule") {}
106 else if( textAfter.substr(0,1) !== "<" && textAfter.substr(0,1) === ">" )n=n-1;
107 else if( textAfter === ">") {}
108 else n=n-1;
109 //over rule them all
110 if(type == null || type == "]")n--;
111 }
112
113 return state.baseIndent + n * cx.unit;
114 },
115
116 languageData: {
117 indentOnInput: /^\s*[\]>]$/
118 }
119 };
120