[[nbpython-commits]] [hg] main/contrib: 173320: syntax error not detected for "if con...

  • From: Tor Norbye < >
  • To:
  • Subject: [[nbpython-commits]] [hg] main/contrib: 173320: syntax error not detected for "if con...
  • Date: Thu, 21 Oct 2010 08:34:40 -0700

changeset 448fc5b7362b in main/contrib
details: http://hg.netbeans.org/main/contrib?cmd=changeset;node=448fc5b7362b
description:
        173320: syntax error not detected for "if cond" without colon

diffstat:

 python.editor/src/org/netbeans/modules/python/editor/PythonParser.java       
        |  24 ++++++++++
 python.editor/test/unit/data/testfiles/errors1.py.errors                     
        |   1 +
 python.editor/test/unit/data/testfiles/errors10.py                           
        |   6 ++
 python.editor/test/unit/data/testfiles/errors10.py.errors                    
        |   1 +
 python.editor/test/unit/data/testfiles/errors11.py                           
        |  12 +++++
 python.editor/test/unit/data/testfiles/errors11.py.errors                    
        |   3 +
 python.editor/test/unit/data/testfiles/errors4.py.errors                     
        |   3 +
 
python.editor/test/unit/src/org/netbeans/modules/python/editor/PythonParserTest.java
 |  10 ++++
 8 files changed, 60 insertions(+), 0 deletions(-)

diffs (122 lines):

diff --git 
a/python.editor/src/org/netbeans/modules/python/editor/PythonParser.java 
b/python.editor/src/org/netbeans/modules/python/editor/PythonParser.java
--- a/python.editor/src/org/netbeans/modules/python/editor/PythonParser.java
+++ b/python.editor/src/org/netbeans/modules/python/editor/PythonParser.java
@@ -58,6 +58,7 @@
 import org.netbeans.modules.gsf.spi.DefaultError;
 import org.netbeans.modules.gsf.spi.GsfUtilities;
 import org.openide.filesystems.FileUtil;
+import org.openide.text.NbDocument;
 import org.openide.util.Exceptions;
 import org.python.antlr.BaseParser;
 import org.python.antlr.GrammarActions;
@@ -180,6 +181,17 @@
 
                 @Override
                 public Object recoverFromMismatchedToken(BaseRecognizer br, 
IntStream input, int ttype, BitSet follow) {
+                    MismatchedTokenException mt = new 
MismatchedTokenException(ttype, input);
+                    String message = br.getErrorMessage(mt, 
br.getTokenNames());
+                    if (mt.line >= 1) {
+                        int lineOffset = findLineOffset(context.source, 
mt.line-1);
+                        if (mt.charPositionInLine > 0) {
+                            lineOffset += mt.charPositionInLine;
+                        }
+                        int start = lineOffset;//t.getCharStartIndex();
+                        int stop = lineOffset;//t.getCharStopIndex();
+                        errors.add(new DefaultError(null, message, null, 
file.getFileObject(), start, stop, Severity.ERROR));
+                    }
                     return super.recoverFromMismatchedToken(br, input, 
ttype, follow);
                 }
 
@@ -599,6 +611,18 @@
         return false;
     }
 
+    private static int findLineOffset(String source, int line) {
+        int offset = -1;
+        for (int i = 0; i < line; i++) {
+            offset = source.indexOf("\n", offset+1);
+            if (offset == -1) {
+                return source.length();
+            }
+        }
+
+        return Math.min(source.length(), offset+1);
+    }
+
     /** Attempts to sanitize the input buffer */
     public static enum Sanitize {
         /** Only parse the current file accurately, don't try heuristics */
diff --git a/python.editor/test/unit/data/testfiles/errors1.py.errors 
b/python.editor/test/unit/data/testfiles/errors1.py.errors
--- a/python.editor/test/unit/data/testfiles/errors1.py.errors
+++ b/python.editor/test/unit/data/testfiles/errors1.py.errors
@@ -1,1 +1,2 @@
 169-169:mismatched input '' expecting INDENT
+170-170:mismatched input '' expecting INDENT
diff --git a/python.editor/test/unit/data/testfiles/errors10.py 
b/python.editor/test/unit/data/testfiles/errors10.py
new file mode 100644
--- /dev/null
+++ b/python.editor/test/unit/data/testfiles/errors10.py
@@ -0,0 +1,6 @@
+def myFunc(p):
+    if p
+        pass
+    else:
+        pass
+
diff --git a/python.editor/test/unit/data/testfiles/errors10.py.errors 
b/python.editor/test/unit/data/testfiles/errors10.py.errors
new file mode 100644
--- /dev/null
+++ b/python.editor/test/unit/data/testfiles/errors10.py.errors
@@ -0,0 +1,1 @@
+23-23:mismatched input '\n' expecting COLON
diff --git a/python.editor/test/unit/data/testfiles/errors11.py 
b/python.editor/test/unit/data/testfiles/errors11.py
new file mode 100644
--- /dev/null
+++ b/python.editor/test/unit/data/testfiles/errors11.py
@@ -0,0 +1,12 @@
+def myFunc(p):
+    if p:
+        pass
+    else
+        pass
+
+def myFunc(p):
+    try
+        p
+    except
+        pass
+
diff --git a/python.editor/test/unit/data/testfiles/errors11.py.errors 
b/python.editor/test/unit/data/testfiles/errors11.py.errors
new file mode 100644
--- /dev/null
+++ b/python.editor/test/unit/data/testfiles/errors11.py.errors
@@ -0,0 +1,3 @@
+104-104:mismatched input '\n' expecting COLON
+46-46:mismatched input '\n' expecting COLON
+83-83:mismatched input '\n' expecting COLON
diff --git a/python.editor/test/unit/data/testfiles/errors4.py.errors 
b/python.editor/test/unit/data/testfiles/errors4.py.errors
--- a/python.editor/test/unit/data/testfiles/errors4.py.errors
+++ b/python.editor/test/unit/data/testfiles/errors4.py.errors
@@ -1,1 +1,4 @@
 83-105:rule STRINGPART failed predicate: {partial}?
+86-86:mismatched input 'Missing' expecting NEWLINE
+94-94:mismatched input 'end' expecting NEWLINE
+98-98:mismatched input 'markers' expecting NEWLINE
diff --git 
a/python.editor/test/unit/src/org/netbeans/modules/python/editor/PythonParserTest.java
 
b/python.editor/test/unit/src/org/netbeans/modules/python/editor/PythonParserTest.java
--- 
a/python.editor/test/unit/src/org/netbeans/modules/python/editor/PythonParserTest.java
+++ 
b/python.editor/test/unit/src/org/netbeans/modules/python/editor/PythonParserTest.java
@@ -125,4 +125,14 @@
     public void testPython26Syntax() throws Exception {
         checkNoParseAbort("testfiles/exceptas.py");
     }
+
+    public void testMissingColons1() throws Exception {
+        checkNoParseAbort("testfiles/errors10.py");
+        checkErrors("testfiles/errors10.py");
+    }
+
+    public void testMissingColons2() throws Exception {
+        checkNoParseAbort("testfiles/errors11.py");
+        checkErrors("testfiles/errors11.py");
+    }
 }

[[nbpython-commits]] [hg] main/contrib: 173320: syntax error not detected for "if con...

Tor Norbye 10/21/2010

Project Features

About this Project

www was started in November 2009, is owned by jpirek, and has 21 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20140418.2d69abc). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close