(self, grammar: 'Union[Grammar, str, IO[str]]', **options)
| 265 | terminals: Collection[TerminalDef] |
| 266 | |
| 267 | def __init__(self, grammar: 'Union[Grammar, str, IO[str]]', **options) -> None: |
| 268 | self.options = LarkOptions(options) |
| 269 | re_module: types.ModuleType |
| 270 | |
| 271 | # Set regex or re module |
| 272 | use_regex = self.options.regex |
| 273 | if use_regex: |
| 274 | if _has_regex: |
| 275 | re_module = regex |
| 276 | else: |
| 277 | raise ImportError('`regex` module must be installed if calling `Lark(regex=True)`.') |
| 278 | else: |
| 279 | re_module = re |
| 280 | |
| 281 | # Some, but not all file-like objects have a 'name' attribute |
| 282 | if self.options.source_path is None: |
| 283 | try: |
| 284 | self.source_path = grammar.name # type: ignore[union-attr] |
| 285 | except AttributeError: |
| 286 | self.source_path = '<string>' |
| 287 | else: |
| 288 | self.source_path = self.options.source_path |
| 289 | |
| 290 | # Drain file-like objects to get their contents |
| 291 | try: |
| 292 | read = grammar.read # type: ignore[union-attr] |
| 293 | except AttributeError: |
| 294 | pass |
| 295 | else: |
| 296 | grammar = read() |
| 297 | |
| 298 | cache_fn = None |
| 299 | cache_sha256 = None |
| 300 | if isinstance(grammar, str): |
| 301 | self.source_grammar = grammar |
| 302 | if self.options.use_bytes: |
| 303 | if not grammar.isascii(): |
| 304 | raise ConfigurationError("Grammar must be ascii only, when use_bytes=True") |
| 305 | |
| 306 | if self.options.cache: |
| 307 | if self.options.parser != 'lalr': |
| 308 | raise ConfigurationError("cache only works with parser='lalr' for now") |
| 309 | |
| 310 | unhashable = ('transformer', 'postlex', 'lexer_callbacks', 'edit_terminals', '_plugins') |
| 311 | options_str = ''.join(k+str(v) for k, v in options.items() if k not in unhashable) |
| 312 | from . import __version__ |
| 313 | s = grammar + options_str + __version__ + str(sys.version_info[:2]) |
| 314 | cache_sha256 = sha256_digest(s) |
| 315 | |
| 316 | if isinstance(self.options.cache, str): |
| 317 | cache_fn = self.options.cache |
| 318 | else: |
| 319 | if self.options.cache is not True: |
| 320 | raise ConfigurationError("cache argument must be bool or str") |
| 321 | |
| 322 | try: |
| 323 | username = getpass.getuser() |
| 324 | except Exception: |
nothing calls this directly
no test coverage detected