2022-12-13 17:30:41 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
2023-02-22 18:43:48 +00:00
|
|
|
* (C) Copyright 2009-2024, Arnaud Roques
|
2022-12-13 17:30:41 +00:00
|
|
|
*
|
2023-02-22 18:43:48 +00:00
|
|
|
* Project Info: https://plantuml.com
|
2022-12-13 17:30:41 +00:00
|
|
|
*
|
|
|
|
* If you like this project or if you find it useful, you can support us at:
|
|
|
|
*
|
2023-02-22 18:43:48 +00:00
|
|
|
* https://plantuml.com/patreon (only 1$ per month!)
|
|
|
|
* https://plantuml.com/paypal
|
2022-12-13 17:30:41 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Original Author: Arnaud Roques
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2023-02-02 17:59:43 +00:00
|
|
|
package net.sourceforge.plantuml.regexdiagram;
|
2022-12-13 17:30:41 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.ArrayDeque;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Deque;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import net.sourceforge.plantuml.FileFormatOption;
|
|
|
|
import net.sourceforge.plantuml.TitledDiagram;
|
|
|
|
import net.sourceforge.plantuml.command.CommandExecutionResult;
|
|
|
|
import net.sourceforge.plantuml.core.DiagramDescription;
|
|
|
|
import net.sourceforge.plantuml.core.ImageData;
|
|
|
|
import net.sourceforge.plantuml.core.UmlSource;
|
|
|
|
import net.sourceforge.plantuml.ebnf.ETile;
|
|
|
|
import net.sourceforge.plantuml.ebnf.ETileAlternation;
|
|
|
|
import net.sourceforge.plantuml.ebnf.ETileBox;
|
|
|
|
import net.sourceforge.plantuml.ebnf.ETileConcatenation;
|
|
|
|
import net.sourceforge.plantuml.ebnf.ETileOneOrMore;
|
|
|
|
import net.sourceforge.plantuml.ebnf.ETileOptional;
|
|
|
|
import net.sourceforge.plantuml.ebnf.ETileZeroOrMore;
|
|
|
|
import net.sourceforge.plantuml.ebnf.Symbol;
|
|
|
|
import net.sourceforge.plantuml.ebnf.TextBlockable;
|
2023-02-02 17:59:43 +00:00
|
|
|
import net.sourceforge.plantuml.klimt.color.HColor;
|
|
|
|
import net.sourceforge.plantuml.klimt.color.HColorSet;
|
|
|
|
import net.sourceforge.plantuml.klimt.color.HColors;
|
2023-02-22 18:43:48 +00:00
|
|
|
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
|
2023-02-02 17:59:43 +00:00
|
|
|
import net.sourceforge.plantuml.klimt.font.FontConfiguration;
|
|
|
|
import net.sourceforge.plantuml.klimt.font.StringBounder;
|
2023-02-22 18:43:48 +00:00
|
|
|
import net.sourceforge.plantuml.klimt.geom.XDimension2D;
|
|
|
|
import net.sourceforge.plantuml.klimt.shape.AbstractTextBlock;
|
|
|
|
import net.sourceforge.plantuml.klimt.shape.TextBlock;
|
|
|
|
import net.sourceforge.plantuml.klimt.shape.TextBlockUtils;
|
|
|
|
import net.sourceforge.plantuml.skin.UmlDiagramType;
|
|
|
|
import net.sourceforge.plantuml.style.ISkinParam;
|
2022-12-13 17:30:41 +00:00
|
|
|
import net.sourceforge.plantuml.style.PName;
|
|
|
|
import net.sourceforge.plantuml.style.Style;
|
2022-12-17 11:01:10 +00:00
|
|
|
import net.sourceforge.plantuml.utils.BlocLines;
|
|
|
|
import net.sourceforge.plantuml.utils.CharInspector;
|
2022-12-13 17:30:41 +00:00
|
|
|
|
|
|
|
public class PSystemRegex extends TitledDiagram {
|
|
|
|
|
|
|
|
private final List<TextBlockable> expressions = new ArrayList<>();
|
|
|
|
|
|
|
|
public PSystemRegex(UmlSource source) {
|
|
|
|
super(source, UmlDiagramType.REGEX, null);
|
|
|
|
final ISkinParam skinParam = getSkinParam();
|
|
|
|
this.style = ETile.getStyleSignature().getMergedStyle(skinParam.getCurrentStyleBuilder());
|
|
|
|
this.fontConfiguration = style.getFontConfiguration(skinParam.getIHtmlColorSet());
|
|
|
|
this.colorSet = skinParam.getIHtmlColorSet();
|
|
|
|
this.lineColor = style.value(PName.LineColor).asColor(skinParam.getIHtmlColorSet());
|
|
|
|
}
|
|
|
|
|
|
|
|
public DiagramDescription getDescription() {
|
|
|
|
return new DiagramDescription("(Regular Expression)");
|
|
|
|
}
|
|
|
|
|
|
|
|
private final Deque<ETile> stack = new ArrayDeque<>();
|
|
|
|
private final FontConfiguration fontConfiguration;
|
|
|
|
private final Style style;
|
|
|
|
private final HColorSet colorSet;
|
|
|
|
private final HColor lineColor;
|
|
|
|
|
|
|
|
// public CommandExecutionResult addBlocLines(BlocLines blines, String commentAbove, String commentBelow) {
|
|
|
|
// final boolean isCompact = getPragma().isDefine("compact");
|
|
|
|
// final CharIterator it = new CharIteratorImpl(blines);
|
|
|
|
// final EbnfExpression tmp1 = EbnfExpression.create(it, isCompact, commentAbove, commentBelow);
|
|
|
|
// if (tmp1.isEmpty())
|
|
|
|
// return CommandExecutionResult.error("Unparsable expression");
|
|
|
|
// expressions.add(tmp1);
|
|
|
|
// return CommandExecutionResult.ok();
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public CommandExecutionResult addNote(final Display note, Colors colors) {
|
|
|
|
// expressions.add(new TextBlockable() {
|
|
|
|
// @Override
|
|
|
|
// public TextBlock getUDrawable(ISkinParam skinParam) {
|
|
|
|
// final FloatingNote f = FloatingNote.create(note, skinParam, SName.ebnf);
|
|
|
|
// return TextBlockUtils.withMargin(f, 0, 0, 5, 15);
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// return CommandExecutionResult.ok();
|
|
|
|
// }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ImageData exportDiagramNow(OutputStream os, int index, FileFormatOption fileFormatOption)
|
|
|
|
throws IOException {
|
|
|
|
return createImageBuilder(fileFormatOption).drawable(getTextBlock()).write(os);
|
|
|
|
}
|
|
|
|
|
2023-02-22 18:43:48 +00:00
|
|
|
@Override
|
2023-02-26 18:51:17 +00:00
|
|
|
protected TextBlock getTextBlock() {
|
2022-12-13 17:30:41 +00:00
|
|
|
// while (stack.size() > 1)
|
|
|
|
// concatenation();
|
|
|
|
final ETile peekFirst = stack.peekFirst();
|
|
|
|
final TextBlock tb = new AbstractTextBlock() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void drawU(UGraphic ug) {
|
|
|
|
peekFirst.drawU(ug.apply(HColors.BLACK));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public XDimension2D calculateDimension(StringBounder stringBounder) {
|
|
|
|
return peekFirst.calculateDimension(stringBounder);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return TextBlockUtils.addBackcolor(tb, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public CommandExecutionResult addBlocLines(BlocLines from) {
|
2022-12-17 11:01:10 +00:00
|
|
|
final CharInspector it = from.inspector();
|
2022-12-13 17:30:41 +00:00
|
|
|
final List<ReToken> parsed1 = RegexExpression.parse(it);
|
2022-12-16 16:20:14 +00:00
|
|
|
// System.err.println("parsed1=" + parsed1);
|
2022-12-13 17:30:41 +00:00
|
|
|
final List<ReToken> parsed2 = addImplicitConcatenation(parsed1);
|
2022-12-16 16:20:14 +00:00
|
|
|
// System.err.println("parsed2=" + parsed2);
|
2022-12-13 17:30:41 +00:00
|
|
|
final ShuntingYard shuntingYard = new ShuntingYard(parsed2.iterator());
|
|
|
|
final List<ReToken> result = shuntingYard.getOuputQueue();
|
2022-12-16 16:20:14 +00:00
|
|
|
// System.err.println("result=" + result);
|
2022-12-13 17:30:41 +00:00
|
|
|
for (ReToken token : result)
|
|
|
|
if (token.getType() == ReTokenType.SIMPLE_CHAR)
|
|
|
|
push(token, Symbol.TERMINAL_STRING1);
|
|
|
|
else if (token.getType() == ReTokenType.ESCAPED_CHAR)
|
|
|
|
push(token, Symbol.TERMINAL_STRING1);
|
|
|
|
else if (token.getType() == ReTokenType.GROUP)
|
|
|
|
push(token, Symbol.SPECIAL_SEQUENCE);
|
|
|
|
else if (token.getType() == ReTokenType.CLASS)
|
|
|
|
push(token, Symbol.LITTERAL);
|
|
|
|
else if (token.getType() == ReTokenType.ANCHOR)
|
|
|
|
push(token, Symbol.LITTERAL);
|
|
|
|
else if (token.getType() == ReTokenType.CONCATENATION_IMPLICIT)
|
|
|
|
concatenation();
|
|
|
|
else if (token.getType() == ReTokenType.ALTERNATIVE)
|
|
|
|
alternation();
|
2022-12-16 16:20:14 +00:00
|
|
|
else if (token.getType() == ReTokenType.QUANTIFIER && token.getData().startsWith("*"))
|
2022-12-13 17:30:41 +00:00
|
|
|
repetitionZeroOrMore(false);
|
2022-12-16 16:20:14 +00:00
|
|
|
else if (token.getType() == ReTokenType.QUANTIFIER && token.getData().startsWith("+"))
|
2022-12-13 17:30:41 +00:00
|
|
|
repetitionOneOrMore();
|
2022-12-16 16:20:14 +00:00
|
|
|
else if (token.getType() == ReTokenType.QUANTIFIER && token.getData().startsWith("?"))
|
2022-12-13 17:30:41 +00:00
|
|
|
optional();
|
|
|
|
else
|
|
|
|
throw new UnsupportedOperationException(token.toString());
|
|
|
|
|
|
|
|
return CommandExecutionResult.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<ReToken> addImplicitConcatenation(List<ReToken> list) {
|
|
|
|
final List<ReToken> result = new ArrayList<>();
|
|
|
|
for (ReToken token : list) {
|
|
|
|
if (result.size() > 0
|
|
|
|
&& ReTokenType.needImplicitConcatenation(result.get(result.size() - 1).getType(), token.getType()))
|
|
|
|
result.add(new ReToken(ReTokenType.CONCATENATION_IMPLICIT, ""));
|
|
|
|
result.add(token);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void push(ReToken element, Symbol type) {
|
|
|
|
// final Symbol type = Symbol.LITTERAL;
|
|
|
|
stack.addFirst(new ETileBox(element.getData(), type, fontConfiguration, style, colorSet, getSkinParam()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void repetitionZeroOrMore(boolean isCompact) {
|
|
|
|
final ETile arg1 = stack.removeFirst();
|
|
|
|
if (isCompact)
|
|
|
|
stack.addFirst(new ETileZeroOrMore(arg1));
|
|
|
|
else
|
|
|
|
stack.addFirst(new ETileOptional(new ETileOneOrMore(arg1), getSkinParam()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void optional() {
|
|
|
|
final ETile arg1 = stack.removeFirst();
|
|
|
|
stack.addFirst(new ETileOptional(arg1, getSkinParam()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void repetitionOneOrMore() {
|
|
|
|
final ETile arg1 = stack.removeFirst();
|
|
|
|
stack.addFirst(new ETileOneOrMore(arg1));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void alternation() {
|
|
|
|
final ETile arg1 = stack.removeFirst();
|
|
|
|
final ETile arg2 = stack.removeFirst();
|
|
|
|
if (arg1 instanceof ETileAlternation) {
|
|
|
|
arg1.push(arg2);
|
|
|
|
stack.addFirst(arg1);
|
|
|
|
} else if (arg2 instanceof ETileAlternation) {
|
|
|
|
arg2.push(arg1);
|
|
|
|
stack.addFirst(arg2);
|
|
|
|
} else {
|
|
|
|
final ETile concat = new ETileAlternation();
|
|
|
|
concat.push(arg1);
|
|
|
|
concat.push(arg2);
|
|
|
|
stack.addFirst(concat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void concatenation() {
|
|
|
|
final ETile arg1 = stack.removeFirst();
|
|
|
|
final ETile arg2 = stack.removeFirst();
|
|
|
|
if (isBoxMergeable(arg1) && isBoxMergeable(arg2)) {
|
|
|
|
final ETileBox box1 = (ETileBox) arg1;
|
|
|
|
final ETileBox box2 = (ETileBox) arg2;
|
|
|
|
stack.addFirst(box2.mergeWith(box1));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isConcatenation1Mergeable(arg1) && isBoxMergeable(arg2)) {
|
|
|
|
final ETileConcatenation concat1 = (ETileConcatenation) arg1;
|
|
|
|
final ETileBox box1 = (ETileBox) concat1.getFirst();
|
|
|
|
final ETileBox box2 = (ETileBox) arg2;
|
|
|
|
concat1.overideFirst(box2.mergeWith(box1));
|
|
|
|
stack.addFirst(concat1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (arg1 instanceof ETileConcatenation) {
|
|
|
|
arg1.push(arg2);
|
|
|
|
stack.addFirst(arg1);
|
|
|
|
} else if (arg2 instanceof ETileConcatenation) {
|
|
|
|
arg2.push(arg1);
|
|
|
|
stack.addFirst(arg2);
|
|
|
|
} else {
|
|
|
|
final ETile concat = new ETileConcatenation();
|
|
|
|
concat.push(arg1);
|
|
|
|
concat.push(arg2);
|
|
|
|
stack.addFirst(concat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isConcatenation1Mergeable(ETile tile) {
|
|
|
|
if (tile instanceof ETileConcatenation) {
|
|
|
|
final ETileConcatenation concat = (ETileConcatenation) tile;
|
|
|
|
return isBoxMergeable(concat.getFirst());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isBoxMergeable(ETile tile) {
|
|
|
|
if (tile instanceof ETileBox) {
|
|
|
|
final ETileBox box = (ETileBox) tile;
|
|
|
|
return box.getSymbol() == Symbol.TERMINAL_STRING1;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|