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.

141 lines
3.6 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. #
  16. # It is licensed under GPLv2 or above.
  17. #
  18. # If you wish to purchase a commercial license for this tool and its
  19. # generated code, contact me via email.
  20. #
  21. # If GPL is incompatible with your free or open-source project,
  22. # contact me and we'll work it out (for free).
  23. #
  24. # This program is free software: you can redistribute it and/or modify
  25. # it under the terms of the GNU General Public License as published by
  26. # the Free Software Foundation, either version 2 of the License, or
  27. # (at your option) any later version.
  28. #
  29. # This program is distributed in the hope that it will be useful,
  30. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. # GNU General Public License for more details.
  33. #
  34. # See <http://www.gnu.org/licenses/>.
  35. #
  36. #
  37. import os
  38. from io import open
  39. ###}
  40. import pprint
  41. import codecs
  42. import sys
  43. import os
  44. from pprint import pprint
  45. from os import path
  46. from collections import defaultdict
  47. import lark
  48. from lark import Lark
  49. from lark.parsers.lalr_analysis import Reduce
  50. from lark.grammar import RuleOptions, Rule
  51. from lark.lexer import TerminalDef
  52. _dir = path.dirname(__file__)
  53. _larkdir = path.join(_dir, path.pardir)
  54. EXTRACT_STANDALONE_FILES = [
  55. 'tools/standalone.py',
  56. 'exceptions.py',
  57. 'utils.py',
  58. 'tree.py',
  59. 'visitors.py',
  60. 'indenter.py',
  61. 'grammar.py',
  62. 'lexer.py',
  63. 'common.py',
  64. 'parse_tree_builder.py',
  65. 'parsers/lalr_parser.py',
  66. 'parsers/lalr_analysis.py',
  67. 'parser_frontends.py',
  68. 'lark.py',
  69. ]
  70. def extract_sections(lines):
  71. section = None
  72. text = []
  73. sections = defaultdict(list)
  74. for l in lines:
  75. if l.startswith('###'):
  76. if l[3] == '{':
  77. section = l[4:].strip()
  78. elif l[3] == '}':
  79. sections[section] += text
  80. section = None
  81. text = []
  82. else:
  83. raise ValueError(l)
  84. elif section:
  85. text.append(l)
  86. return {name:''.join(text) for name, text in sections.items()}
  87. def main(fobj, start):
  88. lark_inst = Lark(fobj, parser="lalr", lexer="contextual", start=start)
  89. print('# The file was automatically generated by Lark v%s' % lark.__version__)
  90. for pyfile in EXTRACT_STANDALONE_FILES:
  91. with open(os.path.join(_larkdir, pyfile)) as f:
  92. print (extract_sections(f)['standalone'])
  93. data, m = lark_inst.memo_serialize([TerminalDef, Rule])
  94. print( 'DATA = (' )
  95. # pprint(data, width=160)
  96. print(data)
  97. print(')')
  98. print( 'MEMO = (')
  99. print(m)
  100. print(')')
  101. print('Shift = 0')
  102. print('Reduce = 1')
  103. print("def Lark_StandAlone(transformer=None, postlex=None):")
  104. print(" namespace = {'Rule': Rule, 'TerminalDef': TerminalDef}")
  105. print(" return Lark.deserialize(DATA, namespace, MEMO, transformer=transformer, postlex=postlex)")
  106. if __name__ == '__main__':
  107. if len(sys.argv) < 2:
  108. print("Lark Stand-alone Generator Tool")
  109. print("Usage: python -m lark.tools.standalone <grammar-file> [<start>]")
  110. sys.exit(1)
  111. if len(sys.argv) == 3:
  112. fn, start = sys.argv[1:]
  113. elif len(sys.argv) == 2:
  114. fn, start = sys.argv[1], 'start'
  115. else:
  116. assert False, sys.argv
  117. with codecs.open(fn, encoding='utf8') as f:
  118. main(f, start)