def _createQuantRule3(self, child, quantity, lineNum, line, segtypesHelper): if quantity <= 0: raise exceptions.ConfigFileException( segtypesHelper.filename, lineNum, u'%s - invalid quantity: %d' % (line, quantity)) else: return rules.ConcatRule([ rules.ConcatRule(quantity * [child], lineNum), rules.ZeroOrMoreRule(child, lineNum) ], lineNum)
def _doParseOneLine(self, lineNum, line, segtypesHelper, filename): rule = Forward() tagRule = Word(alphanums + '_') shiftOrthRule = Word(alphanums + '_') + Suppress('>') parenRule = Suppress('(') + rule + Suppress(')') parenWithShiftOrthRule = parenRule + Suppress('>') atomicRule = tagRule ^ shiftOrthRule ^ parenWithShiftOrthRule ^ parenRule zeroOrMoreRule = atomicRule + Suppress('*') oneOrMoreRule = atomicRule + Suppress('+') optionalRule = atomicRule + Suppress('?') quantRule1 = atomicRule + Suppress('{') + Word(nums) + Suppress('}') quantRule2 = atomicRule + Suppress('{') + Word(nums) + Suppress( ',') + Word(nums) + Suppress('}') quantRule3 = atomicRule + Suppress('{') + Word(nums) + Suppress( ',') + Suppress('}') unaryRule = atomicRule ^ zeroOrMoreRule ^ oneOrMoreRule ^ optionalRule ^ quantRule1 ^ quantRule2 ^ quantRule3 oneOfRule = delimitedList(unaryRule, delim='|') complexRule = unaryRule ^ oneOfRule concatRule = OneOrMore(complexRule) # if self.rulesType == RulesParser.PARSE4ANALYZER: # concatRule = OneOrMore(complexRule) # else: # concatRule = ZeroOrMore(shiftOrthRule) + tagRule rule << concatRule completeRule = rule + Optional(CaselessLiteral('!weak')) tagRule.setParseAction( lambda string, loc, toks: self._createNewTagRule( toks[0], False, lineNum, line, segtypesHelper)) shiftOrthRule.setParseAction( lambda string, loc, toks: self._createNewTagRule( toks[0], True, lineNum, line, segtypesHelper)) parenWithShiftOrthRule.setParseAction( lambda string, loc, toks: self._createNewParenWithShiftOrthRule( toks[0], lineNum, line, segtypesHelper)) parenRule.setParseAction(lambda string, loc, toks: toks[0]) zeroOrMoreRule.setParseAction( lambda string, loc, toks: rules.ZeroOrMoreRule(toks[0], lineNum)) quantRule1.setParseAction( lambda string, loc, toks: self._createQuantRule1( toks[0], int(toks[1], 10), lineNum, line, segtypesHelper)) quantRule2.setParseAction( lambda string, loc, toks: self._createQuantRule2( toks[0], int(toks[1], 10), int(toks[2], 10), lineNum, line, segtypesHelper)) quantRule3.setParseAction( lambda string, loc, toks: self._createQuantRule3( toks[0], int(toks[1], 10), lineNum, line, segtypesHelper)) optionalRule.setParseAction( lambda string, loc, toks: rules.OptionalRule(toks[0], lineNum)) oneOrMoreRule.setParseAction( lambda string, loc, toks: rules.ConcatRule( [toks[0], rules.ZeroOrMoreRule(toks[0], lineNum)], lineNum)) oneOfRule.setParseAction( lambda string, loc, toks: rules.OrRule(list(toks), lineNum)) concatRule.setParseAction(lambda string, loc, toks: toks[0] if len( toks) == 1 else rules.ConcatRule(list(toks), lineNum)) completeRule.setParseAction( lambda string, loc, toks: toks[0].setWeak(len(toks) == 2)) parsedRule = pyparseString.pyparseString(completeRule, lineNum, line, filename)[0] # print parsedRule, '-->', parsedRule.transformToGeneratorVersion() return parsedRule