Skip to content

Commit

Permalink
do not create char class entries for atBOL matching
Browse files Browse the repository at this point in the history
The scanning engine is testing for newline on raw input characters without
translation through the char-class map, so there is no need for a separate
class in that case.
  • Loading branch information
lsf37 committed Dec 12, 2019
1 parent 23cd099 commit 2b166cb
Show file tree
Hide file tree
Showing 16 changed files with 440 additions and 167 deletions.
8 changes: 8 additions & 0 deletions javatests/jflex/testcase/bol/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ jflex(
outputs = ["BolScanner.java"],
)

jflex(
name = "gen_bol2_scanner",
srcs = ["bol2.flex"],
jflex_bin = "//jflex:jflex_bin",
outputs = ["Bol2Scanner.java"],
)

java_test(
name = "BolTest",
srcs = [
"BolTest.java",
"State.java",
":gen_bol2_scanner",
":gen_bol_scanner",
],
deps = [
Expand Down
63 changes: 62 additions & 1 deletion javatests/jflex/testcase/bol/BolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,25 @@

/** Tests BOL and EOL operators. */
public class BolTest {
// standard BOL and EOL tests
BolScanner scanner;
// test BOL in absence of char class for NL characters
Bol2Scanner scanner2;

@Before
public void testMustInitializeScanner() {
scanner = null;
scanner2 = null;
}

@After
public void end() throws Exception {
assertThat(scanner.yylex()).isEqualTo(State.END_OF_FILE);
if (scanner != null) {
assertThat(scanner.yylex()).isEqualTo(State.END_OF_FILE);
}
if (scanner2 != null) {
assertThat(scanner2.yylex()).isEqualTo(State.END_OF_FILE);
}
}

@Test
Expand All @@ -35,6 +44,30 @@ public void bolAndEol() throws Exception {
assertThat(scanner.yylex()).isEqualTo(State.LINE_FEED);
}

@Test
public void bolAndEol2() throws Exception {
scanner = createScanner("hello\r\n");
assertThat(scanner.yylex()).isEqualTo(State.HELLO_AT_BOL_AND_EOL);
assertThat(scanner.yylex()).isEqualTo(State.OTHER);
assertThat(scanner.yylex()).isEqualTo(State.LINE_FEED);
}

@Test
public void bolAndEol3() throws Exception {
scanner = createScanner("\nhello\r\n");
assertThat(scanner.yylex()).isEqualTo(State.LINE_FEED);
assertThat(scanner.yylex()).isEqualTo(State.HELLO_AT_BOL_AND_EOL);
assertThat(scanner.yylex()).isEqualTo(State.OTHER);
assertThat(scanner.yylex()).isEqualTo(State.LINE_FEED);
}

@Test
public void bolAndEof() throws Exception {
// EOF does not count as EOL. Should it?
scanner = createScanner("hello");
assertThat(scanner.yylex()).isEqualTo(State.HELLO_AT_BOL);
}

@Test
public void eol() throws Exception {
scanner = createScanner(" hello\n");
Expand Down Expand Up @@ -79,7 +112,35 @@ public void other() throws Exception {
assertThat(scanner.yylex()).isEqualTo(State.LINE_FEED);
}

@Test
public void bolNoNLCCL() throws Exception {
scanner2 = createScanner2("hello");
assertThat(scanner2.yylex()).isEqualTo(State.HELLO_AT_BOL);
}

@Test
public void bolNoNLCCL2() throws Exception {
scanner2 = createScanner2("hello\n");
assertThat(scanner2.yylex()).isEqualTo(State.HELLO_AT_BOL);
assertThat(scanner2.yylex()).isEqualTo(State.OTHER);
}

@Test
public void noBolNoNLCCL() throws Exception {
scanner2 = createScanner2("\n hello ");
assertThat(scanner2.yylex()).isEqualTo(State.OTHER);
assertThat(scanner2.yylex()).isEqualTo(State.OTHER);
assertThat(scanner2.yylex()).isEqualTo(State.OTHER);
assertThat(scanner2.yylex()).isEqualTo(State.HELLO_SIMPLY);
assertThat(scanner2.yylex()).isEqualTo(State.OTHER);
assertThat(scanner2.yylex()).isEqualTo(State.OTHER);
}

private static BolScanner createScanner(final String content) throws IOException {
return new BolScanner(CharSource.wrap(content).openStream());
}

private static Bol2Scanner createScanner2(final String content) throws IOException {
return new Bol2Scanner(CharSource.wrap(content).openStream());
}
}
1 change: 0 additions & 1 deletion javatests/jflex/testcase/bol/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public enum State {
HELLO_AT_BOL,
HELLO_AT_EOL,
HELLO_SIMPLY,
CARRIAGE_RETURN,
LINE_FEED,
SPACE,
OTHER,
Expand Down
3 changes: 1 addition & 2 deletions javatests/jflex/testcase/bol/bol.flex
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ package jflex.testcase.bol;
"hello"$ { return State.HELLO_AT_EOL; }
"hello" { return State.HELLO_SIMPLY; }

\r { return State.CARRIAGE_RETURN; }
\n { return State.LINE_FEED; }

" " { return State.SPACE; }
. { return State.OTHER; }
[^] { return State.OTHER; }

<<EOF>> { return State.END_OF_FILE; }
19 changes: 19 additions & 0 deletions javatests/jflex/testcase/bol/bol2.flex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package jflex.testcase.bol;

%%

%public
%class Bol2Scanner
%type State
%debug

%unicode

%%

^"hello" { return State.HELLO_AT_BOL; }
"hello" { return State.HELLO_SIMPLY; }

[^] { return State.OTHER; }

<<EOF>> { return State.END_OF_FILE; }
6 changes: 1 addition & 5 deletions jflex/src/main/cup/LexParse.cup
Original file line number Diff line number Diff line change
Expand Up @@ -596,11 +596,7 @@ states ::= IDENT:id COMMA states:list
;

hatOPT ::= HAT
{:
// assumption: newline chars have no uppercase variant
charClasses.makeClass("\n\r\u000B\u000C\u0085\u2028\u2029", false);
RESULT = true;
:}
{: RESULT = true; :}
| /* empty */
{: RESULT = false; :}
;
Expand Down
1 change: 1 addition & 0 deletions testsuite/testcases/src/test/cases/bol/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Bol.java
Bol2.java
1 change: 1 addition & 0 deletions testsuite/testcases/src/test/cases/bol/bol-0.input
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ hello
hello

sdfa
hello
49 changes: 26 additions & 23 deletions testsuite/testcases/src/test/cases/bol/bol-0.output
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,90 @@ line: 1 col: 1 char: 0 match: --hello--
action [19] { System.out.println("hello at BOL and EOL"); }
hello at BOL and EOL
line: 1 col: 6 char: 5 match: --\u000A--
action [25] { System.out.println("\\n"); }
action [24] { System.out.println("\\n"); }
\n
line: 2 col: 1 char: 6 match: -- --
action [27] { System.out.println( "\" \"" ); }
action [26] { System.out.println( "\" \"" ); }
" "
line: 2 col: 2 char: 7 match: -- --
action [27] { System.out.println( "\" \"" ); }
action [26] { System.out.println( "\" \"" ); }
" "
line: 2 col: 3 char: 8 match: --hello--
action [21] { System.out.println("hello at EOL"); }
hello at EOL
line: 2 col: 8 char: 13 match: --\u000A--
action [25] { System.out.println("\\n"); }
action [24] { System.out.println("\\n"); }
\n
line: 3 col: 1 char: 14 match: -- --
action [27] { System.out.println( "\" \"" ); }
action [26] { System.out.println( "\" \"" ); }
" "
line: 3 col: 2 char: 15 match: -- --
action [27] { System.out.println( "\" \"" ); }
action [26] { System.out.println( "\" \"" ); }
" "
line: 3 col: 3 char: 16 match: --hello--
action [22] { System.out.println("just hello"); }
just hello
line: 3 col: 8 char: 21 match: -- --
action [27] { System.out.println( "\" \"" ); }
action [26] { System.out.println( "\" \"" ); }
" "
line: 3 col: 9 char: 22 match: -- --
action [27] { System.out.println( "\" \"" ); }
action [26] { System.out.println( "\" \"" ); }
" "
line: 3 col: 10 char: 23 match: --\u000A--
action [25] { System.out.println("\\n"); }
action [24] { System.out.println("\\n"); }
\n
line: 4 col: 1 char: 24 match: --hello--
action [20] { System.out.println("hello at BOL"); }
hello at BOL
line: 4 col: 6 char: 29 match: -- --
action [27] { System.out.println( "\" \"" ); }
action [26] { System.out.println( "\" \"" ); }
" "
line: 4 col: 7 char: 30 match: --\u000A--
action [25] { System.out.println("\\n"); }
action [24] { System.out.println("\\n"); }
\n
line: 5 col: 1 char: 31 match: -- --
action [27] { System.out.println( "\" \"" ); }
action [26] { System.out.println( "\" \"" ); }
" "
line: 5 col: 2 char: 32 match: -- --
action [27] { System.out.println( "\" \"" ); }
action [26] { System.out.println( "\" \"" ); }
" "
line: 5 col: 3 char: 33 match: --hello--
action [21] { System.out.println("hello at EOL"); }
hello at EOL
line: 5 col: 8 char: 38 match: --\u000A--
action [25] { System.out.println("\\n"); }
action [24] { System.out.println("\\n"); }
\n
line: 6 col: 1 char: 39 match: --hello--
action [20] { System.out.println("hello at BOL"); }
hello at BOL
line: 6 col: 6 char: 44 match: -- --
action [27] { System.out.println( "\" \"" ); }
action [26] { System.out.println( "\" \"" ); }
" "
line: 6 col: 7 char: 45 match: --\u000A--
action [25] { System.out.println("\\n"); }
action [24] { System.out.println("\\n"); }
\n
line: 7 col: 1 char: 46 match: -- --
action [27] { System.out.println( "\" \"" ); }
action [26] { System.out.println( "\" \"" ); }
" "
line: 7 col: 2 char: 47 match: --\u000A--
action [25] { System.out.println("\\n"); }
action [24] { System.out.println("\\n"); }
\n
line: 8 col: 1 char: 48 match: --s--
action [28] { System.out.println( yytext() ); }
action [27] { System.out.println( yytext() ); }
s
line: 8 col: 2 char: 49 match: --d--
action [28] { System.out.println( yytext() ); }
action [27] { System.out.println( yytext() ); }
d
line: 8 col: 3 char: 50 match: --f--
action [28] { System.out.println( yytext() ); }
action [27] { System.out.println( yytext() ); }
f
line: 8 col: 4 char: 51 match: --a--
action [28] { System.out.println( yytext() ); }
action [27] { System.out.println( yytext() ); }
a
line: 8 col: 5 char: 52 match: --\u000A--
action [25] { System.out.println("\\n"); }
action [24] { System.out.println("\\n"); }
\n
line: 9 col: 1 char: 53 match: --hello--
action [20] { System.out.println("hello at BOL"); }
hello at BOL
-1
Loading

0 comments on commit 2b166cb

Please sign in to comment.