Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/spp/Spp.java
32287 views
1
/*
2
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package build.tools.spp;
27
28
import java.util.*;
29
import java.util.regex.*;
30
31
/*
32
* Spp: A simple regex-based stream preprocessor based on Mark Reinhold's
33
* sed-based spp.sh
34
*
35
* Usage: java build.tools.spp.Spp [-be] [-Kkey] -Dvar=value ... <in >out
36
*
37
* Source-file constructs
38
*
39
* Meaningful only at beginning of line, works with any number of keys:
40
*
41
* #if[key] Includes text between #if/#end if -Kkey specified,
42
* #else[key] otherwise changes text to blank lines; key test
43
* #end[key] may be negated by prefixing !, e.g., #if[!key]
44
*
45
* #begin If -be is specified then lines up to and including
46
* #end #begin, and from #end to EOF, are deleted
47
*
48
* #warn Changed into warning that file is generated
49
*
50
* // ## Changed into blank line
51
*
52
* Meaningful anywhere in line
53
*
54
* {#if[key]?yes} Expands to yes if -Kkey specified
55
* {#if[key]?yes:no} Expands to yes if -Kkey, otherwise no
56
* {#if[!key]?yes} Expands to yes if -Kother
57
* {#if[!key]?yes:no} Expands to yes if -Kother, otherwise no
58
* $var$ Expands to value if -Dvar=value given
59
*
60
* yes, no must not contain whitespace
61
*
62
* @author Xueming Shen
63
*/
64
65
public class Spp {
66
public static void main(String args[]) throws Exception {
67
Map<String, String> vars = new HashMap<String, String>();
68
Set<String> keys = new HashSet<String>();
69
boolean be = false;
70
71
for (String arg:args) {
72
if (arg.startsWith("-D")) {
73
int i = arg.indexOf('=');
74
vars.put(arg.substring(2, i),arg.substring(i+1));
75
} else if (arg.startsWith("-K")) {
76
keys.add(arg.substring(2));
77
} else if ("-be".equals(arg)) {
78
be = true;
79
} else {
80
System.err.println("Usage: java build.tools.spp.Spp [-be] [-Kkey] -Dvar=value ... <in >out");
81
System.exit(-1);
82
}
83
}
84
85
StringBuffer out = new StringBuffer();
86
new Spp().spp(new Scanner(System.in),
87
out, "",
88
keys, vars, be,
89
false);
90
System.out.print(out.toString());
91
}
92
93
static final String LNSEP = System.getProperty("line.separator");
94
static final String KEY = "([a-zA-Z0-9]+)";
95
static final String VAR = "([a-zA-Z0-9_\\-]+)";
96
static final String TEXT = "([a-zA-Z0-9&;,.<>/#() \\$]+)"; // $ -- hack embedded $var$
97
98
static final int GN_NOT = 1;
99
static final int GN_KEY = 2;
100
static final int GN_YES = 3;
101
static final int GN_NO = 5;
102
static final int GN_VAR = 6;
103
104
Matcher ifkey = Pattern.compile("^#if\\[(!)?" + KEY + "\\]").matcher("");
105
Matcher elsekey = Pattern.compile("^#else\\[(!)?" + KEY + "\\]").matcher("");
106
Matcher endkey = Pattern.compile("^#end\\[(!)?" + KEY + "\\]").matcher("");
107
Matcher vardef = Pattern.compile("\\{#if\\[(!)?" + KEY + "\\]\\?" + TEXT + "(:"+ TEXT + ")?\\}|\\$" + VAR + "\\$").matcher("");
108
Matcher vardef2 = Pattern.compile("\\$" + VAR + "\\$").matcher("");
109
110
void append(StringBuffer buf, String ln,
111
Set<String> keys, Map<String, String> vars) {
112
vardef.reset(ln);
113
while (vardef.find()) {
114
String repl = "";
115
if (vardef.group(GN_VAR) != null)
116
repl = vars.get(vardef.group(GN_VAR));
117
else {
118
boolean test = keys.contains(vardef.group(GN_KEY));
119
if (vardef.group(GN_NOT) != null)
120
test = !test;
121
repl = test?vardef.group(GN_YES):vardef.group(GN_NO);
122
if (repl == null)
123
repl = "";
124
else { // embedded $var$
125
while (vardef2.reset(repl).find()) {
126
repl = vardef2.replaceFirst(vars.get(vardef2.group(1)));
127
}
128
}
129
}
130
vardef.appendReplacement(buf, repl);
131
}
132
vardef.appendTail(buf);
133
}
134
135
// return true if #end[key], #end or EOF reached
136
boolean spp(Scanner in, StringBuffer buf, String key,
137
Set<String> keys, Map<String, String> vars,
138
boolean be, boolean skip) {
139
while (in.hasNextLine()) {
140
String ln = in.nextLine();
141
if (be) {
142
if (ln.startsWith("#begin")) {
143
buf.setLength(0); //clean up to this line
144
continue;
145
}
146
if (ln.equals("#end")) {
147
while (in.hasNextLine())
148
in.nextLine();
149
return true; //discard the rest to EOF
150
}
151
}
152
if (ifkey.reset(ln).find()) {
153
String k = ifkey.group(GN_KEY);
154
boolean test = keys.contains(k);
155
if (ifkey.group(GN_NOT) != null)
156
test = !test;
157
buf.append(LNSEP);
158
if (!spp(in, buf, k, keys, vars, be, skip || !test)) {
159
spp(in, buf, k, keys, vars, be, skip || test);
160
}
161
continue;
162
}
163
if (elsekey.reset(ln).find()) {
164
if (!key.equals(elsekey.group(GN_KEY))) {
165
throw new Error("Mis-matched #if-else-end at line <" + ln + ">");
166
}
167
buf.append(LNSEP);
168
return false;
169
}
170
if (endkey.reset(ln).find()) {
171
if (!key.equals(endkey.group(GN_KEY))) {
172
throw new Error("Mis-matched #if-else-end at line <" + ln + ">");
173
}
174
buf.append(LNSEP);
175
return true;
176
}
177
if (ln.startsWith("#warn")) {
178
ln = "// -- This file was mechanically generated: Do not edit! -- //";
179
} else if (ln.trim().startsWith("// ##")) {
180
ln = "";
181
}
182
if (!skip) {
183
append(buf, ln, keys, vars);
184
}
185
buf.append(LNSEP);
186
}
187
return true;
188
}
189
}
190
191