Debian Codegraph @

src/com/github/weisj/jsvg/parser/css/impl/SimpleCssParser.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2023-2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22 package com.github.weisj.jsvg.parser.css.impl;
23
24 import java.text.MessageFormat;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Objects;
29
30 import org.jetbrains.annotations.NotNull;
31
32 import com.github.weisj.jsvg.logging.Logger;
33 import com.github.weisj.jsvg.logging.Logger.Level;
34 import com.github.weisj.jsvg.logging.impl.LogFactory;
35 import com.github.weisj.jsvg.parser.css.CssParser;
36 import com.github.weisj.jsvg.parser.css.StyleProperty;
37
38 public final class SimpleCssParser implements CssParser {
39
40 private static final Logger LOGGER = LogFactory.createLogger(SimpleCssParser.class);
41
42 public enum Mode {
43 STYLE_SHEET,
44 SINGLE_RULE
45 }
46
47 @Override
48 public @NotNull SimpleStyleSheet parse(@NotNull List<char[]> input) {
49 return new Parser(input, Mode.STYLE_SHEET).parse();
50 }
51
52 public @NotNull List<@NotNull StyleProperty> parseRules(@NotNull List<char[]> input) {
53 return new Parser(input, Mode.SINGLE_RULE).parseRules();
54 }
55
56 private static final class Parser {
57
58 private final @NotNull Lexer lexer;
59 private final @NotNull SimpleStyleSheet sheet = new SimpleStyleSheet();
60 private final TokenType ruleListEndType;
61 private @NotNull Token current = new Token(TokenType.START);
62
63
64 private Parser(@NotNull List<char[]> input, Mode mode) {
65 this.ruleListEndType = mode == Mode.SINGLE_RULE
66 ? TokenType.EOF
67 : TokenType.CURLY_CLOSE;
68 this.lexer = new Lexer(input, mode);
69 }
70
71 private void next() {
72 Token next;
73 do {
74 next = lexer.nextToken();
75 } while (next.type() == TokenType.COMMENT);
76 current = next;
77 }
78
79 private void expected(@NotNull String type) {
80 LOGGER.log(Level.WARNING, () -> MessageFormat.format("Expected ''{0}'' but got ''{1}''", type, current));
81 }
82
83 private void consumeOrSkipAllowedToken(TokenType type, TokenType allowedTokeToSkip) {
84 if (current.type() != type) {
85 if (current.type() != allowedTokeToSkip) {
86 expected(type.toString());
87 throw new ParserException();
88 }
89 return;
90 }
91 next();
92 }
93
94 private void consume(TokenType type) {
95 consumeOrSkipAllowedToken(type, null);
96 }
97
98 private @NotNull String consumeValue(TokenType type) {
99 if (current.type() != type) {
100 expected(type.toString());
101 throw new ParserException();
102 }
103 if (current.data() == null) {
104 throw new ParserException();
105 }
106 String value = Objects.requireNonNull(current.data());
107 next();
108 return value;
109 }
110
111 private @NotNull List<Token> readIdentifierList() {
112 List<Token> list = new ArrayList<>();
113
114 while (current.type() != TokenType.CURLY_OPEN && current.type() != TokenType.EOF) {
115 TokenType type = current.type();
116 if (type != TokenType.IDENTIFIER
117 && type != TokenType.ID_NAME
118 && type != TokenType.CLASS_NAME) {
119 expected("identifier");
120 throw new ParserException();
121 }
122 list.add(current);
123 next();
124
125 if (current.type() == TokenType.COMMA) {
126 next();
127 } else {
128 break;
129 }
130 }
131 return list;
132 }
133
134 private @NotNull List<StyleProperty> readProperties() {
135 List<StyleProperty> list = new ArrayList<>();
136
137 while (current.type() != TokenType.CURLY_CLOSE && current.type() != TokenType.EOF) {
138 String name = consumeValue(TokenType.IDENTIFIER);
139 consume(TokenType.COLON);
140 String value = consumeValue(TokenType.RAW_DATA);
141 consumeOrSkipAllowedToken(TokenType.SEMICOLON, ruleListEndType);
142 list.add(new StyleProperty(name, value.trim()));
143 }
144
145 return list;
146 }
147
148 private void skipToNextDefinition() {
149 while (current.type() != TokenType.CURLY_CLOSE && current.type() != TokenType.EOF) {
150 try {
151 next();
152 } catch (ParserException ignored) {
153 // Errors are to be expected at this point, as we are handling an invalid definition.
154 }
155 }
156 if (current.type() != TokenType.EOF) {
157 current = new Token(TokenType.START);
158 }
159 }
160
161 @NotNull
162 List<@NotNull StyleProperty> parseRules() {
163 try {
164 if (current.type() == TokenType.START) {
165 next();
166 }
167 return readProperties();
168 } catch (ParserException e) {
169 return Collections.emptyList();
170 }
171 }
172
173 @NotNull
174 SimpleStyleSheet parse() {
175 do {
176 try {
177 if (current.type() == TokenType.START) {
178 next();
179 }
180
181 List<Token> identifierList = readIdentifierList();
182
183 consume(TokenType.CURLY_OPEN);
184 List<StyleProperty> properties = readProperties();
185 consume(TokenType.CURLY_CLOSE);
186
187 for (Token token : identifierList) {
188 switch (token.type()) {
189 case CLASS_NAME:
190 sheet.addClassRules(Objects.requireNonNull(token.data()), properties);
191 break;
192 case ID_NAME:
193 sheet.addIdRules(Objects.requireNonNull(token.data()), properties);
194 break;
195 case IDENTIFIER:
196 sheet.addTagNameRules(Objects.requireNonNull(token.data()), properties);
197 break;
198 default:
199 throw new IllegalStateException("Toke = " + token);
200 }
201 }
202 } catch (ParserException e) {
203 skipToNextDefinition();
204 }
205 } while (current.type() != TokenType.EOF);
206 return sheet;
207 }
208 }
209 }