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.

541 lines
21 KiB

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