-
Notifications
You must be signed in to change notification settings - Fork 3
Adding a Json BNF and use-case #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
6b5d34b
0aa8728
c5d9687
a77dabc
63ad8af
f685df8
4735560
a5295fa
f0067a8
e43fef9
b388999
c401d03
6b73ad6
f829965
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,4 +88,4 @@ | |
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> | ||
| </project> | ||
| 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). | ||
| build(); | ||
| } | ||
| } | ||
| 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"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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
$().