You are viewing a plain text version of this content. The canonical link for it is here.
Posted to yoko-commits@incubator.apache.org by lk...@apache.org on 2007/05/01 21:09:16 UTC

svn commit: r534245 - in /incubator/yoko/trunk/tools/src: main/java/org/apache/yoko/tools/idlpreprocessor/ test/java/org/apache/yoko/tools/idlpreprocessor/ test/resources/idlpreprocessor/

Author: lkuehne
Date: Tue May  1 14:09:15 2007
New Revision: 534245

URL: http://svn.apache.org/viewvc?view=rev&rev=534245
Log:
YOKO-320: IDL preprocessor - fixed line numbers in location markers and added a test for if-else handling

Added:
    incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/C-resolved.idl
    incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/C.idl
Modified:
    incubator/yoko/trunk/tools/src/main/java/org/apache/yoko/tools/idlpreprocessor/IdlPreprocessorReader.java
    incubator/yoko/trunk/tools/src/main/java/org/apache/yoko/tools/idlpreprocessor/IncludeStackEntry.java
    incubator/yoko/trunk/tools/src/test/java/org/apache/yoko/tools/idlpreprocessor/IdlPreprocessorReaderTest.java
    incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/A-resolved.idl
    incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/B-resolved.idl

Modified: incubator/yoko/trunk/tools/src/main/java/org/apache/yoko/tools/idlpreprocessor/IdlPreprocessorReader.java
URL: http://svn.apache.org/viewvc/incubator/yoko/trunk/tools/src/main/java/org/apache/yoko/tools/idlpreprocessor/IdlPreprocessorReader.java?view=diff&rev=534245&r1=534244&r2=534245
==============================================================================
--- incubator/yoko/trunk/tools/src/main/java/org/apache/yoko/tools/idlpreprocessor/IdlPreprocessorReader.java (original)
+++ incubator/yoko/trunk/tools/src/main/java/org/apache/yoko/tools/idlpreprocessor/IdlPreprocessorReader.java Tue May  1 14:09:15 2007
@@ -28,7 +28,7 @@
 /**
  * A Reader that implements the #include functionality of the preprocessor.
  * Starting from one URL, it generates one stream of characters by tracking
- * #defines and following #includes accordingly.
+ * #defines, #ifdefs, etc. and following #includes accordingly.
  * 
  * <p>
  * This reader augments the stream with
@@ -36,7 +36,7 @@
  * location information</a> when the source URL is switched.
  * This improves error reporting (with correct file and linenumber information) in the
  * subsequent compilation steps like IDL parsing and also allows the implentation
- * of code generation options like the -emitAll flag in the JDK idlj tool.
+ * of code generation options like the -emitAll flag available in the JDK idlj tool.
  * </p>
  */
 public final class IdlPreprocessorReader extends Reader {
@@ -134,6 +134,9 @@
         return numCharsRead;
     }
 
+    /**
+     * @see Reader#read()
+     */
     public int read() throws IOException {
 
         if (buf.length() == 0) {
@@ -147,6 +150,7 @@
     private void fillBuffer() throws IOException {
         while (!includeStack.isEmpty()) {
             LineNumberReader reader = getReader();
+            final int lineNo = reader.getLineNumber();
             String line = reader.readLine();
 
             if (line == null) {
@@ -157,13 +161,12 @@
             if (!line.startsWith("#")) {
                 if (!skips()) {
                     buf.append(line);
-                    buf.append(LF);
                 }
+                buf.append(LF);
                 continue;
             }
 
             final IncludeStackEntry ise = includeStack.peek();
-            final int lineNo = reader.getLineNumber();
 
             if (line.startsWith("#include")) {
                 handleInclude(line, lineNo, ise);
@@ -180,7 +183,6 @@
             } else {
                 throw new PreprocessingException("unknown preprocessor instruction", ise.getURL(), lineNo);
             }
-            buf.append(LF);
         }
     }
 
@@ -188,6 +190,7 @@
      * TODO: support multiline definitions, functions, etc. 
      */
     private void handleDefine(String line) {
+        buf.append(LF);
         if (skips()) {
             return;
         }
@@ -206,8 +209,9 @@
         if (ifStack.isEmpty()) {
             throw new PreprocessingException("unexpected #endif", ise.getURL(), lineNo);
         }
-        Boolean top = ifStack.pop();
-        ifStack.push(Boolean.valueOf(!top.booleanValue()));
+        boolean top = ifStack.pop();
+        ifStack.push(!top);
+        buf.append(LF);
     }
 
     private void handleEndif(int lineNo, final IncludeStackEntry ise) {
@@ -215,23 +219,27 @@
             throw new PreprocessingException("unexpected #endif", ise.getURL(), lineNo);
         }
         ifStack.pop();
+        buf.append(LF);
     }
 
     private void handleIfdef(String line) {
         String symbol = line.substring("#ifdef".length()).trim();
         boolean isDefined = defineState.isDefined(symbol);
         registerIf(!isDefined);
+        buf.append(LF);
     }
 
     private void handleIfndef(String line) {
         String symbol = line.substring("#ifndef".length()).trim();
         boolean isDefined = defineState.isDefined(symbol);
         registerIf(isDefined);
+        buf.append(LF);
     }
 
     private void handleInclude(String line, int lineNo, final IncludeStackEntry ise) throws IOException {
         
         if (skips()) {
+            buf.append(LF);
             return;
         }
 
@@ -264,16 +272,17 @@
         pushInclude(include, spec);
     }
 
-    /**
-     * 
-     */
     private void popInclude() throws IOException {
         final IncludeStackEntry poppedStackEntry = includeStack.pop();
+        if (!includeStack.isEmpty()) {
+            buf.append(LF);
+        }
         try {
             if (includeStack.size() > 0) {
+                final IncludeStackEntry newTopEntry = includeStack.peek();
                 final LineNumberReader reader = getReader();
                 final int lineNumber = reader.getLineNumber();
-                final String location = poppedStackEntry.getLocation();
+                final String location = newTopEntry.getLocation();
                 signalFileChange(location, lineNumber, POP);
             }
         } finally {
@@ -286,12 +295,12 @@
             return false;
         }
 
-        Boolean top = ifStack.peek();
-        return top.booleanValue();
+        boolean top = ifStack.peek();
+        return top;
     }
 
     private void registerIf(boolean skip) {
-        ifStack.push(Boolean.valueOf(skip));
+        ifStack.push(skip);
     }
 
     private LineNumberReader getReader() {

Modified: incubator/yoko/trunk/tools/src/main/java/org/apache/yoko/tools/idlpreprocessor/IncludeStackEntry.java
URL: http://svn.apache.org/viewvc/incubator/yoko/trunk/tools/src/main/java/org/apache/yoko/tools/idlpreprocessor/IncludeStackEntry.java?view=diff&rev=534245&r1=534244&r2=534245
==============================================================================
--- incubator/yoko/trunk/tools/src/main/java/org/apache/yoko/tools/idlpreprocessor/IncludeStackEntry.java (original)
+++ incubator/yoko/trunk/tools/src/main/java/org/apache/yoko/tools/idlpreprocessor/IncludeStackEntry.java Tue May  1 14:09:15 2007
@@ -38,6 +38,7 @@
         this.url = link;
         this.location = loc;
         this.reader = new LineNumberReader(new InputStreamReader(url.openStream(), "ISO-8859-1"));
+        this.reader.setLineNumber(1);
     }
 
     public String getLocation() {
@@ -52,4 +53,8 @@
         return reader;
     }
 
+    public String toString() {
+        return "IncludeStackEntry[url=" + url + ", location=" + location
+            + ", line=" + reader.getLineNumber() + "]";
+    }
 }

Modified: incubator/yoko/trunk/tools/src/test/java/org/apache/yoko/tools/idlpreprocessor/IdlPreprocessorReaderTest.java
URL: http://svn.apache.org/viewvc/incubator/yoko/trunk/tools/src/test/java/org/apache/yoko/tools/idlpreprocessor/IdlPreprocessorReaderTest.java?view=diff&rev=534245&r1=534244&r2=534245
==============================================================================
--- incubator/yoko/trunk/tools/src/test/java/org/apache/yoko/tools/idlpreprocessor/IdlPreprocessorReaderTest.java (original)
+++ incubator/yoko/trunk/tools/src/test/java/org/apache/yoko/tools/idlpreprocessor/IdlPreprocessorReaderTest.java Tue May  1 14:09:15 2007
@@ -33,7 +33,6 @@
     public void testResolvedInA() throws Exception {
         final String location = "A.idl";
         final IdlPreprocessorReader includeReader = createPreprocessorReader(location);
-
         final String expectedResultLocation = "A-resolved.idl";
         assertExpectedPreprocessingResult(expectedResultLocation, includeReader);
     }
@@ -41,11 +40,17 @@
     public void testMultiFileResolve() throws Exception {
         final String location = "B.idl";
         final IdlPreprocessorReader includeReader = createPreprocessorReader(location);
-
         final String expectedResultLocation = "B-resolved.idl";
         assertExpectedPreprocessingResult(expectedResultLocation, includeReader);
     }
 
+    public void testIfElseHandling() throws Exception {
+        final String location = "C.idl";
+        final IdlPreprocessorReader includeReader = createPreprocessorReader(location);
+        final String expectedResultLocation = "C-resolved.idl";
+        assertExpectedPreprocessingResult(expectedResultLocation, includeReader);
+    }
+
     public void testMaximumIncludeDepthIsDetected() throws IOException {
         final String location = "MaximumIncludeDepthExceeded.idl";
         try {
@@ -85,15 +90,19 @@
         LineNumberReader oReader = new LineNumberReader(includeReader);
         InputStream resolved = findTestResource(expectedResultLocation).openStream();
         LineNumberReader rReader = new LineNumberReader(new InputStreamReader(resolved, "ISO-8859-1"));
-        boolean eof = false;
-        do {
-            int line = rReader.getLineNumber();
-            String actualLine = oReader.readLine();
-            String expectedLine = rReader.readLine();
-            assertEquals("difference in line " + line, expectedLine, actualLine);
-            eof = actualLine == null || expectedLine == null;
-        } while (!eof);
-        rReader.close();
+        try {
+        	boolean eof = false;
+        	do {
+        		int line = rReader.getLineNumber() + 1;
+        		String actualLine = oReader.readLine();
+        		String expectedLine = rReader.readLine();
+        		assertEquals("difference in line " + line, expectedLine, actualLine);
+        		eof = actualLine == null || expectedLine == null;
+        	} while (!eof);
+        }
+        finally {
+        	rReader.close();
+        }
     }
 
     private void consumeReader(final Reader includeReader) throws IOException {

Modified: incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/A-resolved.idl
URL: http://svn.apache.org/viewvc/incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/A-resolved.idl?view=diff&rev=534245&r1=534244&r2=534245
==============================================================================
--- incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/A-resolved.idl (original)
+++ incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/A-resolved.idl Tue May  1 14:09:15 2007
@@ -1,9 +1,11 @@
-# 0 A.idl 1
+# 1 A.idl 1
 
 
 
 
-# 0 A.idl 1
+# 1 A.idl 1
+
+
 
 
 
@@ -18,7 +20,16 @@
 };
 
 
-# 5 A.idl 2
+
+# 6 A.idl 2
+
+
+
+
+
+
+
+
 
 
 

Modified: incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/B-resolved.idl
URL: http://svn.apache.org/viewvc/incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/B-resolved.idl?view=diff&rev=534245&r1=534244&r2=534245
==============================================================================
--- incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/B-resolved.idl (original)
+++ incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/B-resolved.idl Tue May  1 14:09:15 2007
@@ -1,11 +1,12 @@
-# 0 B.idl 1
-# 0 A.idl 1
+# 1 B.idl 1
+# 1 A.idl 1
 
 
 
 
+# 1 A.idl 1
+
 
-# 0 A.idl 1
 
 
 
@@ -20,11 +21,21 @@
 };
 
 
-# 5 A.idl 2
+
+# 6 A.idl 2
+
+
+
+
+
+
+
+
+
 
 
 
-# 1 A.idl 2
+# 2 B.idl 2
 
 module a
 {

Added: incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/C-resolved.idl
URL: http://svn.apache.org/viewvc/incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/C-resolved.idl?view=auto&rev=534245
==============================================================================
--- incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/C-resolved.idl (added)
+++ incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/C-resolved.idl Tue May  1 14:09:15 2007
@@ -0,0 +1,26 @@
+# 1 C.idl 1
+
+
+
+
+
+
+
+
+module c
+{
+  interface C {
+
+    const boolean b = TRUE;
+
+
+
+
+
+    const boolean c = FALSE;
+
+
+
+  };
+};
+

Added: incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/C.idl
URL: http://svn.apache.org/viewvc/incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/C.idl?view=auto&rev=534245
==============================================================================
--- incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/C.idl (added)
+++ incubator/yoko/trunk/tools/src/test/resources/idlpreprocessor/C.idl Tue May  1 14:09:15 2007
@@ -0,0 +1,25 @@
+#define _a
+
+#ifdef _a
+#define _b
+#else
+#define _c
+#endif
+
+module c
+{
+  interface C {
+#ifdef _b
+    const boolean b = TRUE;
+#else
+    const boolean b = FALSE;
+#endif
+
+#ifndef _c
+    const boolean c = FALSE;
+#else
+    const boolean c = TRUE;
+#endif
+  };
+};
+