Commit ca877338 authored by jl@opera.com's avatar jl@opera.com

IDL parser: fix rebuilding of (stale) cached lexer tables

The PLY lexer caches its table as a Python module (lextab.py). The
cache is loaded by attempting to import the module, which succeeds
if there's a lextab.py{,c,o} anywhere in the Python path. If the
import succeeds, the cache is assumed by PLY to be up-to-date, so
to update the cache, we need to remove those files before calling
PLY to initialize the lexer.

This patch makes blink_idl_lexer.main() do just that. It also
extends blink_idl_parser.main() to call the former, since the GYP
(and GN) build systems only actually call the latter script.

BUG=397909

Review URL: https://codereview.chromium.org/425953002

git-svn-id: svn://svn.chromium.org/blink/trunk@179129 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 53474bc5
...@@ -88,7 +88,8 @@ class BlinkIDLLexer(IDLLexer): ...@@ -88,7 +88,8 @@ class BlinkIDLLexer(IDLLexer):
for token in tokens: for token in tokens:
self._RemoveToken(token) self._RemoveToken(token)
def __init__(self, debug=False, optimize=True, outputdir=None): def __init__(self, debug=False, optimize=True, outputdir=None,
rewrite_tables=False):
if debug: if debug:
# Turn off optimization and caching to help debugging # Turn off optimization and caching to help debugging
optimize = False optimize = False
...@@ -98,6 +99,21 @@ class BlinkIDLLexer(IDLLexer): ...@@ -98,6 +99,21 @@ class BlinkIDLLexer(IDLLexer):
# as a Python module # as a Python module
sys.path.append(outputdir) sys.path.append(outputdir)
if rewrite_tables:
tablefile = os.path.join(outputdir, 'lextab.py')
def unlink(filename):
try:
os.unlink(filename)
except OSError:
pass
unlink(tablefile)
# Also remove the .pyc/.pyo files, or they'll be used even if
# the .py file doesn't exist.
unlink(tablefile + 'c')
unlink(tablefile + 'o')
IDLLexer.__init__(self) IDLLexer.__init__(self)
# Overrides to parent class # Overrides to parent class
self._RemoveTokens(REMOVE_TOKENS) self._RemoveTokens(REMOVE_TOKENS)
...@@ -108,6 +124,7 @@ class BlinkIDLLexer(IDLLexer): ...@@ -108,6 +124,7 @@ class BlinkIDLLexer(IDLLexer):
self._lexobj = lex.lex(object=self, self._lexobj = lex.lex(object=self,
debug=debug, debug=debug,
optimize=optimize, optimize=optimize,
lextab='lextab',
outputdir=outputdir) outputdir=outputdir)
...@@ -120,7 +137,10 @@ def main(argv): ...@@ -120,7 +137,10 @@ def main(argv):
except IndexError as err: except IndexError as err:
print 'Usage: %s OUTPUT_DIR' % argv[0] print 'Usage: %s OUTPUT_DIR' % argv[0]
return 1 return 1
lexer = BlinkIDLLexer(outputdir=outputdir) # Important: rewrite_tables=True causes the cache file to be deleted if it
# exists, thus making sure that PLY doesn't load it instead of regenerating
# the parse table.
lexer = BlinkIDLLexer(outputdir=outputdir, rewrite_tables=True)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -71,6 +71,7 @@ from idl_parser.idl_parser import IDLParser, ListFromConcat ...@@ -71,6 +71,7 @@ from idl_parser.idl_parser import IDLParser, ListFromConcat
from idl_parser.idl_parser import ParseFile as parse_file from idl_parser.idl_parser import ParseFile as parse_file
from blink_idl_lexer import BlinkIDLLexer from blink_idl_lexer import BlinkIDLLexer
import blink_idl_lexer
# Explicitly set starting symbol to rule defined only in base parser. # Explicitly set starting symbol to rule defined only in base parser.
...@@ -440,12 +441,13 @@ class BlinkIDLParser(IDLParser): ...@@ -440,12 +441,13 @@ class BlinkIDLParser(IDLParser):
################################################################################ ################################################################################
def main(argv): def main(argv):
# If file itself executed, cache parse table # If file itself executed, cache lex/parse tables
try: try:
outputdir = argv[1] outputdir = argv[1]
except IndexError as err: except IndexError as err:
print 'Usage: %s OUTPUT_DIR' % argv[0] print 'Usage: %s OUTPUT_DIR' % argv[0]
return 1 return 1
blink_idl_lexer.main(argv)
# Important: rewrite_tables=True causes the cache file to be deleted if it # Important: rewrite_tables=True causes the cache file to be deleted if it
# exists, thus making sure that PLY doesn't load it instead of regenerating # exists, thus making sure that PLY doesn't load it instead of regenerating
# the parse table. # the parse table.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment