Debian Codegraph @

rhino/toolsrc/org/mozilla/javascript/tools/idswitch/Main.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 4; indent-tabs-mode: 1; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 package org.mozilla.javascript.tools.idswitch;
7
8 import java.io.*;
9 import java.util.*;
10 import java.text.SimpleDateFormat;
11
12 import org.mozilla.javascript.EvaluatorException;
13 import org.mozilla.javascript.tools.ToolErrorReporter;
14
15 public class Main {
16
17 private static final String SWITCH_TAG_STR = "string_id_map";
18 private static final String GENERATED_TAG_STR = "generated";
19 private static final String STRING_TAG_STR = "string";
20
21 private static final int
22 NORMAL_LINE = 0,
23 SWITCH_TAG = 1,
24 GENERATED_TAG = 2,
25 STRING_TAG = 3;
26
27 private final List<IdValuePair> all_pairs = new ArrayList<IdValuePair>();
28
29 private ToolErrorReporter R;
30 private CodePrinter P;
31 private FileBody body;
32 private String source_file;
33
34 private int tag_definition_end;
35
36 private int tag_value_start;
37 private int tag_value_end;
38
39 private static boolean is_value_type(int id) {
40 if (id == STRING_TAG) { return true; }
41 return false;
42 }
43
44 private static String tag_name(int id) {
45 switch (id) {
46 case SWITCH_TAG: return SWITCH_TAG_STR;
47 case -SWITCH_TAG: return "/" + SWITCH_TAG_STR;
48 case GENERATED_TAG: return GENERATED_TAG_STR;
49 case -GENERATED_TAG: return "/" + GENERATED_TAG_STR;
50 }
51 return "";
52 }
53
54 void process_file(String file_path) throws IOException {
55 source_file = file_path;
56
57 body = new FileBody();
58
59 InputStream is;
60 if (file_path.equals("-")) {
61 is = System.in;
62 }
63 else {
64 is = new FileInputStream(file_path);
65 }
66 try {
67 Reader r = new InputStreamReader(is, "ASCII");
68 body.readData(r);
69 }
70 finally { is.close(); }
71
72 process_file();
73
74 if (body.wasModified()) {
75 OutputStream os;
76 if (file_path.equals("-")) {
77 os = System.out;
78 }
79 else {
80 os = new FileOutputStream(file_path);
81 }
82
83 try {
84 Writer w = new OutputStreamWriter(os);
85 body.writeData(w);
86 w.flush();
87 }
88 finally { os.close(); }
89 }
90 }
91
92 private void process_file() {
93 int cur_state = 0;
94 char[] buffer = body.getBuffer();
95
96 int generated_begin = -1, generated_end = -1;
97 int time_stamp_begin = -1, time_stamp_end = -1;
98
99 body.startLineLoop();
100 while (body.nextLine()) {
101 int begin = body.getLineBegin();
102 int end = body.getLineEnd();
103
104 int tag_id = extract_line_tag_id(buffer, begin, end);
105 boolean bad_tag = false;
106 switch (cur_state) {
107 case NORMAL_LINE:
108 if (tag_id == SWITCH_TAG) {
109 cur_state = SWITCH_TAG;
110 all_pairs.clear();
111 generated_begin = -1;
112 }
113 else if (tag_id == -SWITCH_TAG) {
114 bad_tag = true;
115 }
116 break;
117 case SWITCH_TAG:
118 if (tag_id == 0) {
119 look_for_id_definitions(buffer, begin, end, false);
120 }
121 else if (tag_id == STRING_TAG) {
122 look_for_id_definitions(buffer, begin, end, true);
123 }
124 else if (tag_id == GENERATED_TAG) {
125 if (generated_begin >= 0) { bad_tag = true; }
126 else {
127 cur_state = GENERATED_TAG;
128 time_stamp_begin = tag_definition_end;
129 time_stamp_end = end;
130 }
131 }
132 else if (tag_id == -SWITCH_TAG) {
133 cur_state = 0;
134 if (generated_begin >= 0 && !all_pairs.isEmpty()) {
135 generate_java_code();
136 String code = P.toString();
137 boolean different = body.setReplacement
138 (generated_begin, generated_end, code);
139 if (different) {
140 String stamp = get_time_stamp();
141 body.setReplacement
142 (time_stamp_begin, time_stamp_end, stamp);
143 }
144 }
145
146 break;
147 }
148 else {
149 bad_tag = true;
150 }
151 break;
152 case GENERATED_TAG:
153 if (tag_id == 0) {
154 if (generated_begin < 0) { generated_begin = begin; }
155 }
156 else if (tag_id == -GENERATED_TAG) {
157 if (generated_begin < 0) { generated_begin = begin; }
158 cur_state = SWITCH_TAG;
159 generated_end = begin;
160 }
161 else {
162 bad_tag = true;
163 }
164 break;
165 }
166 if (bad_tag) {
167 String text = ToolErrorReporter.getMessage(
168 "msg.idswitch.bad_tag_order", tag_name(tag_id));
169 throw R.runtimeError
170 (text, source_file, body.getLineNumber(), null, 0);
171 }
172 }
173
174 if (cur_state != 0) {
175 String text = ToolErrorReporter.getMessage(
176 "msg.idswitch.file_end_in_switch", tag_name(cur_state));
177 throw R.runtimeError
178 (text, source_file, body.getLineNumber(), null, 0);
179 }
180 }
181
182 private String get_time_stamp() {
183 SimpleDateFormat f = new SimpleDateFormat
184 (" 'Last update:' yyyy-MM-dd HH:mm:ss z");
185 return f.format(new Date());
186 }
187
188 private void generate_java_code() {
189
190 P.clear();
191
192 IdValuePair[] pairs = new IdValuePair[all_pairs.size()];
193 all_pairs.toArray(pairs);
194
195 SwitchGenerator g = new SwitchGenerator();
196 g.char_tail_test_threshold = 2;
197 g.setReporter(R);
198 g.setCodePrinter(P);
199
200 g.generateSwitch(pairs, "0");
201 }
202
203 private int extract_line_tag_id(char[] array, int cursor, int end) {
204 int id = 0;
205 cursor = skip_white_space(array, cursor, end);
206 int after_leading_white_space = cursor;
207 cursor = look_for_slash_slash(array, cursor, end);
208 if (cursor != end) {
209 boolean at_line_start = (after_leading_white_space + 2 == cursor);
210 cursor = skip_white_space(array, cursor, end);
211 if (cursor != end && array[cursor] == '#') {
212 ++cursor;
213
214 boolean end_tag = false;
215 if (cursor != end && array[cursor] == '/') {
216 ++cursor; end_tag = true;
217 }
218
219 int tag_start = cursor;
220
221 for (; cursor != end; ++cursor) {
222 int c = array[cursor];
223 if (c == '#' || c == '=' ||is_white_space(c)) { break; }
224 }
225
226 if (cursor != end) {
227 int tag_end = cursor;
228 cursor = skip_white_space(array, cursor, end);
229 if (cursor != end) {
230 int c = array[cursor];
231 if (c == '=' || c == '#') {
232 id = get_tag_id
233 (array, tag_start, tag_end, at_line_start);
234 if (id != 0) {
235 String bad = null;
236 if (c == '#') {
237 if (end_tag) {
238 id = -id;
239 if (is_value_type(id)) {
240 bad = "msg.idswitch.no_end_usage";
241 }
242 }
243 tag_definition_end = cursor + 1;
244 }
245 else {
246 if (end_tag) {
247 bad = "msg.idswitch.no_end_with_value";
248 }
249 else if (!is_value_type(id)) {
250 bad = "msg.idswitch.no_value_allowed";
251 }
252 id = extract_tag_value
253 (array, cursor + 1, end, id);
254 }
255 if (bad != null) {
256 String s = ToolErrorReporter.getMessage(
257 bad, tag_name(id));
258 throw R.runtimeError
259 (s, source_file, body.getLineNumber(),
260 null, 0);
261 }
262 }
263 }
264 }
265 }
266 }
267 }
268 return id;
269 }
270
271 // Return position after first of // or end if not found
272 private int look_for_slash_slash(char[] array, int cursor, int end) {
273 while (cursor + 2 <= end) {
274 int c = array[cursor++];
275 if (c == '/') {
276 c = array[cursor++];
277 if (c == '/') {
278 return cursor;
279 }
280 }
281 }
282 return end;
283 }
284
285 private int extract_tag_value(char[] array, int cursor, int end, int id) {
286 // cursor points after #[^#=]+=
287 // ALERT: implement support for quoted strings
288 boolean found = false;
289 cursor = skip_white_space(array, cursor, end);
290 if (cursor != end) {
291 int value_start = cursor;
292 int value_end = cursor;
293 while (cursor != end) {
294 int c = array[cursor];
295 if (is_white_space(c)) {
296 int after_space = skip_white_space(array, cursor + 1, end);
297 if (after_space != end && array[after_space] == '#') {
298 value_end = cursor;
299 cursor = after_space;
300 break;
301 }
302 cursor = after_space + 1;
303 }
304 else if (c == '#') {
305 value_end = cursor;
306 break;
307 }
308 else {
309 ++cursor;
310 }
311 }
312 if (cursor != end) {
313 // array[cursor] is '#' here
314 found = true;
315 tag_value_start = value_start;
316 tag_value_end = value_end;
317 tag_definition_end = cursor + 1;
318 }
319 }
320 return (found) ? id : 0;
321 }
322
323 private int get_tag_id
324 (char[] array, int begin, int end, boolean at_line_start)
325 {
326 if (at_line_start) {
327 if (equals(SWITCH_TAG_STR, array, begin, end)) {
328 return SWITCH_TAG;
329 }
330 if (equals(GENERATED_TAG_STR, array, begin, end)) {
331 return GENERATED_TAG;
332 }
333 }
334 if (equals(STRING_TAG_STR, array, begin, end)) {
335 return STRING_TAG;
336 }
337 return 0;
338 }
339
340 private void look_for_id_definitions
341 (char[] array, int begin, int end, boolean use_tag_value_as_string)
342 {
343 // Look for the pattern
344 // '^[ \t]+Id_([a-zA-Z0-9_]+)[ \t]*=.*$'
345 // where \1 gives field or method name
346 int cursor = begin;
347 // Skip tab and spaces at the beginning
348 cursor = skip_white_space(array, cursor, end);
349 int id_start = cursor;
350 int name_start = skip_matched_prefix("Id_", array, cursor, end);
351 if (name_start >= 0) {
352 // Found Id_ prefix
353 cursor = name_start;
354 cursor = skip_name_char(array, cursor, end);
355 int name_end = cursor;
356 if (name_start != name_end) {
357 cursor = skip_white_space(array, cursor, end);
358 if (cursor != end) {
359 if (array[cursor] == '=') {
360 int id_end = name_end;
361 if (use_tag_value_as_string) {
362 name_start = tag_value_start;
363 name_end = tag_value_end;
364 }
365 // Got the match
366 add_id(array, id_start, id_end, name_start, name_end);
367 }
368 }
369 }
370 }
371 }
372
373 private void add_id
374 (char[] array, int id_start, int id_end, int name_start, int name_end)
375 {
376 String name = new String(array, name_start, name_end - name_start);
377 String value = new String(array, id_start, id_end - id_start);
378
379 IdValuePair pair = new IdValuePair(name, value);
380
381 pair.setLineNumber(body.getLineNumber());
382
383 all_pairs.add(pair);
384 }
385
386 private static boolean is_white_space(int c) {
387 return c == ' ' || c == '\t';
388 }
389
390 private static int skip_white_space(char[] array, int begin, int end) {
391 int cursor = begin;
392 for (; cursor != end; ++cursor) {
393 int c = array[cursor];
394 if (!is_white_space(c)) { break; }
395 }
396 return cursor;
397 }
398
399 private static int skip_matched_prefix
400 (String prefix, char[] array, int begin, int end)
401 {
402 int cursor = -1;
403 int prefix_length = prefix.length();
404 if (prefix_length <= end - begin) {
405 cursor = begin;
406 for (int i = 0; i != prefix_length; ++i, ++cursor) {
407 if (prefix.charAt(i) != array[cursor]) {
408 cursor = -1; break;
409 }
410 }
411 }
412 return cursor;
413 }
414
415 private static boolean equals(String str, char[] array, int begin, int end)
416 {
417 if (str.length() == end - begin) {
418 for (int i = begin, j = 0; i != end; ++i, ++j) {
419 if (array[i] != str.charAt(j)) { return false; }
420 }
421 return true;
422 }
423 return false;
424 }
425
426 private static int skip_name_char(char[] array, int begin, int end) {
427 int cursor = begin;
428 for (; cursor != end; ++cursor) {
429 int c = array[cursor];
430 if (!('a' <= c && c <= 'z') && !('A' <= c && c <= 'Z')) {
431 if (!('0' <= c && c <= '9')) {
432 if (c != '_') {
433 break;
434 }
435 }
436 }
437 }
438 return cursor;
439 }
440
441 public static void main(String[] args) {
442 Main self = new Main();
443 int status = self.exec(args);
444 System.exit(status);
445 }
446
447 private int exec(String[] args) {
448 R = new ToolErrorReporter(true, System.err);
449
450 int arg_count = process_options(args);
451
452 if (arg_count == 0) {
453 option_error(ToolErrorReporter.getMessage(
454 "msg.idswitch.no_file_argument"));
455 return -1;
456 }
457 if (arg_count > 1) {
458 option_error(ToolErrorReporter.getMessage(
459 "msg.idswitch.too_many_arguments"));
460 return -1;
461 }
462
463 P = new CodePrinter();
464 P.setIndentStep(4);
465 P.setIndentTabSize(0);
466
467 try {
468 process_file(args[0]);
469 }
470 catch (IOException ex) {
471 print_error(ToolErrorReporter.getMessage(
472 "msg.idswitch.io_error", ex.toString()));
473 return -1;
474 }
475 catch (EvaluatorException ex) {
476 return -1;
477 }
478 return 0;
479 }
480
481 private int process_options(String[] args) {
482
483 int status = 1;
484
485 boolean show_usage = false;
486 boolean show_version = false;
487
488 int N = args.length;
489 L: for (int i = 0; i != N; ++i) {
490 String arg = args[i];
491 int arg_length = arg.length();
492 if (arg_length >= 2) {
493 if (arg.charAt(0) == '-') {
494 if (arg.charAt(1) == '-') {
495 if (arg_length == 2) {
496 args[i] = null; break;
497 }
498 if (arg.equals("--help")) {
499 show_usage = true;
500 }
501 else if (arg.equals("--version")) {
502 show_version = true;
503 }
504 else {
505 option_error(ToolErrorReporter.getMessage(
506 "msg.idswitch.bad_option", arg));
507 status = -1; break L;
508 }
509 }
510 else {
511 for (int j = 1; j != arg_length; ++j) {
512 char c = arg.charAt(j);
513 switch (c) {
514 case 'h': show_usage = true; break;
515 default:
516 option_error(
517 ToolErrorReporter.getMessage(
518 "msg.idswitch.bad_option_char",
519 String.valueOf(c)));
520 status = -1;
521 break L;
522 }
523
524 }
525 }
526 args[i] = null;
527 }
528 }
529 }
530
531 if (status == 1) {
532 if (show_usage) { show_usage(); status = 0; }
533 if (show_version) { show_version(); status = 0; }
534 }
535
536 if (status != 1) { System.exit(status); }
537
538 return remove_nulls(args);
539 }
540
541 private void show_usage() {
542 System.out.println(
543 ToolErrorReporter.getMessage("msg.idswitch.usage"));
544 System.out.println();
545 }
546
547 private void show_version() {
548 System.out.println(
549 ToolErrorReporter.getMessage("msg.idswitch.version"));
550 }
551
552 private void option_error(String str) {
553 print_error(
554 ToolErrorReporter.getMessage("msg.idswitch.bad_invocation", str));
555 }
556
557 private void print_error(String text) {
558 System.err.println(text);
559 }
560
561 private int remove_nulls(String[] array) {
562 int N = array.length;
563 int cursor = 0;
564 for (; cursor != N; ++cursor) {
565 if (array[cursor] == null) { break; }
566 }
567 int destination = cursor;
568 if (cursor != N) {
569 ++cursor;
570 for (; cursor != N; ++cursor) {
571 String elem = array[cursor];
572 if (elem != null) {
573 array[destination] = elem; ++destination;
574 }
575 }
576 }
577 return destination;
578 }
579 }
580