Skip to content

Commit ad2942b

Browse files
authored
Merge pull request #9 from QuickWrite/develop
Version `0.2.1-alpha`
2 parents 5844803 + 9219144 commit ad2942b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1760
-576
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>net.quickwrite</groupId>
88
<artifactId>fluent4j</artifactId>
9-
<version>0.2.0-alpha</version>
9+
<version>0.2.1-alpha</version>
1010
<build>
1111
<plugins>
1212
<plugin>
Lines changed: 8 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
package net.quickwrite.fluent4j.ast;
22

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;
83
import net.quickwrite.fluent4j.exception.FluentParseException;
9-
import net.quickwrite.fluent4j.exception.FluentSelectException;
10-
import net.quickwrite.fluent4j.parser.FluentParser;
114
import net.quickwrite.fluent4j.util.StringSlice;
125
import net.quickwrite.fluent4j.util.StringSliceUtil;
136
import net.quickwrite.fluent4j.util.args.FluentArgs;
147
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
15-
import org.apache.commons.lang3.tuple.ImmutablePair;
16-
import org.apache.commons.lang3.tuple.Pair;
178

18-
import java.util.ArrayList;
199
import java.util.LinkedList;
2010
import java.util.List;
2111

@@ -51,11 +41,17 @@ private List<FluentElement> parse(final StringSlice content) {
5141

5242
while (!content.isBigger()) {
5343
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();
5551
} else {
5652
FluentTextElement text = getText(content, firstLine);
5753

58-
if (text != null) {
54+
if (text != null && !text.isEmpty()) {
5955
elements.add(text);
6056
}
6157
}
@@ -88,107 +84,10 @@ private FluentTextElement getText(final StringSlice content, final boolean first
8884
return new FluentTextElement(content.substring(start, content.getPosition()), whitespace);
8985
}
9086

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-
14887
public String getIdentifier() {
14988
return this.identifier;
15089
}
15190

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-
19291
private StringSlice getVariantIdentifier(final StringSlice content) {
19392
char character = content.getChar();
19493
final int start = content.getPosition();
@@ -238,16 +137,4 @@ public String stringValue() {
238137
public List<FluentElement> getElements() {
239138
return this.fluentElements;
240139
}
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-
}
253140
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ private String getText(final StringSlice content, final int whitespace) {
6767
return text.toString();
6868
}
6969

70+
public boolean isEmpty() {
71+
return text.isEmpty();
72+
}
73+
7074
@Override
7175
public boolean matches(final DirectFluentBundle bundle, final FluentElement selector) {
7276
return selector.stringValue().equals(this.text);

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.quickwrite.fluent4j.ast;
22

3+
import net.quickwrite.fluent4j.exception.FluentParseException;
34
import net.quickwrite.fluent4j.util.args.FluentArgs;
45
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
56
import net.quickwrite.fluent4j.ast.placeable.NumberLiteral;
@@ -21,14 +22,18 @@ public class FluentVariant implements FluentElement {
2122
private final FluentPlaceable identifier;
2223
private final FluentAttribute content;
2324

24-
public FluentVariant(FluentAttribute content) {
25+
public FluentVariant(final FluentAttribute content) {
2526
this.identifier = getIdentifier(content.identifier);
2627
this.content = content;
2728
}
2829

29-
private FluentPlaceable getIdentifier(String slice) {
30+
private FluentPlaceable getIdentifier(final String slice) {
3031
if (Character.isDigit(slice.charAt(0))) {
31-
return NumberLiteral.getNumberLiteral(slice);
32+
try {
33+
return NumberLiteral.getNumberLiteral(slice);
34+
} catch (final NumberFormatException ignored) {
35+
throw new FluentParseException("Expected Number but got \"" + slice + "\"");
36+
}
3237
}
3338

3439
return new StringLiteral(slice);

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,19 @@ public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs
5353
return getErrorString();
5454
}
5555

56-
return attribute.getResult(bundle, arguments);
56+
return attribute.getResult(bundle, getArguments(arguments));
5757
}
5858

5959
@Override
6060
public FluentElement getArgumentResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
61-
final FluentAttribute attribute = this.getMessage(bundle, reference.stringValue())
62-
.getAttribute(this.attributeIdentifier);
63-
if (attribute == null) {
61+
final FluentMessage fluentMessage = this.getMessage(bundle, reference.stringValue());
62+
if (fluentMessage == null) {
63+
return this;
64+
}
65+
66+
final FluentAttribute attribute = fluentMessage.getAttribute(this.attributeIdentifier);
67+
68+
if(attribute == null) {
6469
return this;
6570
}
6671

@@ -71,7 +76,7 @@ public FluentElement getArgumentResult(final DirectFluentBundle bundle, final Fl
7176
}
7277

7378
// No recursion (unfortunately :d)
74-
return (FluentElement) elementList.get(0);
79+
return elementList.get(0);
7580
}
7681

7782
protected FluentMessage getMessage(final DirectFluentBundle bundle, final String key) {
@@ -82,6 +87,10 @@ protected String getErrorString() {
8287
return "{" + reference.stringValue() + "." + attributeIdentifier + "}";
8388
}
8489

90+
protected FluentArgs getArguments(final FluentArgs defaultArgs) {
91+
return defaultArgs;
92+
}
93+
8594
@Override
8695
public String toString() {
8796
return "FluentAttributeReference: {\n" +
@@ -91,8 +100,12 @@ public String toString() {
91100
}
92101

93102
public static class TermAttributeReference extends AttributeReference implements FluentSelectable {
94-
public TermAttributeReference(FluentPlaceable reference, StringSlice content) {
103+
private final FluentArgs arguments;
104+
105+
public TermAttributeReference(final FluentPlaceable reference, final StringSlice content, final FluentArgs arguments) {
95106
super(reference, content);
107+
108+
this.arguments = arguments;
96109
}
97110

98111
@Override
@@ -104,5 +117,19 @@ protected FluentMessage getMessage(final DirectFluentBundle bundle, final String
104117
protected String getErrorString() {
105118
return "{-" + reference.stringValue() + "." + attributeIdentifier + "}";
106119
}
120+
121+
@Override
122+
protected FluentArgs getArguments(final FluentArgs defaultArgs) {
123+
return this.arguments;
124+
}
125+
126+
@Override
127+
public String toString() {
128+
return "FluentTermAttributeReference: {\n" +
129+
"\t\t\tvalue: \"" + this.reference + "\"\n" +
130+
"\t\t\tattribute: \"" + this.attributeIdentifier + "\"\n" +
131+
"\t\t\targuments: \"" + this.arguments + "\"\n" +
132+
"\t\t}";
133+
}
107134
}
108135
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*/
1717
public class FunctionReference extends FluentFunction implements FluentSelectable {
1818

19-
public FunctionReference(final String functionName, final StringSlice content) {
20-
super(functionName, content);
19+
public FunctionReference(final String functionName, final FluentArgs arguments) {
20+
super(functionName, arguments);
2121
}
2222

2323
@Override

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import net.quickwrite.fluent4j.ast.placeable.base.FluentFunction;
66
import net.quickwrite.fluent4j.util.StringSlice;
77
import net.quickwrite.fluent4j.util.args.FluentArgs;
8-
import net.quickwrite.fluent4j.util.args.FluentArguments;
98
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
109

1110
/**
@@ -30,8 +29,8 @@ public TermReference(final StringSlice name) {
3029
super(name, null);
3130
}
3231

33-
public TermReference(final String name, final StringSlice content) {
34-
super(name, content);
32+
public TermReference(final String name, final FluentArgs arguments) {
33+
super(name, arguments);
3534
}
3635

3736
@Override
@@ -60,11 +59,6 @@ public FluentElement getArgumentResult(final DirectFluentBundle bundle, final Fl
6059
return term;
6160
}
6261

63-
@Override
64-
protected FluentArgs getFluentArgumentInstance() {
65-
return new FluentArguments();
66-
}
67-
6862
@Override
6963
public String toString() {
7064
return "FluentTermReference: {\n" +

0 commit comments

Comments
 (0)