Debian Codegraph @

rhino/toolsrc/org/mozilla/javascript/tools/idswitch/README

raw
1 This Source Code Form is subject to the terms of the Mozilla Public
2 License, v. 2.0. If a copy of the MPL was not distributed with this
3 file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 USING IDSWITCH GENERATOR TOOL
6
7 Usage:
8 java org.mozilla.javascript.tools.idswitch.Main <SINGLE-JAVA-SOURCE>
9
10 The main purpose of this utility is to generate Java code to map strings to some ids that can be used, for example, in a switch statement.
11
12 The utility scans the input file for lines with the following structure:
13
14 // #string_id_map#
15 ... <DEFINITION AREA>
16 // #generated#
17 ...<GENERATED AREA>
18 // #/generated#
19 ... <DEFINITION AREA>
20 // #/string_id_map#
21
22 Then every line in <DEFINITION AREA> is scanned for the pattern:
23 ^[ \t]*Id_([0-9a-zA-Z_]+)[ \t]*=.*$
24
25 Each such patterns adds a mapping form string $1 to Id_\$ or if the line also contains the pattern //\s*#string=\s*([^#]+)\s*#, then it adds map of $1 in this pattern to Id_\$
26
27 After that lines in <GENERATED AREA> are replaced by a code block that sets variable "id" to Id_<name> if variable "s" equals <name> (or value defined by //#string=...# construction in the line with Id_<name>) or 0 otherwise.
28 If the new code for <GENERATED AREA> is identical to old one, the file is not touched otherwise <GENERATED AREA> is overwritten by the new code and a time stamp is appended after #generated#.
29
30 For example, if file x.java contains:
31
32 // #string_id_map#
33
34 private int getId(String s) {
35 int id;
36 // #generated# Initial version
37 // #/generated#
38 return id;
39 }
40
41 private static final int
42 Id_x = 1,
43 Id_y = 2,
44 Id_hello = 3, // #string = Hello, World! #
45 Id_symbols = 4, // #string=<<*Symbols*>>#
46 Id_nice = 5,
47 Id_for = 6,
48 Id_bar = 7;
49
50 // #/string_id_map#
51
52 ....
53
54 private double getFieldValue(String s) {
55
56 // #string_id_map#
57 final int
58 Id_field1 = 1,
59 Id_field2 = 2,
60 Id_field3 = 3,
61 Id_one_more_field = 4; // #string = ONE%MORE%FIELD#
62
63 int id;
64 // #generated# Initial version
65 // #/generated#
66 // #/string_id_map#
67 switch (id) {
68 case Id_field1: return field1;
69 case Id_field2: return field2;
70 case Id_field3: return field3;
71 case Id_one_more_field: return one_more_field;
72 }
73 throw new RuntimeException("No such field");
74 }
75
76
77 then invocation
78
79 java org.mozilla.javascript.tools.idswitch.Main x.java
80
81 would replace that by a code fragment similar to:
82
83 // #string_id_map#
84
85 private int getId(String s) {
86 int id;
87 // #generated# Last update: 2001-05-25 18:00:24 GMT+02:00
88 L0: { id = 0; String X = null; int c;
89 L: switch (s.length()) {
90 case 1: c=s.charAt(0);
91 if (c=='x') { id=Id_x; break L0; }
92 else if (c=='y') { id=Id_y; break L0; }
93 break L;
94 case 3: c=s.charAt(0);
95 if (c=='b') { if (s.charAt(2)=='r' && s.charAt(1)=='a') {id=Id_bar; break L0;} }
96 else if (c=='f') { if (s.charAt(2)=='r' && s.charAt(1)=='o') {id=Id_for; break L0;} }
97 break L;
98 case 4: X="nice";id=Id_nice; break L;
99 case 13: c=s.charAt(0);
100 if (c=='<') { X="<<*Symbols*>>";id=Id_symbols; }
101 else if (c=='H') { X="Hello, World!";id=Id_hello; }
102 break L;
103 }
104 if (X!=null && X!=s && !X.equals(s)) id = 0;
105 }
106 // #/generated#
107 return id;
108 }
109
110 private static final int
111 Id_x = 1,
112 Id_y = 2,
113 Id_hello = 3, // #string = Hello, World! #
114 Id_symbols = 4, // #string=<<*Symbols*>>#
115 Id_nice = 5,
116 Id_for = 6,
117 Id_bar = 7;
118
119 // #/string_id_map#
120
121 ....
122
123 private double getFieldValue(String s) {
124
125 // #string_id_map#
126 final int
127 Id_field1 = 1,
128 Id_field2 = 2,
129 Id_field3 = 3,
130 Id_one_more_field = 4; // #string = ONE%MORE%FIELD#
131
132 int id;
133 // #generated# Last update: 2001-05-25 16:48:50 GMT+02:00
134 L0: { id = 0; String X = null; int c;
135 int s_length = s.length();
136 if (s_length==6) {
137 c=s.charAt(5);
138 if (c=='1') { X="field1";id=Id_field1; }
139 else if (c=='2') { X="field2";id=Id_field2; }
140 else if (c=='3') { X="field3";id=Id_field3; }
141 }
142 else if (s_length==14) { X="ONE%MORE%FIELD";id=Id_one_more_field; }
143 if (X!=null && X!=s && !X.equals(s)) id = 0;
144 }
145 // #/generated#
146 // #/string_id_map#
147 switch (id) {
148 case Id_field1: return field1;
149 case Id_field2: return field2;
150 case Id_field3: return field3;
151 case Id_one_more_field: return one_more_field;
152 }
153 throw new RuntimeException("No such field");
154 }