|
1 | 1 | package net.quickwrite.fluent4j.ast; |
2 | 2 |
|
3 | | -import net.quickwrite.fluent4j.ast.placeable.AttributeReference; |
4 | | -import net.quickwrite.fluent4j.ast.placeable.SelectExpression; |
5 | | -import net.quickwrite.fluent4j.ast.placeable.TermReference; |
6 | | -import net.quickwrite.fluent4j.ast.placeable.base.FluentPlaceable; |
7 | | -import net.quickwrite.fluent4j.ast.placeable.base.FluentSelectable; |
8 | 3 | import net.quickwrite.fluent4j.exception.FluentParseException; |
9 | | -import net.quickwrite.fluent4j.exception.FluentSelectException; |
10 | | -import net.quickwrite.fluent4j.parser.FluentParser; |
11 | 4 | import net.quickwrite.fluent4j.util.StringSlice; |
12 | 5 | import net.quickwrite.fluent4j.util.StringSliceUtil; |
13 | 6 | import net.quickwrite.fluent4j.util.args.FluentArgs; |
14 | 7 | import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle; |
15 | | -import org.apache.commons.lang3.tuple.ImmutablePair; |
16 | | -import org.apache.commons.lang3.tuple.Pair; |
17 | 8 |
|
18 | | -import java.util.ArrayList; |
19 | 9 | import java.util.LinkedList; |
20 | 10 | import java.util.List; |
21 | 11 |
|
@@ -51,11 +41,17 @@ private List<FluentElement> parse(final StringSlice content) { |
51 | 41 |
|
52 | 42 | while (!content.isBigger()) { |
53 | 43 | if (content.getChar() == '{') { |
54 | | - elements.add(getPlaceable(content)); |
| 44 | + content.increment(); |
| 45 | + elements.add(StringSliceUtil.getPlaceable(content)); |
| 46 | + |
| 47 | + if (content.getChar() != '}') { |
| 48 | + throw new FluentParseException('}', content.getCharUTF16(), content.getAbsolutePosition()); |
| 49 | + } |
| 50 | + content.increment(); |
55 | 51 | } else { |
56 | 52 | FluentTextElement text = getText(content, firstLine); |
57 | 53 |
|
58 | | - if (text != null) { |
| 54 | + if (text != null && !text.isEmpty()) { |
59 | 55 | elements.add(text); |
60 | 56 | } |
61 | 57 | } |
@@ -88,107 +84,10 @@ private FluentTextElement getText(final StringSlice content, final boolean first |
88 | 84 | return new FluentTextElement(content.substring(start, content.getPosition()), whitespace); |
89 | 85 | } |
90 | 86 |
|
91 | | - private FluentPlaceable getPlaceable(final StringSlice content) { |
92 | | - content.increment(); |
93 | | - StringSliceUtil.skipWhitespaceAndNL(content); |
94 | | - |
95 | | - FluentPlaceable placeable = StringSliceUtil.getExpression(content); |
96 | | - |
97 | | - boolean canSelect = placeable instanceof FluentSelectable; |
98 | | - |
99 | | - StringSliceUtil.skipWhitespaceAndNL(content); |
100 | | - |
101 | | - if (canSelect && content.getChar() == '-') { |
102 | | - content.increment(); |
103 | | - if (content.getChar() != '>') { |
104 | | - throw new FluentParseException("->", "-" + content.getCharUTF16(), content.getAbsolutePosition()); |
105 | | - } |
106 | | - |
107 | | - content.increment(); |
108 | | - |
109 | | - StringSliceUtil.skipWhitespaceAndNL(content); |
110 | | - |
111 | | - List<FluentVariant> fluentVariants = new ArrayList<>(); |
112 | | - FluentVariant defaultVariant = null; |
113 | | - |
114 | | - while (content.getChar() != '}') { |
115 | | - Pair<FluentVariant, Boolean> variant = getVariant(content); |
116 | | - |
117 | | - fluentVariants.add(variant.getLeft()); |
118 | | - |
119 | | - if (!variant.getRight()) { |
120 | | - continue; |
121 | | - } |
122 | | - |
123 | | - if (defaultVariant != null) { |
124 | | - throw new FluentSelectException("Only one variant can be marked as default (*)"); |
125 | | - } |
126 | | - |
127 | | - defaultVariant = variant.getLeft(); |
128 | | - } |
129 | | - |
130 | | - if (defaultVariant == null) { |
131 | | - throw new FluentSelectException("Expected one of the variants to be marked as default (*)"); |
132 | | - } |
133 | | - |
134 | | - placeable = new SelectExpression(placeable, fluentVariants, defaultVariant); |
135 | | - } |
136 | | - |
137 | | - StringSliceUtil.skipWhitespaceAndNL(content); |
138 | | - |
139 | | - if (content.getChar() != '}') { |
140 | | - throw new FluentParseException('}', content.getCharUTF16(), content.getAbsolutePosition()); |
141 | | - } |
142 | | - |
143 | | - content.increment(); |
144 | | - |
145 | | - return placeable; |
146 | | - } |
147 | | - |
148 | 87 | public String getIdentifier() { |
149 | 88 | return this.identifier; |
150 | 89 | } |
151 | 90 |
|
152 | | - private Pair<FluentVariant, Boolean> getVariant(final StringSlice content) { |
153 | | - StringSliceUtil.skipWhitespaceAndNL(content); |
154 | | - |
155 | | - boolean isDefault = false; |
156 | | - |
157 | | - if (content.getChar() == '*') { |
158 | | - isDefault = true; |
159 | | - content.increment(); |
160 | | - } |
161 | | - |
162 | | - if (content.getChar() != '[') { |
163 | | - throw new FluentParseException('[', content.getCharUTF16(), content.getAbsolutePosition()); |
164 | | - } |
165 | | - |
166 | | - content.increment(); |
167 | | - |
168 | | - StringSliceUtil.skipWhitespace(content); |
169 | | - |
170 | | - StringSlice identifier = getVariantIdentifier(content); |
171 | | - |
172 | | - StringSliceUtil.skipWhitespace(content); |
173 | | - |
174 | | - if (content.getChar() != ']') { |
175 | | - throw getVariantException(content, identifier.toString(), "]"); |
176 | | - } |
177 | | - |
178 | | - content.increment(); |
179 | | - |
180 | | - final Pair<StringSlice, Integer> stringSliceContent = FluentParser.getMessageContent(content, |
181 | | - character -> character == '[' || character == '*' || character == '}'); |
182 | | - |
183 | | - final FluentAttribute attribute = new FluentAttribute( |
184 | | - identifier, |
185 | | - stringSliceContent.getLeft(), |
186 | | - stringSliceContent.getRight() |
187 | | - ); |
188 | | - |
189 | | - return new ImmutablePair<>(new FluentVariant(attribute), isDefault); |
190 | | - } |
191 | | - |
192 | 91 | private StringSlice getVariantIdentifier(final StringSlice content) { |
193 | 92 | char character = content.getChar(); |
194 | 93 | final int start = content.getPosition(); |
@@ -238,16 +137,4 @@ public String stringValue() { |
238 | 137 | public List<FluentElement> getElements() { |
239 | 138 | return this.fluentElements; |
240 | 139 | } |
241 | | - |
242 | | - private FluentParseException getVariantException(final StringSlice content, final String prev, final String expected) { |
243 | | - int start = content.getPosition(); |
244 | | - |
245 | | - while (content.getChar() != ']' && !content.isBigger()) { |
246 | | - content.increment(); |
247 | | - } |
248 | | - |
249 | | - return new FluentParseException(expected, |
250 | | - prev + content.substring(start, content.getPosition()).toString(), |
251 | | - content.getAbsolutePosition()); |
252 | | - } |
253 | 140 | } |
0 commit comments