Skip to content

Commit

Permalink
Add: nmlc option to force regeneration of parser tables
Browse files Browse the repository at this point in the history
  • Loading branch information
glx22 committed Dec 4, 2019
1 parent 10be1f1 commit 7c9b137
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
9 changes: 5 additions & 4 deletions nml/main.py
Expand Up @@ -40,7 +40,7 @@ def parse_cli(argv):
opt_parser = optparse.OptionParser(usage=usage, version=version_info.get_cli_version())
opt_parser.set_defaults(debug=False, crop=False, compress=True, outputs=[], start_sprite_num=0,
custom_tags="custom_tags.txt", lang_dir="lang", default_lang="english.lng", cache_dir=".nmlcache",
forced_palette="ANY", quiet=False, md5_filename=None, keep_orphaned=True, verbosity=generic.verbosity_level)
forced_palette="ANY", quiet=False, md5_filename=None, keep_orphaned=True, verbosity=generic.verbosity_level, rebuild_parser=False)
opt_parser.add_option("-d", "--debug", action="store_true", dest="debug", help="write the AST to stdout")
opt_parser.add_option("-s", "--stack", action="store_true", dest="stack", help="Dump stack when an error occurs")
opt_parser.add_option("--grf", dest="grf_filename", metavar="<file>", help="write the resulting grf to <file>")
Expand Down Expand Up @@ -70,6 +70,7 @@ def parse_cli(argv):
opt_parser.add_option("--cache-dir", dest="cache_dir", metavar="<dir>", help="Cache files are stored in directory <dir> [default: %default]")
opt_parser.add_option("--clear-orphaned", action="store_false", dest="keep_orphaned", help="Remove unused/orphaned items from cache files.")
opt_parser.add_option("--verbosity", type="int", dest="verbosity", metavar="<level>", help="Set the verbosity level for informational output. [default: %default, max: {}]".format(generic.VERBOSITY_MAX))
opt_parser.add_option("-R", "--rebuild-parser", action="store_true", dest="rebuild_parser", help="Force regeneration of parser tables.")

opts, args = opt_parser.parse_args(argv)

Expand Down Expand Up @@ -164,15 +165,15 @@ def main(argv):
generic.print_error("Unknown output format {}".format(outext))
sys.exit(2)

ret = nml(input, input_filename, opts.debug, outputs, opts.start_sprite_num, opts.compress, opts.crop, not opts.no_cache, opts.forced_palette, opts.md5_filename)
ret = nml(input, input_filename, opts.debug, outputs, opts.start_sprite_num, opts.compress, opts.crop, not opts.no_cache, opts.forced_palette, opts.md5_filename, opts.rebuild_parser)

input.close()
sys.exit(ret)

def filename_output_from_input(name, ext):
return os.path.splitext(name)[0] + ext

def nml(inputfile, input_filename, output_debug, outputfiles, start_sprite_num, compress_grf, crop_sprites, enable_cache, forced_palette, md5_filename):
def nml(inputfile, input_filename, output_debug, outputfiles, start_sprite_num, compress_grf, crop_sprites, enable_cache, forced_palette, md5_filename, rebuild_parser):
"""
Compile an NML file.
Expand Down Expand Up @@ -220,7 +221,7 @@ def nml(inputfile, input_filename, output_debug, outputfiles, start_sprite_num,

generic.print_progress("Init parser ...")

nml_parser = parser.NMLParser()
nml_parser = parser.NMLParser(rebuild_parser)
if input_filename is None:
input_filename = 'input'

Expand Down
6 changes: 3 additions & 3 deletions nml/parser.py
Expand Up @@ -29,12 +29,12 @@ class NMLParser:
@ivar parser: PLY parser.
@type parser: L{ply.yacc}
"""
def __init__(self):
def __init__(self, rebuild = False):
self.lexer = tokens.NMLLexer()
self.lexer.build()
self.lexer.build(rebuild)
self.tokens = self.lexer.tokens
self.parser = yacc.yacc(module=self,
debug=False, optimize=True,
debug=False, optimize=not rebuild,
write_tables=True,
tabmodule='nml.generated.parsetab')

Expand Down
13 changes: 10 additions & 3 deletions nml/tokens.py
Expand Up @@ -15,7 +15,7 @@

import sys, re
import ply.lex as lex
from nml import expression, generic
from nml import expression, generic, generated

reserved = {
'grf' : 'GRF',
Expand Down Expand Up @@ -243,11 +243,18 @@ def t_error(self, t):



def build(self):
def build(self, rebuild = False):
"""
Initial construction of the scanner.
"""
self.lexer = lex.lex(module=self, optimize=1, lextab='nml.generated.lextab')
lextabfile = 'nml.generated.lextab'
if rebuild:
try:
import os
os.remove(os.path.normpath(os.path.join(os.path.dirname(__file__), "generated", "lextab.py")))
except FileNotFoundError:
pass
self.lexer = lex.lex(module=self, optimize=1, lextab=lextabfile)


def setup(self, text, fname):
Expand Down

1 comment on commit 7c9b137

@FLHerne
Copy link
Contributor

@FLHerne FLHerne commented on 7c9b137 Dec 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this, I was meaning to look at how to solve the problem I created. :-/

I think this commit should probably modify setup.py to force a table rebuild when installing?

Please sign in to comment.