<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[46046] trunk/WebKitTools</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/46046">46046</a></dd>
<dt>Author</dt> <dd>levin@chromium.org</dd>
<dt>Date</dt> <dd>2009-07-17 12:11:11 -0700 (Fri, 17 Jul 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>2009-07-17  Shinichiro Hamaji  &lt;hamaji@chromium.org&gt;

        Reviewed by David Levin.

        Add a parser of patches for linter.
        https://bugs.webkit.org/show_bug.cgi?id=27363

        Adds a simple parser for unified diff format.

        * Scripts/modules/diff_parser.py: Added.
        * Scripts/modules/diff_parser_unittest.py: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkWebKitToolsChangeLog">trunk/WebKitTools/ChangeLog</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkWebKitToolsScriptsmodulesdiff_parserpy">trunk/WebKitTools/Scripts/modules/diff_parser.py</a></li>
<li><a href="#trunkWebKitToolsScriptsmodulesdiff_parser_unittestpy">trunk/WebKitTools/Scripts/modules/diff_parser_unittest.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkWebKitToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/WebKitTools/ChangeLog (46045 => 46046)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebKitTools/ChangeLog        2009-07-17 18:41:14 UTC (rev 46045)
+++ trunk/WebKitTools/ChangeLog        2009-07-17 19:11:11 UTC (rev 46046)
</span><span class="lines">@@ -1,3 +1,15 @@
</span><ins>+2009-07-17  Shinichiro Hamaji  &lt;hamaji@chromium.org&gt;
+
+        Reviewed by David Levin.
+
+        Add a parser of patches for linter.
+        https://bugs.webkit.org/show_bug.cgi?id=27363
+
+        Adds a simple parser for unified diff format.
+
+        * Scripts/modules/diff_parser.py: Added.
+        * Scripts/modules/diff_parser_unittest.py: Added.
+
</ins><span class="cx"> 2009-06-30  Holger Hans Peter Freyther  &lt;zecke@selfish.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Reviewed by Simon Hausmann.
</span></span></pre></div>
<a id="trunkWebKitToolsScriptsmodulesdiff_parserpy"></a>
<div class="addfile"><h4>Added: trunk/WebKitTools/Scripts/modules/diff_parser.py (0 => 46046)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebKitTools/Scripts/modules/diff_parser.py                                (rev 0)
+++ trunk/WebKitTools/Scripts/modules/diff_parser.py        2009-07-17 19:11:11 UTC (rev 46046)
</span><span class="lines">@@ -0,0 +1,161 @@
</span><ins>+# Copyright (C) 2009 Google Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#    * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#    * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+#    * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+&quot;&quot;&quot;WebKit's Python module for interacting with patches.&quot;&quot;&quot;
+
+import logging
+import re
+
+
+_regexp_compile_cache = {}
+
+
+def match(pattern, string):
+    &quot;&quot;&quot;Matches the string with the pattern, caching the compiled regexp.&quot;&quot;&quot;
+    if not pattern in _regexp_compile_cache:
+        _regexp_compile_cache[pattern] = re.compile(pattern)
+    return _regexp_compile_cache[pattern].match(string)
+
+
+def git_diff_to_svn_diff(line):
+    &quot;&quot;&quot;Converts a git formatted diff line to a svn formatted line.
+
+    Args:
+      line: A string representing a line of the diff.
+    &quot;&quot;&quot;
+    conversion_patterns = ((&quot;^diff --git a/(.+) b/(?P&lt;FilePath&gt;.+)&quot;, lambda matched: &quot;Index: &quot; + matched.group('FilePath') + &quot;\n&quot;),
+                           (&quot;^new file.*&quot;, lambda matched: &quot;\n&quot;),
+                           (&quot;^index [0-9a-f]{7}\.\.[0-9a-f]{7} [0-9]{6}&quot;, lambda matched: &quot;===================================================================\n&quot;),
+                           (&quot;^--- a/(?P&lt;FilePath&gt;.+)&quot;, lambda matched: &quot;--- &quot; + matched.group('FilePath') + &quot;\n&quot;),
+                           (&quot;^\+\+\+ b/(?P&lt;FilePath&gt;.+)&quot;, lambda matched: &quot;+++ &quot; + matched.group('FilePath') + &quot;\n&quot;))
+
+    for pattern, conversion in conversion_patterns:
+        matched = match(pattern, line)
+        if matched:
+            return conversion(matched)
+    return line
+
+
+def get_diff_converter(first_diff_line):
+    &quot;&quot;&quot;Gets a converter function of diff lines.
+
+    Args:
+      first_diff_line: The first filename line of a diff file.
+                       If this line is git formatted, we'll return a
+                       converter from git to SVN.
+    &quot;&quot;&quot;
+    if match(r&quot;^diff --git a/&quot;, first_diff_line):
+        return git_diff_to_svn_diff
+    return lambda input: input
+
+
+_INITIAL_STATE = 1
+_DECLARED_FILE_PATH = 2
+_PROCESSING_CHUNK = 3
+
+
+class DiffFile:
+    &quot;&quot;&quot;Contains the information for one file in a patch.
+
+    The field &quot;lines&quot; is a list which contains tuples in this format:
+       (old_line_number, new_line_number, line_string)
+    If old_line_number is zero, it means this line is newly added.
+    If new_line_number is zero, it means this line is deleted.
+    &quot;&quot;&quot;
+
+    def __init__(self, filename):
+        self.filename = filename
+        self.lines = []
+
+    def add_new_line(self, line_number, line):
+        self.lines.append((0, line_number, line))
+
+    def add_old_line(self, line_number, line):
+        self.lines.append((line_number, 0, line))
+
+    def add_unchanged_line(self, old_line_number, new_line_number, line):
+        self.lines.append((old_line_number, new_line_number, line))
+
+
+class DiffParser:
+    &quot;&quot;&quot;A parser for a patch file.
+
+    The field &quot;files&quot; is a dict whose key is the filename and value is
+    a DiffFile object.
+    &quot;&quot;&quot;
+
+    def __init__(self, diff_input):
+        &quot;&quot;&quot;Parses a diff.
+
+        Args:
+          diff_input: An iterable object.
+        &quot;&quot;&quot;
+        state = _INITIAL_STATE
+
+        self.files = {}
+        current_file = None
+        old_diff_line = None
+        new_diff_line = None
+        for line in diff_input:
+            if state == _INITIAL_STATE:
+                transform_line = get_diff_converter(line)
+            line = transform_line(line)
+
+            file_declaration = match(r&quot;^Index: (?P&lt;FilePath&gt;.+)&quot;, line)
+            if file_declaration:
+                filename = file_declaration.group('FilePath')
+                current_file = DiffFile(filename)
+                self.files[filename] = current_file
+                state = _DECLARED_FILE_PATH
+                continue
+
+            lines_changed = match(r&quot;^@@ -(?P&lt;OldStartLine&gt;\d+)(,\d+)? \+(?P&lt;NewStartLine&gt;\d+)(,\d+)? @@&quot;, line)
+            if lines_changed:
+                if state != _DECLARED_FILE_PATH and state != _PROCESSING_CHUNK:
+                    logging.error('Unexpected line change without file path declaration: %r' % line)
+                old_diff_line = int(lines_changed.group('OldStartLine'))
+                new_diff_line = int(lines_changed.group('NewStartLine'))
+                state = _PROCESSING_CHUNK
+                continue
+
+            if state == _PROCESSING_CHUNK:
+                if line.startswith('+'):
+                    current_file.add_new_line(new_diff_line, line[1:])
+                    new_diff_line += 1
+                elif line.startswith('-'):
+                    current_file.add_old_line(old_diff_line, line[1:])
+                    old_diff_line += 1
+                elif line.startswith(' '):
+                    current_file.add_unchanged_line(old_diff_line, new_diff_line, line[1:])
+                    old_diff_line += 1
+                    new_diff_line += 1
+                elif line == '\\ No newline at end of file\n':
+                    # Nothing to do.  We may still have some added lines.
+                    pass
+                else:
+                    logging.error('Unexpected diff format when parsing a chunk: %r' % line)
</ins></span></pre></div>
<a id="trunkWebKitToolsScriptsmodulesdiff_parser_unittestpy"></a>
<div class="addfile"><h4>Added: trunk/WebKitTools/Scripts/modules/diff_parser_unittest.py (0 => 46046)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebKitTools/Scripts/modules/diff_parser_unittest.py                                (rev 0)
+++ trunk/WebKitTools/Scripts/modules/diff_parser_unittest.py        2009-07-17 19:11:11 UTC (rev 46046)
</span><span class="lines">@@ -0,0 +1,118 @@
</span><ins>+# Copyright (C) 2009 Google Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#    * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#    * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+#    * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import unittest
+import diff_parser
+
+
+class DiffParserTest(unittest.TestCase):
+
+    _PATCH = '''diff --git a/WebCore/rendering/style/StyleFlexibleBoxData.h b/WebCore/rendering/style/StyleFlexibleBoxData.h
+index f5d5e74..3b6aa92 100644
+--- a/WebCore/rendering/style/StyleFlexibleBoxData.h
++++ b/WebCore/rendering/style/StyleFlexibleBoxData.h
+@@ -47,7 +47,6 @@ public:

+     unsigned align : 3; // EBoxAlignment
+     unsigned pack: 3; // EBoxAlignment
+-    unsigned orient: 1; // EBoxOrient
+     unsigned lines : 1; // EBoxLines

+ private:
+diff --git a/WebCore/rendering/style/StyleRareInheritedData.cpp b/WebCore/rendering/style/StyleRareInheritedData.cpp
+index ce21720..324929e 100644
+--- a/WebCore/rendering/style/StyleRareInheritedData.cpp
++++ b/WebCore/rendering/style/StyleRareInheritedData.cpp
+@@ -39,6 +39,7 @@ StyleRareInheritedData::StyleRareInheritedData()
+     , textSizeAdjust(RenderStyle::initialTextSizeAdjust())
+     , resize(RenderStyle::initialResize())
+     , userSelect(RenderStyle::initialUserSelect())
++    , boxOrient(RenderStyle::initialBoxOrient())
+ {
+ }

+@@ -58,6 +59,7 @@ StyleRareInheritedData::StyleRareInheritedData(const StyleRareInheritedData&amp; o)
+     , textSizeAdjust(o.textSizeAdjust)
+     , resize(o.resize)
+     , userSelect(o.userSelect)
++    , boxOrient(o.boxOrient)
+ {
+ }

+@@ -81,7 +83,8 @@ bool StyleRareInheritedData::operator==(const StyleRareInheritedData&amp; o) const
+         &amp;&amp; khtmlLineBreak == o.khtmlLineBreak
+         &amp;&amp; textSizeAdjust == o.textSizeAdjust
+         &amp;&amp; resize == o.resize
+-        &amp;&amp; userSelect == o.userSelect;
++        &amp;&amp; userSelect == o.userSelect
++        &amp;&amp; boxOrient == o.boxOrient;
+ }

+ bool StyleRareInheritedData::shadowDataEquivalent(const StyleRareInheritedData&amp; o) const
+'''.splitlines()
+
+
+    def test_diff_parser(self):
+        parser = diff_parser.DiffParser(self._PATCH)
+        self.assertEquals(2, len(parser.files))
+
+        self.assertTrue('WebCore/rendering/style/StyleFlexibleBoxData.h' in parser.files)
+        diff = parser.files['WebCore/rendering/style/StyleFlexibleBoxData.h']
+        self.assertEquals(7, len(diff.lines))
+        # The first two unchaged lines.
+        self.assertEquals((47, 47), diff.lines[0][0:2])
+        self.assertEquals('', diff.lines[0][2])
+        self.assertEquals((48, 48), diff.lines[1][0:2])
+        self.assertEquals('    unsigned align : 3; // EBoxAlignment', diff.lines[1][2])
+        # The deleted line
+        self.assertEquals((50, 0), diff.lines[3][0:2])
+        self.assertEquals('    unsigned orient: 1; // EBoxOrient', diff.lines[3][2])
+
+        # The first file looks OK. Let's check the next, more complicated file.
+        self.assertTrue('WebCore/rendering/style/StyleRareInheritedData.cpp' in parser.files)
+        diff = parser.files['WebCore/rendering/style/StyleRareInheritedData.cpp']
+        # There are 3 chunks.
+        self.assertEquals(7 + 7 + 9, len(diff.lines))
+        # Around an added line.
+        self.assertEquals((60, 61), diff.lines[9][0:2])
+        self.assertEquals((0, 62), diff.lines[10][0:2])
+        self.assertEquals((61, 63), diff.lines[11][0:2])
+        # Look through the last chunk, which contains both add's and delete's.
+        self.assertEquals((81, 83), diff.lines[14][0:2])
+        self.assertEquals((82, 84), diff.lines[15][0:2])
+        self.assertEquals((83, 85), diff.lines[16][0:2])
+        self.assertEquals((84, 0), diff.lines[17][0:2])
+        self.assertEquals((0, 86), diff.lines[18][0:2])
+        self.assertEquals((0, 87), diff.lines[19][0:2])
+        self.assertEquals((85, 88), diff.lines[20][0:2])
+        self.assertEquals((86, 89), diff.lines[21][0:2])
+        self.assertEquals((87, 90), diff.lines[22][0:2])
+
+
+if __name__ == '__main__':
+    unittest.main()
</ins></span></pre>
</div>
</div>

</body>
</html>