Skip to content

Commit

Permalink
Add data attribute to the jflex rule (#19)
Browse files Browse the repository at this point in the history
* 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
regisd authored Dec 8, 2019
1 parent 5604f3d commit 49122db
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 1 deletion.
19 changes: 19 additions & 0 deletions java/jflex/examples/nested_grammar/BUILD
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 = [],
)
8 changes: 8 additions & 0 deletions java/jflex/examples/nested_grammar/Token.java
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,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"hello" {return Token.HELLO;}
18 changes: 18 additions & 0 deletions java/jflex/examples/nested_grammar/nested_grammar.jflex
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; }
9 changes: 9 additions & 0 deletions javatests/jflex/examples/nested_grammar/BUILD
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 javatests/jflex/examples/nested_grammar/NestedGrammarTest.java
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());
}
}
6 changes: 5 additions & 1 deletion jflex/jflex.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _jflex_impl(ctx):
)
ctx.actions.run(
mnemonic = "jflex",
inputs = ctx.files.srcs + maybe_skel,
inputs = ctx.files.srcs + ctx.files.data + maybe_skel,
outputs = ctx.outputs.outputs,
executable = ctx.executable.jflex_bin,
arguments = arguments,
Expand All @@ -47,6 +47,10 @@ jflex = rule(
mandatory = True,
doc = "a list of grammar specifications",
),
"data": attr.label_list(
allow_files = True,
doc = "extra files to pass to the rule, e.g. included grammar specs",
),
"jlex": attr.bool(
doc = "JLex compatibility increaed. In particular, this changes how caseless behaves.",
),
Expand Down

0 comments on commit 49122db

Please sign in to comment.