PKšžŸ5“×2EGG-INFO/dependency_links.txt PKšžŸ5¦Z‡¼¼EGG-INFO/PKG-INFOMetadata-Version: 1.0 Name: ZestyParser Version: 0.4.2 Summary: A simple but highly flexible approach to parsing Home-page: http://adamatlas.org/2006/12/ZestyParser Author: Adam Atlas Author-email: adam@atlas.st License: GPL Description: ZestyParser is a small parsing toolkit for Python. It doesn't use the traditional separated lexer/parser approach, nor does it make you learn a new ugly syntax for specifying grammar. It is based entirely on Python regular expressions and callbacks; its flow is very simple, but can accomodate a vast array of parsing situations. Platform: UNKNOWN Classifier: Development Status :: 4 - Beta Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: GNU General Public License (GPL) Classifier: Natural Language :: English Classifier: Programming Language :: Python Classifier: Topic :: Software Development :: Libraries :: Python Modules Classifier: Topic :: Text Processing PKšžŸ50ÒÐÌttEGG-INFO/SOURCES.txtCHANGES.txt Docs.html Docs.text LICENSE.txt MANIFEST.in setup.py ZestyParser/Parser.py ZestyParser/__init__.py ZestyParser.egg-info/PKG-INFO ZestyParser.egg-info/SOURCES.txt ZestyParser.egg-info/dependency_links.txt ZestyParser.egg-info/top_level.txt examples/bdecode.py examples/calcy.py examples/phpserialize.py examples/sexp-bench.py examples/sexp.py examples/testy.py PKšžŸ5µ& EGG-INFO/top_level.txtZestyParser PKšžŸ5“×2EGG-INFO/zip-safe PKÒ‹5s¾H--ZestyParser/__init__.py# ZestyParser 0.4.2 -- Parses in Python zestily # Copyright (C) 2006 Adam Atlas # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. from Parser import * PKšžŸ5ŸKݧ¦¦ZestyParser/__init__.pyc;ò —•Ec@s dkTdS((s*N(sParser(((s9build/bdist.darwin-8.8.2-i386/egg/ZestyParser/__init__.pys?sPK”Ÿ5°;Ñ„ÎÎZestyParser/Parser.py# ZestyParser 0.4.2 -- Parses in Python zestily # Copyright (C) 2006 Adam Atlas # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. import re, copy __all__ = ('ZestyParser', 'NotMatched', 'ParseError', 'EOF', 'ReturnRaw', 'AbstractToken', 'Token', 'RawToken', 'CompositeToken', 'TokenSequence', 'TakeToken', 'CallbackFor') class Error(Exception): pass class NotMatched(Error): pass class ParseError(Error): def __init__(self, parser, message): self.parser, self.message, self.coord = parser, message, parser.coord() def __str__(self): return "%s at line %i column %i" % (self.message, self.coord[0], self.coord[1]) def EOF(parser, origCursor): if parser.cursor != parser.len: raise NotMatched def ReturnRaw(matches): return matches.group() class CallbackFor: def __init__(self, token): self.token = token def __call__(self, func): self.token.callback = func return self.token class AbstractToken: def __init__(self, desc, callback=None, name=None): self.desc, self.callback, self.name, self.failMessage = desc, callback, name, None def __repr__(self): return '%s %s' % (self.__class__.__name__, (self.name or str(self))) def preprocessResult(self, parser, data, origCursor): if not self.callback: return data try: c = self.callback.func_code.co_argcount if c == 2: return self.callback(parser, data) elif c == 1: return self.callback(data) else: return self.callback(parser, data, origCursor) except NotMatched: parser.cursor = origCursor raise NotMatched def __add__(self, other): return TokenSequence([self, other]) def __or__(self, other): return CompositeToken([self, other]) def __rshift__(self, callback): new = copy.copy(self) new.callback = callback return new def __xor__(self, message): new = copy.copy(self) new.failMessage = message return new class Token (AbstractToken): def __init__(self, regex, callback=None, name=None): if not hasattr(regex, 'match'): regex = re.compile(regex, re.DOTALL) AbstractToken.__init__(self, regex, callback, name) def __call__(self, parser, origCursor): matches = self.desc.match(parser.data, origCursor) if not matches: raise NotMatched #parser.cursor += len(matches.group()) parser.cursor = matches.end() return self.preprocessResult(parser, matches, origCursor) def __str__(self): return self.desc.pattern class RawToken (AbstractToken): def __init__(self, string, callback=None, name=None): AbstractToken.__init__(self, string, callback, name) self.len = len(string) def __call__(self, parser, origCursor): if parser.data[origCursor:origCursor+self.len] == self.desc: parser.cursor += self.len return self.preprocessResult(parser, self.desc, origCursor) else: raise NotMatched def __str__(self): return repr(self.desc) class CompositeToken (AbstractToken): def __init__(self, tokens, callback=None, name=None): AbstractToken.__init__(self, tokens, callback, name) def __call__(self, parser, origCursor): r = parser.scan(self.desc) if parser.last in (None, EOF): raise NotMatched return self.preprocessResult(parser, r, origCursor) def __str__(self): return '(' + ' | '.join([str(t) for t in self.desc]) + ')' def __or__(self, other): if isinstance(other, CompositeToken): return CompositeToken(self.desc + other.desc) else: return CompositeToken(self.desc + [other]) class TokenSequence (AbstractToken): def __init__(self, tokenGroups, callback=None, name=None): AbstractToken.__init__(self, tokenGroups, callback, name) def __call__(self, parser, origCursor): o = [] for g in self.desc: r = parser.scan(g) if parser.last is None: parser.cursor = origCursor raise NotMatched o.append(r) return self.preprocessResult(parser, o, origCursor) def __str__(self): return '(' + ' + '.join([str(t) for t in self.desc]) + ')' def __add__(self, other): if isinstance(other, TokenSequence): return TokenSequence(self.desc + other.desc) else: return TokenSequence(self.desc + [other]) class TakeToken (AbstractToken): def __init__(self, length, callback=None, name=None): AbstractToken.__init__(self, length, callback, name) def __call__(self, parser, start): end = start + self.desc if parser.len < end: raise NotMatched parser.cursor = end return parser.data[start:end] class ZestyParser: tokens = {} data = None cursor = 0 len = 0 def __init__(self, data=None): if data: self.useData(data) self.last = None def useData(self, data): self.data = data self.cursor = 0 self.len = len(data) def addTokens(self, *tokens, **moreTokens): for t in tokens: self.tokens[t.name] = t for n in moreTokens: self.tokens[n] = moreTokens[n] def scan(self, tokens): if not hasattr(tokens, '__iter__'): tokens = (tokens,) for t in tokens: if t.__class__ is str: t = self.tokens[t] oldCursor = self.cursor try: r = t(self, oldCursor) self.last = t return r except NotMatched: self.cursor = oldCursor if hasattr(t, 'failMessage') and t.failMessage: raise ParseError(self, t.failMessage) self.last = None return None def skip(self, t): if t.__class__ is str: t = self.tokens[t] oldCursor = self.cursor try: t(self, oldCursor) except NotMatched: self.cursor = oldCursor if hasattr(t, 'failMessage') and t.failMessage: raise ParseError(self, t.failMessage) else: return False return True def iter(self, tokens): return ParserIterator(tokens, self) def coord(self, loc=None): if loc is None: loc = self.cursor row = self.data.count('\n', 0, loc) col = loc - self.data.rfind('\n', 0, loc) return (row + 1, col) class ParserIterator: def __init__(self, tokens, parser): self.tokens = tokens self.parser = parser def __iter__(self): return self def next(self): r = self.parser.scan(self.tokens) if self.parser.last is None: raise StopIteration return r PKšžŸ5ÛAJ‹Ï<Ï<ZestyParser/Parser.pyc;ò xY˜Ec @sNdkZdkZddddddddd d d d f Zd efd„ƒYZdefd„ƒYZdefd„ƒYZd„Zd„Zd fd„ƒYZ dfd„ƒYZ de fd„ƒYZ de fd„ƒYZ d e fd„ƒYZ d e fd„ƒYZd e fd„ƒYZdfd„ƒYZdfd„ƒYZdS(Ns ZestyParsers NotMatcheds ParseErrorsEOFs ReturnRaws AbstractTokensTokensRawTokensCompositeTokens TokenSequences TakeTokens CallbackForsErrorcBstZRS(N(s__name__s __module__(((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pysErrorscBstZRS(N(s__name__s __module__(((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys NotMatchedscBstZd„Zd„ZRS(NcCs+|||iƒf\|_|_|_dS(N(sparsersmessagescoordsself(sselfsparsersmessage((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__init__scCs&d|i|id|idfSdS(Ns%s at line %i column %iii(sselfsmessagescoord(sself((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__str__s(s__name__s __module__s__init__s__str__(((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys ParseErrors cCs!|i|ijo t‚ndS(N(sparserscursorslens NotMatched(sparsers origCursor((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pysEOFscCs|iƒSdS(N(smatchessgroup(smatches((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys ReturnRaw!scBstZd„Zd„ZRS(NcCs ||_dS(N(stokensself(sselfstoken((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__init__$scCs||i_|iSdS(N(sfuncsselfstokenscallback(sselfsfunc((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__call__'s (s__name__s __module__s__init__s__call__(((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys CallbackFor#s cBsMtZeed„Zd„Zd„Zd„Zd„Zd„Zd„Z RS(NcCs.|||tf\|_|_|_|_dS(N(sdescscallbacksnamesNonesselfs failMessage(sselfsdescscallbacksname((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__init__,scCs(d|ii|ip t|ƒfSdS(Ns%s %s(sselfs __class__s__name__snamesstr(sself((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__repr__/scCs¡|i o|Snye|iii}|djo|i||ƒSn2|djo|i|ƒSn|i|||ƒSWn"tj o||_ t‚nXdS(Nii( sselfscallbacksdatas func_codes co_argcountscsparsers origCursors NotMatchedscursor(sselfsparsersdatas origCursorsc((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pyspreprocessResult2s    cCst||gƒSdS(N(s TokenSequencesselfsother(sselfsother((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__add__>scCst||gƒSdS(N(sCompositeTokensselfsother(sselfsother((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__or__AscCs ti|ƒ}||_|SdS(N(scopysselfsnewscallback(sselfscallbacksnew((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys __rshift__Ds cCs ti|ƒ}||_|SdS(N(scopysselfsnewsmessages failMessage(sselfsmessagesnew((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__xor__Is ( s__name__s __module__sNones__init__s__repr__spreprocessResults__add__s__or__s __rshift__s__xor__(((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys AbstractToken+s    cBs)tZeed„Zd„Zd„ZRS(NcCsDt|dƒ oti|tiƒ}nti||||ƒdS(Nsmatch( shasattrsregexsrescompilesDOTALLs AbstractTokens__init__sselfscallbacksname(sselfsregexscallbacksname((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__init__OscCsP|ii|i|ƒ}| o t‚n|iƒ|_ |i |||ƒSdS(N( sselfsdescsmatchsparsersdatas origCursorsmatchess NotMatchedsendscursorspreprocessResult(sselfsparsers origCursorsmatches((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__call__Ts  cCs|iiSdS(N(sselfsdescspattern(sself((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__str__\s(s__name__s __module__sNones__init__s__call__s__str__(((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pysTokenNs cBs)tZeed„Zd„Zd„ZRS(NcCs)ti||||ƒt|ƒ|_dS(N(s AbstractTokens__init__sselfsstringscallbacksnameslen(sselfsstringscallbacksname((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__init__`scCsW|i|||i!|ijo,|i|i7_|i||i|ƒSnt‚dS(N( sparsersdatas origCursorsselfslensdescscursorspreprocessResults NotMatched(sselfsparsers origCursor((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__call__ds!cCst|iƒSdS(N(sreprsselfsdesc(sself((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__str__ks(s__name__s __module__sNones__init__s__call__s__str__(((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pysRawToken_s cBs2tZeed„Zd„Zd„Zd„ZRS(NcCsti||||ƒdS(N(s AbstractTokens__init__sselfstokensscallbacksname(sselfstokensscallbacksname((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__init__oscCsI|i|iƒ}|ittfjo t‚n|i |||ƒSdS(N( sparsersscansselfsdescsrslastsNonesEOFs NotMatchedspreprocessResults origCursor(sselfsparsers origCursorsr((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__call__rs cCsCddigi}|iD]}|t|ƒƒq~ƒdSdS(Ns(s | s)(sjoinsappends_[1]sselfsdescstsstr(sselfs_[1]st((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__str__xscCs@t|tƒot|i|iƒSnt|i|gƒSdS(N(s isinstancesothersCompositeTokensselfsdesc(sselfsother((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__or__{s(s__name__s __module__sNones__init__s__call__s__str__s__or__(((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pysCompositeTokenns  cBs2tZeed„Zd„Zd„Zd„ZRS(NcCsti||||ƒdS(N(s AbstractTokens__init__sselfs tokenGroupsscallbacksname(sselfs tokenGroupsscallbacksname((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__init__‚scCspg}xP|iD]E}|i|ƒ}|itjo||_ t ‚n|i |ƒqW|i |||ƒSdS(N(sosselfsdescsgsparsersscansrslastsNones origCursorscursors NotMatchedsappendspreprocessResult(sselfsparsers origCursorsgsosr((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__call__…s   cCsCddigi}|iD]}|t|ƒƒq~ƒdSdS(Ns(s + s)(sjoinsappends_[1]sselfsdescstsstr(sselfs_[1]st((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__str__scCs@t|tƒot|i|iƒSnt|i|gƒSdS(N(s isinstancesothers TokenSequencesselfsdesc(sselfsother((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__add__’s(s__name__s __module__sNones__init__s__call__s__str__s__add__(((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys TokenSequences cBs tZeed„Zd„ZRS(NcCsti||||ƒdS(N(s AbstractTokens__init__sselfslengthscallbacksname(sselfslengthscallbacksname((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__init__™scCsB||i}|i|jo t‚n||_|i||!SdS(N( sstartsselfsdescsendsparserslens NotMatchedscursorsdata(sselfsparsersstartsend((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__call__œs    (s__name__s __module__sNones__init__s__call__(((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys TakeToken˜scBsetZhZeZdZdZed„Zd„Zd„Z d„Z d„Z d„Z ed„Z RS( NicCs%|o|i|ƒnt|_dS(N(sdatasselfsuseDatasNoneslast(sselfsdata((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pys__init__¨scCs%||_d|_t|ƒ|_dS(Ni(sdatasselfscursorslen(sselfsdata((s7build/bdist.darwin-8.8.2-i386/egg/ZestyParser/Parser.pysuseData¬s  cOsGx|D]}||i|i