Browse Source

Interface improvements for the Lark instance

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.6.0
Erez Shinan 6 years ago
parent
commit
209a3fe8fd
2 changed files with 33 additions and 15 deletions
  1. +7
    -11
      examples/python_parser.py
  2. +26
    -4
      lark/lark.py

+ 7
- 11
examples/python_parser.py View File

@@ -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')


+ 26
- 4
lark/lark.py View File

@@ -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 = '<string>'
self.source = '<string>'
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'):


Loading…
Cancel
Save