-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into cup-bazel-rule
- Loading branch information
Showing
20 changed files
with
316 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# https://errorprone.info/ | ||
# TODO(#222) Increase to error when we build from jflex-1.7.1 with a lexer annotated with | ||
# @SuppressWarnings | ||
build --javacopt "-Xep:FallThrough:WARN" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Copyright 2018 Google LLC. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
container: | ||
image: cirrusci/bazel:latest | ||
task: | ||
name: Bazel build and test | ||
build_script: | ||
- bazel --bazelrc=.ci.bazelrc info --remote_http_cache=http://$CIRRUS_HTTP_CACHE_HOST release | ||
- bazel --bazelrc=.ci.bazelrc build --remote_http_cache=http://$CIRRUS_HTTP_CACHE_HOST //... | ||
test_script: | ||
- bazel --bazelrc=.ci.bazelrc test --remote_http_cache=http://$CIRRUS_HTTP_CACHE_HOST //... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
load("@jflex_rules//jflex:jflex.bzl", "jflex") | ||
load("//cup:cup.bzl", "cup") | ||
|
||
# The best practice is to define the rules in their respective directory in | ||
# - src/main/java/org/example/foo/BUILD | ||
# - src/main/jflex/BUILD | ||
# - src/test/java/org/example/foo/BUILD | ||
# - etc. | ||
# However, this example is simple enough and we can define all rules here. | ||
|
||
java_binary( | ||
name = "minijava_bin", | ||
main_class = "Yylex", | ||
runtime_deps = [":minijava"], | ||
) | ||
|
||
java_library( | ||
name = "minijava", | ||
# glob is not a best practice, but it's good enough for this example | ||
srcs = glob(["src/main/java/**/*.java"]) + [ | ||
":gen_lexer", | ||
":gen_parser", | ||
], | ||
deps = ["//cup:cup_runtime"], | ||
) | ||
|
||
jflex( | ||
name = "gen_lexer", | ||
srcs = ["src/main/jflex/minijava.flex"], | ||
jflex_bin = "//jflex:jflex_bin", | ||
outputs = ["Lexer.java"], | ||
) | ||
|
||
cup( | ||
name = "gen_parser", | ||
src = "src/main/cup/minijava.cup", | ||
symbols = "sym", | ||
) | ||
|
||
# Tests | ||
java_test( | ||
name = "LexerTest", | ||
srcs = ["src/test/java/jflex/examples/minijava/LexerTest.java"], | ||
deps = [ | ||
":minijava", | ||
"//cup/cup_runtime", | ||
"//third_party/com/google/truth", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Example from the user manual | ||
|
||
This is the [example from the user manual](http://jflex.de/manual.html#Example). | ||
|
||
This example does not describe the whole lexical structure of Java programs, but only a small and | ||
simplified part of it (some keywords, some operators, comments and only two kinds of literals). | ||
It also shows how to interface with the LALR parser generator CUP and therefore uses a class `sym` | ||
(generated by CUP), where integer constants for the terminal tokens of the CUP grammar are declared. | ||
|
||
For a full implementation of **Java 1.2**, see [cup-java](../cup-java). | ||
|
||
## Build, run, test | ||
|
||
### Using Maven | ||
|
||
../../mvnw package | ||
|
||
To run the parser: | ||
|
||
java -cp target/cup-java-simplified-1.0.jar:../../../cup/cup/java-cup-11b.jar JavaParser <inputfile> | ||
|
||
or more simply the uberjar version: | ||
|
||
java -jar target/cup-java-simplified-full-1.0.jar <inputfile> | ||
|
||
|
||
## Files | ||
|
||
* `src/main/jflex/minijava.flex` | ||
Partial (simplified) specification of Java. | ||
* `src/test/java/jflex/examples/minijava/LexerTest.java` | ||
Test of the generated lexer. |
67 changes: 67 additions & 0 deletions
67
jflex/examples/cup-java-minijava/src/main/cup/minijava.cup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (C) 1998 C. Scott Ananian <[email protected]> | ||
* Copyright (C) 1999 Gerwin Klein <[email protected]> | ||
* Copyright (C) 2018 Google LLC | ||
* | ||
* This program is released under the terms of the GPL; see the file | ||
* COPYING for more details. There is NO WARRANTY on this code. | ||
*/ | ||
package jflex.examples.minijava; | ||
|
||
import java_cup.runtime.*; | ||
|
||
|
||
/** Parser for a lamguages inspired by Java. */ | ||
|
||
// Keywords | ||
terminal ABSTRACT; // "abstract" | ||
terminal BOOLEAN; // "boolean" primitive_type | ||
terminal BREAK; // "break" break_statement | ||
|
||
// Identifier matches each string that starts with a character of class jletter followed by zero or | ||
// more characters of class jletterdigit | ||
terminal java.lang.String IDENTIFIER; // name | ||
|
||
// Literals | ||
terminal java.lang.Number INTEGER_LITERAL; | ||
terminal java.lang.String STRING_LITERAL; | ||
|
||
// Operators | ||
terminal EQ; | ||
terminal EQEQ; // equality_expression | ||
terminal PLUS; | ||
|
||
|
||
// 19.3) Lexical Structure | ||
non terminal literal; | ||
// 19.4) Types, Values, and Variables | ||
non terminal primitive_type; | ||
// 19.5) Names | ||
non terminal name; | ||
// 19.12) Expressions | ||
non terminal equality_expression; | ||
|
||
// Our simplified grammar | ||
non terminal goal; | ||
|
||
// TODO | ||
start with goal; | ||
|
||
goal ::= name | ||
; | ||
|
||
// 19.3) Lexical Structure. | ||
literal ::= INTEGER_LITERAL | ||
| STRING_LITERAL | ||
; | ||
|
||
// 19.4) Types, Values, and Variables | ||
type ::= primitive_type | ||
; | ||
primitive_type ::= | ||
| BOOLEAN | ||
; | ||
|
||
// 19.5) Names | ||
name ::= IDENTIFIER | ||
; |
9 changes: 7 additions & 2 deletions
9
...ases/src/test/cases/manual-ex/manual.flex → ...ava-minijava/src/main/jflex/minijava.flex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
jflex/examples/cup-java-minijava/src/test/java/jflex/examples/minijava/LexerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package jflex.examples.minijava; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test for the generated {@link Lexer}. | ||
* | ||
* <p>The lexer is probably already correct thanks to the regression tests. This test class is | ||
* mostly here to show how the lexer behaves. | ||
*/ | ||
public class LexerTest { | ||
|
||
private Lexer lexer; | ||
|
||
@After | ||
public void resetLexer() { | ||
lexer = null; | ||
} | ||
|
||
@Test | ||
public void scan_tokenIdentifier() throws IOException { | ||
scan("helloWorld"); | ||
assertThat(nextToken()).isEqualTo(sym.IDENTIFIER); | ||
} | ||
|
||
@Test | ||
public void scan_assignment() throws IOException { | ||
scan("boolean debug = 2 == 1 + 1"); | ||
assertThat(nextToken()).isEqualTo(sym.BOOLEAN); | ||
assertThat(nextToken()).isEqualTo(sym.IDENTIFIER); | ||
assertThat(nextToken()).isEqualTo(sym.EQ); | ||
assertThat(nextToken()).isEqualTo(sym.INTEGER_LITERAL); | ||
assertThat(nextToken()).isEqualTo(sym.EQEQ); | ||
assertThat(nextToken()).isEqualTo(sym.INTEGER_LITERAL); | ||
assertThat(nextToken()).isEqualTo(sym.PLUS); | ||
assertThat(nextToken()).isEqualTo(sym.INTEGER_LITERAL); | ||
assertThat(nextToken()).isEqualTo(sym.EOF); | ||
} | ||
|
||
@SuppressWarnings("TryFailThrowable") | ||
@Test | ||
public void scan_illegalChar() throws IOException { | ||
scan("boolean debug;"); | ||
assertThat(nextToken()).isEqualTo(sym.BOOLEAN); | ||
assertThat(nextToken()).isEqualTo(sym.IDENTIFIER); | ||
try { | ||
nextToken(); | ||
fail("Character `;` is not declared in the minijava.flex"); | ||
} catch (Error expected) { | ||
// This is bad, but the JFlex API doesn't allow better | ||
// https://errorprone.info/bugpattern/TryFailThrowable | ||
} | ||
} | ||
|
||
private void scan(String input) { | ||
Reader in = new StringReader(input); | ||
lexer = new Lexer(in); | ||
} | ||
|
||
private int nextToken() throws IOException { | ||
return lexer.next_token().sym; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
echo "Push to https://github.com/jflex-de/jflex/tree/aggregated-java-sources" | ||
cd repo | ||
# SECURITY NOTICE: Be sure to send stdout & stderr to /dev/null so that the the ${GITHUB_TOKEN} is$ | ||
git remote set-url --push origin "https://${GITHUB_TOKEN}@github.com/jflex-de/jflex.git" > /dev/null | ||
git push |
Oops, something went wrong.