Skip to content

Commit

Permalink
HHH-14152 sql-script.g fix for antlr non deterministic warning
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 authored and Sanne committed Aug 26, 2020
1 parent b7c871d commit 8beb1a2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
40 changes: 28 additions & 12 deletions hibernate-core/src/main/antlr/sql-script.g
Expand Up @@ -42,6 +42,11 @@ options {
protected void statementEnded() {
// by default, nothing to do
}

protected void skip() {
// by default, nothing to do
}

}


Expand All @@ -52,11 +57,11 @@ options {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

script
: (statement)+ EOF
: (newLineToSkip)* (statement)+ EOF
;

statement
: { statementStarted(); } (statementPart)* DELIMITER (WS_CHAR)* { statementEnded(); }
: { statementStarted(); } (statementPart (afterStatementPartNewline)*)* DELIMITER (newLineToSkip)* { statementEnded(); }
;

statementPart
Expand All @@ -70,12 +75,22 @@ quotedString
}
;

afterStatementPartNewline
: NEWLINE {
out(" ");
}
;

newLineToSkip
: NEWLINE {
skip();
}
;


nonSkippedChar
: w:WS_CHAR {
out( w );
}
| o:OTHER_CHAR {
out( o );
: c:CHAR {
out( c );
}
;

Expand Down Expand Up @@ -108,13 +123,14 @@ QUOTED_TEXT
protected
ESCqs : '\'' '\'' ;

WS_CHAR
: ' '
| '\t'
| ( "\r\n" | '\r' | '\n' ) {newline();}
CHAR
: ( ' ' | '\t' ) => ( ' ' | '\t' )
| ~( ';' | '\n' | '\r' )
;

OTHER_CHAR : ~( ';' | ' ' | '\t' | '\n' | '\r' );
NEWLINE
: ( '\r' | '\n' | '\r''\n' )
;

LINE_COMMENT
// match `//` or `--` followed by anything other than \n or \r until NEWLINE
Expand Down
Expand Up @@ -33,9 +33,9 @@ public class MultiLineImportExtractorTest {
public void testExtraction() throws IOException {
final ClassLoader classLoader = ClassLoader.getSystemClassLoader();

try ( final InputStream stream = classLoader.getResourceAsStream( IMPORT_FILE ) ) {
try (final InputStream stream = classLoader.getResourceAsStream( IMPORT_FILE )) {
assertThat( stream, notNullValue() );
try ( final InputStreamReader reader = new InputStreamReader( stream ) ) {
try (final InputStreamReader reader = new InputStreamReader( stream )) {
final String[] commands = extractor.extractCommands( reader );
assertThat( commands, notNullValue() );
assertThat( commands.length, is( 6 ) );
Expand All @@ -46,15 +46,15 @@ public void testExtraction() throws IOException {

assertThat( commands[1], is( "INSERT INTO test_data VALUES (1, 'sample')" ) );

assertThat( commands[2], is( "DELETE" + System.lineSeparator() + " FROM test_data" ) );
assertThat( commands[2], is( "DELETE FROM test_data" ) );

assertThat( commands[3], startsWith( "INSERT INTO test_data VALUES (2," ) );
assertThat( commands[3], containsString( "-- line 2" ) );

assertThat( commands[4], startsWith( "INSERT INTO test_data VALUES (3" ) );
assertThat( commands[4], not( containsString( "third record" ) ) );

assertThat( commands[5], startsWith( "INSERT INTO test_data" + System.lineSeparator() + "VALUES" + System.lineSeparator() ) );
assertThat( commands[5].replace( "\t", "" ), is( "INSERT INTO test_data VALUES ( 4 , NULL )" ) );
}
}
}
Expand Down

0 comments on commit 8beb1a2

Please sign in to comment.