Skip to content

Commit

Permalink
Introduce parser-level block comments (#1062)
Browse files Browse the repository at this point in the history
* Introduce parser-level block comments

* Polish
  • Loading branch information
nakamura-to authored Feb 17, 2024
1 parent 4f4f19c commit e492cb0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ public SqlNode parse() {
parseEmbeddedVariableBlockComment();
break;
}
case PARSER_LEVEL_BLOCK_COMMENT:
{
parseParserLevelBlockComment();
break;
}
case IF_BLOCK_COMMENT:
{
parseIfBlockComment();
Expand Down Expand Up @@ -456,6 +461,10 @@ protected void parseEmbeddedVariableBlockComment() {
push(node);
}

protected void parseParserLevelBlockComment() {
// do nothing
}

protected void parseIfBlockComment() {
IfBlockNode ifBlockNode = new IfBlockNode();
appendNode(ifBlockNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public enum SqlTokenType {

BLOCK_COMMENT,

PARSER_LEVEL_BLOCK_COMMENT,

BIND_VARIABLE_BLOCK_COMMENT {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.seasar.doma.internal.jdbc.sql.SqlTokenType.ORDER_BY_WORD;
import static org.seasar.doma.internal.jdbc.sql.SqlTokenType.OR_WORD;
import static org.seasar.doma.internal.jdbc.sql.SqlTokenType.OTHER;
import static org.seasar.doma.internal.jdbc.sql.SqlTokenType.PARSER_LEVEL_BLOCK_COMMENT;
import static org.seasar.doma.internal.jdbc.sql.SqlTokenType.POPULATE_BLOCK_COMMENT;
import static org.seasar.doma.internal.jdbc.sql.SqlTokenType.QUOTE;
import static org.seasar.doma.internal.jdbc.sql.SqlTokenType.SELECT_WORD;
Expand Down Expand Up @@ -380,7 +381,9 @@ protected void peekTwoChars(char c, char c2) {
} else if (c3 == '%') {
if (buf.hasRemaining()) {
char c4 = buf.get();
if (buf.hasRemaining()) {
if (c4 == '!') {
type = PARSER_LEVEL_BLOCK_COMMENT;
} else if (buf.hasRemaining()) {
char c5 = buf.get();
if (c4 == 'i' && c5 == 'f') {
if (isBlockCommentDirectiveTerminated()) {
Expand Down Expand Up @@ -467,7 +470,8 @@ protected void peekTwoChars(char c, char c2) {
buf.position(buf.position() - 1);
}
}
if (type != IF_BLOCK_COMMENT
if (type != PARSER_LEVEL_BLOCK_COMMENT
&& type != IF_BLOCK_COMMENT
&& type != FOR_BLOCK_COMMENT
&& type != END_BLOCK_COMMENT
&& type != ELSE_BLOCK_COMMENT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public enum Message implements MessageResource {
DOMA2119(
"Failed to build the SQL on line {1} at column {2}. "
+ "When the directive starts with \"/*%\", "
+ "the following string must be either \"if\", \"else\", \"elseif\", \"for\", \"end\", \"expand\" or \"populate\". SQL=[{0}]"),
+ "the following string must be either \"!\", \"if\", \"else\", \"elseif\", \"for\", \"end\", \"expand\" or \"populate\". SQL=[{0}]"),
DOMA2120(
"Failed to parse the SQL on line {1} at column {2}. "
+ "While the bind variable directive \"{3}\" is defined, the expression is none. SQL=[{0}]"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,18 @@ public void testFor_removeOr() {
assertEquals(0, sql.getParameters().size());
}

@Test
public void testParserLevelBlockComment() {
ExpressionEvaluator evaluator = new ExpressionEvaluator();
SqlParser parser = new SqlParser("select /*%! comment */a from b");
SqlNode sqlNode = parser.parse();
PreparedSql sql =
new NodePreparedSqlBuilder(
config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED)
.build(sqlNode, Function.identity());
assertEquals("select a from b", sql.getRawSql());
}

@Test
public void testFor_index() {
ExpressionEvaluator evaluator = new ExpressionEvaluator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.seasar.doma.internal.jdbc.sql.SqlTokenType.ORDER_BY_WORD;
import static org.seasar.doma.internal.jdbc.sql.SqlTokenType.OR_WORD;
import static org.seasar.doma.internal.jdbc.sql.SqlTokenType.OTHER;
import static org.seasar.doma.internal.jdbc.sql.SqlTokenType.PARSER_LEVEL_BLOCK_COMMENT;
import static org.seasar.doma.internal.jdbc.sql.SqlTokenType.POPULATE_BLOCK_COMMENT;
import static org.seasar.doma.internal.jdbc.sql.SqlTokenType.QUOTE;
import static org.seasar.doma.internal.jdbc.sql.SqlTokenType.SELECT_WORD;
Expand Down Expand Up @@ -125,6 +126,21 @@ public void testBlockComment_empty() {
assertNull(tokenizer.getToken());
}

@Test
public void testParserLevelBlockComment() {
SqlTokenizer tokenizer = new SqlTokenizer("where /*%!aaa*/bbb");
assertEquals(WHERE_WORD, tokenizer.next());
assertEquals("where", tokenizer.getToken());
assertEquals(WHITESPACE, tokenizer.next());
assertEquals(" ", tokenizer.getToken());
assertEquals(PARSER_LEVEL_BLOCK_COMMENT, tokenizer.next());
assertEquals("/*%!aaa*/", tokenizer.getToken());
assertEquals(WORD, tokenizer.next());
assertEquals("bbb", tokenizer.getToken());
assertEquals(EOF, tokenizer.next());
assertNull(tokenizer.getToken());
}

@Test
public void testQuote() {
SqlTokenizer tokenizer = new SqlTokenizer("where 'aaa'");
Expand Down

0 comments on commit e492cb0

Please sign in to comment.