Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: Java CI with Maven

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Cache Maven packages
uses: actions/cache@v1
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build with Maven
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: mvn -B compile --file pom.xml
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ $.*
texput.*
Untitled*
.~lock.*
_*

# Prefixes // vim: +,/^\s*$/-1!sort -u
a.out
Expand Down Expand Up @@ -148,3 +147,10 @@ out/
.classpath
.project
.settings/

# MacOS junk
.DS_Store

# IntelliJunk
.idea/
.iml
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
new RegularExpression(), //
new HTMLTable(), //
new SimpleArithmetic(), //
new Json(),
};
private static final Map<String, String> files = ((Supplier<Map<String, String>>) () -> {
final Map<String, String> $ = new LinkedHashMap<>();
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/il/ac/technion/cs/fling/examples/languages/Json.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package il.ac.technion.cs.fling.examples.languages;

import il.ac.technion.cs.fling.BNF;
import il.ac.technion.cs.fling.Terminal;
import il.ac.technion.cs.fling.Variable;
import il.ac.technion.cs.fling.examples.FluentLanguageAPI;
import il.ac.technion.cs.fling.examples.languages.Json.V;
import il.ac.technion.cs.fling.examples.languages.Json.Σ;

import static il.ac.technion.cs.fling.Symbol.noneOrMore;
import static il.ac.technion.cs.fling.examples.languages.Json.V.*;
import static il.ac.technion.cs.fling.examples.languages.Json.Σ.*;
import static il.ac.technion.cs.fling.grammars.api.BNFAPI.bnf;

@SuppressWarnings("NonAsciiCharacters")
public class Json implements FluentLanguageAPI<Σ, V> {
public enum Σ implements Terminal {
array, end, isSetTo, key, object, value, _null_
}

public enum V implements Variable {
Element, Field, Json, Value
}

@Override public Class<Σ> Σ() {
return Σ.class;
}
@Override public Class<V> V() {
return V.class;
}
@Override public BNF BNF() {
return bnf().
start(Json).
derive(Json).to(Element).
derive(Element).to(object, noneOrMore(Field), end).
or(array, noneOrMore(Element), end).
or(Value).
derive(Value).to(value.with(String.class)).
or(value.with(Number.class)).
or(value.with(Boolean.class)).
or(_null_).
derive(Field).to(key.with(String.class), isSetTo, Element).
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want, you can try instead isSetTo.with(Element). Fling does support nested APIs. (I.e., API as API function argument.)
Notice inner API calls should not end with a call to $().

build();
}
}
44 changes: 44 additions & 0 deletions src/test/java/il/ac/technion/cs/fling/examples/usecases/Json.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package il.ac.technion.cs.fling.examples.usecases;

import static il.ac.technion.cs.fling.examples.generated.Json.value;
import static il.ac.technion.cs.fling.examples.generated.Json.array;
import static il.ac.technion.cs.fling.examples.generated.Json.object;

// @formatter:off
public class Json {
public static void main(String[] args) {
value("f");
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want, you can practice AST traversal by, say, pretty-printing the JSON objects. You can check the other use-cases for examples.

value(8);
value(true);
array(); //TODO: how come this is legal?
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array() is a legal prefix of your grammar. To terminate a fluent program (and generate AST) you should call $() as well. Notice array().$() should not compile.
You can read on AST generation in the paper.

array().end();
array().value("f").end();
array()
.value(8)
.value("fg")
.array().end()
.end();

object().end();

object().key("name").isSetTo().value(8).end();

object()
.key("name").isSetTo().value("Joe")
.key("age").isSetTo().value(8)
.end();

object().
key("name").isSetTo()
.object()
.key("first").isSetTo().value("Joe")
.key("last").isSetTo()._null_()
.end().
key("ages").isSetTo()
.array()
.value(60)
.value(70)
.end()
.end();
}
}