001// Generated by delombok at Sat Feb 25 17:58:34 UTC 2023
002package io.cui.tools.formatting.template.lexer;
003
004import static io.cui.tools.base.Preconditions.checkArgument;
005import static io.cui.tools.collect.CollectionLiterals.immutableList;
006import static io.cui.tools.string.MoreStrings.isEmpty;
007import static java.util.Objects.requireNonNull;
008import java.io.Serializable;
009import java.util.List;
010import io.cui.tools.formatting.template.FormatterSupport;
011import io.cui.tools.formatting.template.token.Token;
012
013/**
014 * Functionality of scanning plain text and split this to tokens
015 *
016 * @param <T> bounds lexer to one type to avoid cross using accidentally
017 * @author Eugen Fischer
018 */
019public abstract class Lexer<T extends FormatterSupport> implements Serializable {
020    private static final long serialVersionUID = 8645233576605974741L;
021    private final List<String> tokenList;
022
023    /**
024     * Constructor of Lexer.
025     * Source provide information of "tokens" which he supports.
026     * Therefore {@link FormatterSupport#getSupportedPropertyNames()} will be used.
027     *
028     * @param source must not be null
029     * @throws NullPointerException if source is missing
030     * @throws IllegalArgumentException if attribute is null or empty
031     */
032    protected Lexer(final T source) {
033        requireNonNull(source, "Source must not be null");
034        this.tokenList = immutableList(requireNonNull(source.getSupportedPropertyNames()));
035        for (final String attribute : this.tokenList) {
036            checkArgument(!isEmpty(attribute), "Attributes must not be null or empty. \'" + this.tokenList + "\'");
037        }
038    }
039
040    /**
041     * Throw IllegalArgumentException with information about wrong token and supported tokens
042     *
043     * @param wrongToken must not be null
044     * @param allowedTokens must not be null
045     */
046    protected static final void throwUnsupportedTokenException(final String wrongToken, final List<String> allowedTokens) {
047        final var builder = new StringBuilder();
048        builder.append("Unsupported token \'").append(wrongToken).append("\' was detected.\n").append("Allowed are :\n");
049        for (final String allowedToken : allowedTokens) {
050            builder.append(" - ").append(allowedToken).append("\n");
051        }
052        throw new IllegalArgumentException(builder.toString());
053    }
054
055    /**
056     * Parse template into Token List according attribute list
057     *
058     * @param input template string
059     * @return created list of token, list could be empty if input template is null or empty
060     * @throws IllegalArgumentException if template include unknown token, or doesn't fit the rules
061     */
062    public abstract List<Token> scan(final String input);
063
064    /**
065     * Validate template by scan this
066     *
067     * @param input to be validated
068     */
069    public final void validateTemplate(final String input) {
070        scan(input);
071    }
072
073
074    /**
075     * Supported expression language
076     */
077    public enum ExpressionLanguage {
078        /** {@code [attribute1][attribute2]..[attribute n]} */
079        SIMPLE_SQUARED_BRACKTES, 
080        /** {attribute1}{attribute2}..{attribute n} */
081        SIMPLE_CURLY_BRACKETS, 
082        /** {@code <attribute1><attribute2>..<attribute n>} */
083        SIMPLE_ANGLE_BRACKET, 
084        /**
085         * usage of String Template Expression Language
086         *
087         * @see <a
088         *      href="http://www.antlr.org/wiki/display/ST/StringTemplate+3+Documentation">
089         *      Documentation</a>
090         */
091        STEL;
092    }
093
094    @java.lang.Override
095    @java.lang.SuppressWarnings("all")
096    @lombok.Generated
097    public java.lang.String toString() {
098        return "Lexer(tokenList=" + this.getTokenList() + ")";
099    }
100
101    @java.lang.Override
102    @java.lang.SuppressWarnings("all")
103    @lombok.Generated
104    public boolean equals(final java.lang.Object o) {
105        if (o == this) return true;
106        if (!(o instanceof Lexer)) return false;
107        final Lexer<?> other = (Lexer<?>) o;
108        if (!other.canEqual((java.lang.Object) this)) return false;
109        final java.lang.Object this$tokenList = this.getTokenList();
110        final java.lang.Object other$tokenList = other.getTokenList();
111        if (this$tokenList == null ? other$tokenList != null : !this$tokenList.equals(other$tokenList)) return false;
112        return true;
113    }
114
115    @java.lang.SuppressWarnings("all")
116    @lombok.Generated
117    protected boolean canEqual(final java.lang.Object other) {
118        return other instanceof Lexer;
119    }
120
121    @java.lang.Override
122    @java.lang.SuppressWarnings("all")
123    @lombok.Generated
124    public int hashCode() {
125        final int PRIME = 59;
126        int result = 1;
127        final java.lang.Object $tokenList = this.getTokenList();
128        result = result * PRIME + ($tokenList == null ? 43 : $tokenList.hashCode());
129        return result;
130    }
131
132    @java.lang.SuppressWarnings("all")
133    @lombok.Generated
134    protected List<String> getTokenList() {
135        return this.tokenList;
136    }
137}