def grammar(): dneg = pp.Literal('not ') name = pp.Combine( pp.Literal('-') * (0, 1) + pp.Word(pp.srange('[a-z_]')) + pp.Word(pp.alphanums + '_') * (0, 1)) lpar = pp.Suppress('(') rpar = pp.Suppress(')') sepr = pp.Suppress(';') | pp.Suppress(',') num = pp.Word(pp.nums) # arg_body = pp.nestedExpr(opener='(', closer=')', content=pp.Word(pp.alphanums)).setResultsName('arg_body') arg_body = pp.Forward() literal = pp.Forward() func = pp.Forward() term = pp.Forward() term <<= (func | num) arg_body <<= pp.Group(lpar + ((term + (sepr + term) * (0, None)) | pp.empty()) + rpar) func <<= pp.Group(name + arg_body * (0, 1)) literal <<= (dneg * (0, 1) + func) #Semantic Actions func.setParseAction(PA_Functionc) num.setParseAction(PA_Num) literal.setParseAction(PA_Literal) return literal
def grammar_annotated(): dneg = pp.Literal('not ').setName('dneg') name = pp.Combine( pp.Literal('-') * (0, 1) + pp.Word(pp.srange('[a-z_]')) + pp.Word(pp.alphanums + '_') * (0, 1)).setName('name') lpar = pp.Suppress('(') rpar = pp.Suppress(')') sepr = pp.Suppress(';') | pp.Suppress(',') num = pp.Word(pp.nums).setName('num') # arg_body = pp.nestedExpr(opener='(', closer=')', content=pp.Word(pp.alphanums)).setResultsName('arg_body') arg_body = pp.Forward() literal = pp.Forward() func = pp.Forward() term = pp.Forward() term <<= (func('func*') | num('num_arg*')) arg_body <<= pp.Group(lpar + ((term + (sepr + term) * (0, None)) | pp.empty()) + rpar) func <<= pp.Group( name('fname') + arg_body.setResultsName('fbody') * (0, 1))('func') literal <<= (dneg('dneg') * (0, 1) + func('atom'))('literal') return literal
return data, label # identifier = Word(alphanums) identifier = Word(alphas + "_", alphanums + "_") literal = quotedString ^ pyparsing_common.number building = Literal('Building') ^ Literal('SubBuilding') sub_extraction = Group( identifier + Literal('(').suppress() + delimitedList(identifier) + Literal(')').suppress()).setName('Subontology Extraction') algebra = infixNotation(sub_extraction, [('-', 1, opAssoc.RIGHT), (oneOf('%'), 2, opAssoc.LEFT), (oneOf('* /'), 2, opAssoc.LEFT), (oneOf('+ -'), 2, opAssoc.LEFT)]).setName('Algebra') buildings = Group(delimitedList(Group(building + empty() + identifier))).setName('Building List') predicate = Group(identifier + Literal('.') + identifier + oneOf('> < = >= <= !=') + literal) predicates = infixNotation(predicate, [(AND, 2, opAssoc.LEFT), (OR, 2, opAssoc.LEFT)]).setName('Predicates') quantifications = predicates labels = Group(delimitedList(literal)) query = Group(SELECT.suppress() + algebra + Optional(FROM.suppress() + buildings + WHERE.suppress() + predicates +
op = pp.Word(identifier_chars) op.addParseAction( lambda s, loc, toks: Opcode(toks[0].lower(), loc=Location(loc))) arg = identifier | string_constant | address_constant | integer_constant kw_arg = identifier + '=' + arg kw_arg.addParseAction( lambda s, loc, toks: Map(toks[0], toks[2], loc=Location(loc))) args = pp.delimitedList(kw_arg | arg, pp.White('\t ')) args = pp.Optional(args) kw_arg.addParseAction(lambda s, loc, toks: toks[0] if len(toks) else []) empty = pp.empty() empty.addParseAction(lambda s, loc, toks: Empty('', Location(loc))) comment = '#' + pp.restOfLine() comment.addParseAction(lambda s, loc, toks: Comment(toks[1], Location(loc))) def map_command(s, loc, toks): if isinstance(toks[0], Label): return Command(toks[0], toks[1], toks[2:-1], toks[-1], Location(loc)) else: return Command(Label(None, Location(loc)), toks[0], toks[1:-1], toks[-1], Location(loc)) command = pp.Optional(label) + op + args + pp.Optional(comment, default=None)