xdr_parse.py 826 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python3
  2. # ex: set filetype=python:
  3. """Common parsing code for xdrgen"""
  4. from lark import Lark
  5. # Set to True to emit annotation comments in generated source
  6. annotate = False
  7. def set_xdr_annotate(set_it: bool) -> None:
  8. """Set 'annotate' if --annotate was specified on the command line"""
  9. global annotate
  10. annotate = set_it
  11. def get_xdr_annotate() -> bool:
  12. """Return True if --annotate was specified on the command line"""
  13. return annotate
  14. def xdr_parser() -> Lark:
  15. """Return a Lark parser instance configured with the XDR language grammar"""
  16. return Lark.open(
  17. "grammars/xdr.lark",
  18. rel_to=__file__,
  19. start="specification",
  20. debug=True,
  21. strict=True,
  22. propagate_positions=True,
  23. parser="lalr",
  24. lexer="contextual",
  25. )