Changeset 104402 in webkit


Ignore:
Timestamp:
Jan 8, 2012 8:46:40 AM (12 years ago)
Author:
haraken@chromium.org
Message:

Rewrite the CSS parser of prepare-ChangeLog with unittests.
https://bugs.webkit.org/show_bug.cgi?id=75202

Reviewed by David Kilzer.

The current CSS parser can just parse simple CSSes like

foo bar baz {

property1: value;
property2: value;

}

, and cannot parse comments nor a CSS in which a selector and {
appears in different lines. This patch rewrites the CSS parser
(i.e. get_selector_line_ranges_for_css()) so that it can parse more CSSes
shown in css_unittests.css.

Test: Scripts/webkitperl/prepare-ChangeLog_unittest/resources/css_unittests.css

  • Scripts/prepare-ChangeLog:

(get_selector_line_ranges_for_css):

  • Scripts/webkitperl/prepare-ChangeLog_unittest/parser_unittests.pl:
  • Scripts/webkitperl/prepare-ChangeLog_unittest/resources/css_unittests-expected.txt: Added.
  • Scripts/webkitperl/prepare-ChangeLog_unittest/resources/css_unittests.css: Added.

(element1):
(element2):
(element3):
(element4.p):
(element5.p.q.r.s):
(element6#p):
(element7 element8):
(element9.p element10.q):
(element11#p element12#q):
(element13, element14):
(.p):
(#p):
(.p element15 #q element16.r element17#s):
(element18:target):
(element19):
(element20):
(element21):
(element22):

Location:
trunk/Tools
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Tools/ChangeLog

    r104401 r104402  
     12012-01-04  Kentaro Hara  <haraken@chromium.org>
     2
     3        Rewrite the CSS parser of prepare-ChangeLog with unittests.
     4        https://bugs.webkit.org/show_bug.cgi?id=75202
     5
     6        Reviewed by David Kilzer.
     7
     8        The current CSS parser can just parse simple CSSes like
     9
     10            foo bar baz {
     11                property1: value;
     12                property2: value;
     13            }
     14
     15        , and cannot parse comments nor a CSS in which a selector and {
     16        appears in different lines. This patch rewrites the CSS parser
     17        (i.e. get_selector_line_ranges_for_css()) so that it can parse more CSSes
     18        shown in css_unittests.css.
     19
     20        Test: Scripts/webkitperl/prepare-ChangeLog_unittest/resources/css_unittests.css
     21
     22        * Scripts/prepare-ChangeLog:
     23        (get_selector_line_ranges_for_css):
     24        * Scripts/webkitperl/prepare-ChangeLog_unittest/parser_unittests.pl:
     25        * Scripts/webkitperl/prepare-ChangeLog_unittest/resources/css_unittests-expected.txt: Added.
     26        * Scripts/webkitperl/prepare-ChangeLog_unittest/resources/css_unittests.css: Added.
     27        (element1):
     28        (element2):
     29        (element3):
     30        (element4.p):
     31        (element5.p.q.r.s):
     32        (element6#p):
     33        (element7 element8):
     34        (element9.p element10.q):
     35        (element11#p element12#q):
     36        (element13, element14):
     37        (.p):
     38        (#p):
     39        (.p element15 #q element16.r element17#s):
     40        (element18:target):
     41        (element19):
     42        (element20):
     43        (element21):
     44        (element22):
     45
    1462012-01-04  Kentaro Hara  <haraken@chromium.org>
    247
  • trunk/Tools/Scripts/prepare-ChangeLog

    r104401 r104402  
    13941394# anything before an opening brace on a line. A selector starts at the line containing the opening
    13951395# brace and ends at the closing brace.
    1396 # FIXME: Comments are parsed just like uncommented text.
    13971396#
    13981397# Result is a list of triples: [ start_line, end_line, selector ].
     
    14061405    my $currentSelector = "";
    14071406    my $start = 0;
     1407    my $inComment = 0;
     1408    my $inBrace = 0;
    14081409
    14091410    while (<$fileHandle>) {
    1410         if (/^[ \t]*(.*[^ \t])[ \t]*{/) {
    1411             $currentSelector = $1;
    1412             $start = $.;
    1413         }
    1414         if (index($_, "}") >= 0) {
    1415             unless ($start) {
    1416                 warn "mismatched braces in $fileName\n";
    1417                 next;
    1418             }
    1419             push(@ranges, [$start, $., $currentSelector]);
    1420             $currentSelector = "";
    1421             $start = 0;
    1422             next;
     1411        foreach my $token (split m-(\{|\}|/\*|\*/)-, $_) {
     1412            if ($token eq "{") {
     1413                if (!$inComment) {
     1414                    warn "mismatched brace found in $fileName\n" if $inBrace;
     1415                    $inBrace = 1;
     1416                }
     1417            } elsif ($token eq "}") {
     1418                if (!$inComment) {
     1419                    warn "mismatched brace found in $fileName\n" if !$inBrace;
     1420                    $inBrace = 0;
     1421                    push(@ranges, [$start, $., $currentSelector]);
     1422                    $currentSelector = "";
     1423                    $start = 0;
     1424                }
     1425            } elsif ($token eq "/*") {
     1426                $inComment = 1;
     1427            } elsif ($token eq "*/") {
     1428                warn "mismatched comment found in $fileName\n" if !$inComment;
     1429                $inComment = 0;
     1430            } else {
     1431                if (!$inComment and !$inBrace and $token !~ /^[\s\t]*$/) {
     1432                    $token =~ s/^[\s\t]*|[\s\t]*$//g;
     1433                    $currentSelector = $token;
     1434                    $start = $.;
     1435                }
     1436            }
    14231437        }
    14241438    }
  • trunk/Tools/Scripts/webkitperl/prepare-ChangeLog_unittest/parser_unittests.pl

    r104400 r104402  
    3333use LoadAsModule qw(PrepareChangeLog prepare-ChangeLog);
    3434
    35 my %testFiles = ("perl_unittests.pl" => "perl",
    36                  "python_unittests.py" => "python",
    37                  "java_unittests.java" => "java",
    38                  "cpp_unittests.cpp" => "cpp",
    39                  "javascript_unittests.js" => "javascript",
     35my %testFiles = ("perl_unittests.pl" => "get_function_line_ranges_for_perl",
     36                 "python_unittests.py" => "get_function_line_ranges_for_python",
     37                 "java_unittests.java" => "get_function_line_ranges_for_java",
     38                 "cpp_unittests.cpp" => "get_function_line_ranges_for_cpp",
     39                 "javascript_unittests.js" => "get_function_line_ranges_for_javascript",
     40                 "css_unittests.css" => "get_selector_line_ranges_for_css",
    4041                );
    4142
     
    4748    my $basename = $testFile;
    4849    $basename = $1 if $basename =~ /^(.*)\.[^\.]*$/;
    49     push @testSet, {language => $testFiles{$testFile},
     50    push @testSet, {method => $testFiles{$testFile},
    5051                    inputFile => File::Spec->catdir($FindBin::Bin, "resources", $testFile),
    5152                    expectedFile => File::Spec->catdir($FindBin::Bin, "resources", $basename . "-expected.txt")};
     
    5556foreach my $test (@testSet) {
    5657    open FH, "< $test->{inputFile}" or die "Cannot open $test->{inputFile}: $!";
    57     my $parser = eval "\\&PrepareChangeLog::get_function_line_ranges_for_$test->{language}";
     58    my $parser = eval "\\&PrepareChangeLog::$test->{method}";
    5859    my @actualOutput = $parser->(\*FH, $test->{inputFile});;
    5960    close FH;
Note: See TracChangeset for help on using the changeset viewer.