1 | | |
2 | | |
3 | | |
4 | | |
5 | | |
6 | | |
7 | | |
8 | | |
9 | | |
10 | | |
11 | | |
12 | | |
13 | | |
14 | | |
15 | | |
16 | | |
17 | | |
18 | | |
19 | | |
20 | | |
21 | | |
22 | | |
23 | | |
24 | | |
25 | | |
26 | | |
27 | | |
28 | | |
29 | | |
30 | | |
31 | | |
32 | | |
33 | | |
34 | | |
35 | | |
36 | | |
37 | | |
38 | | |
39 | | |
40 | | |
41 | | |
42 | | |
43 | | |
44 | | |
45 | | |
46 | | |
47 | | |
48 | | |
49 | | |
50 | | |
51 | | |
52 | | |
53 | | |
54 | | |
55 | | |
56 | | |
57 | | |
58 | | |
59 | | |
60 | | |
61 | | |
62 | | |
63 | | |
64 | | |
65 | | |
66 | | |
67 | | |
68 | | |
69 | | |
70 | | |
71 | | |
72 | | |
73 | | |
74 | | |
75 | | |
76 | | |
77 | | |
78 | | |
79 | | |
80 | | |
81 | | |
82 | | |
83 | | |
84 | | |
85 | | |
86 | | |
87 | | |
88 | | |
89 | | |
90 | | |
91 | | |
92 | | |
93 | | |
94 | | |
95 | | |
96 | | |
97 | | |
98 | | |
99 | | |
100 | | |
101 | | |
102 | | |
103 | | |
104 | | |
105 | | |
106 | | |
107 | | |
108 | | |
109 | | |
110 | | |
111 | | |
112 | | |
113 | | |
114 | | |
115 | | |
116 | | |
117 | | |
118 | | |
119 | | |
120 | | |
121 | | |
122 | | |
123 | | |
124 | | |
125 | | |
126 | | |
127 | | |
128 | | |
129 | | |
130 | | |
131 | | |
132 | | |
133 | | |
134 | | |
135 | | |
136 | | |
137 | | |
138 | | |
139 | | |
140 | | |
141 | | |
142 | | |
143 | | |
144 | | |
145 | | |
146 | | |
147 | | |
148 | | |
149 | | |
150 | | |
151 | | |
152 | | |
153 | | |
154 | | |
155 | | |
156 | | |
157 | | |
158 | | |
159 | | |
160 | | |
161 | | |
162 | | |
163 | | |
164 | | |
165 | | |
166 | | |
167 | | |
168 | | |
169 | | |
170 | | |
171 | | |
172 | | |
173 | | |
174 | | |
175 | | |
176 | | |
177 | | |
178 | | |
179 | | |
180 | | |
181 | | |
182 | | |
183 | | |
184 | | |
185 | | |
186 | | |
187 | | |
188 | | |
189 | | |
190 | | |
191 | | |
192 | | |
193 | | |
194 | | |
195 | | |
196 | | |
197 | | |
198 | | |
199 | | |
200 | | |
201 | | |
202 | | |
203 | | |
204 | | |
205 | | |
206 | | |
207 | | |
208 | | |
209 | | |
210 | | |
211 | | |
212 | | |
213 | | |
214 | | |
215 | | |
216 | | |
217 | | |
218 | | |
219 | | |
220 | | |
221 | | |
222 | | |
223 | | |
224 | | |
225 | | |
226 | | |
227 | | |
228 | | |
229 | | |
230 | | |
231 | | |
232 | | |
233 | | |
234 | | |
235 | | |
236 | | |
237 | | |
238 | | |
239 | | |
240 | | |
241 | | |
242 | | |
243 | | |
244 | | |
245 | | |
246 | | |
247 | | |
248 | | |
249 | | |
250 | | |
251 | | |
252 | | |
253 | | |
254 | | |
255 | | |
256 | | |
257 | | |
258 | | |
259 | | |
260 | | |
261 | | |
262 | | |
263 | | |
264 | | |
265 | | |
266 | | |
| | | | | string quote(string s) | { | return replace(s,({ "<", ">", "&", }),({"<", ">", "&" })); | } | | string highlight_string(string s,mapping m) | { | if(m->dark) | return "<i><font color='darkred'>"+quote(s)+"</font></i>"; | else | return "<i><font color='skyblue'>"+quote(s)+"</font></i>"; | } | | string highlight_comment(string s, mapping m) | { | if(m->dark) | return ("<font color='red'>"+quote(s)+"</font>"); | return ("<font color='yellow'>"+quote(s)+"</font>"); | } | | string highlight_keyword(string s, mapping m) | { | if(m->dark) return ("<b><font color='darkblue'>"+quote(s)+"</font></b>"); | return ("<b><font color='lightblue'>"+quote(s)+"</font></b>"); | } | | string highlight_type(string s, mapping m) | { | if(m->dark) return ("<b><font color='darkgreen'>"+quote(s)+"</font></b>"); | return ("<b><font color='lightgreen'>"+quote(s)+"</font></b>"); | } | | string highlight_pre(string s, mapping m) | { | if(m->dark) return ("<font color=brown>"+quote(s)+"</font>"); | return ("<font color='pink'>"+quote(s)+"</font>"); | } | | string highlight_declarator(string s, mapping m) | { | if(m->dark) return ("<b><font color='darkbrown'>"+quote(s)+"</font></b>"); | return ("<b><font color='#ffeeaa'>"+quote(s)+"</font></b>"); | } | | | string highlight_case(string s, mapping m) | { | if(m->dark) return ("<font color='black'>"+quote(s)+"</font>"); | return ("<font color='aquamarine'>"+quote(s)+"</font>"); | } | | constant keywords=({"foreach","break","constant","catch","gauge","class","continue","do","else","for","foreach","if","import","inherit","inline","lambda","nomask","private","protected","public","return","static","final", "switch","throw","while",}); | | constant types=({"mapping","function","multiset","array","object","program","float","int","mixed","string","void"}); | | | | array (string) find_decl(string in) | { | string pre,decl; | sscanf(in, "%[ \t\r\n]%s", pre, in); | if(!strlen(in)) return ({"",pre+in}); | if(in[0]==')') | return ({"",pre+in}); | if(sscanf(in, "%[^(),; \t\n\r]%s", decl,in)==2) | return ({ pre+decl, in }); | return ({ "", pre+in }); | } | | string find_complex_type(string post) | { | string p=""; | if(strlen(post) && post[0]=='(') | { | int level=1, i=1; | while(level && i < strlen(post)) | { | switch(post[i++]) | { | case '(': level++;break; | case ')': level--;break; | } | } | p = p+post[..i-1]; | post = post[i..]; | if(sscanf(post, "|%s", post)) | { | string q; | if(sscanf(post, "%s(", q)) | { | p+=q; | post = post[strlen(q)..]; | } else if(sscanf(post, "%s%*[ \t\n\r]", post)>1) { | p+="|"+post; | return p; | } | p+="|"+find_complex_type(post); | } | return p; | } | return p; | } | | array (string) find_type(string in) | { | string s,pre,post; | int min=10000000000,i; | string mt; | foreach(types, s) | if(((i=search(in, s))!=-1) && i<min) | { | if(i) switch(in[i-1]) | { | default: | | continue; | case ' ': | case '\n': | case '\r': | case '\t': | case '(': | case ')': | } | min = i; | mt = s; | } | | if(!(s=mt)) return 0; | | if(sscanf(in, "%s"+s+"%s", pre, post)==2) | { | string op = post; | string p=""; | sscanf(post, "%[ \t\n\r]%s", p, post); | | p += find_complex_type(post); | | p = op[..strlen(p)-1]; | post = op[strlen(p)..]; | | return ({ pre, s+p, @find_decl(post) }); | } | } | | array (string) find_keyword(string in) | { | string s,pre,post; | foreach(keywords, s) if(sscanf(in, "%s"+s+"%s", pre, post)==2) | if(!strlen(pre) || pre[-1]==' ' || pre[-1]=='\t' || pre[-1]==':' || | pre[-1]=='}' || pre[-1]==')' || pre[-1]==';' || pre[-1]=='\n') | if(!strlen(post) || post[0]==' ' || post[0]=='\t' || post[0]=='(' || | post[0]=='{'|| post[0]==';'||post[0]=='\n') | return ({ pre, s, post }); | } | | array (string) find_string(string in) | { | string s,pre,post; | in = replace(in, "\\\"", "\0"); | if(sscanf(in, "%[^\"]\"%[^\"]\"%s", pre, s, post)==3) | return ({ pre, "\""+replace(s, "\0", "\\\"")+"\"", post }); | } | | array (string) find_comment(string in) | { | string s,pre,post; | if(sscanf(in, "%s//%s\n%s", pre, post,s)==3) return ({ pre, "//"+post+"\n",s }); | if(sscanf(in, "#!%s\n%s", post,s)) return ({ "", "#!"+post+"\n",s }); | if(sscanf(in, "%s/*%s*/%s", pre, s, post)==3) | return ({ pre, "/*"+s+"*/", post }); | } | | array (string) find_comment_outside_string(string in) | { | string s,pre,post,q; | if(sscanf(in, "%s\n//%s\n%s", pre,post,s)==3) | return ({ pre+"\n", "//"+post+"\n", s}); | if(sscanf(in, "//%s\n%s", post,s)==2) | return ({ "", "//"+post+"\n", s}); | if(sscanf(in, "%s\n%[^\"]//%s\n%s", q,pre, post,s)==4) | return ({ q+"\n"+pre, "//"+post+"\n", s }); | if(sscanf(in, "%[^\"]/*%s*/%s", pre, s, post)==3) | return ({ pre, "/*"+s+"*/", post }); | } | | array (string) find_case(string in) | { | string mid,pre,post; | if(sscanf(in, "%scase%s:%s", pre, mid, post)==3) | return ({ pre, "case", mid, ":", post }); | if(sscanf(in, "%sdefault%s:%s", pre, mid, post)==3) | return ({ pre, "default"+mid+":", "", "", post }); | | if(sscanf(in, "%scase%s", pre, post)==2) | if(!strlen(pre) || pre[-1]==' ' || pre[-1]=='\t' || pre[-1]==':') | if(!strlen(post) || post[0]==' ' || post[0]=='\t') | return ({ pre, "case", post, "", "" }); | | if(sscanf(in, "%sdefault%s", pre, post)==2) | if(!strlen(pre) || pre[-1]==' ' || pre[-1]=='\t' || pre[-1]==':') | if(!strlen(post) || post[0]==' ' || post[0]=='\t') | return ({ pre, "default", post, "", "" }); | } | | array (string) find_preparse(string in) | { | string s,post,q; | if(sscanf(in, "%s#%s\n%s", q,s,post)==3) | return ({ q,"#"+s+"\n", post }); | } | | array highlight_patterns = | ({ | ({ find_comment_outside_string, ({ 0, highlight_comment }),}), | ({ find_string, ({ 0, highlight_string }),}), | ({ find_comment, ({ 0, highlight_comment }),}), | ({ find_preparse, ({ 0, highlight_pre }),}), | ({ find_type, ({ 0, highlight_type, highlight_declarator }),}), | ({ find_keyword, ({ 0, highlight_keyword }),}), | ({ find_case, ({ 0, highlight_case, 0, highlight_case, }),}), | }); | | #define push(X) res += X | | string highlight_line(string l, mapping m) | { | array p,r; | string res = ""; | | foreach(highlight_patterns, p) | { | if(r=p[0](l)) | { | | | for(int i=0; i<sizeof(p[1]) && i<sizeof(r); i++) | if(functionp(p[1][i])) | push(p[1][i](r[i],m)); | else | push(highlight_line(r[i],m)); | for(int i=sizeof(p[1]); i<sizeof(r); i++) | push(highlight_line(r[i],m)); | return res; | } | } | return quote(l); | } | | string do_the_highlighting(string s, mapping m) | { | return highlight_line(s, m); | } | | string highlight_pike(string t, mapping m, string contents) | { | if(!m->light) m->dark="yep"; | if(m->nopre) return do_the_highlighting(contents,m); | return "<pre>"+do_the_highlighting(contents,m)+"</pre>"; | } | | | |
|