From 209a3fe8fd1b1f6cd9267c84178cfbfb496065a3 Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Wed, 25 Apr 2018 01:54:16 +0300 Subject: [PATCH] Interface improvements for the Lark instance --- examples/python_parser.py | 18 +++++++----------- lark/lark.py | 30 ++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/examples/python_parser.py b/examples/python_parser.py index d953a79..ddbd5c4 100644 --- a/examples/python_parser.py +++ b/examples/python_parser.py @@ -10,7 +10,7 @@ import glob, time from lark import Lark from lark.indenter import Indenter -__path__ = os.path.dirname(__file__) +# __path__ = os.path.dirname(__file__) class PythonIndenter(Indenter): NL_type = '_NEWLINE' @@ -20,18 +20,14 @@ class PythonIndenter(Indenter): DEDENT_type = '_DEDENT' tab_len = 8 +kwargs = dict(rel_to=__file__, postlex=PythonIndenter(), start='file_input') -grammar2_filename = os.path.join(__path__, 'python2.g') -grammar3_filename = os.path.join(__path__, 'python3.g') -with open(grammar2_filename) as f: - python_parser2 = Lark(f, parser='lalr', postlex=PythonIndenter(), start='file_input') -with open(grammar3_filename) as f: - python_parser3 = Lark(f, parser='lalr', postlex=PythonIndenter(), start='file_input') +python_parser2 = Lark.open('python2.g', parser='lalr', **kwargs) +python_parser3 = Lark.open('python3.g',parser='lalr', **kwargs) +python_parser2_earley = Lark.open('python2.g', parser='earley', lexer='standard', **kwargs) +print(python_parser3) -with open(grammar2_filename) as f: - python_parser2_earley = Lark(f, parser='lalr', lexer='standard', postlex=PythonIndenter(), start='file_input') - def _read(fn, *args): kwargs = {'encoding': 'iso-8859-1'} with open(fn, *args, **kwargs) as f: @@ -82,6 +78,6 @@ def test_earley_equals_lalr(): if __name__ == '__main__': test_python_lib() - # test_earley_equals_lalr() + test_earley_equals_lalr() # python_parser3.parse(_read(sys.argv[1]) + '\n') diff --git a/lark/lark.py b/lark/lark.py index 2660bd7..3641a40 100644 --- a/lark/lark.py +++ b/lark/lark.py @@ -3,6 +3,7 @@ from __future__ import absolute_import import os import time from collections import defaultdict +from io import open from .utils import STRING_TYPE from .load_grammar import load_grammar @@ -105,12 +106,12 @@ class Lark: # Some, but not all file-like objects have a 'name' attribute try: - source = grammar.name + self.source = grammar.name except AttributeError: - source = '' + self.source = '' cache_file = "larkcache_%s" % str(hash(grammar)%(2**32)) else: - cache_file = "larkcache_%s" % os.path.basename(source) + cache_file = "larkcache_%s" % os.path.basename(self.source) # Drain file-like objects to get their contents try: @@ -150,7 +151,7 @@ class Lark: assert self.options.ambiguity in ('resolve', 'explicit', 'auto', 'resolve__antiscore_sum') # Parse the grammar file and compose the grammars (TODO) - self.grammar = load_grammar(grammar, source) + self.grammar = load_grammar(grammar, self.source) # Compile the EBNF grammar into BNF tokens, self.rules, self.ignore_tokens = self.grammar.compile(lexer=bool(lexer), start=self.options.start) @@ -183,6 +184,27 @@ class Lark: return self.parser_class(self.lexer_conf, parser_conf, options=self.options) + @classmethod + def open(cls, grammar_filename, rel_to=None, **options): + """Create an instance of Lark with the grammar given by its filename + + If rel_to is provided, the function will find the grammar filename in relation to it. + + Example: + + >>> Lark.open("grammar_file.g", rel_to=__file__, parser="lalr") + Lark(...) + + """ + if rel_to: + basepath = os.path.dirname(rel_to) + grammar_filename = os.path.join(basepath, grammar_filename) + with open(grammar_filename) as f: + return cls(f, **options) + + def __repr__(self): + return 'Lark(open(%r), parser=%r, lexer=%r, ...)' % (self.source, self.options.parser, self.options.lexer) + def lex(self, text): if not hasattr(self, 'lexer'):