def term(tokens): first = [TType.TTRUE, TType.TFALSE, TType.TVAR, TType.TLPAREN] follow = [TType.TEOF, TType.TRPAREN, TType.TARROW, TType.TOR, TType.TAND] e = None if tokens[0].ttype == TType.TVAR: e = pred(tokens) elif tokens[0].ttype == TType.TTRUE: e = true() tokens.pop(0) elif tokens[0].ttype == TType.TFALSE: e = false() tokens.pop(0) elif tokens[0].ttype == TType.TLPAREN: tokens.pop(0) e = expr(tokens) if tokens[0].ttype != TType.TRPAREN: raise ParseException(tokens[0].pos, [TType.TRPAREN], tokens[0].val) tokens.pop(0) elif tokens[0].ttype == TType.TFA or \ tokens[0].ttype == TType.TEX: e = expr(tokens) else: raise ParseException(tokens[0].pos, first, tokens[0].val) if tokens[0].ttype not in follow: raise ParseException(tokens[0].pos, follow, tokens[0].val) return e
def pred(tokens): follow = [TType.TEOF, TType.TRPAREN, TType.TARROW, TType.TOR, TType.TAND] e = None # initial name name = tokens.pop(0).val # P(v {, v} ) if tokens[0].ttype == TType.TLPAREN: tokens.pop(0) vs = [] if tokens[0].ttype == TType.TVAR: vs = [tokens.pop(0).val] # P() elif tokens[0].ttype == TType.TRPAREN: pass else: raise ParseException(tokens[0].pos, [TType.TVAR], tokens[0].val) # {, v} while tokens[0].ttype != TType.TRPAREN: if tokens[0].ttype == TType.TCOMMA and \ tokens[1].ttype == TType.TVAR: tokens.pop(0) vs.append(tokens.pop(0).val) else: raise ParseException(tokens[0].pos, [TType.TCOMMA], tokens[0].val) tokens.pop(0) e = Pred(name, vs) # v else: e = Var(name) if tokens[0].ttype not in follow: raise ParseException(tokens[0].pos, follow, tokens[0].val) return e
def expr(tokens): follow = [TType.TEOF, TType.TRPAREN] e = None if tokens[0].ttype == TType.TFA: if tokens[1].ttype == TType.TVAR: if tokens[2].ttype == TType.TDOT: tokens.pop(0) v = tokens.pop(0).val tokens.pop(0) e = Forall(v, expr(tokens)) else: raise ParseException(tokens[2].pos, [TType.TDOT], tokens[2].val) else: raise ParseException(tokens[1].pos, [TType.TVAR], tokens[1].val) elif tokens[0].ttype == TType.TEX: if tokens[1].ttype == TType.TVAR: if tokens[2].ttype == TType.TDOT: tokens.pop(0) v = tokens.pop(0).val tokens.pop(0) e = Exists(v, expr(tokens)) else: raise ParseException(tokens[2].pos, [TType.TDOT], tokens[2].val) else: raise ParseException(tokens[1].pos, [TType.TVAR], tokens[1].val) else: e = arrow_expr(tokens) if tokens[0].ttype not in follow: raise ParseException(tokens[0].pos, follow, tokens[0].val) return e
def objectHook(self, dictionary): """ Object has method load. If it returns None, then the instance of the object will be replaced with a dictionary. If load returns value X that is not none, dictionary will be updated with X. It is possible to dynamically add dictionaries. If __name__ start with @, it means that it will be added as dictionary into objectHook. If it ends with ?, it will be added only if it is not there already. """ if "__name__" not in dictionary: return dictionary cn = dictionary["__name__"] if len(cn) > 0 and cn[0] == '@': if cn[-1] == "?": cnn = cn[1:-1] check_if_exists = True else: cnn = cn[1:] check_if_exists = False del dictionary['__name__'] dictionary['__name__'] = cnn self.addDictionary(cnn, dictionary, check_if_exists) return None if len(cn) > 0 and cn[0] == '#': res = self.fileRegExp.match(cn) if not res: raise ParseException('Unknown function') res = res.groups() if len(res) != 1: raise ParseException('Internal error') filename = res[0] if filename in self.files: filename = self.files[filename] if len(filename) > 0 and filename[0] == '/': fn = filename else: fn = os.path.join(os.path.dirname(self.filenameStack[-1]), filename) return self.load(fn) if cn in self.objects: obj = self.objects[cn]() ret = obj.load(dictionary) if ret == None: return obj return ret elif cn in self.functions: ret = self.functions[cn](dictionary) return ret elif cn in self.dictionary: dct = self.dictionary[cn] if "key" not in dictionary: raise ParseException("Key not found in dictionary") ret = dct[dictionary['key']] return ret elif cn in self.constants: return self.constants[cn] else: return None
def readClause(tokens): if tokens[0].ttype != TType.TVAR: raise ParseException(tokens[0].row, tokens[0].col, [TType.TVAR], tokens[0].val) v = tokens.pop(0).val if tokens[0].ttype == TType.TARROW: tokens.pop(0) if tokens[0].ttype != TType.TVAR: raise ParseException(tokens[0].row, tokens[0].col, [TType.TVAR], tokens[0].val) clause = [tokens.pop(0).val] while tokens[0].ttype != TType.TPERIOD: if tokens[0].ttype != TType.TCOMMA: raise ParseException(tokens[0].row, tokens[0].col, [TType.TVAR], tokens[0].val) if tokens[1].ttype != TType.TVAR: raise ParseException(tokens[1].row, tokens[1].col, [TType.TVAR], tokens[1].val) clause.append(tokens[1].val) tokens.pop(0) tokens.pop(0) tokens.pop(0) return (v, clause) if tokens[0].ttype == TType.TPERIOD: tokens.pop(0) return (v, []) raise ParseException(tokens[0].row, tokens[0].col, [TType.TARROW, TType.TPERIOD], tokens[0].val)
def and_expr(tokens): follow = [TType.TEOF, TType.TRPAREN, TType.TARROW, TType.TOR] lhs = not_expr(tokens) while tokens[0].ttype == TType.TAND: tokens.pop(0) rhs = not_expr(tokens) lhs = And(lhs, rhs) if tokens[0].ttype not in follow: raise ParseException(tokens[0].pos, follow, tokens[0].val) return lhs
def arrow_expr(tokens): follow = [TType.TEOF, TType.TRPAREN] lhs = or_expr(tokens) if tokens[0].ttype == TType.TARROW: tokens.pop(0) rhs = arrow_expr(tokens) lhs = Arrow(lhs, rhs) if tokens[0].ttype not in follow: raise ParseException(tokens[0].pos, follow, tokens[0].val) return lhs
def not_expr(tokens): follow = [TType.TEOF, TType.TRPAREN, TType.TARROW, TType.TOR, TType.TAND] e = None if tokens[0].ttype == TType.TNOT: tokens.pop(0) ne = not_expr(tokens) e = Not(ne) else: e = term(tokens) if tokens[0].ttype not in follow: raise ParseException(tokens[0].pos, follow, tokens[0].val) return e
def load(self, dictionary): if "__name__" not in dictionary: raise ParseException("Wrong object type") if dictionary["__name__"] != self.__class__.__name__: raise ParseException("Wrong object file")