Skip to content
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

Add data attribute to the jflex rule #19

Merged
merged 2 commits into from
Dec 8, 2019
Merged
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
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