def read_constants_from_file(cls, filename, type_repository): stripped_file = '' for line in open(filename, 'r'): line = line.strip() line = re.split('\\s*//', line)[0] if len(line) != 0: stripped_file += line + " " ret = set() lisp_reader = LispReader(StringIO(stripped_file)) while lisp_reader.has_next(): expr = LogicalConstant.read(lisp_reader.next(), type_repository) ret.add(expr) return ret
def read(cls, string, mapping, type_repository, type_comparator, reader): try: lisp_reader = LispReader(StringIO(string)) predicate_string = lisp_reader.next() predicate = reader.read(predicate_string, mapping, type_repository, type_comparator) arguments = [] while lisp_reader.has_next(): element_string = lisp_reader.next() argument = reader.read(element_string, mapping, type_repository, type_comparator) arguments.append(argument) return Literal(predicate, arguments, type_comparator, type_repository) except Exception, e: Literal.LOG.error("Literal syntax error: %s" % string) raise e
def read(cls, string, mapping, type_repository, type_comparator, reader): try: lisp_reader = LispReader(StringIO(string)) lisp_reader.next() variables_org_size = len(mapping) variable = reader.read(lisp_reader.next(), mapping, type_repository, type_comparator) if not isinstance(variable, Variable): raise LogicalExpressionRuntimeError('Invalid lambda argument: ' + string) if variables_org_size + 1 != len(mapping): raise LogicalExpressionRuntimeError('Lambda expression must introduce a new variable: %s' % string) body = reader.read(lisp_reader.next(), mapping, type_repository, type_comparator) if lisp_reader.has_next(): raise LogicalExpressionRuntimeError('Invalid lambda expression: ' + string) removed = None for key, var in mapping.iteritems(): if var == variable: removed = key break if removed is None: raise LogicalExpressionRuntimeError( 'Failed to remove variable from mapping. Something weird is happening') mapping.pop(removed, None) return Lambda(variable, body) except Exception, e: Lambda.LOG.error("Lambda syntax error: %s" % string) raise e