-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add data attribute to the jflex rule (#19)
* Add data attribute to the jflex rule * Add example to demonstrate how the data atribute can be used with %include directive in jflex spec.
- Loading branch information
Showing
7 changed files
with
108 additions
and
1 deletion.
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,19 @@ | ||
# Use of the data attribute for nested grammars | ||
load("//jflex:jflex.bzl", "jflex") | ||
|
||
jflex( | ||
name = "gen_nested_grammar", | ||
srcs = ["nested_grammar.jflex"], | ||
data = ["extra-jflex-rules.inc.jflex"], | ||
outputs = ["NestedRulesScanner.java"], | ||
) | ||
|
||
java_library( | ||
name = "nested_grammar", | ||
srcs = [ | ||
"Token.java", | ||
":gen_nested_grammar", | ||
], | ||
visibility = ["//javatests/jflex/examples/nested_grammar:__pkg__"], | ||
deps = [], | ||
) |
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,8 @@ | ||
package jflex.examples.nested_grammar; | ||
|
||
public enum Token { | ||
FOO, | ||
BAR, | ||
HELLO, | ||
EOF, | ||
} |
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 @@ | ||
"hello" {return Token.HELLO;} |
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,18 @@ | ||
package jflex.examples.nested_grammar; | ||
|
||
%% | ||
|
||
%unicode | ||
%public | ||
%class NestedRulesScanner | ||
%type Token | ||
|
||
%% | ||
|
||
"foo" { return Token.FOO; } | ||
%include extra-jflex-rules.inc.jflex | ||
"bar" { return Token.BAR; } | ||
|
||
[^] { } | ||
|
||
<<EOF>> { return Token.EOF; } |
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,9 @@ | ||
java_test( | ||
name = "NestedGrammarTest", | ||
srcs = ["NestedGrammarTest.java"], | ||
deps = [ | ||
"//java/jflex/examples/nested_grammar", | ||
"//third_party/com/google/guava", | ||
"//third_party/com/google/truth", | ||
], | ||
) |
48 changes: 48 additions & 0 deletions
48
javatests/jflex/examples/nested_grammar/NestedGrammarTest.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,48 @@ | ||
package jflex.examples.nested_grammar; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.common.io.CharSource; | ||
import java.io.IOException; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
public class NestedGrammarTest { | ||
|
||
private NestedRulesScanner scanner; | ||
|
||
@After | ||
public void eof() throws Exception { | ||
assertThat(scanner.yylex()).isEqualTo(Token.EOF); | ||
} | ||
|
||
@Test | ||
public void foo() throws Exception { | ||
scanner = createScanner("foo"); | ||
assertThat(scanner.yylex()).isEqualTo(Token.FOO); | ||
} | ||
|
||
@Test | ||
public void bar() throws Exception { | ||
scanner = createScanner("bar"); | ||
assertThat(scanner.yylex()).isEqualTo(Token.BAR); | ||
} | ||
|
||
@Test | ||
public void hello() throws Exception { | ||
scanner = createScanner("hello"); | ||
assertThat(scanner.yylex()).isEqualTo(Token.HELLO); | ||
} | ||
|
||
@Test | ||
public void sentence() throws Exception { | ||
scanner = createScanner("foo bar hello world"); | ||
assertThat(scanner.yylex()).isEqualTo(Token.FOO); | ||
assertThat(scanner.yylex()).isEqualTo(Token.BAR); | ||
assertThat(scanner.yylex()).isEqualTo(Token.HELLO); | ||
} | ||
|
||
private NestedRulesScanner createScanner(String content) throws IOException { | ||
return new NestedRulesScanner(CharSource.wrap(content).openStream()); | ||
} | ||
} |
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