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@1 | 34 | """ |
paulb@1 | 35 | A convenience class whose objects enforce grouping and naming of grammar |
paulb@1 | 36 | rules. |
paulb@1 | 37 | """ |
paulb@1 | 38 | |
paulb@0 | 39 | def __setattr__(self, name, value): |
paulb@0 | 40 | self.__dict__[name] = Group(value).setResultsName(name) |
paulb@0 | 41 | |
paulb@1 | 42 | g = grammar = Grammar() |
paulb@0 | 43 | |
paulb@0 | 44 | g.non_literal = OneOrMore(CharsNotIn("'")) |
paulb@0 | 45 | g.value = ZeroOrMore(Or([Literal("''"), CharsNotIn("'")])) |
paulb@0 | 46 | g.literal = 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@0 | 54 | return g.query.parseString(s) |
paulb@0 | 55 | |
paulb@0 | 56 | # vim: tabstop=4 expandtab shiftwidth=4 |