def parseParameters(text, verbose=False): t2 = text.strip("\n \t") if len(t2)==0: return [] ids = Word(srange("[a-zA-Z0-9_]")) varDec = Group(Combine(ids.setResultsName("var")+Literal(':')+ids.setResultsName("t"))) params = OneOrMore(varDec) r = params.parseString(t2) return r
def parseParameters(text, verbose=False): t2 = text.strip("\n \t") if len(t2) == 0: return [] ids = Word(srange("[a-zA-Z0-9_]")) varDec = Group( Combine( ids.setResultsName("var") + Literal(':') + ids.setResultsName("t"))) params = OneOrMore(varDec) r = params.parseString(t2) return r
def parseFormula(text, verbose=False): if verbose: print 'Verbose:', verbose # Basic elements ids = Word(srange("[a-zA-Z0-9_]")) po = Suppress("(") pc = Suppress(")") lt = Suppress("<") gt = Suppress(">") eq = Word("=") not_ = Word("not") and_ = Word("and") or_ = Word("or") forall_ = Word("forall") exists_ = Word("exists") when_ = Word("when") create_ = Word("create") delete_ = Word("delete") retype_ = Word("retype") # Identifiers ids = Word(srange("[a-zA-Z0-9_]")) # List of identifiers varDec = Group(Combine(ids.setResultsName("var")+Literal(':')+ids.setResultsName("t"))) params = OneOrMore(varDec) # Groups: complex elements, not, forall, when, create and the like formula = Forward() link = Group( po + ids.setResultsName("type") + ids.setResultsName("a") + ids.setResultsName("b") + pc ) equalFormula = Group( po + eq.setResultsName("type") + ids.setResultsName("a") + ids.setResultsName("b") + pc ) notFormula = Group( po + not_.setResultsName("type") + formula.setResultsName("child") + pc ) andFormula = Group( po + and_.setResultsName("type") + OneOrMore(formula).setResultsName("more") + pc ) orFormula = Group( po + or_.setResultsName("type") + OneOrMore(formula).setResultsName("more") + pc ) existsFormula = Group( po + exists_.setResultsName("type") + params.setResultsName("vars") + formula.setResultsName("child") + pc ) forallFormula = Group( po + forall_.setResultsName("type") + params.setResultsName("vars") + formula.setResultsName("child") + pc ) whenFormula = Group( po + when_.setResultsName("type") + formula.setResultsName("iff") + formula.setResultsName("then") + pc ) functionCall = Group( lt + ids.setResultsName("func") + OneOrMore(ids).setResultsName("arguments") + gt ) create = Group( po + create_.setResultsName("type") + ids.setResultsName("name") + ids.setResultsName("stype") + pc ) delete = Group( po + delete_.setResultsName("type") + ids.setResultsName("name") + pc ) retype = Group( po + retype_.setResultsName("type") + ids.setResultsName("name") + ids.setResultsName("stype") + pc ) # Parse call formula << (notFormula | andFormula | link | equalFormula | orFormula | existsFormula | forallFormula | whenFormula | create | delete | retype | functionCall ) formulaOpt = Optional(formula) return formulaOpt.parseWithTabs().parseString(text)
def getAGGLMetaModels(): # Define AGM's DSL meta-model an = Word(srange("[a-zA-Z0-9_.]")) ids = Word(srange("[a-zA-Z0-9_]")) almostanything = CharsNotIn("{}") incPath = CharsNotIn("()") parameters = Suppress("parameters") precondition = Suppress("precondition") effect = Suppress("effect") plusorminus = Literal('+') | Literal('-') number = Word(nums) nu = Combine(Optional(plusorminus) + number) neg = Optional(Literal('*')) sep = Suppress("===") eq = Suppress("=") cn = Suppress(":") lk = Suppress("->") ar = Suppress("=>") op = Suppress("{") cl = Suppress("}") po = Suppress("(") co = Suppress(",") pt = Suppress(".") pc = Suppress(")") no = Suppress("!") dormant = (Optional(CaselessLiteral("dormant"))).setResultsName("dormant") activatesRules = (Optional( Suppress(CaselessLiteral("activates")) + po + ids + ZeroOrMore(co + ids) + pc)).setResultsName("activates") # LINK link = Group( an.setResultsName("lhs") + lk + an.setResultsName("rhs") + po + Optional(no).setResultsName("no") + an.setResultsName("linkType") + pc + neg.setResultsName("enabled")) # NODE node = Group( an.setResultsName("symbol") + cn + an.setResultsName("symbolType") + Optional(po + nu.setResultsName("x") + co + nu.setResultsName("y") + pc)) # GRAPH graph = Group(op + ZeroOrMore(node).setResultsName("nodes") + ZeroOrMore(link).setResultsName("links") + cl) # RULE SUCCESS ruleSuccess = CaselessLiteral("success") + po + an.setResultsName( "success") + pc # COMBO RULE atom = Group( ids.setResultsName("name") + Suppress("as") + ids.setResultsName("alias") + Optional("optional")) equivElement = Group( ids.setResultsName("rule") + pt + ids.setResultsName("variable")) equivRhs = eq + equivElement equiv = Group( equivElement.setResultsName("first") + OneOrMore(equivRhs).setResultsName("more")) rule_seq = Group( an.setResultsName("name") + cn + an.setResultsName("passive") + po + nu.setResultsName("cost") + pc + op + OneOrMore(atom).setResultsName("atomss") + Suppress("where:") + ZeroOrMore(equiv).setResultsName("equivalences") + cl) # NORMAL RULE Prm = Optional(parameters + op + almostanything + cl).setResultsName("parameters") Cnd = Optional(precondition + op + almostanything + cl).setResultsName("precondition") Eft = Optional(effect + op + almostanything + cl).setResultsName("effect") rule_nrm = Group( dormant.setResultsName("dormant") + an.setResultsName("name") + cn + an.setResultsName("passive") + po + nu.setResultsName("cost") + pc + Optional(ruleSuccess) + activatesRules.setResultsName("activates") + op + graph.setResultsName("lhs") + ar + graph.setResultsName("rhs") + Prm + Cnd + Eft + cl) # HIERARCHICAL RULE rule_hierarchical = Group( dormant.setResultsName("dormant") + Literal("hierarchical").setResultsName("hierarchical") + dormant.setResultsName("dormant") + an.setResultsName("name") + cn + an.setResultsName("passive") + po + nu.setResultsName("cost") + pc + Optional(ruleSuccess) + activatesRules.setResultsName("activates") + op + graph.setResultsName("lhs") + ar + graph.setResultsName("rhs") + Prm + Cnd + Eft + cl) # indlude include = Group( Suppress("include") + po + incPath.setResultsName("includefile") + pc) # GENERAL RULE rule = rule_nrm | rule_seq | rule_hierarchical | include # PROPERTY prop = Group(an.setResultsName("prop") + eq + an.setResultsName("value")) # TYPES typeDefinition = Group( Suppress(po) + Group(OneOrMore(ids).setResultsName("lhs")) + Optional(Suppress(cn) + Group(OneOrMore(ids).setResultsName("rhs"))) + Suppress(pc)) typesDefinition = Suppress("types") + Suppress(op) + OneOrMore( typeDefinition).setResultsName("types") + Suppress(cl) # WHOLE AGGL FILE aggl = OneOrMore(prop).setResultsName( "props") + sep + typesDefinition.setResultsName( "types") + sep + OneOrMore(rule).setResultsName( "rules") + StringEnd() # WHOLE AGGT FILE aggt = Optional(graph.setResultsName("graph")) + Optional(Cnd) ret = {} ret['aggl'] = aggl ret['aggt'] = aggt return ret
def getAGGLMetaModels(): # Define AGM's DSL meta-model an = Word(srange("[a-zA-Z0-9_.]")) ids = Word(srange("[a-zA-Z0-9_]")) almostanything = CharsNotIn("{}") incPath = CharsNotIn("()") parameters = Suppress("parameters") precondition = Suppress("precondition") effect = Suppress("effect") plusorminus = Literal('+') | Literal('-') number = Word(nums) nu = Combine( Optional(plusorminus) + number ) neg = Optional(Literal('*')) sep = Suppress("===") eq = Suppress("=") cn = Suppress(":") lk = Suppress("->") ar = Suppress("=>") op = Suppress("{") cl = Suppress("}") po = Suppress("(") co = Suppress(",") pt = Suppress(".") pc = Suppress(")") no = Suppress("!") dormant = (Optional(CaselessLiteral("dormant"))).setResultsName("dormant") activatesRules = (Optional(Suppress(CaselessLiteral("activates")) + po + ids + ZeroOrMore(co + ids) + pc)).setResultsName("activates") # LINK link = Group(an.setResultsName("lhs") + lk + an.setResultsName("rhs") + po + Optional(no).setResultsName("no") + an.setResultsName("linkType") + pc + neg.setResultsName("enabled")) # NODE node = Group(an.setResultsName("symbol") + cn + an.setResultsName("symbolType") + Optional(po + nu.setResultsName("x") + co + nu.setResultsName("y") + pc)) # GRAPH graph = Group(op + ZeroOrMore(node).setResultsName("nodes") + ZeroOrMore(link).setResultsName("links") + cl) # RULE SUCCESS ruleSuccess = CaselessLiteral("success") + po + an.setResultsName("success") + pc # COMBO RULE atom = Group(ids.setResultsName("name") + Suppress("as") + ids.setResultsName("alias") + Optional("optional")) equivElement = Group(ids.setResultsName("rule") + pt + ids.setResultsName("variable")) equivRhs = eq + equivElement equiv = Group(equivElement.setResultsName("first") + OneOrMore(equivRhs).setResultsName("more")) rule_seq = Group(an.setResultsName("name") + cn + an.setResultsName("passive") + po + nu.setResultsName("cost") + pc + op + OneOrMore(atom).setResultsName("atomss") + Suppress("where:") + ZeroOrMore(equiv).setResultsName("equivalences") + cl) # NORMAL RULE Prm = Optional(parameters + op + almostanything + cl).setResultsName("parameters") Cnd = Optional(precondition + op + almostanything + cl).setResultsName("precondition") Eft = Optional(effect + op + almostanything + cl).setResultsName("effect") rule_nrm = Group(dormant.setResultsName("dormant") + an.setResultsName("name") + cn + an.setResultsName("passive") + po + nu.setResultsName("cost") + pc + Optional(ruleSuccess) + activatesRules.setResultsName("activates") + op + graph.setResultsName("lhs") + ar + graph.setResultsName("rhs") + Prm + Cnd + Eft + cl) # HIERARCHICAL RULE rule_hierarchical = Group(dormant.setResultsName("dormant") + Literal("hierarchical").setResultsName("hierarchical") + dormant.setResultsName("dormant") + an.setResultsName("name") + cn + an.setResultsName("passive") + po + nu.setResultsName("cost") + pc + Optional(ruleSuccess) + activatesRules.setResultsName("activates") + op + graph.setResultsName("lhs") + ar + graph.setResultsName("rhs") + Prm + Cnd + Eft + cl) # indlude include = Group(Suppress("include") + po + incPath.setResultsName("includefile") + pc) # GENERAL RULE rule = rule_nrm | rule_seq | rule_hierarchical | include # PROPERTY prop = Group(an.setResultsName("prop") + eq + an.setResultsName("value")) # WHOLE AGGL FILE aggl = OneOrMore(prop).setResultsName("props") + sep + OneOrMore(rule).setResultsName("rules") + StringEnd() # WHOLE AGGT FILE aggt = Optional(graph.setResultsName("graph")) + Optional(Cnd) ret = {} ret['aggl'] = aggl ret['aggt'] = aggt return ret
def parseFormula(text, verbose=False): if verbose: print 'Verbose:', verbose # Basic elements ids = Word(srange("[a-zA-Z0-9_]")) po = Suppress("(") pc = Suppress(")") lt = Suppress("<") gt = Suppress(">") eq = Word("=") not_ = Word("not") and_ = Word("and") or_ = Word("or") forall_ = Word("forall") exists_ = Word("exists") when_ = Word("when") create_ = Word("create") delete_ = Word("delete") retype_ = Word("retype") # Identifiers ids = Word(srange("[a-zA-Z0-9_]")) # List of identifiers varDec = Group( Combine( ids.setResultsName("var") + Literal(':') + ids.setResultsName("t"))) params = OneOrMore(varDec) # Groups: complex elements, not, forall, when, create and the like formula = Forward() link = Group(po + ids.setResultsName("type") + ids.setResultsName("a") + ids.setResultsName("b") + pc) equalFormula = Group(po + eq.setResultsName("type") + ids.setResultsName("a") + ids.setResultsName("b") + pc) notFormula = Group(po + not_.setResultsName("type") + formula.setResultsName("child") + pc) andFormula = Group(po + and_.setResultsName("type") + OneOrMore(formula).setResultsName("more") + pc) orFormula = Group(po + or_.setResultsName("type") + OneOrMore(formula).setResultsName("more") + pc) existsFormula = Group(po + exists_.setResultsName("type") + params.setResultsName("vars") + formula.setResultsName("child") + pc) forallFormula = Group(po + forall_.setResultsName("type") + params.setResultsName("vars") + formula.setResultsName("child") + pc) whenFormula = Group(po + when_.setResultsName("type") + formula.setResultsName("iff") + formula.setResultsName("then") + pc) functionCall = Group(lt + ids.setResultsName("func") + OneOrMore(ids).setResultsName("arguments") + gt) create = Group(po + create_.setResultsName("type") + ids.setResultsName("name") + ids.setResultsName("stype") + pc) delete = Group(po + delete_.setResultsName("type") + ids.setResultsName("name") + pc) retype = Group(po + retype_.setResultsName("type") + ids.setResultsName("name") + ids.setResultsName("stype") + pc) # Parse call formula << (notFormula | andFormula | link | equalFormula | orFormula | existsFormula | forallFormula | whenFormula | create | delete | retype | functionCall) formulaOpt = Optional(formula) return formulaOpt.parseWithTabs().parseString(text)