changeset 10722:c4fb2103e734

check-code: improve quote detection regexp, add tests
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
date Wed, 17 Mar 2010 14:15:33 +0100
parents 67ba66070aee
children 8ea152e94484
files contrib/check-code.py tests/test-check-code tests/test-check-code.out
diffstat 3 files changed, 18 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/check-code.py	Wed Mar 17 10:55:28 2010 +0100
+++ b/contrib/check-code.py	Wed Mar 17 14:15:33 2010 +0100
@@ -10,9 +10,9 @@
 import sys, re, glob
 
 def repquote(m):
-    t = re.sub(r"\w", "x", m.group(2))
+    t = re.sub(r"\w", "x", m.group('text'))
     t = re.sub(r"[^\sx]", "o", t)
-    return m.group(1) + t + m.group(1)
+    return m.group('quote') + t + m.group('quote')
 
 def repcomment(m):
     return m.group(1) + "#" * len(m.group(2))
@@ -96,10 +96,9 @@
 ]
 
 pyfilters = [
-    (r'''(?<!")(")(([^"\n]|\\")+)"(?!")''', repquote),
-    (r"""(?<!')(')(([^'\n]|\\')+)'(?!')""", repquote),
-    (r"""(''')(([^']|\\'|'{1,2}(?!'))*)'''""", repquote),
-    (r'''(""")(([^"]|\\"|"{1,2}(?!"))*)"""''', repquote),
+    (r"""(?msx)(?P<quote>('''|\"\"\"|(?<!')'(?!')|(?<!")"(?!")))
+         (?P<text>(.*?))
+         (?<!\\)(?P=quote)""", repquote),
     (r"( *)(#([^\n]*\S)?)", repcomment),
 ]
 
@@ -123,7 +122,7 @@
 
 cfilters = [
     (r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment),
-    (r'''(?<!")(")(([^"]|\\")+"(?!"))''', repquote),
+    (r'''(?P<quote>(?<!")")(?P<text>([^"]|\\")+)"(?!")''', repquote),
     (r'''(#\s*include\s+<)([^>]+)>''', repinclude),
     (r'(\()([^)]+\))', repcallspaces),
 ]
--- a/tests/test-check-code	Wed Mar 17 10:55:28 2010 +0100
+++ b/tests/test-check-code	Wed Mar 17 14:15:33 2010 +0100
@@ -12,5 +12,13 @@
     return ( 5+6, 9)
 EOF
 
+cat > quote.py <<EOF
+(''' ( 4x5 )
+but """\''' and finally''',
+"""let's fool checkpatch""", '1+2',
+'"""', 42+1, """and
+( 4-1 ) """, "( 1+1 )\" and ")
+EOF
+
 check_code=`dirname $0`/../contrib/check-code.py
-${check_code} ./wrong.py ./correct.py
+${check_code} ./wrong.py ./correct.py ./quote.py
--- a/tests/test-check-code.out	Wed Mar 17 10:55:28 2010 +0100
+++ b/tests/test-check-code.out	Wed Mar 17 14:15:33 2010 +0100
@@ -8,3 +8,6 @@
  >     return ( 5+6, 9)
  missing whitespace in expression
  gratuitous whitespace in () or []
+./quote.py:4:
+ > '"""', 42+1, """and
+ missing whitespace in expression