The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

CSS::Selector::Grammar - Generate parse trees for CSS3 selectors.

VERSION

version 0.001

SYNOPSIS

  use CSS::Selector::Grammar;

  my $ast = parse_selector('html|*:not(:link):not(:visited)');

DESCRIPTION

CSS::Selector::Grammar translates the grammar defined in http://www.w3.org/TR/css3-selectors/#w3cselgrammar into the Regexp::Grammars formalism with a few minimal changes in structure, but not semantics, to facilitate examining the resulting parse tree.

CSS::Selector::Grammar exports one function by default: parse_selector. If you are using Regexp::Grammars, it also defines two grammars: CSS3::Lexemes and CSS3::Selectors.

In addition to the normal output of Regexp::Grammars, certain nodes in the CSS selector parse trees will have an i attribute representing the index of their first character. E.g.,

  parse_selector('*[foo]');

gives you

  {
      ''                => '*[foo]',
      'selectors_group' => {
          ''         => '*[foo]',
          'selector' => [
              {
                  ''      => '*[foo]',
                  'first' => {
                      ''                 => '*[foo]',
                      'initial_selector' => {
                          ''          => '*',
                          'universal' => {
                              ''  => '*',
                              'i' => '0'
                          }
                      },
                      'simple_selector_sequence_element' => [
                          {
                              ''       => '[foo]',
                              'attrib' => {
                                  ''            => '[foo]',
                                  'attrib_name' => {
                                      ''      => 'foo',
                                      'IDENT' => {
                                          ''  => 'foo',
                                          'i' => '2'
                                      }
                                  }
                              }
                          }
                      ]
                  }
              }
          ]
      }
  };

In general, identifiers and strings will have their index indicated.

FUNCTIONS

parse_selector

  my $ast = parse_selector($expression);

For a given selector returns a parse tree, or undef if the grammar cannot parse the expression.

KNOWN BUGS

Regexp::Grammars itself does not work with Perl version 5.18, so if that's your version, CSS::Selector::Grammar won't work for you either. I suggest you try App::perlbrew.

AUTHOR

David F. Houghton <dfhoughton@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by David F. Houghton.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.