2010-11-15 20:35:36 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
2015-04-07 18:18:37 +00:00
|
|
|
* (C) Copyright 2009-2014, Arnaud Roques
|
2010-11-15 20:35:36 +00:00
|
|
|
*
|
|
|
|
* Project Info: http://plantuml.sourceforge.net
|
|
|
|
*
|
|
|
|
* This file is part of PlantUML.
|
|
|
|
*
|
|
|
|
* PlantUML is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PlantUML distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
2013-12-10 19:36:50 +00:00
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
2010-11-15 20:35:36 +00:00
|
|
|
* License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
|
|
|
|
* in the United States and other countries.]
|
|
|
|
*
|
|
|
|
* Original Author: Arnaud Roques
|
|
|
|
*
|
2015-04-07 18:18:37 +00:00
|
|
|
* Revision $Revision: 14321 $
|
2010-11-15 20:35:36 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
package net.sourceforge.plantuml.preproc;
|
|
|
|
|
2011-01-29 15:09:35 +00:00
|
|
|
import java.io.File;
|
2010-11-15 20:35:36 +00:00
|
|
|
import java.io.IOException;
|
2015-04-07 18:18:37 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2011-01-29 15:09:35 +00:00
|
|
|
import java.util.Set;
|
2010-11-15 20:35:36 +00:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2015-04-07 18:18:37 +00:00
|
|
|
import net.sourceforge.plantuml.command.regex.MyPattern;
|
|
|
|
import net.sourceforge.plantuml.utils.StartUtils;
|
|
|
|
import net.sourceforge.plantuml.StringUtils;
|
|
|
|
|
2010-11-15 20:35:36 +00:00
|
|
|
public class Preprocessor implements ReadLine {
|
|
|
|
|
2015-04-07 18:18:37 +00:00
|
|
|
private static final String ID = "[A-Za-z_][A-Za-z_0-9]*";
|
|
|
|
private static final String ARG = "(?:\\(" + ID + "(?:," + ID + ")*?\\))?";
|
|
|
|
private static final Pattern definePattern = MyPattern.cmpile("^[%s]*!define[%s]+(" + ID + ARG + ")"
|
|
|
|
+ "(?:[%s]+(.*))?$");
|
|
|
|
private static final Pattern undefPattern = MyPattern.cmpile("^[%s]*!undef[%s]+(" + ID + ")$");
|
|
|
|
private static final Pattern definelongPattern = MyPattern.cmpile("^[%s]*!definelong[%s]+(" + ID + ARG + ")");
|
|
|
|
private static final Pattern enddefinelongPattern = MyPattern.cmpile("^[%s]*!enddefinelong[%s]*$");
|
2010-11-15 20:35:36 +00:00
|
|
|
|
|
|
|
private final Defines defines;
|
|
|
|
private final PreprocessorInclude rawSource;
|
2015-04-07 18:18:37 +00:00
|
|
|
private final ReadLineInsertable source;
|
2010-11-15 20:35:36 +00:00
|
|
|
|
2013-12-10 19:36:50 +00:00
|
|
|
public Preprocessor(ReadLine reader, String charset, Defines defines, Set<File> filesUsed, File newCurrentDir) {
|
2010-11-15 20:35:36 +00:00
|
|
|
this.defines = defines;
|
2015-04-07 18:18:37 +00:00
|
|
|
this.defines.saveState();
|
|
|
|
this.rawSource = new PreprocessorInclude(reader, defines, charset, filesUsed, newCurrentDir);
|
|
|
|
this.source = new ReadLineInsertable(new IfManager(rawSource, defines));
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String readLine() throws IOException {
|
2015-04-07 18:18:37 +00:00
|
|
|
final String s = source.readLine();
|
2010-11-15 20:35:36 +00:00
|
|
|
if (s == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-04-07 18:18:37 +00:00
|
|
|
if (StartUtils.isArobaseStartDiagram(s)) {
|
|
|
|
this.defines.restoreState();
|
|
|
|
}
|
2010-11-15 20:35:36 +00:00
|
|
|
|
|
|
|
Matcher m = definePattern.matcher(s);
|
|
|
|
if (m.find()) {
|
|
|
|
return manageDefine(m);
|
|
|
|
}
|
|
|
|
|
2015-04-07 18:18:37 +00:00
|
|
|
m = definelongPattern.matcher(s);
|
|
|
|
if (m.find()) {
|
|
|
|
return manageDefineLong(m);
|
|
|
|
}
|
|
|
|
|
2010-11-15 20:35:36 +00:00
|
|
|
m = undefPattern.matcher(s);
|
|
|
|
if (m.find()) {
|
|
|
|
return manageUndef(m);
|
|
|
|
}
|
|
|
|
|
2015-04-07 18:18:37 +00:00
|
|
|
if (ignoreDefineDuringSeveralLines > 0) {
|
|
|
|
ignoreDefineDuringSeveralLines--;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
final List<String> result = defines.applyDefines(s);
|
|
|
|
if (result.size() > 1) {
|
|
|
|
ignoreDefineDuringSeveralLines = result.size() - 2;
|
|
|
|
source.insert(result.subList(1, result.size() - 1));
|
|
|
|
}
|
|
|
|
return result.get(0);
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 18:18:37 +00:00
|
|
|
private int ignoreDefineDuringSeveralLines = 0;
|
|
|
|
|
2010-11-15 20:35:36 +00:00
|
|
|
private String manageUndef(Matcher m) throws IOException {
|
|
|
|
defines.undefine(m.group(1));
|
|
|
|
return this.readLine();
|
|
|
|
}
|
|
|
|
|
2015-04-07 18:18:37 +00:00
|
|
|
private String manageDefineLong(Matcher m) throws IOException {
|
|
|
|
final String group1 = m.group(1);
|
|
|
|
final List<String> def = new ArrayList<String>();
|
|
|
|
while (true) {
|
|
|
|
final String read = this.readLine();
|
|
|
|
if (read == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
def.add(read);
|
|
|
|
if (enddefinelongPattern.matcher(read).find()) {
|
|
|
|
defines.define(group1, def);
|
|
|
|
return this.readLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-15 20:35:36 +00:00
|
|
|
private String manageDefine(Matcher m) throws IOException {
|
2015-04-07 18:18:37 +00:00
|
|
|
final String group1 = m.group(1);
|
|
|
|
final String group2 = m.group(2);
|
|
|
|
if (group2 == null) {
|
|
|
|
defines.define(group1, null);
|
|
|
|
} else {
|
|
|
|
final List<String> strings = defines.applyDefines(group2);
|
|
|
|
if (strings.size() > 1) {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
final StringBuilder value = new StringBuilder(strings.get(0));
|
|
|
|
while (StringUtils.endsWithBackslash(value.toString())) {
|
|
|
|
value.setLength(value.length() - 1);
|
|
|
|
final String read = this.readLine();
|
|
|
|
value.append(read);
|
|
|
|
}
|
|
|
|
final List<String> li = new ArrayList<String>();
|
|
|
|
li.add(value.toString());
|
|
|
|
defines.define(group1, li);
|
|
|
|
}
|
2010-11-15 20:35:36 +00:00
|
|
|
return this.readLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getLineNumber() {
|
|
|
|
return rawSource.getLineNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void close() throws IOException {
|
|
|
|
rawSource.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|