Add support for version field on Modules

This commit is contained in:
Martin v. Löwis 2006-02-28 00:12:47 +00:00
parent a7446e3438
commit eae93b763c
3 changed files with 29 additions and 8 deletions

View File

@ -1,6 +1,6 @@
-- ASDL's five builtin types are identifier, int, string, object, bool -- ASDL's five builtin types are identifier, int, string, object, bool
module Python module Python version "$Revision$"
{ {
mod = Module(stmt* body) mod = Module(stmt* body)
| Interactive(stmt* body) | Interactive(stmt* body)

View File

@ -6,6 +6,8 @@
Only supports top level module decl, not view. I'm guessing that view Only supports top level module decl, not view. I'm guessing that view
is intended to support the browser and I'm not interested in the is intended to support the browser and I'm not interested in the
browser. browser.
Changes for Python: Add support for module versions
""" """
#__metaclass__ = type #__metaclass__ = type
@ -36,6 +38,12 @@ def __init__(self, value, lineno):
def __str__(self): def __str__(self):
return self.value return self.value
class String(Token):
def __init__(self, value, lineno):
self.type = 'String'
self.value = value
self.lineno = lineno
class ASDLSyntaxError: class ASDLSyntaxError:
@ -63,6 +71,10 @@ def t_id(self, s):
# XXX doesn't distinguish upper vs. lower, which is # XXX doesn't distinguish upper vs. lower, which is
# significant for ASDL. # significant for ASDL.
self.rv.append(Id(s, self.lineno)) self.rv.append(Id(s, self.lineno))
def t_string(self, s):
r'"[^"]*"'
self.rv.append(String(s, self.lineno))
def t_xxx(self, s): # not sure what this production means def t_xxx(self, s): # not sure what this production means
r"<=" r"<="
@ -98,19 +110,26 @@ def typestring(self, tok):
def error(self, tok): def error(self, tok):
raise ASDLSyntaxError(tok.lineno, tok) raise ASDLSyntaxError(tok.lineno, tok)
def p_module_0(self, (module, name, _0, _1)): def p_module_0(self, (module, name, version, _0, _1)):
" module ::= Id Id { } " " module ::= Id Id version { } "
if module.value != "module": if module.value != "module":
raise ASDLSyntaxError(module.lineno, raise ASDLSyntaxError(module.lineno,
msg="expected 'module', found %s" % module) msg="expected 'module', found %s" % module)
return Module(name, None) return Module(name, None, version)
def p_module(self, (module, name, _0, definitions, _1)): def p_module(self, (module, name, version, _0, definitions, _1)):
" module ::= Id Id { definitions } " " module ::= Id Id version { definitions } "
if module.value != "module": if module.value != "module":
raise ASDLSyntaxError(module.lineno, raise ASDLSyntaxError(module.lineno,
msg="expected 'module', found %s" % module) msg="expected 'module', found %s" % module)
return Module(name, definitions) return Module(name, definitions, version)
def p_version(self, (version, V)):
"version ::= Id String"
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, (definition,)):
" definitions ::= definition " " definitions ::= definition "
@ -209,9 +228,10 @@ class AST:
pass # a marker class pass # a marker class
class Module(AST): class Module(AST):
def __init__(self, name, dfns): def __init__(self, name, dfns, version):
self.name = name self.name = name
self.dfns = dfns self.dfns = dfns
self.version = version
self.types = {} # maps type name to value (from dfns) self.types = {} # maps type name to value (from dfns)
for type in dfns: for type in dfns:
self.types[type.name.value] = type.value self.types[type.name.value] = type.value

View File

@ -524,6 +524,7 @@ def visitModule(self, mod):
self.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return;', 1) self.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return;', 1)
self.emit('if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)', 1) self.emit('if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)', 1)
self.emit("return;", 2) self.emit("return;", 2)
self.emit("/* %s */" % mod.version.value, 1)
for dfn in mod.dfns: for dfn in mod.dfns:
self.visit(dfn) self.visit(dfn)
self.emit("}", 0) self.emit("}", 0)