This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

170 рядки
6.0 KiB

  1. import re
  2. import sre_parse
  3. from .lexer import Lexer, ContextualLexer, Token
  4. from .common import is_terminal, GrammarError, ParserConf, Terminal_Regexp, Terminal_Token
  5. from .parsers import lalr_parser, earley, xearley, resolve_ambig
  6. class WithLexer:
  7. def __init__(self, lexer_conf):
  8. self.lexer_conf = lexer_conf
  9. self.lexer = Lexer(lexer_conf.tokens, ignore=lexer_conf.ignore)
  10. def lex(self, text):
  11. stream = self.lexer.lex(text)
  12. if self.lexer_conf.postlex:
  13. return self.lexer_conf.postlex.process(stream)
  14. else:
  15. return stream
  16. class LALR(WithLexer):
  17. def __init__(self, lexer_conf, parser_conf, options=None):
  18. WithLexer.__init__(self, lexer_conf)
  19. self.parser_conf = parser_conf
  20. self.parser = lalr_parser.Parser(parser_conf)
  21. def parse(self, text):
  22. tokens = self.lex(text)
  23. return self.parser.parse(tokens)
  24. class LALR_ContextualLexer:
  25. def __init__(self, lexer_conf, parser_conf, options=None):
  26. self.lexer_conf = lexer_conf
  27. self.parser_conf = parser_conf
  28. self.parser = lalr_parser.Parser(parser_conf)
  29. d = {idx:t.keys() for idx, t in self.parser.analysis.states_idx.items()}
  30. always_accept = lexer_conf.postlex.always_accept if lexer_conf.postlex else ()
  31. self.lexer = ContextualLexer(lexer_conf.tokens, d, ignore=lexer_conf.ignore, always_accept=always_accept)
  32. def parse(self, text):
  33. tokens = self.lexer.lex(text)
  34. if self.lexer_conf.postlex:
  35. tokens = self.lexer_conf.postlex.process(tokens)
  36. return self.parser.parse(tokens, self.lexer.set_parser_state)
  37. def get_ambiguity_resolver(options):
  38. if not options or options.ambiguity == 'resolve':
  39. return resolve_ambig.standard_resolve_ambig
  40. elif options.ambiguity == 'resolve__antiscore_sum':
  41. return resolve_ambig.antiscore_sum_resolve_ambig
  42. elif options.ambiguity == 'explicit':
  43. return None
  44. raise ValueError(options)
  45. def tokenize_text(text):
  46. new_text = []
  47. line = 1
  48. col_start_pos = 0
  49. for i, ch in enumerate(text):
  50. if '\n' in ch:
  51. line += ch.count('\n')
  52. col_start_pos = i + ch.rindex('\n')
  53. new_text.append(Token('CHAR', ch, line=line, column=i - col_start_pos))
  54. return new_text
  55. class Earley_NoLex:
  56. def __init__(self, lexer_conf, parser_conf, options=None):
  57. self.token_by_name = {t.name:t for t in lexer_conf.tokens}
  58. rules = [(n, list(self._prepare_expansion(x)), a, o) for n,x,a,o in parser_conf.rules]
  59. self.parser = earley.Parser(rules,
  60. parser_conf.start,
  61. parser_conf.callback,
  62. resolve_ambiguity=get_ambiguity_resolver(options))
  63. def _prepare_expansion(self, expansion):
  64. for sym in expansion:
  65. if is_terminal(sym):
  66. regexp = self.token_by_name[sym].pattern.to_regexp()
  67. width = sre_parse.parse(regexp).getwidth()
  68. if width != (1,1):
  69. raise GrammarError('Scanless parsing (lexer=None) requires all tokens to have a width of 1 (terminal %s: %s is %s)' % (sym, regexp, width))
  70. yield Terminal_Regexp(sym, regexp)
  71. else:
  72. yield sym
  73. def parse(self, text):
  74. new_text = tokenize_text(text)
  75. return self.parser.parse(new_text)
  76. class Earley(WithLexer):
  77. def __init__(self, lexer_conf, parser_conf, options=None):
  78. WithLexer.__init__(self, lexer_conf)
  79. rules = [(n, self._prepare_expansion(x), a, o) for n,x,a,o in parser_conf.rules]
  80. self.parser = earley.Parser(rules,
  81. parser_conf.start,
  82. parser_conf.callback,
  83. resolve_ambiguity=get_ambiguity_resolver(options))
  84. def _prepare_expansion(self, expansion):
  85. return [Terminal_Token(sym) if is_terminal(sym) else sym for sym in expansion]
  86. def parse(self, text):
  87. tokens = self.lex(text)
  88. return self.parser.parse(tokens)
  89. class XEarley:
  90. def __init__(self, lexer_conf, parser_conf, options=None):
  91. self.token_by_name = {t.name:t for t in lexer_conf.tokens}
  92. rules = [(n, list(self._prepare_expansion(x)), a, o) for n,x,a,o in parser_conf.rules]
  93. ignore = [Terminal_Regexp(x, self.token_by_name[x].pattern.to_regexp()) for x in lexer_conf.ignore]
  94. self.parser = xearley.Parser(rules,
  95. parser_conf.start,
  96. parser_conf.callback,
  97. resolve_ambiguity=get_ambiguity_resolver(options),
  98. ignore=ignore,
  99. )
  100. def _prepare_expansion(self, expansion):
  101. for sym in expansion:
  102. if is_terminal(sym):
  103. regexp = self.token_by_name[sym].pattern.to_regexp()
  104. width = sre_parse.parse(regexp).getwidth()
  105. assert width
  106. yield Terminal_Regexp(sym, regexp)
  107. else:
  108. yield sym
  109. def parse(self, text):
  110. return self.parser.parse(text)
  111. def get_frontend(parser, lexer):
  112. if parser=='lalr':
  113. if lexer is None:
  114. raise ValueError('The LALR parser requires use of a lexer')
  115. elif lexer == 'standard':
  116. return LALR
  117. elif lexer == 'contextual':
  118. return LALR_ContextualLexer
  119. else:
  120. raise ValueError('Unknown lexer: %s' % lexer)
  121. elif parser=='earley':
  122. if lexer is None:
  123. return Earley_NoLex
  124. elif lexer=='standard':
  125. return Earley
  126. elif lexer=='dynamic':
  127. return XEarley
  128. elif lexer=='contextual':
  129. raise ValueError('The Earley parser does not support the contextual parser')
  130. else:
  131. raise ValueError('Unknown lexer: %s' % lexer)
  132. else:
  133. raise ValueError('Unknown parser: %s' % parser)