Lines 1-82
Link Here
|
1 |
--- ply/lex.py.orig 2015-04-26 21:17:41 UTC |
|
|
2 |
+++ ply/lex.py |
3 |
@@ -171,7 +171,10 @@ class Lexer: |
4 |
# ------------------------------------------------------------ |
5 |
# writetab() - Write lexer information to a table file |
6 |
# ------------------------------------------------------------ |
7 |
- def writetab(self, basetabmodule, outputdir=''): |
8 |
+ def writetab(self, lextab, outputdir=''): |
9 |
+ if isinstance(lextab, types.ModuleType): |
10 |
+ raise IOError("Won't overwrite existing lextab module") |
11 |
+ basetabmodule = lextab.split('.')[-1] |
12 |
filename = os.path.join(outputdir, basetabmodule) + '.py' |
13 |
with open(filename, 'w') as tf: |
14 |
tf.write('# %s.py. This file automatically created by PLY (version %s). Don\'t edit!\n' % (basetabmodule, __version__)) |
15 |
@@ -856,6 +859,10 @@ class LexerReflect(object): |
16 |
# ----------------------------------------------------------------------------- |
17 |
def lex(module=None, object=None, debug=False, optimize=False, lextab='lextab', |
18 |
reflags=0, nowarn=False, outputdir=None, debuglog=None, errorlog=None): |
19 |
+ |
20 |
+ if lextab is None: |
21 |
+ lextab = 'lextab' |
22 |
+ |
23 |
global lexer |
24 |
|
25 |
ldict = None |
26 |
@@ -885,29 +892,13 @@ def lex(module=None, object=None, debug= |
27 |
else: |
28 |
ldict = get_caller_module_dict(2) |
29 |
|
30 |
- if outputdir is None: |
31 |
- # If no output directory is set, the location of the output files |
32 |
- # is determined according to the following rules: |
33 |
- # - If lextab specifies a package, files go into that package directory |
34 |
- # - Otherwise, files go in the same directory as the specifying module |
35 |
- if '.' not in lextab: |
36 |
- srcfile = ldict['__file__'] |
37 |
- else: |
38 |
- parts = lextab.split('.') |
39 |
- pkgname = '.'.join(parts[:-1]) |
40 |
- exec('import %s' % pkgname) |
41 |
- srcfile = getattr(sys.modules[pkgname], '__file__', '') |
42 |
- outputdir = os.path.dirname(srcfile) |
43 |
- |
44 |
# Determine if the module is package of a package or not. |
45 |
# If so, fix the tabmodule setting so that tables load correctly |
46 |
pkg = ldict.get('__package__') |
47 |
- if pkg: |
48 |
+ if pkg and isinstance(lextab, str): |
49 |
if '.' not in lextab: |
50 |
lextab = pkg + '.' + lextab |
51 |
|
52 |
- baselextab = lextab.split('.')[-1] |
53 |
- |
54 |
# Collect parser information from the dictionary |
55 |
linfo = LexerReflect(ldict, log=errorlog, reflags=reflags) |
56 |
linfo.get_all() |
57 |
@@ -1029,8 +1020,24 @@ def lex(module=None, object=None, debug= |
58 |
|
59 |
# If in optimize mode, we write the lextab |
60 |
if lextab and optimize: |
61 |
+ if outputdir is None: |
62 |
+ # If no output directory is set, the location of the output files |
63 |
+ # is determined according to the following rules: |
64 |
+ # - If lextab specifies a package, files go into that package directory |
65 |
+ # - Otherwise, files go in the same directory as the specifying module |
66 |
+ if isinstance(lextab, types.ModuleType): |
67 |
+ srcfile = lextab.__file__ |
68 |
+ else: |
69 |
+ if '.' not in lextab: |
70 |
+ srcfile = ldict['__file__'] |
71 |
+ else: |
72 |
+ parts = lextab.split('.') |
73 |
+ pkgname = '.'.join(parts[:-1]) |
74 |
+ exec('import %s' % pkgname) |
75 |
+ srcfile = getattr(sys.modules[pkgname], '__file__', '') |
76 |
+ outputdir = os.path.dirname(srcfile) |
77 |
try: |
78 |
- lexobj.writetab(baselextab, outputdir) |
79 |
+ lexobj.writetab(lextab, outputdir) |
80 |
except IOError as e: |
81 |
errorlog.warning("Couldn't write lextab module %r. %s" % (lextab, e)) |
82 |
|