Skip to content

Commit

Permalink
Reduce nesting level in the parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Apr 19, 2012
1 parent 35a2f57 commit 95e655d
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions cssselect/parser.py
Expand Up @@ -373,36 +373,33 @@ def parse_simple_selector(stream, inside_negation=False):
# Any new pseudo-element must have two.
pseudo_element = ident
continue
if stream.peek() == '(':
stream.next()
stream.skip_whitespace()
is_negation = ident.lower() == 'not'
if is_negation:
if inside_negation:
raise SelectorSyntaxError('Got nested :not()')
argument, argument_pseudo_element = parse_simple_selector(
stream, inside_negation=True)
if argument_pseudo_element:
raise SelectorSyntaxError(
'Pseudo-elements are not allowed inside :not()')
else:
peek = stream.peek()
if isinstance(peek, (Symbol, String)):
argument = stream.next()
else:
raise SelectorSyntaxError(
"Expected argument, got '%s'" % peek)
stream.skip_whitespace()
next = stream.next()
if not next == ')':
if stream.peek() != '(':
result = Pseudo(result, ident)
continue
stream.next()
stream.skip_whitespace()
if ident.lower() == 'not':
if inside_negation:
raise SelectorSyntaxError('Got nested :not()')
argument, argument_pseudo_element = parse_simple_selector(
stream, inside_negation=True)
if argument_pseudo_element:
raise SelectorSyntaxError(
"Expected ')', got '%s'" % next)
if is_negation:
result = Negation(result, argument)
else:
result = Function(result, ident, argument)
'Pseudo-elements are not allowed inside :not()')
result = Negation(result, argument)
else:
result = Pseudo(result, ident)
peek = stream.peek()
if isinstance(peek, (Symbol, String)):
argument = stream.next()
else:
raise SelectorSyntaxError(
"Expected argument, got '%s'" % peek)
result = Function(result, ident, argument)
stream.skip_whitespace()
next = stream.next()
if not next == ')':
raise SelectorSyntaxError(
"Expected ')', got '%s'" % next)
continue
else:
raise SelectorSyntaxError(
Expand Down

0 comments on commit 95e655d

Please sign in to comment.