2019-03-29 22:14:07 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
2023-02-22 18:43:48 +00:00
|
|
|
* (C) Copyright 2009-2024, Arnaud Roques
|
2019-03-29 22:14:07 +00:00
|
|
|
*
|
2023-02-22 18:43:48 +00:00
|
|
|
* Project Info: https://plantuml.com
|
2019-03-29 22:14:07 +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
|
2019-03-29 22:14:07 +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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package net.sourceforge.plantuml.tim;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2020-05-17 21:15:50 +00:00
|
|
|
import java.util.Collections;
|
2019-05-24 19:59:31 +00:00
|
|
|
import java.util.HashMap;
|
2020-05-17 21:15:50 +00:00
|
|
|
import java.util.HashSet;
|
2019-03-29 22:14:07 +00:00
|
|
|
import java.util.List;
|
2019-05-24 19:59:31 +00:00
|
|
|
import java.util.Map;
|
2020-05-17 21:15:50 +00:00
|
|
|
import java.util.Set;
|
2019-03-29 22:14:07 +00:00
|
|
|
|
2023-02-02 17:59:43 +00:00
|
|
|
import net.sourceforge.plantuml.text.StringLocated;
|
|
|
|
import net.sourceforge.plantuml.text.TLineType;
|
2019-03-29 22:14:07 +00:00
|
|
|
import net.sourceforge.plantuml.tim.expression.TValue;
|
2022-12-17 11:01:10 +00:00
|
|
|
import net.sourceforge.plantuml.utils.LineLocation;
|
2019-03-29 22:14:07 +00:00
|
|
|
|
|
|
|
public class TFunctionImpl implements TFunction {
|
|
|
|
|
|
|
|
private final TFunctionSignature signature;
|
|
|
|
private final List<TFunctionArgument> args;
|
2021-05-14 08:42:57 +00:00
|
|
|
private final List<StringLocated> body = new ArrayList<>();
|
2019-03-29 22:14:07 +00:00
|
|
|
private final boolean unquoted;
|
2020-04-19 16:04:39 +00:00
|
|
|
private /* final */ TFunctionType functionType;// = TFunctionType.VOID;
|
2019-03-29 22:14:07 +00:00
|
|
|
private String legacyDefinition;
|
2020-04-19 16:04:39 +00:00
|
|
|
private boolean containsReturn;
|
2019-03-29 22:14:07 +00:00
|
|
|
|
2020-04-19 16:04:39 +00:00
|
|
|
public TFunctionImpl(String functionName, List<TFunctionArgument> args, boolean unquoted,
|
|
|
|
TFunctionType functionType) {
|
2021-05-14 08:42:57 +00:00
|
|
|
final Set<String> names = new HashSet<>();
|
2020-05-17 21:15:50 +00:00
|
|
|
for (TFunctionArgument tmp : args) {
|
|
|
|
names.add(tmp.getName());
|
|
|
|
}
|
|
|
|
this.signature = new TFunctionSignature(functionName, args.size(), names);
|
2019-03-29 22:14:07 +00:00
|
|
|
this.args = args;
|
|
|
|
this.unquoted = unquoted;
|
2020-04-19 16:04:39 +00:00
|
|
|
this.functionType = functionType;
|
2019-03-29 22:14:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 21:15:50 +00:00
|
|
|
public boolean canCover(int nbArg, Set<String> namedArguments) {
|
|
|
|
for (String n : namedArguments) {
|
|
|
|
if (signature.getNamedArguments().contains(n) == false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-03-29 22:14:07 +00:00
|
|
|
if (nbArg > args.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-17 21:15:50 +00:00
|
|
|
assert nbArg <= args.size();
|
|
|
|
int neededArgument = 0;
|
|
|
|
for (TFunctionArgument arg : args) {
|
|
|
|
if (namedArguments.contains(arg.getName())) {
|
|
|
|
continue;
|
2019-03-29 22:14:07 +00:00
|
|
|
}
|
2020-05-17 21:15:50 +00:00
|
|
|
if (arg.getOptionalDefaultValue() == null) {
|
|
|
|
neededArgument++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nbArg < neededArgument) {
|
|
|
|
return false;
|
2019-03-29 22:14:07 +00:00
|
|
|
}
|
2020-05-17 21:15:50 +00:00
|
|
|
assert nbArg >= neededArgument;
|
2019-03-29 22:14:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-17 21:15:50 +00:00
|
|
|
private TMemory getNewMemory(TMemory memory, List<TValue> values, Map<String, TValue> namedArguments) {
|
|
|
|
final Map<String, TValue> result = new HashMap<String, TValue>();
|
|
|
|
int ivalue = 0;
|
|
|
|
for (TFunctionArgument arg : args) {
|
|
|
|
final TValue value;
|
|
|
|
if (namedArguments.containsKey(arg.getName())) {
|
|
|
|
value = namedArguments.get(arg.getName());
|
|
|
|
} else if (ivalue < values.size()) {
|
|
|
|
value = values.get(ivalue);
|
|
|
|
ivalue++;
|
|
|
|
} else {
|
|
|
|
value = arg.getOptionalDefaultValue();
|
|
|
|
}
|
|
|
|
if (value == null) {
|
|
|
|
throw new IllegalStateException();
|
|
|
|
}
|
|
|
|
result.put(arg.getName(), value);
|
|
|
|
}
|
|
|
|
return memory.forkFromGlobal(result);
|
|
|
|
}
|
|
|
|
|
2019-03-29 22:14:07 +00:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "FUNCTION " + signature + " " + args;
|
|
|
|
}
|
|
|
|
|
2020-04-19 16:04:39 +00:00
|
|
|
public void addBody(StringLocated s) throws EaterExceptionLocated {
|
2019-03-29 22:14:07 +00:00
|
|
|
body.add(s);
|
2020-03-03 22:29:34 +00:00
|
|
|
if (s.getType() == TLineType.RETURN) {
|
2020-04-19 16:04:39 +00:00
|
|
|
this.containsReturn = true;
|
|
|
|
if (functionType == TFunctionType.PROCEDURE) {
|
2020-05-17 21:15:50 +00:00
|
|
|
throw EaterExceptionLocated
|
|
|
|
.located("A procedure cannot have !return directive. Declare it as a function instead ?", s);
|
2020-04-19 16:04:39 +00:00
|
|
|
// this.functionType = TFunctionType.RETURN;
|
|
|
|
}
|
2019-03-29 22:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 16:04:39 +00:00
|
|
|
public void executeProcedure(TContext context, TMemory memory, LineLocation location, String s)
|
2020-03-03 22:29:34 +00:00
|
|
|
throws EaterException, EaterExceptionLocated {
|
|
|
|
final EaterFunctionCall call = new EaterFunctionCall(new StringLocated(s, location),
|
|
|
|
context.isLegacyDefine(signature.getFunctionName()), unquoted);
|
|
|
|
call.analyze(context, memory);
|
2019-09-14 18:12:04 +00:00
|
|
|
final String endOfLine = call.getEndOfLine();
|
2019-05-24 19:59:31 +00:00
|
|
|
final List<TValue> args = call.getValues();
|
2020-05-17 21:15:50 +00:00
|
|
|
final Map<String, TValue> named = call.getNamedArguments();
|
|
|
|
executeProcedureInternal(context, memory, args, named);
|
2019-09-14 18:12:04 +00:00
|
|
|
context.appendEndOfLine(endOfLine);
|
2019-05-24 19:59:31 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 21:15:50 +00:00
|
|
|
public void executeProcedureInternal(TContext context, TMemory memory, List<TValue> args, Map<String, TValue> named)
|
2020-03-03 22:29:34 +00:00
|
|
|
throws EaterException, EaterExceptionLocated {
|
2020-04-19 16:04:39 +00:00
|
|
|
if (functionType != TFunctionType.PROCEDURE && functionType != TFunctionType.LEGACY_DEFINELONG) {
|
2019-05-24 19:59:31 +00:00
|
|
|
throw new IllegalStateException();
|
|
|
|
}
|
2020-05-17 21:15:50 +00:00
|
|
|
final TMemory copy = getNewMemory(memory, args, named);
|
2020-04-19 16:04:39 +00:00
|
|
|
context.executeLines(copy, body, TFunctionType.PROCEDURE, false);
|
2019-03-29 22:14:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 21:15:50 +00:00
|
|
|
public TValue executeReturnFunction(TContext context, TMemory memory, LineLocation location, List<TValue> args,
|
|
|
|
Map<String, TValue> named) throws EaterException, EaterExceptionLocated {
|
2019-03-29 22:14:07 +00:00
|
|
|
if (functionType == TFunctionType.LEGACY_DEFINE) {
|
2020-03-03 22:29:34 +00:00
|
|
|
return executeReturnLegacyDefine(location, context, memory, args);
|
2019-03-29 22:14:07 +00:00
|
|
|
}
|
2020-04-19 16:04:39 +00:00
|
|
|
if (functionType != TFunctionType.RETURN_FUNCTION) {
|
|
|
|
throw EaterException.unlocated("Illegal call here. Is there a return directive in your function?");
|
2019-03-29 22:14:07 +00:00
|
|
|
}
|
2020-05-17 21:15:50 +00:00
|
|
|
final TMemory copy = getNewMemory(memory, args, named);
|
2020-04-19 16:04:39 +00:00
|
|
|
final TValue result = context.executeLines(copy, body, TFunctionType.RETURN_FUNCTION, true);
|
|
|
|
if (result == null) {
|
|
|
|
throw EaterException.unlocated("No return directive found in your function");
|
2019-03-29 22:14:07 +00:00
|
|
|
}
|
2020-04-19 16:04:39 +00:00
|
|
|
return result;
|
2019-03-29 22:14:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 22:29:34 +00:00
|
|
|
private TValue executeReturnLegacyDefine(LineLocation location, TContext context, TMemory memory, List<TValue> args)
|
|
|
|
throws EaterException, EaterExceptionLocated {
|
2019-03-29 22:14:07 +00:00
|
|
|
if (legacyDefinition == null) {
|
|
|
|
throw new IllegalStateException();
|
|
|
|
}
|
2020-05-17 21:15:50 +00:00
|
|
|
final TMemory copy = getNewMemory(memory, args, Collections.<String, TValue>emptyMap());
|
2020-03-03 22:29:34 +00:00
|
|
|
final String tmp = context.applyFunctionsAndVariables(copy, location, legacyDefinition);
|
2019-05-24 19:59:31 +00:00
|
|
|
if (tmp == null) {
|
|
|
|
return TValue.fromString("");
|
|
|
|
}
|
2019-03-29 22:14:07 +00:00
|
|
|
return TValue.fromString(tmp);
|
|
|
|
// eaterReturn.execute(context, copy);
|
|
|
|
// // System.err.println("s3=" + eaterReturn.getValue2());
|
|
|
|
// return eaterReturn.getValue2();
|
|
|
|
}
|
|
|
|
|
|
|
|
public final TFunctionType getFunctionType() {
|
|
|
|
return functionType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final TFunctionSignature getSignature() {
|
|
|
|
return signature;
|
|
|
|
}
|
|
|
|
|
2020-04-19 16:04:39 +00:00
|
|
|
// public void setFunctionType(TFunctionType type) {
|
|
|
|
// this.functionType = type;
|
|
|
|
// }
|
2019-03-29 22:14:07 +00:00
|
|
|
|
|
|
|
public void setLegacyDefinition(String legacyDefinition) {
|
|
|
|
this.legacyDefinition = legacyDefinition;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isUnquoted() {
|
|
|
|
return unquoted;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasBody() {
|
|
|
|
return body.size() > 0;
|
|
|
|
}
|
|
|
|
|
2019-05-24 19:59:31 +00:00
|
|
|
public void finalizeEnddefinelong() {
|
|
|
|
if (functionType != TFunctionType.LEGACY_DEFINELONG) {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
if (body.size() == 1) {
|
|
|
|
this.functionType = TFunctionType.LEGACY_DEFINE;
|
|
|
|
this.legacyDefinition = body.get(0).getString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 16:04:39 +00:00
|
|
|
public final boolean doesContainReturn() {
|
|
|
|
return containsReturn;
|
|
|
|
}
|
|
|
|
|
2019-03-29 22:14:07 +00:00
|
|
|
}
|