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@0 | 23 | from pyparsing import * |
paulb@0 | 24 | |
paulb@1 | 25 | try: |
paulb@1 | 26 | ParserElement.enablePackrat() |
paulb@1 | 27 | except AttributeError: |
paulb@1 | 28 | pass |
paulb@1 | 29 | |
paulb@1 | 30 | # Classes and functions. |
paulb@1 | 31 | |
paulb@0 | 32 | class Grammar: |
paulb@1 | 33 | |
paulb@2 | 34 | "A convenience class whose objects enforce the naming of grammar rules." |
paulb@1 | 35 | |
paulb@0 | 36 | def __setattr__(self, name, value): |
paulb@2 | 37 | self.__dict__[name] = value.setResultsName(name) |
paulb@2 | 38 | |
paulb@2 | 39 | # NOTE: Sufficient grouping seems to be provided by pyparsing. |
paulb@2 | 40 | # NOTE: Otherwise, we might introduce Group objects in the above class. |
paulb@0 | 41 | |
paulb@1 | 42 | g = grammar = Grammar() |
paulb@0 | 43 | |
paulb@2 | 44 | g.non_literal = Combine(OneOrMore(CharsNotIn("'"))) |
paulb@0 | 45 | g.value = ZeroOrMore(Or([Literal("''"), CharsNotIn("'")])) |
paulb@2 | 46 | g.literal = Combine(Literal("'") + g.value + Literal("'")) |
paulb@0 | 47 | g.query = ZeroOrMore(Or([g.non_literal, g.literal])) |
paulb@0 | 48 | g.query.leaveWhitespace() |
paulb@0 | 49 | |
paulb@0 | 50 | def parseString(s): |
paulb@1 | 51 | |
paulb@1 | 52 | "Return a pyparsing result object from the parsing of the string 's'." |
paulb@1 | 53 | |
paulb@2 | 54 | regions = [] |
paulb@2 | 55 | first = 1 |
paulb@2 | 56 | for region in g.query.parseString(s).asList(): |
paulb@2 | 57 | if isinstance(region, list): |
paulb@2 | 58 | if first: |
paulb@2 | 59 | regions.append("") |
paulb@2 | 60 | regions.append(region[0]) |
paulb@2 | 61 | else: |
paulb@2 | 62 | regions.append(region) |
paulb@2 | 63 | first = 0 |
paulb@2 | 64 | return regions |
paulb@0 | 65 | |
paulb@0 | 66 | # vim: tabstop=4 expandtab shiftwidth=4 |