diff -r 9b6de25c6054 Parser/asdl.py --- a/Parser/asdl.py Mon Apr 08 20:57:13 2013 -0500 +++ b/Parser/asdl.py Wed Apr 10 23:23:17 2013 -0500 @@ -96,7 +96,7 @@ def t_default(self, s): r" . +" - raise ValueError, "unmatched input: %s" % `s` + raise ValueError("unmatched input: %s" % repr(s)) class ASDLParser(spark.GenericParser, object): def __init__(self): @@ -108,49 +108,58 @@ def error(self, tok): raise ASDLSyntaxError(tok.lineno, tok) - def p_module_0(self, (module, name, version, _0, _1)): + def p_module_0(self, info): " module ::= Id Id version { } " + (module, name, version, _0, _1) = info if module.value != "module": raise ASDLSyntaxError(module.lineno, msg="expected 'module', found %s" % module) return Module(name, None, version) - def p_module(self, (module, name, version, _0, definitions, _1)): + def p_module(self, info): " module ::= Id Id version { definitions } " + (module, name, version, _0, definitions, _1) = info if module.value != "module": raise ASDLSyntaxError(module.lineno, msg="expected 'module', found %s" % module) return Module(name, definitions, version) - def p_version(self, (version, V)): + def p_version(self, info): "version ::= Id String" + (version, V) = info if version.value != "version": raise ASDLSyntaxError(version.lineno, msg="expected 'version', found %" % version) return V - def p_definition_0(self, (definition,)): + def p_definition_0(self, info): " definitions ::= definition " + (definition,) = info return definition - def p_definition_1(self, (definitions, definition)): + def p_definition_1(self, info): " definitions ::= definition definitions " + (definitions, definition) = info return definitions + definition - def p_definition(self, (id, _, type)): + def p_definition(self, info): " definition ::= Id = type " + (id, _, type) = info return [Type(id, type)] - def p_type_0(self, (product,)): + def p_type_0(self, info): " type ::= product " + (product,) = info return product - def p_type_1(self, (sum,)): + def p_type_1(self, info): " type ::= sum " + (sum,) = info return Sum(sum) - def p_type_2(self, (sum, id, _0, attributes, _1)): + def p_type_2(self, info): " type ::= sum Id ( fields ) " + (sum, id, _0, attributes, _1) = info if id.value != "attributes": raise ASDLSyntaxError(id.lineno, msg="expected attributes, found %s" % id) @@ -158,64 +167,76 @@ attributes.reverse() return Sum(sum, attributes) - def p_product(self, (_0, fields, _1)): + def p_product(self, info): " product ::= ( fields ) " - # XXX can't I just construct things in the right order? + (_0, fields, _1) = info fields.reverse() return Product(fields) - def p_sum_0(self, (constructor,)): + def p_sum_0(self, info): " sum ::= constructor " + (constructor,) = info return [constructor] - def p_sum_1(self, (constructor, _, sum)): + def p_sum_1(self, info): " sum ::= constructor | sum " + (constructor, _, sum) = info return [constructor] + sum - def p_sum_2(self, (constructor, _, sum)): + def p_sum_2(self, info): " sum ::= constructor | sum " + (constructor, _, sum) = info return [constructor] + sum - def p_constructor_0(self, (id,)): + def p_constructor_0(self, info): " constructor ::= Id " + (id,) = info return Constructor(id) - def p_constructor_1(self, (id, _0, fields, _1)): + def p_constructor_1(self, info): " constructor ::= Id ( fields ) " - # XXX can't I just construct things in the right order? + (id, _0, fields, _1) = info fields.reverse() return Constructor(id, fields) - def p_fields_0(self, (field,)): + def p_fields_0(self, info): " fields ::= field " + (field,) = info return [field] - def p_fields_1(self, (field, _, fields)): + def p_fields_1(self, info): " fields ::= field , fields " + (field, _, fields) = info return fields + [field] - def p_field_0(self, (type,)): + def p_field_0(self, info): " field ::= Id " + (type,) = info return Field(type) - def p_field_1(self, (type, name)): + def p_field_1(self, info): " field ::= Id Id " + (type, name) = info return Field(type, name) - def p_field_2(self, (type, _, name)): + def p_field_2(self, info): " field ::= Id * Id " + (type, _, name) = info return Field(type, name, seq=True) - def p_field_3(self, (type, _, name)): + def p_field_3(self, info): " field ::= Id ? Id " + (type, _, name) = info return Field(type, name, opt=True) - def p_field_4(self, (type, _)): + def p_field_4(self, info): " field ::= Id * " + (type, _) = info return Field(type, seq=True) - def p_field_5(self, (type, _)): + def p_field_5(self, info): " field ::= Id ? " + (type, _) = info return Field(type, opt=True) builtin_types = ("identifier", "string", "int", "bool", "object") @@ -304,9 +325,9 @@ return try: meth(object, *args) - except Exception, err: - print "Error visiting", repr(object) - print err + except Exception as err: + print("Error visiting %r" % object) + print(err) traceback.print_exc() # XXX hack if hasattr(self, 'file'): @@ -351,8 +372,8 @@ if conflict is None: self.cons[key] = name else: - print "Redefinition of constructor %s" % key - print "Defined in %s and %s" % (conflict, name) + print("Redefinition of constructor %s" % key) + print("Defined in %s and %s" % (conflict, name)) self.errors += 1 for f in cons.fields: self.visit(f, key) @@ -374,7 +395,7 @@ if t not in mod.types and not t in builtin_types: v.errors += 1 uses = ", ".join(v.types[t]) - print "Undefined type %s, used in %s" % (t, uses) + print("Undefined type %s, used in %s" % (t, uses)) return not v.errors @@ -386,10 +407,10 @@ tokens = scanner.tokenize(buf) try: return parser.parse(tokens) - except ASDLSyntaxError, err: - print err + except ASDLSyntaxError as err: + print(err) lines = buf.split("\n") - print lines[err.lineno - 1] # lines starts at 0, files at 1 + print(lines[err.lineno - 1]) # lines starts at 0, files at 1 if __name__ == "__main__": import glob @@ -402,12 +423,12 @@ files = glob.glob(testdir + "/*.asdl") for file in files: - print file + print(file) mod = parse(file) - print "module", mod.name - print len(mod.dfns), "definitions" + print("module %s" % mod.name) + print("%d definitions" % len(mod.dfns)) if not check(mod): - print "Check failed" + print("Check failed") else: for dfn in mod.dfns: - print dfn.type + print(dfn.type) diff -r 9b6de25c6054 Parser/asdl_c.py --- a/Parser/asdl_c.py Mon Apr 08 20:57:13 2013 -0500 +++ b/Parser/asdl_c.py Wed Apr 10 23:23:17 2013 -0500 @@ -1187,7 +1187,7 @@ sys.exit(1) if INC_DIR: p = "%s/%s-ast.h" % (INC_DIR, mod.name) - f = open(p, "wb") + f = open(p, "w") f.write(auto_gen_msg) f.write('#include "asdl.h"\n\n') c = ChainOfVisitors(TypeDefVisitor(f), @@ -1202,7 +1202,7 @@ if SRC_DIR: p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c") - f = open(p, "wb") + f = open(p, "w") f.write(auto_gen_msg) f.write(c_file_msg % mod.version) f.write('#include "Python.h"\n') @@ -1230,7 +1230,7 @@ SRC_DIR = '' opts, args = getopt.getopt(sys.argv[1:], "h:c:") if len(opts) != 1: - print "Must specify exactly one output file" + print("Must specify exactly one output file") sys.exit(1) for o, v in opts: if o == '-h': @@ -1238,6 +1238,6 @@ if o == '-c': SRC_DIR = v if len(args) != 1: - print "Must specify single input file" + print("Must specify single input file") sys.exit(1) main(args[0]) diff -r 9b6de25c6054 Parser/spark.py --- a/Parser/spark.py Mon Apr 08 20:57:13 2013 -0500 +++ b/Parser/spark.py Wed Apr 10 23:23:17 2013 -0500 @@ -30,7 +30,7 @@ for b in c.__bases__: classlist.append(b) for name in c.__dict__.keys(): - if not namedict.has_key(name): + if name not in namedict: namelist.append(name) namedict[name] = 1 return namelist @@ -56,10 +56,10 @@ rv.append(self.makeRE(name)) rv.append(self.makeRE('t_default')) - return string.join(rv, '|') + return '|'.join(rv) def error(self, s, pos): - print "Lexical error at position %s" % pos + print("Lexical error at position %s" % pos) raise SystemExit def tokenize(self, s): @@ -72,13 +72,13 @@ groups = m.groups() for i in range(len(groups)): - if groups[i] and self.index2func.has_key(i): + if groups[i] and (i in self.index2func): self.index2func[i](groups[i]) pos = m.end() def t_default(self, s): r'( . | \n )+' - print "Specification error: unmatched input" + print("Specification error: unmatched input") raise SystemExit # @@ -140,7 +140,7 @@ for k, v in self.edges.items(): if v is None: state, sym = k - if self.states.has_key(state): + if state in self.states: self.goto(state, sym) changes = 1 rv = self.__dict__.copy() @@ -171,7 +171,7 @@ def addRule(self, doc, func, _preprocess=1): fn = func - rules = string.split(doc) + rules = doc.split() index = [] for i in range(len(rules)): @@ -187,7 +187,7 @@ if _preprocess: rule, fn = self.preprocess(rule, func) - if self.rules.has_key(lhs): + if lhs in self.rules: self.rules[lhs].append(rule) else: self.rules[lhs] = [ rule ] @@ -225,7 +225,7 @@ # grammars. # for sym in rhs: - if not self.rules.has_key(sym): + if sym not in self.rules: break else: tbd.append(rule) @@ -268,7 +268,7 @@ n = len(rhs) while i < n: sym = rhs[i] - if not self.rules.has_key(sym) or \ + if (sym not in self.rules) or \ not self.nullable[sym]: candidate = 0 i = i + 1 @@ -285,7 +285,7 @@ if candidate: lhs = self._NULLABLE+lhs rule = (lhs, rhs) - if self.newrules.has_key(lhs): + if lhs in self.newrules: self.newrules[lhs].append(rule) else: self.newrules[lhs] = [ rule ] @@ -295,7 +295,7 @@ return None def error(self, token): - print "Syntax error at or near `%s' token" % token + print("Syntax error at or near `%s' token" % token) raise SystemExit def parse(self, tokens): @@ -312,7 +312,7 @@ self.states = { 0: self.makeState0() } self.makeState(0, self._BOF) - for i in xrange(len(tokens)): + for i in range(len(tokens)): sets.append([]) if sets[i] == []: @@ -341,7 +341,8 @@ # return self._NULLABLE == sym[0:len(self._NULLABLE)] - def skip(self, (lhs, rhs), pos=0): + def skip(self, sides, pos=0): + lhs, rhs = sides n = len(rhs) while pos < n: if not self.isnullable(rhs[pos]): @@ -364,7 +365,7 @@ core.sort() tcore = tuple(core) - if self.cores.has_key(tcore): + if tcore in self.cores: return self.cores[tcore] # # Nope, doesn't exist. Compute it and the associated @@ -388,13 +389,13 @@ nextSym = rhs[pos] key = (X.stateno, nextSym) - if not rules.has_key(nextSym): - if not edges.has_key(key): + if nextSym not in rules: + if key not in edges: edges[key] = None X.T.append(nextSym) else: edges[key] = None - if not predicted.has_key(nextSym): + if nextSym not in predicted: predicted[nextSym] = 1 for prule in rules[nextSym]: ppos = self.skip(prule) @@ -418,10 +419,10 @@ # need to know the entire set of predicted nonterminals # to do this without accidentally duplicating states. # - core = predicted.keys() + core = list(predicted.keys()) core.sort() tcore = tuple(core) - if self.cores.has_key(tcore): + if tcore in self.cores: self.edges[(k, None)] = self.cores[tcore] return k @@ -432,7 +433,7 @@ def goto(self, state, sym): key = (state, sym) - if not self.edges.has_key(key): + if key not in self.edges: # # No transitions from state on sym. # @@ -630,7 +631,7 @@ for i in range(len(rhs)-1, -1, -1): sym = rhs[i] - if not self.newrules.has_key(sym): + if sym not in self.newrules: if sym != self._BOF: attr[i] = tokens[k-1] key = (item, k) @@ -660,8 +661,8 @@ sortlist.append((len(rhs), name)) name2index[name] = i sortlist.sort() - list = map(lambda (a,b): b, sortlist) - return rules[name2index[self.resolve(list)]] + _list = list(map((lambda ab: ab[1]), sortlist)) + return rules[name2index[self.resolve(_list)]] def resolve(self, list): # @@ -825,15 +826,15 @@ def _dump(tokens, sets, states): for i in range(len(sets)): - print 'set', i + print('set %i' % i) for item in sets[i]: - print '\t', item + print('\t %s' % item) for (lhs, rhs), pos in states[item[0]].items: - print '\t\t', lhs, '::=', - print string.join(rhs[:pos]), - print '.', - print string.join(rhs[pos:]) + s = '\t\t %s ::= ' % str(lhs) + s += ' '.join(rhs[:pos]) + ' ' + s += ' . ' + ' '.join(rhs[pos:]) + print(s) if i < len(tokens): - print - print 'token', str(tokens[i]) - print + print() + print('token %s' % tokens[i]) + print()