Skip to content
Closed
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
6 changes: 5 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -1845,7 +1845,11 @@ TOKEN : /* Numeric Constants */

SPECIAL_TOKEN:
{
< LINE_COMMENT: ("--" | "//") (~["\r","\n"])*>
// The `#` comment requires a following blank: `#` without one stays an
// identifier start (SQL Server `#temp`, `##global`) and `#>` / `#>>`
// JSON operators must keep their usual lexing (issue #1197, #1695).
< LINE_COMMENT: ("--" | "//") (~["\r","\n"])*
| "#" [" ", "\t"] (~["\r","\n"])*>
}

// Nested block comments: /* ... /* ... */ ... */
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/net/sf/jsqlparser/parser/CCJSqlParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,40 @@ public void accept(Statement statement) {
assertEquals(3, list.size());
}

@Test
public void testLineCommentHashTrailing() throws Exception {
assertEquals("SELECT 1", CCJSqlParserUtil.parse("SELECT 1 # trailing comment").toString());
}

@Test
public void testLineCommentHashLeading() throws Exception {
assertEquals("SELECT 1", CCJSqlParserUtil.parse("# leading comment\nSELECT 1").toString());
}

@Test
public void testLineCommentHashWithinSelect() throws Exception {
assertEquals("SELECT 1 + 2, 3",
CCJSqlParserUtil.parse("SELECT 1 + 2 # note\n, 3").toString());
}

@Test
public void testHashWithoutBlankStaysIdentifier() throws Exception {
// without a following blank `#` is an identifier start, not a comment (#1197, #1695)
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT * FROM #temp");
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT * FROM ##global");
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT * FROM #$tab1#");
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT * FROM $#tab1#");
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT * FROM #$tab#tab1");
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT a#b FROM t");
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT 1 #note");
}

@Test
public void testHashJsonOperatorsStayOperators() throws Exception {
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT js #> '{a}' FROM t");
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT js #>> '{a,b}' FROM t");
}

@Test
public void testParseStatementsFail() throws Exception {
String sqlStr = "select * from dual;WHATEVER!!";
Expand Down
Loading