This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.2 KiB

  1. ###{standalone
  2. #
  3. #
  4. # Lark Stand-alone Generator Tool
  5. # ----------------------------------
  6. # Generates a stand-alone LALR(1) parser with a standard lexer
  7. #
  8. # Git: https://github.com/erezsh/lark
  9. # Author: Erez Shinan (erezshin@gmail.com)
  10. #
  11. #
  12. # >>> LICENSE
  13. #
  14. # This tool and its generated code use a separate license from Lark,
  15. # and are subject to the terms of the Mozilla Public License, v. 2.0.
  16. # If a copy of the MPL was not distributed with this
  17. # file, You can obtain one at https://mozilla.org/MPL/2.0/.
  18. #
  19. # If you wish to purchase a commercial license for this tool and its
  20. # generated code, you may contact me via email or otherwise.
  21. #
  22. # If MPL2 is incompatible with your free or open-source project,
  23. # contact me and we'll work it out.
  24. #
  25. #
  26. import os
  27. from io import open
  28. ###}
  29. import codecs
  30. import sys
  31. import os
  32. from pprint import pprint
  33. from os import path
  34. from collections import defaultdict
  35. import lark
  36. from lark import Lark
  37. from lark.parsers.lalr_analysis import Reduce
  38. from lark.grammar import RuleOptions, Rule
  39. from lark.lexer import TerminalDef
  40. _dir = path.dirname(__file__)
  41. _larkdir = path.join(_dir, path.pardir)
  42. EXTRACT_STANDALONE_FILES = [
  43. 'tools/standalone.py',
  44. 'exceptions.py',
  45. 'utils.py',
  46. 'tree.py',
  47. 'visitors.py',
  48. 'indenter.py',
  49. 'grammar.py',
  50. 'lexer.py',
  51. 'common.py',
  52. 'parse_tree_builder.py',
  53. 'parsers/lalr_parser.py',
  54. 'parsers/lalr_analysis.py',
  55. 'parser_frontends.py',
  56. 'lark.py',
  57. ]
  58. def extract_sections(lines):
  59. section = None
  60. text = []
  61. sections = defaultdict(list)
  62. for l in lines:
  63. if l.startswith('###'):
  64. if l[3] == '{':
  65. section = l[4:].strip()
  66. elif l[3] == '}':
  67. sections[section] += text
  68. section = None
  69. text = []
  70. else:
  71. raise ValueError(l)
  72. elif section:
  73. text.append(l)
  74. return {name:''.join(text) for name, text in sections.items()}
  75. def main(fobj, start):
  76. lark_inst = Lark(fobj, parser="lalr", lexer="contextual", start=start)
  77. print('# The file was automatically generated by Lark v%s' % lark.__version__)
  78. for pyfile in EXTRACT_STANDALONE_FILES:
  79. with open(os.path.join(_larkdir, pyfile)) as f:
  80. print (extract_sections(f)['standalone'])
  81. data, m = lark_inst.memo_serialize([TerminalDef, Rule])
  82. print( 'DATA = (' )
  83. # pprint(data, width=160)
  84. print(data)
  85. print(')')
  86. print( 'MEMO = (')
  87. print(m)
  88. print(')')
  89. print('Shift = 0')
  90. print('Reduce = 1')
  91. print("def Lark_StandAlone(transformer=None, postlex=None):")
  92. print(" namespace = {'Rule': Rule, 'TerminalDef': TerminalDef}")
  93. print(" return Lark.deserialize(DATA, namespace, MEMO, transformer=transformer, postlex=postlex)")
  94. if __name__ == '__main__':
  95. if len(sys.argv) < 2:
  96. print("Lark Stand-alone Generator Tool")
  97. print("Usage: python -m lark.tools.standalone <grammar-file> [<start>]")
  98. sys.exit(1)
  99. if len(sys.argv) == 3:
  100. fn, start = sys.argv[1:]
  101. elif len(sys.argv) == 2:
  102. fn, start = sys.argv[1], 'start'
  103. else:
  104. assert False, sys.argv
  105. with codecs.open(fn, encoding='utf8') as f:
  106. main(f, start)