This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

459 líneas
18 KiB

  1. from __future__ import absolute_import
  2. import sys, os, pickle, hashlib
  3. from io import open
  4. from .utils import STRING_TYPE, Serialize, SerializeMemoizer, FS, isascii, logger
  5. from .load_grammar import load_grammar
  6. from .tree import Tree
  7. from .common import LexerConf, ParserConf
  8. from .lexer import Lexer, TraditionalLexer, TerminalDef, UnexpectedToken
  9. from .parse_tree_builder import ParseTreeBuilder
  10. from .parser_frontends import get_frontend, _get_lexer_callbacks
  11. from .grammar import Rule
  12. import re
  13. try:
  14. import regex
  15. except ImportError:
  16. regex = None
  17. ###{standalone
  18. class LarkOptions(Serialize):
  19. """Specifies the options for Lark
  20. """
  21. OPTIONS_DOC = """
  22. **=== General Options ===**
  23. start
  24. The start symbol. Either a string, or a list of strings for multiple possible starts (Default: "start")
  25. debug
  26. Display debug information, such as warnings (default: False)
  27. transformer
  28. Applies the transformer to every parse tree (equivlent to applying it after the parse, but faster)
  29. propagate_positions
  30. Propagates (line, column, end_line, end_column) attributes into all tree branches.
  31. maybe_placeholders
  32. When True, the ``[]`` operator returns ``None`` when not matched.
  33. When ``False``, ``[]`` behaves like the ``?`` operator, and returns no value at all.
  34. (default= ``False``. Recommended to set to ``True``)
  35. cache
  36. Cache the results of the Lark grammar analysis, for x2 to x3 faster loading. LALR only for now.
  37. - When ``False``, does nothing (default)
  38. - When ``True``, caches to a temporary file in the local directory
  39. - When given a string, caches to the path pointed by the string
  40. regex
  41. When True, uses the ``regex`` module instead of the stdlib ``re``.
  42. g_regex_flags
  43. Flags that are applied to all terminals (both regex and strings)
  44. keep_all_tokens
  45. Prevent the tree builder from automagically removing "punctuation" tokens (default: False)
  46. tree_class
  47. Lark will produce trees comprised of instances of this class instead of the default ``lark.Tree``.
  48. **=== Algorithm Options ===**
  49. parser
  50. Decides which parser engine to use. Accepts "earley" or "lalr". (Default: "earley").
  51. (there is also a "cyk" option for legacy)
  52. lexer
  53. Decides whether or not to use a lexer stage
  54. - "auto" (default): Choose for me based on the parser
  55. - "standard": Use a standard lexer
  56. - "contextual": Stronger lexer (only works with parser="lalr")
  57. - "dynamic": Flexible and powerful (only with parser="earley")
  58. - "dynamic_complete": Same as dynamic, but tries *every* variation of tokenizing possible.
  59. ambiguity
  60. Decides how to handle ambiguity in the parse. Only relevant if parser="earley"
  61. - "resolve": The parser will automatically choose the simplest derivation
  62. (it chooses consistently: greedy for tokens, non-greedy for rules)
  63. - "explicit": The parser will return all derivations wrapped in "_ambig" tree nodes (i.e. a forest).
  64. - "forest": The parser will return the root of the shared packed parse forest.
  65. **=== Misc. / Domain Specific Options ===**
  66. postlex
  67. Lexer post-processing (Default: None) Only works with the standard and contextual lexers.
  68. priority
  69. How priorities should be evaluated - auto, none, normal, invert (Default: auto)
  70. lexer_callbacks
  71. Dictionary of callbacks for the lexer. May alter tokens during lexing. Use with caution.
  72. use_bytes
  73. Accept an input of type ``bytes`` instead of ``str`` (Python 3 only).
  74. edit_terminals
  75. A callback for editing the terminals before parse.
  76. import_sources
  77. A List of either paths or loader functions to specify from where grammars are imported
  78. **=== End Options ===**
  79. """
  80. if __doc__:
  81. __doc__ += OPTIONS_DOC
  82. _defaults = {
  83. 'debug': False,
  84. 'keep_all_tokens': False,
  85. 'tree_class': None,
  86. 'cache': False,
  87. 'postlex': None,
  88. 'parser': 'earley',
  89. 'lexer': 'auto',
  90. 'transformer': None,
  91. 'start': 'start',
  92. 'priority': 'auto',
  93. 'ambiguity': 'auto',
  94. 'regex': False,
  95. 'propagate_positions': False,
  96. 'lexer_callbacks': {},
  97. 'maybe_placeholders': False,
  98. 'edit_terminals': None,
  99. 'g_regex_flags': 0,
  100. 'use_bytes': False,
  101. 'import_sources': [],
  102. }
  103. def __init__(self, options_dict):
  104. o = dict(options_dict)
  105. options = {}
  106. for name, default in self._defaults.items():
  107. if name in o:
  108. value = o.pop(name)
  109. if isinstance(default, bool) and name not in ('cache', 'use_bytes'):
  110. value = bool(value)
  111. else:
  112. value = default
  113. options[name] = value
  114. if isinstance(options['start'], STRING_TYPE):
  115. options['start'] = [options['start']]
  116. self.__dict__['options'] = options
  117. assert self.parser in ('earley', 'lalr', 'cyk', None)
  118. if self.parser == 'earley' and self.transformer:
  119. raise ValueError('Cannot specify an embedded transformer when using the Earley algorithm.'
  120. 'Please use your transformer on the resulting parse tree, or use a different algorithm (i.e. LALR)')
  121. if o:
  122. raise ValueError("Unknown options: %s" % o.keys())
  123. def __getattr__(self, name):
  124. try:
  125. return self.options[name]
  126. except KeyError as e:
  127. raise AttributeError(e)
  128. def __setattr__(self, name, value):
  129. assert name in self.options
  130. self.options[name] = value
  131. def serialize(self, memo):
  132. return self.options
  133. @classmethod
  134. def deserialize(cls, data, memo):
  135. return cls(data)
  136. class Lark(Serialize):
  137. """Main interface for the library.
  138. It's mostly a thin wrapper for the many different parsers, and for the tree constructor.
  139. Parameters:
  140. grammar: a string or file-object containing the grammar spec (using Lark's ebnf syntax)
  141. options: a dictionary controlling various aspects of Lark.
  142. Example:
  143. >>> Lark(r'''start: "foo" ''')
  144. Lark(...)
  145. """
  146. def __init__(self, grammar, **options):
  147. self.options = LarkOptions(options)
  148. # Set regex or re module
  149. use_regex = self.options.regex
  150. if use_regex:
  151. if regex:
  152. re_module = regex
  153. else:
  154. raise ImportError('`regex` module must be installed if calling `Lark(regex=True)`.')
  155. else:
  156. re_module = re
  157. # Some, but not all file-like objects have a 'name' attribute
  158. try:
  159. self.source = grammar.name
  160. except AttributeError:
  161. self.source = '<string>'
  162. # Drain file-like objects to get their contents
  163. try:
  164. read = grammar.read
  165. except AttributeError:
  166. pass
  167. else:
  168. grammar = read()
  169. assert isinstance(grammar, STRING_TYPE)
  170. self.grammar_source = grammar
  171. if self.options.use_bytes:
  172. if not isascii(grammar):
  173. raise ValueError("Grammar must be ascii only, when use_bytes=True")
  174. if sys.version_info[0] == 2 and self.options.use_bytes != 'force':
  175. raise NotImplementedError("`use_bytes=True` may have issues on python2."
  176. "Use `use_bytes='force'` to use it at your own risk.")
  177. cache_fn = None
  178. if self.options.cache:
  179. if self.options.parser != 'lalr':
  180. raise NotImplementedError("cache only works with parser='lalr' for now")
  181. if isinstance(self.options.cache, STRING_TYPE):
  182. cache_fn = self.options.cache
  183. else:
  184. if self.options.cache is not True:
  185. raise ValueError("cache argument must be bool or str")
  186. unhashable = ('transformer', 'postlex', 'lexer_callbacks', 'edit_terminals')
  187. from . import __version__
  188. options_str = ''.join(k+str(v) for k, v in options.items() if k not in unhashable)
  189. s = grammar + options_str + __version__
  190. md5 = hashlib.md5(s.encode()).hexdigest()
  191. cache_fn = '.lark_cache_%s.tmp' % md5
  192. if FS.exists(cache_fn):
  193. logger.debug('Loading grammar from cache: %s', cache_fn)
  194. with FS.open(cache_fn, 'rb') as f:
  195. self._load(f, self.options.transformer, self.options.postlex)
  196. return
  197. if self.options.lexer == 'auto':
  198. if self.options.parser == 'lalr':
  199. self.options.lexer = 'contextual'
  200. elif self.options.parser == 'earley':
  201. self.options.lexer = 'dynamic'
  202. elif self.options.parser == 'cyk':
  203. self.options.lexer = 'standard'
  204. else:
  205. assert False, self.options.parser
  206. lexer = self.options.lexer
  207. assert lexer in ('standard', 'contextual', 'dynamic', 'dynamic_complete') or issubclass(lexer, Lexer)
  208. if self.options.ambiguity == 'auto':
  209. if self.options.parser == 'earley':
  210. self.options.ambiguity = 'resolve'
  211. else:
  212. disambig_parsers = ['earley', 'cyk']
  213. assert self.options.parser in disambig_parsers, (
  214. 'Only %s supports disambiguation right now') % ', '.join(disambig_parsers)
  215. if self.options.priority == 'auto':
  216. if self.options.parser in ('earley', 'cyk', ):
  217. self.options.priority = 'normal'
  218. elif self.options.parser in ('lalr', ):
  219. self.options.priority = None
  220. elif self.options.priority in ('invert', 'normal'):
  221. assert self.options.parser in ('earley', 'cyk'), "priorities are not supported for LALR at this time"
  222. assert self.options.priority in ('auto', None, 'normal', 'invert'), 'invalid priority option specified: {}. options are auto, none, normal, invert.'.format(self.options.priority)
  223. assert self.options.ambiguity not in ('resolve__antiscore_sum', ), 'resolve__antiscore_sum has been replaced with the option priority="invert"'
  224. assert self.options.ambiguity in ('resolve', 'explicit', 'forest', 'auto', )
  225. # Parse the grammar file and compose the grammars (TODO)
  226. self.grammar = load_grammar(grammar, self.source, re_module, self.options.import_sources)
  227. # Compile the EBNF grammar into BNF
  228. self.terminals, self.rules, self.ignore_tokens = self.grammar.compile(self.options.start)
  229. if self.options.edit_terminals:
  230. for t in self.terminals:
  231. self.options.edit_terminals(t)
  232. self._terminals_dict = {t.name: t for t in self.terminals}
  233. # If the user asked to invert the priorities, negate them all here.
  234. # This replaces the old 'resolve__antiscore_sum' option.
  235. if self.options.priority == 'invert':
  236. for rule in self.rules:
  237. if rule.options.priority is not None:
  238. rule.options.priority = -rule.options.priority
  239. # Else, if the user asked to disable priorities, strip them from the
  240. # rules. This allows the Earley parsers to skip an extra forest walk
  241. # for improved performance, if you don't need them (or didn't specify any).
  242. elif self.options.priority == None:
  243. for rule in self.rules:
  244. if rule.options.priority is not None:
  245. rule.options.priority = None
  246. # TODO Deprecate lexer_callbacks?
  247. lexer_callbacks = (_get_lexer_callbacks(self.options.transformer, self.terminals)
  248. if self.options.transformer
  249. else {})
  250. lexer_callbacks.update(self.options.lexer_callbacks)
  251. self.lexer_conf = LexerConf(self.terminals, re_module, self.ignore_tokens, self.options.postlex, lexer_callbacks, self.options.g_regex_flags, use_bytes=self.options.use_bytes)
  252. if self.options.parser:
  253. self.parser = self._build_parser()
  254. elif lexer:
  255. self.lexer = self._build_lexer()
  256. if cache_fn:
  257. logger.debug('Saving grammar to cache: %s', cache_fn)
  258. with FS.open(cache_fn, 'wb') as f:
  259. self.save(f)
  260. __doc__ += "\n\n" + LarkOptions.OPTIONS_DOC
  261. __serialize_fields__ = 'parser', 'rules', 'options'
  262. def _build_lexer(self):
  263. return TraditionalLexer(self.lexer_conf)
  264. def _prepare_callbacks(self):
  265. self.parser_class = get_frontend(self.options.parser, self.options.lexer)
  266. self._callbacks = None
  267. # we don't need these callbacks if we aren't building a tree
  268. if self.options.ambiguity != 'forest':
  269. self._parse_tree_builder = ParseTreeBuilder(self.rules, self.options.tree_class or Tree, self.options.propagate_positions, self.options.keep_all_tokens, self.options.parser!='lalr' and self.options.ambiguity=='explicit', self.options.maybe_placeholders)
  270. self._callbacks = self._parse_tree_builder.create_callback(self.options.transformer)
  271. def _build_parser(self):
  272. self._prepare_callbacks()
  273. parser_conf = ParserConf(self.rules, self._callbacks, self.options.start)
  274. return self.parser_class(self.lexer_conf, parser_conf, options=self.options)
  275. def save(self, f):
  276. """Saves the instance into the given file object
  277. Useful for caching and multiprocessing.
  278. """
  279. data, m = self.memo_serialize([TerminalDef, Rule])
  280. pickle.dump({'data': data, 'memo': m}, f)
  281. @classmethod
  282. def load(cls, f):
  283. """Loads an instance from the given file object
  284. Useful for caching and multiprocessing.
  285. """
  286. inst = cls.__new__(cls)
  287. return inst._load(f)
  288. def _load(self, f, transformer=None, postlex=None):
  289. if isinstance(f, dict):
  290. d = f
  291. else:
  292. d = pickle.load(f)
  293. memo = d['memo']
  294. data = d['data']
  295. assert memo
  296. memo = SerializeMemoizer.deserialize(memo, {'Rule': Rule, 'TerminalDef': TerminalDef}, {})
  297. options = dict(data['options'])
  298. if transformer is not None:
  299. options['transformer'] = transformer
  300. if postlex is not None:
  301. options['postlex'] = postlex
  302. self.options = LarkOptions.deserialize(options, memo)
  303. re_module = regex if self.options.regex else re
  304. self.rules = [Rule.deserialize(r, memo) for r in data['rules']]
  305. self.source = '<deserialized>'
  306. self._prepare_callbacks()
  307. self.parser = self.parser_class.deserialize(
  308. data['parser'],
  309. memo,
  310. self._callbacks,
  311. self.options.postlex,
  312. self.options.transformer,
  313. re_module
  314. )
  315. self.terminals = self.parser.lexer_conf.tokens
  316. self._terminals_dict = {t.name: t for t in self.terminals}
  317. return self
  318. @classmethod
  319. def _load_from_dict(cls, data, memo, transformer=None, postlex=None):
  320. inst = cls.__new__(cls)
  321. return inst._load({'data': data, 'memo': memo}, transformer, postlex)
  322. @classmethod
  323. def open(cls, grammar_filename, rel_to=None, **options):
  324. """Create an instance of Lark with the grammar given by its filename
  325. If ``rel_to`` is provided, the function will find the grammar filename in relation to it.
  326. Example:
  327. >>> Lark.open("grammar_file.lark", rel_to=__file__, parser="lalr")
  328. Lark(...)
  329. """
  330. if rel_to:
  331. basepath = os.path.dirname(rel_to)
  332. grammar_filename = os.path.join(basepath, grammar_filename)
  333. with open(grammar_filename, encoding='utf8') as f:
  334. return cls(f, **options)
  335. def __repr__(self):
  336. return 'Lark(open(%r), parser=%r, lexer=%r, ...)' % (self.source, self.options.parser, self.options.lexer)
  337. def lex(self, text):
  338. "Only lex (and postlex) the text, without parsing it. Only relevant when lexer='standard'"
  339. if not hasattr(self, 'lexer'):
  340. self.lexer = self._build_lexer()
  341. stream = self.lexer.lex(text)
  342. if self.options.postlex:
  343. return self.options.postlex.process(stream)
  344. return stream
  345. def get_terminal(self, name):
  346. "Get information about a terminal"
  347. return self._terminals_dict[name]
  348. def parse(self, text, start=None, on_error=None):
  349. """Parse the given text, according to the options provided.
  350. Parameters:
  351. text (str): Text to be parsed.
  352. start (str, optional): Required if Lark was given multiple possible start symbols (using the start option).
  353. on_error (function, optional): if provided, will be called on UnexpectedToken error. Return true to resume parsing.
  354. LALR only. See examples/error_puppet.py for an example of how to use on_error.
  355. Returns:
  356. If a transformer is supplied to ``__init__``, returns whatever is the
  357. result of the transformation. Otherwise, returns a Tree instance.
  358. """
  359. try:
  360. return self.parser.parse(text, start=start)
  361. except UnexpectedToken as e:
  362. if on_error is None:
  363. raise
  364. while True:
  365. if not on_error(e):
  366. raise e
  367. try:
  368. return e.puppet.resume_parse()
  369. except UnexpectedToken as e2:
  370. if e.token.type == e2.token.type == '$END' and e.puppet == e2.puppet:
  371. # Prevent infinite loop
  372. raise e2
  373. e = e2
  374. ###}