Skip to content

Commit

Permalink
Merge pull request #9 from QuickWrite/develop
Browse files Browse the repository at this point in the history
Version `0.2.1-alpha`
  • Loading branch information
QuickWrite authored Sep 25, 2022
2 parents 5844803 + 9219144 commit ad2942b
Show file tree
Hide file tree
Showing 55 changed files with 1,760 additions and 576 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.quickwrite</groupId>
<artifactId>fluent4j</artifactId>
<version>0.2.0-alpha</version>
<version>0.2.1-alpha</version>
<build>
<plugins>
<plugin>
Expand Down
129 changes: 8 additions & 121 deletions src/main/java/net/quickwrite/fluent4j/ast/FluentBase.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
package net.quickwrite.fluent4j.ast;

import net.quickwrite.fluent4j.ast.placeable.AttributeReference;
import net.quickwrite.fluent4j.ast.placeable.SelectExpression;
import net.quickwrite.fluent4j.ast.placeable.TermReference;
import net.quickwrite.fluent4j.ast.placeable.base.FluentPlaceable;
import net.quickwrite.fluent4j.ast.placeable.base.FluentSelectable;
import net.quickwrite.fluent4j.exception.FluentParseException;
import net.quickwrite.fluent4j.exception.FluentSelectException;
import net.quickwrite.fluent4j.parser.FluentParser;
import net.quickwrite.fluent4j.util.StringSlice;
import net.quickwrite.fluent4j.util.StringSliceUtil;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

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

while (!content.isBigger()) {
if (content.getChar() == '{') {
elements.add(getPlaceable(content));
content.increment();
elements.add(StringSliceUtil.getPlaceable(content));

if (content.getChar() != '}') {
throw new FluentParseException('}', content.getCharUTF16(), content.getAbsolutePosition());
}
content.increment();
} else {
FluentTextElement text = getText(content, firstLine);

if (text != null) {
if (text != null && !text.isEmpty()) {
elements.add(text);
}
}
Expand Down Expand Up @@ -88,107 +84,10 @@ private FluentTextElement getText(final StringSlice content, final boolean first
return new FluentTextElement(content.substring(start, content.getPosition()), whitespace);
}

private FluentPlaceable getPlaceable(final StringSlice content) {
content.increment();
StringSliceUtil.skipWhitespaceAndNL(content);

FluentPlaceable placeable = StringSliceUtil.getExpression(content);

boolean canSelect = placeable instanceof FluentSelectable;

StringSliceUtil.skipWhitespaceAndNL(content);

if (canSelect && content.getChar() == '-') {
content.increment();
if (content.getChar() != '>') {
throw new FluentParseException("->", "-" + content.getCharUTF16(), content.getAbsolutePosition());
}

content.increment();

StringSliceUtil.skipWhitespaceAndNL(content);

List<FluentVariant> fluentVariants = new ArrayList<>();
FluentVariant defaultVariant = null;

while (content.getChar() != '}') {
Pair<FluentVariant, Boolean> variant = getVariant(content);

fluentVariants.add(variant.getLeft());

if (!variant.getRight()) {
continue;
}

if (defaultVariant != null) {
throw new FluentSelectException("Only one variant can be marked as default (*)");
}

defaultVariant = variant.getLeft();
}

if (defaultVariant == null) {
throw new FluentSelectException("Expected one of the variants to be marked as default (*)");
}

placeable = new SelectExpression(placeable, fluentVariants, defaultVariant);
}

StringSliceUtil.skipWhitespaceAndNL(content);

if (content.getChar() != '}') {
throw new FluentParseException('}', content.getCharUTF16(), content.getAbsolutePosition());
}

content.increment();

return placeable;
}

public String getIdentifier() {
return this.identifier;
}

private Pair<FluentVariant, Boolean> getVariant(final StringSlice content) {
StringSliceUtil.skipWhitespaceAndNL(content);

boolean isDefault = false;

if (content.getChar() == '*') {
isDefault = true;
content.increment();
}

if (content.getChar() != '[') {
throw new FluentParseException('[', content.getCharUTF16(), content.getAbsolutePosition());
}

content.increment();

StringSliceUtil.skipWhitespace(content);

StringSlice identifier = getVariantIdentifier(content);

StringSliceUtil.skipWhitespace(content);

if (content.getChar() != ']') {
throw getVariantException(content, identifier.toString(), "]");
}

content.increment();

final Pair<StringSlice, Integer> stringSliceContent = FluentParser.getMessageContent(content,
character -> character == '[' || character == '*' || character == '}');

final FluentAttribute attribute = new FluentAttribute(
identifier,
stringSliceContent.getLeft(),
stringSliceContent.getRight()
);

return new ImmutablePair<>(new FluentVariant(attribute), isDefault);
}

private StringSlice getVariantIdentifier(final StringSlice content) {
char character = content.getChar();
final int start = content.getPosition();
Expand Down Expand Up @@ -238,16 +137,4 @@ public String stringValue() {
public List<FluentElement> getElements() {
return this.fluentElements;
}

private FluentParseException getVariantException(final StringSlice content, final String prev, final String expected) {
int start = content.getPosition();

while (content.getChar() != ']' && !content.isBigger()) {
content.increment();
}

return new FluentParseException(expected,
prev + content.substring(start, content.getPosition()).toString(),
content.getAbsolutePosition());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ private String getText(final StringSlice content, final int whitespace) {
return text.toString();
}

public boolean isEmpty() {
return text.isEmpty();
}

@Override
public boolean matches(final DirectFluentBundle bundle, final FluentElement selector) {
return selector.stringValue().equals(this.text);
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/net/quickwrite/fluent4j/ast/FluentVariant.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.quickwrite.fluent4j.ast;

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

public FluentVariant(FluentAttribute content) {
public FluentVariant(final FluentAttribute content) {
this.identifier = getIdentifier(content.identifier);
this.content = content;
}

private FluentPlaceable getIdentifier(String slice) {
private FluentPlaceable getIdentifier(final String slice) {
if (Character.isDigit(slice.charAt(0))) {
return NumberLiteral.getNumberLiteral(slice);
try {
return NumberLiteral.getNumberLiteral(slice);
} catch (final NumberFormatException ignored) {
throw new FluentParseException("Expected Number but got \"" + slice + "\"");
}
}

return new StringLiteral(slice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,19 @@ public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs
return getErrorString();
}

return attribute.getResult(bundle, arguments);
return attribute.getResult(bundle, getArguments(arguments));
}

@Override
public FluentElement getArgumentResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
final FluentAttribute attribute = this.getMessage(bundle, reference.stringValue())
.getAttribute(this.attributeIdentifier);
if (attribute == null) {
final FluentMessage fluentMessage = this.getMessage(bundle, reference.stringValue());
if (fluentMessage == null) {
return this;
}

final FluentAttribute attribute = fluentMessage.getAttribute(this.attributeIdentifier);

if(attribute == null) {
return this;
}

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

// No recursion (unfortunately :d)
return (FluentElement) elementList.get(0);
return elementList.get(0);
}

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

protected FluentArgs getArguments(final FluentArgs defaultArgs) {
return defaultArgs;
}

@Override
public String toString() {
return "FluentAttributeReference: {\n" +
Expand All @@ -91,8 +100,12 @@ public String toString() {
}

public static class TermAttributeReference extends AttributeReference implements FluentSelectable {
public TermAttributeReference(FluentPlaceable reference, StringSlice content) {
private final FluentArgs arguments;

public TermAttributeReference(final FluentPlaceable reference, final StringSlice content, final FluentArgs arguments) {
super(reference, content);

this.arguments = arguments;
}

@Override
Expand All @@ -104,5 +117,19 @@ protected FluentMessage getMessage(final DirectFluentBundle bundle, final String
protected String getErrorString() {
return "{-" + reference.stringValue() + "." + attributeIdentifier + "}";
}

@Override
protected FluentArgs getArguments(final FluentArgs defaultArgs) {
return this.arguments;
}

@Override
public String toString() {
return "FluentTermAttributeReference: {\n" +
"\t\t\tvalue: \"" + this.reference + "\"\n" +
"\t\t\tattribute: \"" + this.attributeIdentifier + "\"\n" +
"\t\t\targuments: \"" + this.arguments + "\"\n" +
"\t\t}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/
public class FunctionReference extends FluentFunction implements FluentSelectable {

public FunctionReference(final String functionName, final StringSlice content) {
super(functionName, content);
public FunctionReference(final String functionName, final FluentArgs arguments) {
super(functionName, arguments);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.quickwrite.fluent4j.ast.placeable.base.FluentFunction;
import net.quickwrite.fluent4j.util.StringSlice;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.args.FluentArguments;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;

/**
Expand All @@ -30,8 +29,8 @@ public TermReference(final StringSlice name) {
super(name, null);
}

public TermReference(final String name, final StringSlice content) {
super(name, content);
public TermReference(final String name, final FluentArgs arguments) {
super(name, arguments);
}

@Override
Expand Down Expand Up @@ -60,11 +59,6 @@ public FluentElement getArgumentResult(final DirectFluentBundle bundle, final Fl
return term;
}

@Override
protected FluentArgs getFluentArgumentInstance() {
return new FluentArguments();
}

@Override
public String toString() {
return "FluentTermReference: {\n" +
Expand Down
Loading

0 comments on commit ad2942b

Please sign in to comment.