| 1 |
# coding: utf8 |
| 2 |
""" |
| 3 |
tinycss.parsing |
| 4 |
--------------- |
| 5 |
|
| 6 |
Utilities for parsing lists of tokens. |
| 7 |
|
| 8 |
:copyright: (c) 2012 by Simon Sapin. |
| 9 |
:license: BSD, see LICENSE for more details. |
| 10 |
""" |
| 11 |
|
| 12 |
|
| 13 |
# TODO: unit tests |
| 14 |
|
| 15 |
def split_on_comma(tokens): |
| 16 |
"""Split a list of tokens on commas, ie ``,`` DELIM tokens. |
| 17 |
|
| 18 |
Only "top-level" comma tokens are splitting points, not commas inside a |
| 19 |
function or other :class:`ContainerToken`. |
| 20 |
|
| 21 |
:param tokens: |
| 22 |
An iterable of :class:`~.token_data.Token` or |
| 23 |
:class:`~.token_data.ContainerToken`. |
| 24 |
:returns: |
| 25 |
A list of lists of tokens |
| 26 |
|
| 27 |
""" |
| 28 |
parts = [] |
| 29 |
this_part = [] |
| 30 |
for token in tokens: |
| 31 |
if token.type == 'DELIM' and token.value == ',': |
| 32 |
parts.append(this_part) |
| 33 |
this_part = [] |
| 34 |
else: |
| 35 |
this_part.append(token) |
| 36 |
parts.append(this_part) |
| 37 |
return parts |
| 38 |
|
| 39 |
|
| 40 |
def strip_whitespace(tokens): |
| 41 |
"""Remove whitespace at the beginning and end of a token list. |
| 42 |
|
| 43 |
Whitespace tokens in-between other tokens in the list are preserved. |
| 44 |
|
| 45 |
:param tokens: |
| 46 |
A list of :class:`~.token_data.Token` or |
| 47 |
:class:`~.token_data.ContainerToken`. |
| 48 |
:return: |
| 49 |
A new sub-sequence of the list. |
| 50 |
|
| 51 |
""" |
| 52 |
for i, token in enumerate(tokens): |
| 53 |
if token.type != 'S': |
| 54 |
break |
| 55 |
else: |
| 56 |
return [] # only whitespace |
| 57 |
tokens = tokens[i:] |
| 58 |
while tokens and tokens[-1].type == 'S': |
| 59 |
tokens.pop() |
| 60 |
return tokens |
| 61 |
|
| 62 |
|
| 63 |
def remove_whitespace(tokens): |
| 64 |
"""Remove any top-level whitespace in a token list. |
| 65 |
|
| 66 |
Whitespace tokens inside recursive :class:`~.token_data.ContainerToken` |
| 67 |
are preserved. |
| 68 |
|
| 69 |
:param tokens: |
| 70 |
A list of :class:`~.token_data.Token` or |
| 71 |
:class:`~.token_data.ContainerToken`. |
| 72 |
:return: |
| 73 |
A new sub-sequence of the list. |
| 74 |
|
| 75 |
""" |
| 76 |
return [token for token in tokens if token.type != 'S'] |
| 77 |
|
| 78 |
|
| 79 |
def validate_value(tokens): |
| 80 |
"""Validate a property value. |
| 81 |
|
| 82 |
:param tokens: |
| 83 |
an iterable of tokens |
| 84 |
:raises: |
| 85 |
:class:`ParseError` if there is any invalid token for the 'value' |
| 86 |
production of the core grammar. |
| 87 |
|
| 88 |
""" |
| 89 |
for token in tokens: |
| 90 |
type_ = token.type |
| 91 |
if type_ == '{': |
| 92 |
validate_block(token.content, 'property value') |
| 93 |
else: |
| 94 |
validate_any(token, 'property value') |
| 95 |
|
| 96 |
def validate_block(tokens, context): |
| 97 |
""" |
| 98 |
:raises: |
| 99 |
:class:`ParseError` if there is any invalid token for the 'block' |
| 100 |
production of the core grammar. |
| 101 |
:param tokens: an iterable of tokens |
| 102 |
:param context: a string for the 'unexpected in ...' message |
| 103 |
|
| 104 |
""" |
| 105 |
for token in tokens: |
| 106 |
type_ = token.type |
| 107 |
if type_ == '{': |
| 108 |
validate_block(token.content, context) |
| 109 |
elif type_ not in (';', 'ATKEYWORD'): |
| 110 |
validate_any(token, context) |
| 111 |
|
| 112 |
|
| 113 |
def validate_any(token, context): |
| 114 |
""" |
| 115 |
:raises: |
| 116 |
:class:`ParseError` if this is an invalid token for the |
| 117 |
'any' production of the core grammar. |
| 118 |
:param token: a single token |
| 119 |
:param context: a string for the 'unexpected in ...' message |
| 120 |
|
| 121 |
""" |
| 122 |
type_ = token.type |
| 123 |
if type_ in ('FUNCTION', '(', '['): |
| 124 |
for token in token.content: |
| 125 |
validate_any(token, type_) |
| 126 |
elif type_ not in ('S', 'IDENT', 'DIMENSION', 'PERCENTAGE', 'NUMBER', |
| 127 |
'INTEGER', 'URI', 'DELIM', 'STRING', 'HASH', ':', |
| 128 |
'UNICODE-RANGE'): |
| 129 |
if type_ in ('}', ')', ']'): |
| 130 |
adjective = 'unmatched' |
| 131 |
else: |
| 132 |
adjective = 'unexpected' |
| 133 |
raise ParseError(token, |
| 134 |
'{0} {1} token in {2}'.format(adjective, type_, context)) |
| 135 |
|
| 136 |
|
| 137 |
class ParseError(ValueError): |
| 138 |
"""Details about a CSS syntax error. Usually indicates that something |
| 139 |
(a rule or a declaration) was ignored and will not appear as a parsed |
| 140 |
object. |
| 141 |
|
| 142 |
This exception is typically logged in a list rather than being propagated |
| 143 |
to the user API. |
| 144 |
|
| 145 |
.. attribute:: line |
| 146 |
|
| 147 |
Source line where the error occurred. |
| 148 |
|
| 149 |
.. attribute:: column |
| 150 |
|
| 151 |
Column in the source line where the error occurred. |
| 152 |
|
| 153 |
.. attribute:: reason |
| 154 |
|
| 155 |
What happened (a string). |
| 156 |
|
| 157 |
""" |
| 158 |
def __init__(self, subject, reason): |
| 159 |
self.line = subject.line |
| 160 |
self.column = subject.column |
| 161 |
self.reason = reason |
| 162 |
super(ParseError, self).__init__( |
| 163 |
'Parse error at {0.line}:{0.column}, {0.reason}'.format(self)) |