From a93029f38fc2a1a182da92cf361d700cbe2b79c2 Mon Sep 17 00:00:00 2001 From: Darik Gamble Date: Wed, 4 Mar 2015 10:39:53 -0500 Subject: Parse square brackets as a group just like parens - add class sql.SquareBrackets - replace group_parenthesis() with more generic group_brackets(), which groups square and round brackets, so each can contain groups of the other --- sqlparse/sql.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'sqlparse/sql.py') diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 8492c5e..25d5243 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -542,6 +542,15 @@ class Parenthesis(TokenList): return self.tokens[1:-1] +class SquareBrackets(TokenList): + """Tokens between square brackets""" + + __slots__ = ('value', 'ttype', 'tokens') + + @property + def _groupable_tokens(self): + return self.tokens[1:-1] + class Assignment(TokenList): """An assignment like 'var := val;'""" __slots__ = ('value', 'ttype', 'tokens') -- cgit v1.2.1 From b61fe36f718ca4f7c0b4e8d1cb81cc1370877905 Mon Sep 17 00:00:00 2001 From: Darik Gamble Date: Wed, 4 Mar 2015 10:41:34 -0500 Subject: Group square-brackets into identifiers Indentifier.get_array_indices() looks for square brackets, and yields lists of bracket grouped tokens as array indices --- sqlparse/sql.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'sqlparse/sql.py') diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 25d5243..9fcb546 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -511,11 +511,12 @@ class Identifier(TokenList): return ordering.value.upper() def get_array_indices(self): - """Returns an iterator of index expressions as strings""" + """Returns an iterator of index token lists""" - # Use [1:-1] index to discard the square brackets - return (tok.value[1:-1] for tok in self.tokens - if tok.ttype in T.ArrayIndex) + for tok in self.tokens: + if isinstance(tok, SquareBrackets): + # Use [1:-1] index to discard the square brackets + yield tok.tokens[1:-1] class IdentifierList(TokenList): -- cgit v1.2.1