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.

171 lines
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. predict_all=options.earley__predict_all
  100. )
  101. def _prepare_expansion(self, expansion):
  102. for sym in expansion:
  103. if is_terminal(sym):
  104. regexp = self.token_by_name[sym].pattern.to_regexp()
  105. width = sre_parse.parse(regexp).getwidth()
  106. assert width
  107. yield Terminal_Regexp(sym, regexp)
  108. else:
  109. yield sym
  110. def parse(self, text):
  111. return self.parser.parse(text)
  112. def get_frontend(parser, lexer):
  113. if parser=='lalr':
  114. if lexer is None:
  115. raise ValueError('The LALR parser requires use of a lexer')
  116. elif lexer == 'standard':
  117. return LALR
  118. elif lexer == 'contextual':
  119. return LALR_ContextualLexer
  120. else:
  121. raise ValueError('Unknown lexer: %s' % lexer)
  122. elif parser=='earley':
  123. if lexer is None:
  124. return Earley_NoLex
  125. elif lexer=='standard':
  126. return Earley
  127. elif lexer=='dynamic':
  128. return XEarley
  129. elif lexer=='contextual':
  130. raise ValueError('The Earley parser does not support the contextual parser')
  131. else:
  132. raise ValueError('Unknown lexer: %s' % lexer)
  133. else:
  134. raise ValueError('Unknown parser: %s' % parser)