Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eae93b7

Browse files
committed
Add support for version field on Modules
1 parent a7446e3 commit eae93b7

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

Parser/Python.asdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- ASDL's five builtin types are identifier, int, string, object, bool
22

3-
module Python
3+
module Python version "$Revision$"
44
{
55
mod = Module(stmt* body)
66
| Interactive(stmt* body)

Parser/asdl.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
Only supports top level module decl, not view. I'm guessing that view
77
is intended to support the browser and I'm not interested in the
88
browser.
9+
10+
Changes for Python: Add support for module versions
911
"""
1012

1113
#__metaclass__ = type
@@ -36,6 +38,12 @@ def __init__(self, value, lineno):
3638

3739
def __str__(self):
3840
return self.value
41+
42+
class String(Token):
43+
def __init__(self, value, lineno):
44+
self.type = 'String'
45+
self.value = value
46+
self.lineno = lineno
3947

4048
class ASDLSyntaxError:
4149

@@ -63,6 +71,10 @@ def t_id(self, s):
6371
# XXX doesn't distinguish upper vs. lower, which is
6472
# significant for ASDL.
6573
self.rv.append(Id(s, self.lineno))
74+
75+
def t_string(self, s):
76+
r'"[^"]*"'
77+
self.rv.append(String(s, self.lineno))
6678

6779
def t_xxx(self, s): # not sure what this production means
6880
r"<="
@@ -98,19 +110,26 @@ def typestring(self, tok):
98110
def error(self, tok):
99111
raise ASDLSyntaxError(tok.lineno, tok)
100112

101-
def p_module_0(self, (module, name, _0, _1)):
102-
" module ::= Id Id { } "
113+
def p_module_0(self, (module, name, version, _0, _1)):
114+
" module ::= Id Id version { } "
103115
if module.value != "module":
104116
raise ASDLSyntaxError(module.lineno,
105117
msg="expected 'module', found %s" % module)
106-
return Module(name, None)
118+
return Module(name, None, version)
107119

108-
def p_module(self, (module, name, _0, definitions, _1)):
109-
" module ::= Id Id { definitions } "
120+
def p_module(self, (module, name, version, _0, definitions, _1)):
121+
" module ::= Id Id version { definitions } "
110122
if module.value != "module":
111123
raise ASDLSyntaxError(module.lineno,
112124
msg="expected 'module', found %s" % module)
113-
return Module(name, definitions)
125+
return Module(name, definitions, version)
126+
127+
def p_version(self, (version, V)):
128+
"version ::= Id String"
129+
if version.value != "version":
130+
raise ASDLSyntaxError(version.lineno,
131+
msg="expected 'version', found %" % version)
132+
return V
114133

115134
def p_definition_0(self, (definition,)):
116135
" definitions ::= definition "
@@ -209,9 +228,10 @@ class AST:
209228
pass # a marker class
210229

211230
class Module(AST):
212-
def __init__(self, name, dfns):
231+
def __init__(self, name, dfns, version):
213232
self.name = name
214233
self.dfns = dfns
234+
self.version = version
215235
self.types = {} # maps type name to value (from dfns)
216236
for type in dfns:
217237
self.types[type.name.value] = type.value

Parser/asdl_c.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ def visitModule(self, mod):
524524
self.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return;', 1)
525525
self.emit('if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)', 1)
526526
self.emit("return;", 2)
527+
self.emit("/* %s */" % mod.version.value, 1)
527528
for dfn in mod.dfns:
528529
self.visit(dfn)
529530
self.emit("}", 0)

0 commit comments

Comments
 (0)