paulb@0 | 1 | #!/usr/bin/env python |
paulb@0 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | A pyparsing implementation of SQL statement tokenisation. |
paulb@1 | 5 | |
paulb@1 | 6 | Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@1 | 7 | |
paulb@1 | 8 | This library is free software; you can redistribute it and/or |
paulb@1 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@1 | 10 | License as published by the Free Software Foundation; either |
paulb@1 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@1 | 12 | |
paulb@1 | 13 | This library is distributed in the hope that it will be useful, |
paulb@1 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@1 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@1 | 16 | Lesser General Public License for more details. |
paulb@1 | 17 | |
paulb@1 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@1 | 19 | License along with this library; if not, write to the Free Software |
paulb@1 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@1 | 21 | """ |
paulb@1 | 22 | |
paulb@4 | 23 | import sqlliterals.common |
paulb@0 | 24 | from pyparsing import * |
paulb@0 | 25 | |
paulb@1 | 26 | try: |
paulb@1 | 27 | ParserElement.enablePackrat() |
paulb@1 | 28 | except AttributeError: |
paulb@1 | 29 | pass |
paulb@1 | 30 | |
paulb@1 | 31 | # Classes and functions. |
paulb@1 | 32 | |
paulb@0 | 33 | class Grammar: |
paulb@1 | 34 | |
paulb@2 | 35 | "A convenience class whose objects enforce the naming of grammar rules." |
paulb@1 | 36 | |
paulb@0 | 37 | def __setattr__(self, name, value): |
paulb@2 | 38 | self.__dict__[name] = value.setResultsName(name) |
paulb@2 | 39 | |
paulb@2 | 40 | # NOTE: Sufficient grouping seems to be provided by pyparsing. |
paulb@2 | 41 | # NOTE: Otherwise, we might introduce Group objects in the above class. |
paulb@0 | 42 | |
paulb@1 | 43 | g = grammar = Grammar() |
paulb@0 | 44 | |
paulb@2 | 45 | g.non_literal = Combine(OneOrMore(CharsNotIn("'"))) |
paulb@0 | 46 | g.value = ZeroOrMore(Or([Literal("''"), CharsNotIn("'")])) |
paulb@2 | 47 | g.literal = Combine(Literal("'") + g.value + Literal("'")) |
paulb@0 | 48 | g.query = ZeroOrMore(Or([g.non_literal, g.literal])) |
paulb@0 | 49 | g.query.leaveWhitespace() |
paulb@0 | 50 | |
paulb@0 | 51 | def parseString(s): |
paulb@1 | 52 | |
paulb@1 | 53 | "Return a pyparsing result object from the parsing of the string 's'." |
paulb@1 | 54 | |
paulb@2 | 55 | regions = [] |
paulb@2 | 56 | first = 1 |
paulb@2 | 57 | for region in g.query.parseString(s).asList(): |
paulb@2 | 58 | if isinstance(region, list): |
paulb@2 | 59 | if first: |
paulb@2 | 60 | regions.append("") |
paulb@2 | 61 | regions.append(region[0]) |
paulb@2 | 62 | else: |
paulb@2 | 63 | regions.append(region) |
paulb@2 | 64 | first = 0 |
paulb@2 | 65 | return regions |
paulb@0 | 66 | |
paulb@4 | 67 | # NOTE: Duplicated across implementations. |
paulb@4 | 68 | |
paulb@4 | 69 | def replace(fromstyle, tostyle, s): |
paulb@4 | 70 | |
paulb@4 | 71 | """ |
paulb@4 | 72 | Return a new string, where the paramstyle 'fromstyle' is |
paulb@4 | 73 | replaced with 'tostyle' in the appropriate regions of 's'. |
paulb@4 | 74 | """ |
paulb@4 | 75 | |
paulb@4 | 76 | regions = parseString(s) |
paulb@4 | 77 | return "".join(sqlliterals.common.replace(fromstyle, tostyle, regions)) |
paulb@4 | 78 | |
paulb@0 | 79 | # vim: tabstop=4 expandtab shiftwidth=4 |