1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
"""
Parse and translate an EBNF grammar into a Python parser for
the described language.
"""
# ruff: noqa: PLR0912
from __future__ import annotations
import argparse
import codecs
import importlib
import sys
from pathlib import Path
from ._version import __version__
from .exceptions import ParseException
from .ngcodegen import codegen as ngpythoncg
from .ngcodegen import objectmodel as ngobjectmodel
from .parser import GrammarGenerator
from .parserconfig import ParserConfig
from .semantics import ModelBuilderSemantics
from .util import eval_escapes
DESCRIPTION = (
'TatSu takes a grammar'
' in a variation of EBNF as input, and outputs a memoizing'
' PEG/Packrat parser in Python.'
)
def parse_args():
argparser = argparse.ArgumentParser(
prog='tatsu', description=DESCRIPTION, add_help=False,
)
main_mode = argparser.add_mutually_exclusive_group()
main_mode.add_argument(
'--generate-parser',
help='generate parser code from the grammar (default)',
action='store_true',
)
main_mode.add_argument(
'--draw',
'-d',
help='generate a diagram of the grammar (requires --outfile)',
action='store_true',
)
main_mode.add_argument(
'--object-model',
'-g',
help='generate object model from the class names given as rule arguments',
action='store_true',
)
main_mode.add_argument(
'--pretty',
'-p',
help='generate a prettified version of the input grammar',
action='store_true',
)
main_mode.add_argument(
'--pretty-lean',
help='like --pretty, but without name: or ::Parameter annotations',
action='store_true',
)
ebnf_opts = argparser.add_argument_group('parse-time options')
argparser.add_argument(
'filename',
metavar='GRAMMAR',
help='the filename of the TatSu grammar to parse',
)
ebnf_opts.add_argument(
'--color',
'-c',
help='use color in traces (requires the colorama library)',
action='store_true',
)
ebnf_opts.add_argument(
'--trace',
'-t',
help='produce verbose parsing output',
action='store_true',
)
generation_opts = argparser.add_argument_group('generation options')
generation_opts.add_argument(
'--no-left-recursion',
'-l',
help='turns left-recursion support off',
dest='left_recursion',
action='store_false',
default=True,
)
generation_opts.add_argument(
'--name',
'-m',
metavar='NAME',
help='Name for the grammar (defaults to GRAMMAR base name)',
)
generation_opts.add_argument(
'--no-nameguard',
'-n',
help='allow tokens that are prefixes of others',
dest='nameguard',
action='store_false',
default=None, # None allows grammar specified
)
generation_opts.add_argument(
'--outfile',
'--output',
'-o',
metavar='FILE',
help='output file (default is stdout)',
)
generation_opts.add_argument(
'--object-model-outfile',
'-G',
metavar='FILE',
help='generate object model and save to FILE',
)
generation_opts.add_argument(
'--whitespace',
'-w',
metavar='CHARACTERS',
help='characters to skip during parsing (use "" to disable)',
)
def import_class(path):
try:
spath = path.rsplit('.', 1)
module = importlib.import_module(spath[0])
return getattr(module, spath[1])
except Exception as e:
raise argparse.ArgumentTypeError(
f"Couldn't find class {path}",
) from e
generation_opts.add_argument(
'--base-type',
metavar='CLASSPATH',
help='class to use as base type for the object model, for example "mymodule.MyNode"',
type=import_class,
)
std_args = argparser.add_argument_group('common options')
std_args.add_argument(
'--help', '-h', help='show this help message and exit', action='help',
)
std_args.add_argument(
'--version',
'-V',
help='provide version information and exit',
action='version',
version=__version__,
)
args = argparser.parse_args()
if args.draw and not args.outfile:
argparser.error('--draw requires --outfile')
return args
__compiled_grammar_cache = {} # type: ignore[var-annotated]
def compile(
grammar,
name=None,
semantics=None,
asmodel=False,
config: ParserConfig | None = None,
**settings,
):
cache = __compiled_grammar_cache
key = (name, grammar, id(semantics))
if key in cache:
model = cache[key]
else:
gen = GrammarGenerator(name, config=config, **settings)
model = cache[key] = gen.parse(grammar, config=config, **settings)
if semantics is not None:
model.semantics = semantics
elif asmodel:
model.semantics = ModelBuilderSemantics()
return model
def parse(
grammar,
input,
start=None,
name=None,
semantics=None,
asmodel=False,
config: ParserConfig | None = None,
**settings,
):
model = compile(
grammar,
name=name,
semantics=semantics,
asmodel=asmodel,
config=config,
**settings,
)
return model.parse(
input, start=start, semantics=semantics, config=config, **settings,
)
def to_python_sourcecode(
grammar,
name=None,
filename=None,
config: ParserConfig | None = None,
**settings,
):
model = compile(
grammar, name=name, filename=filename, config=config, **settings,
)
return ngpythoncg(model)
def to_python_model(
grammar,
name=None,
filename=None,
base_type=None,
config: ParserConfig | None = None,
**settings,
):
model = compile(
grammar, name=name, filename=filename, config=config, **settings,
)
return ngobjectmodel.modelgen(model, base_type=base_type)
# for backwards compatibility. Use `compile()` instead
def genmodel(
name=None,
grammar=None,
semantics=None,
config: ParserConfig | None = None,
**settings,
):
if grammar is None:
raise ParseException('grammar is None')
return compile(
grammar, name=name, semantics=semantics, config=config, **settings,
)
def gencode(
name=None,
grammar=None,
trace=False,
filename=None,
codegen=ngpythoncg,
config: ParserConfig | None = None,
**settings,
):
model = compile(
grammar,
name=name,
filename=filename,
trace=trace,
config=config,
**settings,
)
return codegen(model)
def prepare_for_output(filename):
if filename:
filename = Path(filename)
if filename.is_file():
filename.unlink()
dirname = filename.parent
if dirname.exists():
dirname.mkdir(parents=True, exist_ok=True)
def save(filename, content):
with codecs.open(filename, 'w', encoding='utf-8') as f:
f.write(content)
def main():
args = parse_args()
if args.whitespace:
args.whitespace = eval_escapes(args.whitespace)
outfile = args.outfile
prepare_for_output(outfile)
prepare_for_output(args.object_model_outfile)
grammar = codecs.open(args.filename, encoding='utf-8').read()
try:
model = compile(
grammar,
args.name,
trace=args.trace,
filename=args.filename,
colorize=args.color,
)
model.whitespace = args.whitespace
model.nameguard = args.nameguard
model.left_recursion = args.left_recursion
if args.draw:
from tatsu import diagrams
diagrams.draw(outfile, model)
else:
if args.pretty:
result = model.pretty()
elif args.pretty_lean:
result = model.pretty_lean()
elif args.object_model:
result = ngobjectmodel.modelgen(model, base_type=args.base_type)
else:
result = ngpythoncg(model)
if outfile:
save(outfile, result)
else:
print(result)
# if requested, always save it
if args.object_model_outfile:
save(
args.object_model_outfile,
ngobjectmodel.modelgen(model, base_type=args.base_type),
)
print('-' * 72, file=sys.stderr)
print(
f'{len(grammar.split()):12,d} lines in grammar', file=sys.stderr,
)
print(f'{len(model.rules):12,d} rules in grammar', file=sys.stderr)
print(f'{model.nodecount():12,d} nodes in AST', file=sys.stderr)
except ParseException as e:
print(e, file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()
|