Created
September 27, 2017 06:57
-
-
Save spookylukey/591aa8a6a9af7cf0f1e22129b29288d6 to your computer and use it in GitHub Desktop.
Hollerith constant parsing in pyparsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyparsing as pp | |
def hollerith(): | |
intExpr = pp.Word(pp.nums).setParseAction(lambda t: int(t[0])) | |
stringExpr = pp.Forward() | |
def countedParseAction(toks): | |
n = toks[0] | |
contents = pp.CharsNotIn('', exact=n) | |
stringExpr << (pp.Suppress(pp.CaselessLiteral('H')) + contents) | |
return None | |
intExpr.addParseAction(countedParseAction) | |
return (pp.Suppress(intExpr) + stringExpr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using parsy: