Permalink
Browse files

Cleaned up nest style rules code and added comments

  • Loading branch information...
1 parent 014e390 commit b6b92e3999f7c743bba978209ce43acf00cbafea @migurski migurski committed May 28, 2012
Showing with 27 additions and 43 deletions.
  1. +27 −6 cascadenik/parse.py
  2. +0 −37 nested-rules.py
View
@@ -10,12 +10,18 @@
from .style import Declaration, Property, Value
class ParseException(Exception):
+ """ Exception raised when a parsing error is encountered.
+ Includes the line and column from the origin style text.
+ """
def __init__(self, msg, line, col):
Exception.__init__(self, '%(msg)s (line %(line)d, column %(col)d)' % locals())
class BlockTerminatedValue (Exception):
-
+ """ Exception generated when a value ends at a block instead of a semicolon.
+
+ Caught and handled in parse_block(), not really an error.
+ """
def __init__(self, tokens, important, line, col):
self.tokens = tokens
self.important = important
@@ -402,7 +408,10 @@ def parse_value(tokens):
_tname, _tvalue, _line, _col = tokens.next()
if (_tname, _tvalue) == ('CHAR', ':'):
-
+ #
+ # Retrieve and process a value after a property name.
+ # http://www.w3.org/TR/CSS2/syndata.html#declaration
+ #
if tvalue not in properties:
raise ParseException('Unsupported property name, %s' % tvalue, line, col)
@@ -458,8 +467,16 @@ def parse_value(tokens):
def parse_rule(tokens, neighbors, parents, is_merc):
""" Parse a rule set, return a list of declarations.
+ Selectors in the neighbors list are simply grouped, and are generated
+ from comma-delimited lists of selectors in the stylesheet. Selectors
+ in the parents list should be combined with those found by this
+ functions, and are generated from nested, Less-style rulesets.
+
A rule set is a combination of selectors and declarations:
http://www.w3.org/TR/CSS2/syndata.html#rule-sets
+
+ Nesting is described in the Less CSS spec:
+ http://lesscss.org/#-nested-rules
To handle groups of selectors, use recursion:
http://www.w3.org/TR/CSS2/selector.html#grouping
@@ -601,8 +618,11 @@ def __init__(self, *elements):
selectors = []
- # There might not be any parents,
- # but there will definitely be neighbors.
+ #
+ # Combine lists of parents and neighbors into a single list of
+ # selectors, for passing off to parse_block(). There might not
+ # be any parents, but there will definitely be neighbors.
+ #
for parent in (parents or [DummySelector()]):
for neighbor in neighbors:
if len(neighbor.elements) == 0:
@@ -613,11 +633,12 @@ def __init__(self, *elements):
for element in elements:
if element.__class__ is ConcatenatedElement:
- [selector.elements[-1].addName(name) for name in element.names]
- [selector.elements[-1].addTest(test) for test in element.tests]
+ for name in element.names: selector.elements[-1].addName(name)
+ for test in element.tests: selector.elements[-1].addTest(test)
else:
selector.addElement(deepcopy(element))
+ # selector should be fully valid at this point.
validate_selector_elements(selector.elements, line, col)
selector.convertZoomTests(is_merc)
selectors.append(selector)
View
@@ -1,37 +0,0 @@
-from cascadenik.parse import stylesheet_declarations
-
-s = """
- #roads,
- #stuff
- {
- line-color: #f90;
- line-width: 1;
-
- &.more,
- &[this=that]
- { line-width: 2; }
-
- &.one, &.two
- {
- &[three=3], &[four=4]
- { polygon-fill: #000; }
- }
-
- /*
- .purple
- { line-color: #909; }
-
- #green
- { line-color: #090; }
-
- [color=blue]
- { line-color: #00f; }
- */
-
- yellow
- { line-color: #ff0; }
- }
-"""
-
-for declaration in stylesheet_declarations(s):
- print declaration

0 comments on commit b6b92e3

Please sign in to comment.