Skip to content

Commit

Permalink
fix comment sanning for double dashes
Browse files Browse the repository at this point in the history
  • Loading branch information
xumingming committed Nov 28, 2014
1 parent fa57e7f commit 8572e4e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 39 deletions.
78 changes: 39 additions & 39 deletions src/main/java/com/alibaba/druid/sql/parser/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -736,66 +736,66 @@ public void scanComment() {
throw new NotAllowCommentException();
}

if (ch != '/') {
if ((ch == '/' && charAt(pos + 1) == '/')
|| (ch == '-' && charAt(pos + 1) == '-')) {
scanSingleLineComment();
} else if (ch == '/' && charAt(pos + 1) == '*') {
scanMultiLineComment();
} else {
throw new IllegalStateException();
}
}

private void scanMultiLineComment() {
scanChar();
scanChar();
mark = pos;
bufPos = 0;
scanChar();

if (ch == '*') {
scanChar();
bufPos++;

for (;;) {
if (ch == '*' && charAt(pos + 1) == '/') {
bufPos += 2;
scanChar();
scanChar();
break;
}

for (;;) {
if (ch == '*' && charAt(pos + 1) == '/') {
scanChar();
bufPos++;
scanChar();
break;
}

stringVal = subString(mark, bufPos);
token = Token.MULTI_LINE_COMMENT;
hasComment = true;
return;
}

if (ch == '/') {
scanChar();
bufPos++;
}

for (;;) {
if (ch == '\r') {
if (charAt(pos + 1) == '\n') {
bufPos += 2;
scanChar();
break;
}
bufPos++;
break;
}
stringVal = subString(mark, bufPos);
token = Token.MULTI_LINE_COMMENT;
hasComment = true;
}

if (ch == '\n') {
private void scanSingleLineComment() {
scanChar();
scanChar();
mark = pos;
bufPos = 0;

for (;;) {
if (ch == '\r') {
if (charAt(pos + 1) == '\n') {
scanChar();
bufPos++;
break;
}
bufPos++;
break;
}

if (ch == '\n') {
scanChar();
bufPos++;
break;
}

stringVal = subString(mark + 1, bufPos);
token = Token.LINE_COMMENT;
hasComment = true;
return;
scanChar();
bufPos++;
}

stringVal = subString(mark, bufPos);
token = Token.LINE_COMMENT;
hasComment = true;
}

public void scanIdentifier() {
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/alibaba/druid/sql/parser/CommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,21 @@ public void test_1() throws Exception {

System.out.println(out.toString());
}

public void test_2() throws Exception {
String sql = "//hello world\n";
Lexer lexer = new Lexer(sql);
lexer.nextToken();
assertEquals("hello world", lexer.stringVal());

sql = "/*hello \nworld*/";
lexer = new Lexer(sql);
lexer.nextToken();
assertEquals("hello \nworld", lexer.stringVal());

sql = "--hello world\n";
lexer = new Lexer(sql);
lexer.nextToken();
assertEquals("hello world", lexer.stringVal());
}
}

0 comments on commit 8572e4e

Please sign in to comment.