| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.parser.css.impl; |
| 23 |
|
| 24 |
import java.text.MessageFormat; |
| 25 |
import java.util.List; |
| 26 |
import java.util.function.Predicate; |
| 27 |
|
| 28 |
import org.jetbrains.annotations.NotNull; |
| 29 |
|
| 30 |
import com.github.weisj.jsvg.logging.Logger; |
| 31 |
import com.github.weisj.jsvg.logging.Logger.Level; |
| 32 |
import com.github.weisj.jsvg.logging.impl.LogFactory; |
| 33 |
|
| 34 |
|
| 35 |
public final class Lexer { |
| 36 |
private static final Logger LOGGER = LogFactory.createLogger(Lexer.class); |
| 37 |
|
| 38 |
private final @NotNull List<char[]> input; |
| 39 |
private int listIndex = 0; |
| 40 |
private int index = 0; |
| 41 |
|
| 42 |
private boolean inRuleDefinition; |
| 43 |
private boolean parsingRaw; |
| 44 |
private char current; |
| 45 |
|
| 46 |
|
| 47 |
public Lexer(@NotNull List<char[]> input, SimpleCssParser.Mode mode) { |
| 48 |
this.input = input; |
| 49 |
this.inRuleDefinition = mode == SimpleCssParser.Mode.SINGLE_RULE; |
| 50 |
this.current = 0; |
| 51 |
} |
| 52 |
|
| 53 |
@NotNull |
| 54 |
public Token nextToken() { |
| 55 |
consumeWhiteSpace(); |
| 56 |
|
| 57 |
if (inRuleDefinition && parsingRaw) { |
| 58 |
|
| 59 |
parsingRaw = false; |
| 60 |
return new Token(TokenType.RAW_DATA, readWhile(c -> c != ';' && c != '}')); |
| 61 |
} |
| 62 |
|
| 63 |
if (isEof()) return new Token(TokenType.EOF); |
| 64 |
char c = current(); |
| 65 |
switch (c) { |
| 66 |
case '{': |
| 67 |
inRuleDefinition = true; |
| 68 |
parsingRaw = false; |
| 69 |
next(); |
| 70 |
return new Token(TokenType.CURLY_OPEN); |
| 71 |
case '}': |
| 72 |
inRuleDefinition = false; |
| 73 |
parsingRaw = false; |
| 74 |
next(); |
| 75 |
return new Token(TokenType.CURLY_CLOSE); |
| 76 |
case ':': |
| 77 |
parsingRaw = true; |
| 78 |
next(); |
| 79 |
return new Token(TokenType.COLON); |
| 80 |
case ';': |
| 81 |
next(); |
| 82 |
return new Token(TokenType.SEMICOLON); |
| 83 |
case ',': |
| 84 |
next(); |
| 85 |
return new Token(TokenType.COMMA); |
| 86 |
case '.': |
| 87 |
next(); |
| 88 |
return new Token(TokenType.CLASS_NAME, readIdentifier()); |
| 89 |
case '#': |
| 90 |
next(); |
| 91 |
return new Token(TokenType.ID_NAME, readIdentifier()); |
| 92 |
case '/': |
| 93 |
if (peekNext() == '*') { |
| 94 |
next(); |
| 95 |
next(); |
| 96 |
String comment = readWhile(n -> !(n == '*' && peekNext() == '/')); |
| 97 |
next(); |
| 98 |
next(); |
| 99 |
return new Token(TokenType.COMMENT, comment); |
| 100 |
} |
| 101 |
|
| 102 |
default: |
| 103 |
return new Token(TokenType.IDENTIFIER, readIdentifier()); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
private boolean isEof() { |
| 108 |
return listIndex >= input.size() |
| 109 |
|| (listIndex == input.size() - 1 && index >= input.get(listIndex).length); |
| 110 |
} |
| 111 |
|
| 112 |
private void consumeWhiteSpace() { |
| 113 |
while (Character.isWhitespace(current())) { |
| 114 |
next(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
private boolean isIdentifierCharStart(char c) { |
| 119 |
if ('A' <= c && c <= 'Z') return true; |
| 120 |
if ('a' <= c && c <= 'z') return true; |
| 121 |
if (c == '-') return true; |
| 122 |
return c == '_'; |
| 123 |
} |
| 124 |
|
| 125 |
private boolean isIdentifierChar(char c) { |
| 126 |
if (isIdentifierCharStart(c)) return true; |
| 127 |
return '0' <= c && c <= '9'; |
| 128 |
} |
| 129 |
|
| 130 |
private @NotNull String readIdentifier() { |
| 131 |
if (!isIdentifierCharStart(current()) || !isIdentifierChar(current())) { |
| 132 |
LOGGER.log(Level.WARNING, |
| 133 |
() -> MessageFormat.format("Identifier starting with unexpected char ''{0}''", current())); |
| 134 |
if (readWhile(this::isIdentifierChar).isEmpty()) { |
| 135 |
next(); |
| 136 |
} |
| 137 |
throw new ParserException(); |
| 138 |
} |
| 139 |
return readWhile(this::isIdentifierChar); |
| 140 |
} |
| 141 |
|
| 142 |
private @NotNull String readWhile(@NotNull Predicate<Character> filter) { |
| 143 |
if (isEof()) return ""; |
| 144 |
|
| 145 |
int startListIndex = listIndex; |
| 146 |
int startIndex = index; |
| 147 |
while (!isEof() && filter.test(current())) { |
| 148 |
next(); |
| 149 |
} |
| 150 |
int endListIndex = isEof() ? input.size() - 1 : listIndex; |
| 151 |
int endIndex = isEof() ? input.get(endListIndex).length : index; |
| 152 |
|
| 153 |
StringBuilder builder = new StringBuilder(); |
| 154 |
int start = startIndex; |
| 155 |
for (int i = startListIndex; i <= endListIndex; i++) { |
| 156 |
char[] segment = input.get(i); |
| 157 |
int end = (i == endListIndex) ? endIndex : segment.length; |
| 158 |
|
| 159 |
builder.append(String.valueOf(segment, start, end - start)); |
| 160 |
start = 0; |
| 161 |
} |
| 162 |
return builder.toString(); |
| 163 |
} |
| 164 |
|
| 165 |
private char current() { |
| 166 |
if (current == 0) { |
| 167 |
current = nextChar(); |
| 168 |
} |
| 169 |
return current; |
| 170 |
} |
| 171 |
|
| 172 |
private char nextChar() { |
| 173 |
if (isEof()) return 0; |
| 174 |
return input.get(listIndex)[index]; |
| 175 |
} |
| 176 |
|
| 177 |
private char peekNext() { |
| 178 |
if (isEof()) return 0; |
| 179 |
if (index + 1 < input.get(listIndex).length) return input.get(listIndex)[index + 1]; |
| 180 |
int currentIndex = listIndex + 1; |
| 181 |
while (currentIndex < input.size()) { |
| 182 |
if (input.get(currentIndex).length > 0) { |
| 183 |
return input.get(currentIndex)[0]; |
| 184 |
} else { |
| 185 |
currentIndex++; |
| 186 |
} |
| 187 |
} |
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
private void next() { |
| 192 |
index++; |
| 193 |
if (index >= input.get(listIndex).length && listIndex + 1 < input.size()) { |
| 194 |
index = 0; |
| 195 |
listIndex++; |
| 196 |
} |
| 197 |
current = nextChar(); |
| 198 |
} |
| 199 |
} |