# -*- encoding: utf-8 -*-
# Copyright (c) 2019 Stephen Bunn <stephen@bunn.io>
# ISC License <https://opensource.org/licenses/isc>

"""
"""

from pathlib import Path

import attr
from lark.lark import Lark, Tree
from cached_property import cached_property

from .log import logger


@attr.s
class Parser(object):
    log = logger
    debug: bool = attr.ib(default=False)
    grammer_parser: str = attr.ib(default="lalr")
    grammer_path: Path = Path(__file__).parent / "grammers" / "toml.lark"

    @cached_property
    def grammer(self) -> str:
        return self.grammer_path.read_text()

    @cached_property
    def lark(self) -> Lark:
        return Lark(self.grammer, parser=self.grammer_parser, debug=self.debug)

    def parse(self, content: str) -> Tree:
        return self.lark.parse(content)
