Naming enhancement
This commit is contained in:
parent
a128245cd3
commit
ab75a58b10
3
CHANGES
3
CHANGES
@ -1,5 +1,8 @@
|
|||||||
Version 0.4
|
Version 0.4
|
||||||
-----------
|
-----------
|
||||||
|
05/09/2020 Changed the internal names used for EBNF rules to make them
|
||||||
|
a bit easier to debug in the parser.out file.
|
||||||
|
|
||||||
03/06/2020 Added experimental support for EBNF repetition and optional
|
03/06/2020 Added experimental support for EBNF repetition and optional
|
||||||
syntax. For example, here is a rule for a comma-separated
|
syntax. For example, here is a rule for a comma-separated
|
||||||
expression list:
|
expression list:
|
||||||
|
32
sly/yacc.py
32
sly/yacc.py
@ -1613,23 +1613,19 @@ def _replace_ebnf_optional(syms):
|
|||||||
# Generate grammar rules for repeated items
|
# Generate grammar rules for repeated items
|
||||||
_gencount = 0
|
_gencount = 0
|
||||||
|
|
||||||
def _unique_names(names):
|
|
||||||
from collections import defaultdict, Counter
|
|
||||||
counts = Counter(names)
|
|
||||||
indices = defaultdict(int)
|
|
||||||
newnames = []
|
|
||||||
for name in names:
|
|
||||||
if counts[name] == 1:
|
|
||||||
newnames.append(name)
|
|
||||||
else:
|
|
||||||
newnames.append(f'{name}{indices[name]}')
|
|
||||||
indices[name] += 1
|
|
||||||
return newnames
|
|
||||||
|
|
||||||
# Dictionary mapping name aliases generated by EBNF rules.
|
# Dictionary mapping name aliases generated by EBNF rules.
|
||||||
|
|
||||||
_name_aliases = { }
|
_name_aliases = { }
|
||||||
|
|
||||||
|
def _sanitize_symbols(symbols):
|
||||||
|
for sym in symbols:
|
||||||
|
if sym.startswith("'"):
|
||||||
|
yield str(hex(ord(sym[1])))
|
||||||
|
elif sym.isidentifier():
|
||||||
|
yield sym
|
||||||
|
else:
|
||||||
|
yield sym.encode('utf-8').hex()
|
||||||
|
|
||||||
def _generate_repeat_rules(symbols):
|
def _generate_repeat_rules(symbols):
|
||||||
'''
|
'''
|
||||||
Symbols is a list of grammar symbols [ symbols ]. This
|
Symbols is a list of grammar symbols [ symbols ]. This
|
||||||
@ -1654,9 +1650,10 @@ def _generate_repeat_rules(symbols):
|
|||||||
'''
|
'''
|
||||||
global _gencount
|
global _gencount
|
||||||
_gencount += 1
|
_gencount += 1
|
||||||
name = f'_{_gencount}_repeat'
|
basename = f'_{_gencount}_' + '_'.join(_sanitize_symbols(symbols))
|
||||||
oname = f'_{_gencount}_items'
|
name = f'{basename}_repeat'
|
||||||
iname = f'_{_gencount}_item'
|
oname = f'{basename}_items'
|
||||||
|
iname = f'{basename}_item'
|
||||||
symtext = ' '.join(symbols)
|
symtext = ' '.join(symbols)
|
||||||
|
|
||||||
_name_aliases[name] = symbols
|
_name_aliases[name] = symbols
|
||||||
@ -1709,7 +1706,8 @@ def _generate_optional_rules(symbols):
|
|||||||
'''
|
'''
|
||||||
global _gencount
|
global _gencount
|
||||||
_gencount += 1
|
_gencount += 1
|
||||||
name = f'_{_gencount}_optional'
|
basename = f'_{_gencount}_' + '_'.join(_sanitize_symbols(symbols))
|
||||||
|
name = f'{basename}_optional'
|
||||||
symtext = ' '.join(symbols)
|
symtext = ' '.join(symbols)
|
||||||
|
|
||||||
_name_aliases[name] = symbols
|
_name_aliases[name] = symbols
|
||||||
|
Loading…
Reference in New Issue
Block a user