db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r604871 - in /db/derby/code/trunk/java: testing/org/apache/derbyTesting/functionTests/tests/lang/ testing/org/apache/derbyTesting/functionTests/tests/tools/ tools/org/apache/derby/impl/tools/ij/
Date Mon, 17 Dec 2007 13:55:54 GMT
Author: kahatlen
Date: Mon Dec 17 05:55:52 2007
New Revision: 604871

URL: http://svn.apache.org/viewvc?rev=604871&view=rev
Log:
DERBY-3242: ij doesn't understand bracketed comments

Fix contributed by James F. Adams.

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CommentTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/IJRunScriptTest.java
    db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/StatementFinder.java
    db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/ij.jj

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CommentTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CommentTest.java?rev=604871&r1=604870&r2=604871&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CommentTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CommentTest.java
Mon Dec 17 05:55:52 2007
@@ -120,11 +120,15 @@
             stmt.executeQuery("VALUES '/* a comment \n-- */'"),
             new String [][] {{"/* a comment \n-- */"}});
 
-        // unterminated comments
+        // unterminated comments generate lexical errors
         assertCallError("42X03", getConnection(), "VALUES 1 /*");
         assertCallError("42X03", getConnection(), "VALUES 1 /* comment");
         assertCallError("42X03", getConnection(), "VALUES 1 /* comment /*");
         assertCallError("42X03", getConnection(), "VALUES 1 /* comment /* nested */");
+
+        // just comments generates syntax error
+        assertCompileError("42X01", "/* this is a comment */");
+        assertCompileError("42X01", "-- this is a comment \n");
     }
     
     /**

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/IJRunScriptTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/IJRunScriptTest.java?rev=604871&r1=604870&r2=604871&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/IJRunScriptTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/IJRunScriptTest.java
Mon Dec 17 05:55:52 2007
@@ -82,6 +82,19 @@
     }
 
     /**
+     * Test support for nested bracketed comments.
+     * @throws SQLException
+     * @throws UnsupportedEncodingException
+     */
+    public void testBracketedComment()
+        throws SQLException, UnsupportedEncodingException
+    {
+        runTestingScript("VALUES /* comment /* nested comment */ 5; */ 1;", 0);
+        runTestingScript("/* just a comment */", 0);
+        runTestingScript("/* a /* nested */ comment */", 0);
+    }
+
+    /**
      * Test execution an empty script.
      * @throws SQLException
      * @throws UnsupportedEncodingException 

Modified: db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/StatementFinder.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/StatementFinder.java?rev=604871&r1=604870&r2=604871&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/StatementFinder.java (original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/StatementFinder.java Mon
Dec 17 05:55:52 2007
@@ -36,8 +36,9 @@
 	are not considered to be statement terminators but to be
 	part of those tokens.
 	<p>
-	The only comment form currently recognized is the SQL comment,
-	which begins with "--" and ends at the next EOL.
+    Comments currently recognized include the SQL comment,
+    which begins with "--" and ends at the next EOL, and nested
+    bracketed comments.
 	<p>
 	Strings and delimited identifiers are permitted to contain
 	newlines; the actual IJ or JSQL parsers will report errors when
@@ -78,6 +79,8 @@
 	private static final char SPACE = ' ';
 	private static final char TAB = '\t';
 	private static final char FORMFEED = '\f';
+	private static final char SLASH = '/';
+	private static final char ASTERISK = '*';
 
 	/**
 		The constructor does not assume the stream is data input
@@ -181,6 +184,9 @@
 				case MINUS:
 					readSingleLineComment(nextChar);
 					break;
+				case SLASH:
+				    readBracketedComment();
+				    break;
 				case SINGLEQUOTE:
 				case DOUBLEQUOTE:
 					readString(nextChar);
@@ -224,6 +230,64 @@
 		    	c == RETURN ||
 		    	c == NEWLINE ||
 		    	c == FORMFEED);
+	}
+
+	/**
+	 	* Advance the source stream to the end of a comment
+		* if it is on one, assuming the first character of
+		* a potential bracketed comment has been found.
+		* If it is not a comment, do not advance the stream.
+	 */
+	private void readBracketedComment() {
+		char nextChar = peekChar();
+
+		// if next char is EOF, we are done.
+		if (peekEOF()) return;
+
+		// if nextChar is not an asterisk, then not a comment.
+		if (nextChar != ASTERISK)
+		{
+			continuedStatement = true;
+			return;
+		}
+
+		// we are really in a comment
+		readChar(); // grab the asterisk for real.
+
+		int nestingLevel = 1;
+
+		while (true) {
+			nextChar = readChar();
+
+			if (atEOF()) {
+				// let the caller process the EOF, don't read it
+				state = IN_STATEMENT;
+				return;
+			}
+
+			char peek = peekChar();
+
+			if (nextChar == SLASH && peek == ASTERISK) {
+				readChar();
+				nestingLevel++;
+			} else if (nextChar == ASTERISK && peek == SLASH) {
+				readChar();
+				nestingLevel--;
+				if (nestingLevel == 0) {
+					state = IN_STATEMENT;
+					return;
+				}
+			} else if (nextChar == NEWLINE || nextChar == RETURN) {
+				if (doPrompt) {
+					utilMain.doPrompt(false, promptwriter, "");
+					// If the next character is a NEWLINE, we process
+					// it as well to account for Windows CRLFs.
+					if (nextChar == RETURN && peek == NEWLINE) {
+						readChar();
+					}
+				}
+			}
+		}
 	}
 
 	/**

Modified: db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/ij.jj
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/ij.jj?rev=604871&r1=604870&r2=604871&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/ij.jj (original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/ij.jj Mon Dec 17 05:55:52
2007
@@ -927,6 +927,11 @@
 
 PARSER_END(ij)
 
+TOKEN_MGR_DECLS :
+{
+	int commentNestingDepth = 0;
+}
+
 /* WHITE SPACE */
 
 SKIP :
@@ -939,13 +944,44 @@
 | "\f"
 }
 
-// sqlgrammar only recognizes --, so that's all we will recognize as well.
 SPECIAL_TOKEN : /* COMMENTS */
 {
   <SINGLE_LINE_SQLCOMMENT: "--" (~["\n","\r"])* ("\n"|"\r"|"\r\n")>
 //| <SINGLE_LINE_COMMENT: "//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")>
 //| <FORMAL_COMMENT: "/**" (~["*"])* "*" ("*" | (~["*","/"] (~["*"])* "*"))* "/">
 //| <MULTI_LINE_COMMENT: "/*" (~["*"])* "*" ("*" | (~["*","/"] (~["*"])* "*"))* "/">
+}
+
+
+MORE :
+{
+	"/*" : IN_BRACKETED_COMMENT
+}
+
+<IN_BRACKETED_COMMENT> MORE :
+{
+	"/*" { commentNestingDepth = 1; } : IN_NESTED_BRACKETED_COMMENT
+}
+
+<IN_BRACKETED_COMMENT> SKIP :
+{
+	"*/" : DEFAULT
+}
+
+<IN_NESTED_BRACKETED_COMMENT> MORE :
+{
+	"/*" { commentNestingDepth++; }
+}
+
+<IN_NESTED_BRACKETED_COMMENT> MORE :
+{
+	"*/"
+	{ commentNestingDepth--; SwitchTo(commentNestingDepth == 0 ? IN_BRACKETED_COMMENT : IN_NESTED_BRACKETED_COMMENT);
}
+}
+
+<IN_BRACKETED_COMMENT,IN_NESTED_BRACKETED_COMMENT> MORE :
+{
+	< ~[] >
 }
 
 TOKEN [IGNORE_CASE] :



Mime
View raw message