1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-07 10:50:53 +00:00
plantuml/src/net/sourceforge/plantuml/tim/Eater.java

369 lines
11 KiB
Java
Raw Normal View History

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;
import java.util.List;
2019-07-14 20:09:26 +00:00
import net.sourceforge.plantuml.json.Json;
import net.sourceforge.plantuml.json.JsonValue;
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;
import net.sourceforge.plantuml.tim.expression.Token;
import net.sourceforge.plantuml.tim.expression.TokenStack;
import net.sourceforge.plantuml.tim.expression.TokenType;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.LineLocation;
2019-03-29 22:14:07 +00:00
public abstract class Eater {
private int i = 0;
private final StringLocated stringLocated;
2019-03-29 22:14:07 +00:00
public Eater(StringLocated stringLocated) {
this.stringLocated = stringLocated;
2019-03-29 22:14:07 +00:00
}
public final LineLocation getLineLocation() {
return stringLocated.getLocation();
2020-03-03 22:29:34 +00:00
}
public final StringLocated getStringLocated() {
return stringLocated;
2020-03-03 22:29:34 +00:00
}
public abstract void analyze(TContext context, TMemory memory) throws EaterException, EaterExceptionLocated;
2019-03-29 22:14:07 +00:00
public int getCurrentPosition() {
return i;
}
final protected String eatAllToEnd() throws EaterException {
final String result = stringLocated.getString().substring(i);
i = stringLocated.length();
2019-03-29 22:14:07 +00:00
return result;
}
2021-06-27 16:50:40 +00:00
final public TValue eatExpression(TContext context, TMemory memory) throws EaterException, EaterExceptionLocated {
final char ch = peekChar();
if (ch == '{' || ch == '[') {
2022-11-08 18:45:10 +00:00
final String data = eatAllToEnd();
2023-11-27 20:06:09 +00:00
// System.err.println("data=" + data);
2020-03-03 22:29:34 +00:00
final JsonValue json = Json.parse(data);
2023-11-27 20:06:09 +00:00
// System.err.println("json=" + json);
2019-07-14 20:09:26 +00:00
return TValue.fromJson(json);
}
2020-03-03 22:29:34 +00:00
final TokenStack tokenStack = eatTokenStack();
return tokenStack.getResult(getStringLocated(), context, memory);
2020-03-03 22:29:34 +00:00
}
final protected TokenStack eatTokenStack() throws EaterException {
2019-03-29 22:14:07 +00:00
final TokenStack tokenStack = new TokenStack();
2019-04-21 20:40:01 +00:00
addIntoTokenStack(tokenStack, false);
2022-11-08 18:45:10 +00:00
if (tokenStack.size() == 0)
throw EaterException.located("Missing expression", stringLocated);
2022-11-08 18:45:10 +00:00
2020-03-03 22:29:34 +00:00
return tokenStack;
2019-03-29 22:14:07 +00:00
}
2020-04-19 16:04:39 +00:00
final protected TValue eatExpressionStopAtColon(TContext context, TMemory memory)
throws EaterException, EaterExceptionLocated {
2019-04-21 20:40:01 +00:00
final TokenStack tokenStack = new TokenStack();
addIntoTokenStack(tokenStack, true);
return tokenStack.getResult(getStringLocated(), context, memory);
2019-04-21 20:40:01 +00:00
}
final protected void addIntoTokenStack(TokenStack tokenStack, boolean stopAtColon) throws EaterException {
2022-11-08 18:45:10 +00:00
Token lastToken = null;
2019-03-29 22:14:07 +00:00
while (true) {
2022-11-08 18:45:10 +00:00
final Token token = TokenType.eatOneToken(lastToken, this, stopAtColon);
if (token == null)
2019-03-29 22:14:07 +00:00
return;
2022-11-08 18:45:10 +00:00
2019-03-29 22:14:07 +00:00
tokenStack.add(token);
2022-11-08 18:45:10 +00:00
if (token.getTokenType() != TokenType.SPACES)
lastToken = token;
2019-03-29 22:14:07 +00:00
}
}
final public String eatAndGetQuotedString() throws EaterException {
final char separator = peekChar();
2022-11-08 18:45:10 +00:00
if (TLineType.isQuote(separator) == false)
throw EaterException.located("quote10", stringLocated);
2022-11-08 18:45:10 +00:00
2019-03-29 22:14:07 +00:00
checkAndEatChar(separator);
final StringBuilder value = new StringBuilder();
addUpTo(separator, value);
checkAndEatChar(separator);
return value.toString();
}
final protected String eatAndGetOptionalQuotedString() throws EaterException {
final char quote = peekChar();
2022-11-08 18:45:10 +00:00
if (TLineType.isQuote(quote))
2019-03-29 22:14:07 +00:00
return eatAndGetQuotedString();
2022-11-08 18:45:10 +00:00
2019-03-29 22:14:07 +00:00
final StringBuilder value = new StringBuilder();
2019-08-26 17:07:21 +00:00
// DEPLICATE eatUntilCloseParenthesisOrComma
int level = 0;
while (true) {
char ch = peekChar();
2022-11-08 18:45:10 +00:00
if (ch == 0)
throw EaterException.located("until001", stringLocated);
2022-11-08 18:45:10 +00:00
if (level == 0 && (ch == ',' || ch == ')'))
2019-08-26 17:07:21 +00:00
return value.toString().trim();
2022-11-08 18:45:10 +00:00
2019-08-26 17:07:21 +00:00
ch = eatOneChar();
2022-11-08 18:45:10 +00:00
if (ch == '(')
2019-08-26 17:07:21 +00:00
level++;
2022-11-08 18:45:10 +00:00
else if (ch == ')')
2019-08-26 17:07:21 +00:00
level--;
2022-11-08 18:45:10 +00:00
2019-08-26 17:07:21 +00:00
value.append(ch);
}
// addUpTo(',', ')', value);
// return value.toString();
2019-03-29 22:14:07 +00:00
}
final public String eatAndGetNumber() throws EaterException {
final StringBuilder result = new StringBuilder();
while (true) {
final char ch = peekChar();
2022-11-08 18:45:10 +00:00
if (result.length() == 0 && ch == '-') {
result.append(eatOneChar());
continue;
2019-03-29 22:14:07 +00:00
}
2022-11-08 18:45:10 +00:00
if (ch == 0 || TLineType.isLatinDigit(ch) == false)
return result.toString();
2019-03-29 22:14:07 +00:00
result.append(eatOneChar());
}
}
final public String eatAndGetSpaces() throws EaterException {
final StringBuilder result = new StringBuilder();
while (true) {
final char ch = peekChar();
2022-11-08 18:45:10 +00:00
if (ch == 0 || TLineType.isSpaceChar(ch) == false)
2019-03-29 22:14:07 +00:00
return result.toString();
2022-11-08 18:45:10 +00:00
2019-03-29 22:14:07 +00:00
result.append(eatOneChar());
}
}
2019-05-24 19:59:31 +00:00
final protected String eatAndGetVarname() throws EaterException {
2019-03-29 22:14:07 +00:00
final StringBuilder varname = new StringBuilder("" + eatOneChar());
2022-11-08 18:45:10 +00:00
if (TLineType.isLetterOrUnderscoreOrDollar(varname.charAt(0)) == false)
throw EaterException.located("a002", stringLocated);
2022-11-08 18:45:10 +00:00
2019-03-29 22:14:07 +00:00
addUpToLastLetterOrUnderscoreOrDigit(varname);
return varname.toString();
}
2019-05-24 19:59:31 +00:00
final protected String eatAndGetFunctionName() throws EaterException {
2019-03-29 22:14:07 +00:00
final StringBuilder varname = new StringBuilder("" + eatOneChar());
2022-11-08 18:45:10 +00:00
if (TLineType.isLetterOrUnderscoreOrDollar(varname.charAt(0)) == false)
throw EaterException.located("a003", stringLocated);
2022-11-08 18:45:10 +00:00
2019-03-29 22:14:07 +00:00
addUpToLastLetterOrUnderscoreOrDigit(varname);
return varname.toString();
}
final public void skipSpaces() {
while (i < stringLocated.length() && Character.isWhitespace(stringLocated.charAt(i)))
2019-03-29 22:14:07 +00:00
i++;
2022-11-08 18:45:10 +00:00
2019-03-29 22:14:07 +00:00
}
final protected void skipUntilChar(char ch) {
while (i < stringLocated.length() && stringLocated.charAt(i) != ch)
2019-03-29 22:14:07 +00:00
i++;
2022-11-08 18:45:10 +00:00
2019-03-29 22:14:07 +00:00
}
final public char peekChar() {
if (i >= stringLocated.length())
2019-03-29 22:14:07 +00:00
return 0;
2022-11-08 18:45:10 +00:00
return stringLocated.charAt(i);
2019-03-29 22:14:07 +00:00
}
2021-01-10 20:52:19 +00:00
final public boolean matchAffectation() {
final String tmp = stringLocated.getString().substring(i);
2021-01-10 20:52:19 +00:00
final boolean result = tmp.matches("^\\$?[_\\p{L}][_\\p{L}0-9]*\\s*=.*");
return result;
}
2019-03-29 22:14:07 +00:00
final public char peekCharN2() {
if (i + 1 >= stringLocated.length())
2019-03-29 22:14:07 +00:00
return 0;
2022-11-08 18:45:10 +00:00
return stringLocated.charAt(i + 1);
2019-03-29 22:14:07 +00:00
}
final protected boolean hasNextChar() {
return i < stringLocated.length();
2019-03-29 22:14:07 +00:00
}
final public char eatOneChar() {
final char ch = stringLocated.charAt(i);
2019-03-29 22:14:07 +00:00
i++;
return ch;
}
final protected void checkAndEatChar(char ch) throws EaterException {
if (i >= stringLocated.length() || stringLocated.charAt(i) != ch)
throw EaterException.located("a001", stringLocated);
2022-11-08 18:45:10 +00:00
2019-03-29 22:14:07 +00:00
i++;
}
2019-05-24 19:59:31 +00:00
final protected boolean safeCheckAndEatChar(char ch) throws EaterException {
if (i >= stringLocated.length() || stringLocated.charAt(i) != ch)
2019-05-24 19:59:31 +00:00
return false;
2022-11-08 18:45:10 +00:00
2019-05-24 19:59:31 +00:00
i++;
return true;
}
2019-03-29 22:14:07 +00:00
final protected void optionallyEatChar(char ch) throws EaterException {
if (i >= stringLocated.length() || stringLocated.charAt(i) != ch)
2019-03-29 22:14:07 +00:00
return;
2022-11-08 18:45:10 +00:00
assert stringLocated.charAt(i) == ch;
2019-03-29 22:14:07 +00:00
i++;
}
final protected void checkAndEatChar(String s) throws EaterException {
2022-11-08 18:45:10 +00:00
for (int j = 0; j < s.length(); j++)
2019-03-29 22:14:07 +00:00
checkAndEatChar(s.charAt(j));
2022-11-08 18:45:10 +00:00
2019-03-29 22:14:07 +00:00
}
final protected void addUpToLastLetterOrUnderscoreOrDigit(StringBuilder sb) {
while (i < stringLocated.length()) {
final char ch = stringLocated.charAt(i);
2022-11-08 18:45:10 +00:00
if (TLineType.isLetterOrUnderscoreOrDigit(ch) == false)
2019-03-29 22:14:07 +00:00
return;
2022-11-08 18:45:10 +00:00
2019-03-29 22:14:07 +00:00
i++;
sb.append(ch);
}
}
final protected void addUpTo(char separator, StringBuilder sb) {
while (i < stringLocated.length()) {
2019-03-29 22:14:07 +00:00
final char ch = peekChar();
2022-11-08 18:45:10 +00:00
if (ch == separator)
2019-03-29 22:14:07 +00:00
return;
2022-11-08 18:45:10 +00:00
2019-03-29 22:14:07 +00:00
i++;
sb.append(ch);
}
}
final protected TFunctionImpl eatDeclareFunction(TContext context, TMemory memory, boolean unquoted,
StringLocated location, boolean allowNoParenthesis, TFunctionType type)
2020-04-19 16:04:39 +00:00
throws EaterException, EaterExceptionLocated {
2021-05-14 08:42:57 +00:00
final List<TFunctionArgument> args = new ArrayList<>();
2019-05-24 19:59:31 +00:00
final String functionName = eatAndGetFunctionName();
2019-03-29 22:14:07 +00:00
skipSpaces();
2019-05-24 19:59:31 +00:00
if (safeCheckAndEatChar('(') == false) {
2022-11-08 18:45:10 +00:00
if (allowNoParenthesis)
2020-04-19 16:04:39 +00:00
return new TFunctionImpl(functionName, args, unquoted, type);
2022-11-08 18:45:10 +00:00
throw EaterException.located("Missing opening parenthesis", stringLocated);
2019-05-24 19:59:31 +00:00
}
2019-03-29 22:14:07 +00:00
while (true) {
skipSpaces();
char ch = peekChar();
if (TLineType.isLetterOrUnderscoreOrDollar(ch)) {
2019-05-24 19:59:31 +00:00
final String varname = eatAndGetVarname();
2019-03-29 22:14:07 +00:00
skipSpaces();
final TValue defValue;
if (peekChar() == '=') {
eatOneChar();
final TokenStack def = TokenStack.eatUntilCloseParenthesisOrComma(this);
def.guessFunctions(location);
defValue = def.getResult(getStringLocated(), context, memory);
2019-03-29 22:14:07 +00:00
// System.err.println("result=" + defValue);
} else {
defValue = null;
}
args.add(new TFunctionArgument(varname, defValue));
} else if (ch == ',') {
checkAndEatChar(',');
} else if (ch == ')') {
checkAndEatChar(")");
break;
} else {
throw EaterException.located("Error in function definition", stringLocated);
2019-03-29 22:14:07 +00:00
}
}
skipSpaces();
2020-04-19 16:04:39 +00:00
return new TFunctionImpl(functionName, args, unquoted, type);
2019-03-29 22:14:07 +00:00
}
2020-04-19 16:04:39 +00:00
final protected TFunctionImpl eatDeclareReturnFunctionWithOptionalReturn(TContext context, TMemory memory,
boolean unquoted, StringLocated location) throws EaterException, EaterExceptionLocated {
2020-05-17 21:15:50 +00:00
final TFunctionImpl result = eatDeclareFunction(context, memory, unquoted, location, false,
TFunctionType.RETURN_FUNCTION);
2019-03-29 22:14:07 +00:00
if (peekChar() == 'r') {
checkAndEatChar("return");
skipSpaces();
final String line = "!return " + eatAllToEnd();
result.addBody(new StringLocated(line, location.getLocation()));
2019-06-26 19:24:49 +00:00
} else if (peekChar() == '!') {
checkAndEatChar("!return");
skipSpaces();
final String line = "!return " + eatAllToEnd();
result.addBody(new StringLocated(line, location.getLocation()));
2019-03-29 22:14:07 +00:00
}
return result;
}
2020-04-19 16:04:39 +00:00
final protected TFunctionImpl eatDeclareProcedure(TContext context, TMemory memory, boolean unquoted,
StringLocated location) throws EaterException, EaterExceptionLocated {
2020-05-17 21:15:50 +00:00
final TFunctionImpl result = eatDeclareFunction(context, memory, unquoted, location, false,
TFunctionType.PROCEDURE);
2020-04-19 16:04:39 +00:00
return result;
}
2019-03-29 22:14:07 +00:00
}