1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-01 08:00:48 +00:00
plantuml/src/net/sourceforge/plantuml/tim/expression/TokenType.java

137 lines
4.5 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.expression;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.text.TLineType;
2019-03-29 22:14:07 +00:00
import net.sourceforge.plantuml.tim.Eater;
import net.sourceforge.plantuml.tim.EaterException;
2019-03-29 22:14:07 +00:00
public enum TokenType {
// ::remove folder when __HAXE__
2022-11-08 18:45:10 +00:00
QUOTED_STRING, JSON_DATA, OPERATOR, OPEN_PAREN_MATH, COMMA, CLOSE_PAREN_MATH, NUMBER, PLAIN_TEXT, SPACES,
FUNCTION_NAME, OPEN_PAREN_FUNC, CLOSE_PAREN_FUNC;
public static final char COMMERCIAL_MINUS_SIGN = '\u2052';
2019-03-29 22:14:07 +00:00
private boolean isSingleChar1() {
return this == OPEN_PAREN_MATH || this == COMMA || this == CLOSE_PAREN_MATH;
}
private static boolean isPlainTextBreak(char ch, char ch2) {
final TokenType tmp = fromChar(ch, ch2);
2022-11-08 18:45:10 +00:00
if (tmp.isSingleChar1() || tmp == TokenType.OPERATOR || tmp == SPACES)
2019-03-29 22:14:07 +00:00
return true;
2022-11-08 18:45:10 +00:00
2019-03-29 22:14:07 +00:00
return false;
}
private static TokenType fromChar(char ch, char ch2) {
TokenType result = PLAIN_TEXT;
2022-11-08 18:45:10 +00:00
if (TLineType.isQuote(ch))
2019-03-29 22:14:07 +00:00
result = QUOTED_STRING;
2022-11-08 18:45:10 +00:00
else if (TokenOperator.getTokenOperator(ch, ch2) != null)
2019-03-29 22:14:07 +00:00
result = OPERATOR;
2022-11-08 18:45:10 +00:00
else if (ch == '(')
2019-03-29 22:14:07 +00:00
result = OPEN_PAREN_MATH;
2022-11-08 18:45:10 +00:00
else if (ch == ')')
2019-03-29 22:14:07 +00:00
result = CLOSE_PAREN_MATH;
2022-11-08 18:45:10 +00:00
else if (ch == ',')
2019-03-29 22:14:07 +00:00
result = COMMA;
2022-11-08 18:45:10 +00:00
else if (TLineType.isLatinDigit(ch))
2019-03-29 22:14:07 +00:00
result = NUMBER;
2022-11-08 18:45:10 +00:00
else if (TLineType.isSpaceChar(ch))
2019-03-29 22:14:07 +00:00
result = SPACES;
2022-11-08 18:45:10 +00:00
2019-03-29 22:14:07 +00:00
return result;
}
final static public Token eatOneToken(Token lastToken, Eater eater, boolean manageColon)
throws EaterException {
2022-11-08 18:45:10 +00:00
char ch = eater.peekChar();
if (ch == 0)
2019-03-29 22:14:07 +00:00
return null;
2022-11-08 18:45:10 +00:00
if (manageColon && ch == ':')
2019-04-21 20:40:01 +00:00
return null;
2022-11-08 18:45:10 +00:00
if (ch == '-' && isTheMinusAnOperation(lastToken))
ch = COMMERCIAL_MINUS_SIGN;
2019-03-29 22:14:07 +00:00
final TokenOperator tokenOperator = TokenOperator.getTokenOperator(ch, eater.peekCharN2());
if (TLineType.isQuote(ch)) {
2020-03-03 22:29:34 +00:00
return new Token(eater.eatAndGetQuotedString(), TokenType.QUOTED_STRING, null);
2019-03-29 22:14:07 +00:00
} else if (tokenOperator != null) {
if (tokenOperator.getDisplay().length() == 1) {
2022-11-08 18:45:10 +00:00
eater.eatOneChar();
return new Token(ch, TokenType.OPERATOR, null);
2019-03-29 22:14:07 +00:00
}
2022-11-08 18:45:10 +00:00
2020-03-03 22:29:34 +00:00
return new Token("" + eater.eatOneChar() + eater.eatOneChar(), TokenType.OPERATOR, null);
2019-03-29 22:14:07 +00:00
} else if (ch == '(') {
2020-03-03 22:29:34 +00:00
return new Token(eater.eatOneChar(), TokenType.OPEN_PAREN_MATH, null);
2019-03-29 22:14:07 +00:00
} else if (ch == ')') {
2020-03-03 22:29:34 +00:00
return new Token(eater.eatOneChar(), TokenType.CLOSE_PAREN_MATH, null);
2019-03-29 22:14:07 +00:00
} else if (ch == ',') {
2020-03-03 22:29:34 +00:00
return new Token(eater.eatOneChar(), TokenType.COMMA, null);
2022-11-08 18:45:10 +00:00
} else if (TLineType.isLatinDigit(ch) || ch == '-') {
2020-03-03 22:29:34 +00:00
return new Token(eater.eatAndGetNumber(), TokenType.NUMBER, null);
2019-03-29 22:14:07 +00:00
} else if (TLineType.isSpaceChar(ch)) {
2020-03-03 22:29:34 +00:00
return new Token(eater.eatAndGetSpaces(), TokenType.SPACES, null);
2019-03-29 22:14:07 +00:00
}
2020-03-03 22:29:34 +00:00
return new Token(eatAndGetTokenPlainText(eater), TokenType.PLAIN_TEXT, null);
2019-03-29 22:14:07 +00:00
}
2022-11-08 18:45:10 +00:00
private static boolean isTheMinusAnOperation(Token lastToken) {
if (lastToken == null)
return false;
final TokenType type = lastToken.getTokenType();
2022-11-25 17:50:02 +00:00
if (type == TokenType.OPERATOR || type == TokenType.OPEN_PAREN_MATH || type == TokenType.COMMA)
2022-11-08 18:45:10 +00:00
return false;
return true;
}
static private String eatAndGetTokenPlainText(Eater eater) throws EaterException {
2019-03-29 22:14:07 +00:00
final StringBuilder result = new StringBuilder();
while (true) {
final char ch = eater.peekChar();
2022-11-08 18:45:10 +00:00
if (ch == 0 || TokenType.isPlainTextBreak(ch, eater.peekCharN2()))
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(eater.eatOneChar());
}
}
}