Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/spp/Spp.java
32287 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package build.tools.spp;2627import java.util.*;28import java.util.regex.*;2930/*31* Spp: A simple regex-based stream preprocessor based on Mark Reinhold's32* sed-based spp.sh33*34* Usage: java build.tools.spp.Spp [-be] [-Kkey] -Dvar=value ... <in >out35*36* Source-file constructs37*38* Meaningful only at beginning of line, works with any number of keys:39*40* #if[key] Includes text between #if/#end if -Kkey specified,41* #else[key] otherwise changes text to blank lines; key test42* #end[key] may be negated by prefixing !, e.g., #if[!key]43*44* #begin If -be is specified then lines up to and including45* #end #begin, and from #end to EOF, are deleted46*47* #warn Changed into warning that file is generated48*49* // ## Changed into blank line50*51* Meaningful anywhere in line52*53* {#if[key]?yes} Expands to yes if -Kkey specified54* {#if[key]?yes:no} Expands to yes if -Kkey, otherwise no55* {#if[!key]?yes} Expands to yes if -Kother56* {#if[!key]?yes:no} Expands to yes if -Kother, otherwise no57* $var$ Expands to value if -Dvar=value given58*59* yes, no must not contain whitespace60*61* @author Xueming Shen62*/6364public class Spp {65public static void main(String args[]) throws Exception {66Map<String, String> vars = new HashMap<String, String>();67Set<String> keys = new HashSet<String>();68boolean be = false;6970for (String arg:args) {71if (arg.startsWith("-D")) {72int i = arg.indexOf('=');73vars.put(arg.substring(2, i),arg.substring(i+1));74} else if (arg.startsWith("-K")) {75keys.add(arg.substring(2));76} else if ("-be".equals(arg)) {77be = true;78} else {79System.err.println("Usage: java build.tools.spp.Spp [-be] [-Kkey] -Dvar=value ... <in >out");80System.exit(-1);81}82}8384StringBuffer out = new StringBuffer();85new Spp().spp(new Scanner(System.in),86out, "",87keys, vars, be,88false);89System.out.print(out.toString());90}9192static final String LNSEP = System.getProperty("line.separator");93static final String KEY = "([a-zA-Z0-9]+)";94static final String VAR = "([a-zA-Z0-9_\\-]+)";95static final String TEXT = "([a-zA-Z0-9&;,.<>/#() \\$]+)"; // $ -- hack embedded $var$9697static final int GN_NOT = 1;98static final int GN_KEY = 2;99static final int GN_YES = 3;100static final int GN_NO = 5;101static final int GN_VAR = 6;102103Matcher ifkey = Pattern.compile("^#if\\[(!)?" + KEY + "\\]").matcher("");104Matcher elsekey = Pattern.compile("^#else\\[(!)?" + KEY + "\\]").matcher("");105Matcher endkey = Pattern.compile("^#end\\[(!)?" + KEY + "\\]").matcher("");106Matcher vardef = Pattern.compile("\\{#if\\[(!)?" + KEY + "\\]\\?" + TEXT + "(:"+ TEXT + ")?\\}|\\$" + VAR + "\\$").matcher("");107Matcher vardef2 = Pattern.compile("\\$" + VAR + "\\$").matcher("");108109void append(StringBuffer buf, String ln,110Set<String> keys, Map<String, String> vars) {111vardef.reset(ln);112while (vardef.find()) {113String repl = "";114if (vardef.group(GN_VAR) != null)115repl = vars.get(vardef.group(GN_VAR));116else {117boolean test = keys.contains(vardef.group(GN_KEY));118if (vardef.group(GN_NOT) != null)119test = !test;120repl = test?vardef.group(GN_YES):vardef.group(GN_NO);121if (repl == null)122repl = "";123else { // embedded $var$124while (vardef2.reset(repl).find()) {125repl = vardef2.replaceFirst(vars.get(vardef2.group(1)));126}127}128}129vardef.appendReplacement(buf, repl);130}131vardef.appendTail(buf);132}133134// return true if #end[key], #end or EOF reached135boolean spp(Scanner in, StringBuffer buf, String key,136Set<String> keys, Map<String, String> vars,137boolean be, boolean skip) {138while (in.hasNextLine()) {139String ln = in.nextLine();140if (be) {141if (ln.startsWith("#begin")) {142buf.setLength(0); //clean up to this line143continue;144}145if (ln.equals("#end")) {146while (in.hasNextLine())147in.nextLine();148return true; //discard the rest to EOF149}150}151if (ifkey.reset(ln).find()) {152String k = ifkey.group(GN_KEY);153boolean test = keys.contains(k);154if (ifkey.group(GN_NOT) != null)155test = !test;156buf.append(LNSEP);157if (!spp(in, buf, k, keys, vars, be, skip || !test)) {158spp(in, buf, k, keys, vars, be, skip || test);159}160continue;161}162if (elsekey.reset(ln).find()) {163if (!key.equals(elsekey.group(GN_KEY))) {164throw new Error("Mis-matched #if-else-end at line <" + ln + ">");165}166buf.append(LNSEP);167return false;168}169if (endkey.reset(ln).find()) {170if (!key.equals(endkey.group(GN_KEY))) {171throw new Error("Mis-matched #if-else-end at line <" + ln + ">");172}173buf.append(LNSEP);174return true;175}176if (ln.startsWith("#warn")) {177ln = "// -- This file was mechanically generated: Do not edit! -- //";178} else if (ln.trim().startsWith("// ##")) {179ln = "";180}181if (!skip) {182append(buf, ln, keys, vars);183}184buf.append(LNSEP);185}186return true;187}188}189190191