NL = LineEnd().suppress() integer = Word(nums) plan = '1..' + integer("ubound") OK, NOT_OK = map(Literal, ['ok', 'not ok']) testStatus = (OK | NOT_OK) description = Regex("[^#\n]+") description.setParseAction(lambda t: t[0].lstrip('- ')) TODO, SKIP = map(CaselessLiteral, 'TODO SKIP'.split()) directive = Group( Suppress('#') + (TODO + restOfLine | FollowedBy(SKIP) + restOfLine.copy().setParseAction(lambda t: ['SKIP', t[0]]))) commentLine = Suppress("#") + empty + restOfLine testLine = Group( Optional(OneOrMore(commentLine + NL))("comments") + testStatus("passed") + Optional(integer)("testNumber") + Optional(description)("description") + Optional(directive)("directive")) bailLine = Group( Literal("Bail out!")("BAIL") + empty + Optional(restOfLine)("reason")) tapOutputParser = Optional(Group(plan)("plan") + NL) & \ Group(OneOrMore((testLine|bailLine) + NL))("tests") class TAPTest(object):
ParserElement.setDefaultWhitespaceChars(" \t") NL = LineEnd().suppress() integer = Word(nums) plan = '1..' + integer("ubound") OK,NOT_OK = map(Literal,['ok','not ok']) testStatus = (OK | NOT_OK) description = Regex("[^#\n]+") description.setParseAction(lambda t:t[0].lstrip('- ')) TODO,SKIP = map(CaselessLiteral,'TODO SKIP'.split()) directive = Group(Suppress('#') + (TODO + restOfLine | FollowedBy(SKIP) + restOfLine.copy().setParseAction(lambda t:['SKIP',t[0]]) )) commentLine = Suppress("#") + empty + restOfLine testLine = Group( Optional(OneOrMore(commentLine + NL))("comments") + testStatus("passed") + Optional(integer)("testNumber") + Optional(description)("description") + Optional(directive)("directive") ) bailLine = Group(Literal("Bail out!")("BAIL") + empty + Optional(restOfLine)("reason")) tapOutputParser = Optional(Group(plan)("plan") + NL) & \ Group(OneOrMore((testLine|bailLine) + NL))("tests")
# ------------------------------------------------------------------------ # Grammar from pyparsing import Word, nums, alphas, ZeroOrMore, OneOrMore from pyparsing import Literal, Keyword, ParserElement, SkipTo, Combine from pyparsing import stringEnd, delimitedList, restOfLine, Group, Optional from pyparsing import ParseResults from attendance import Attendance, Checkpoint, gCheckPoint from attendance import integerList, integer ParserElement.setDefaultWhitespaceChars("\t ") varValue = (restOfLine.copy()).setParseAction(lambda s, l, t: [t[0].strip()]) percent = (integer + '%') nameParam = ((Keyword("Name") + '=' + varValue) .setParseAction(lambda s, l, t: t[2])) def processQuorum(s, l, t): return t[2] if len(t) < 6 else -t[2] quorumParam = ((Keyword("Quorum") + '=' + percent + Optional(Keyword("of") + Keyword("all"))) .setParseAction(processQuorum)) comment = (Literal("#") + restOfLine).suppress()