Skip to content

Commit 2234ba0

Browse files
committed
Make attribute references accessible everywhere
1 parent 169511b commit 2234ba0

File tree

3 files changed

+41
-36
lines changed

3 files changed

+41
-36
lines changed

src/main/java/net/quickwrite/fluent4j/ast/FluentBase.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,6 @@ private FluentPlaceable getPlaceable(final StringSlice content) {
9696

9797
boolean canSelect = placeable instanceof FluentSelectable;
9898

99-
if (!canSelect && content.getChar() == '.') {
100-
content.increment();
101-
StringSlice slice = StringSliceUtil.getIdentifier(content);
102-
103-
if (placeable instanceof TermReference) {
104-
placeable = new AttributeReference.TermAttributeReference(placeable, slice);
105-
} else {
106-
placeable = new AttributeReference(placeable, slice);
107-
}
108-
109-
canSelect = true;
110-
}
111-
11299
StringSliceUtil.skipWhitespaceAndNL(content);
113100

114101
if (canSelect && content.getChar() == '-') {

src/main/java/net/quickwrite/fluent4j/ast/placeable/AttributeReference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* -term-test = { -term.attribute }
2222
* </pre>
2323
*/
24-
public class AttributeReference implements FluentPlaceable, FluentArgumentResult {
24+
public class AttributeReference implements FluentPlaceable, FluentSelectable, FluentArgumentResult {
2525
protected final FluentPlaceable reference;
2626
protected final String attributeIdentifier;
2727

src/main/java/net/quickwrite/fluent4j/util/StringSliceUtil.java

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -169,42 +169,60 @@ private static FluentPlaceable expressionGetDefault(final StringSlice slice) {
169169

170170
skipWhitespaceAndNL(slice);
171171

172-
if (slice.getChar() == '(') {
173-
slice.increment();
172+
return switch (slice.getChar()) {
173+
case '(' -> expressionGetFunction(slice, expression, isTerm);
174+
case '.' -> expressionGetAttribute(slice, expression, isTerm);
175+
default -> expression;
176+
};
174177

175-
int start = slice.getPosition();
178+
}
176179

177-
int open = 0;
180+
private static FluentPlaceable expressionGetFunction(final StringSlice slice, FluentPlaceable expression, final boolean isTerm) {
181+
slice.increment();
178182

179-
while (!(slice.getChar() == ')' && open == 0)) {
180-
if (slice.isBigger()) {
181-
throw new FluentParseException(")", "EOF", slice.getAbsolutePosition());
182-
}
183+
int start = slice.getPosition();
183184

184-
if (slice.getChar() == '(') {
185-
open++;
186-
}
185+
int open = 0;
187186

188-
if (slice.getChar() == ')') {
189-
open--;
190-
}
187+
while (!(slice.getChar() == ')' && open == 0)) {
188+
if (slice.isBigger()) {
189+
throw new FluentParseException(")", "EOF", slice.getAbsolutePosition());
190+
}
191191

192-
slice.increment();
192+
if (slice.getChar() == '(') {
193+
open++;
193194
}
194195

195-
if (!isTerm) {
196-
expression = new FunctionReference(
196+
if (slice.getChar() == ')') {
197+
open--;
198+
}
199+
200+
slice.increment();
201+
}
202+
203+
expression = (!isTerm) ?
204+
new FunctionReference(
197205
expression.stringValue(),
198206
slice.substring(start, slice.getPosition())
199-
);
200-
} else {
201-
expression = new TermReference(
207+
) :
208+
new TermReference(
202209
expression.stringValue(),
203210
slice.substring(start, slice.getPosition())
204211
);
205-
}
206212

207-
slice.increment();
213+
slice.increment();
214+
215+
return expression;
216+
}
217+
218+
private static FluentPlaceable expressionGetAttribute(final StringSlice slice, FluentPlaceable expression, final boolean isTerm) {
219+
slice.increment();
220+
final StringSlice identifier = StringSliceUtil.getIdentifier(slice);
221+
222+
if (isTerm) {
223+
expression = new AttributeReference.TermAttributeReference(expression, identifier);
224+
} else {
225+
expression = new AttributeReference(expression, identifier);
208226
}
209227

210228
return expression;

0 commit comments

Comments
 (0)