Skip to content

Commit

Permalink
Merge pull request strictdoc-project#1832 from strictdoc-project/stan…
Browse files Browse the repository at this point in the history
…islaw/statement

backend/sdoc: instantiate doc/grammar meta models at start time
  • Loading branch information
stanislaw authored May 22, 2024
2 parents 69f4905 + f5ab0d9 commit e001f2e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
2 changes: 1 addition & 1 deletion strictdoc/backend/sdoc/error_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def default_view_doesnt_exist(
filename=filename,
)

def to_print_message(self):
def to_print_message(self) -> str:
message = ""
message += f"error: could not parse file: {self.file_path}.\n"
message += f"Semantic error: {self.title}\n"
Expand Down
15 changes: 6 additions & 9 deletions strictdoc/backend/sdoc/grammar/grammar_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# mypy: disable-error-code="no-untyped-call,no-untyped-def"
from strictdoc.backend.sdoc.grammar.grammar import (
DOCUMENT_GRAMMAR,
FREE_TEXT_PARSER_GRAMMAR,
Expand All @@ -16,12 +15,11 @@

class SDocGrammarBuilder:
@staticmethod
def _prep_grammar(grammar):
grammar = (grammar).replace("'\\n'", "'\n'")
return grammar
def _prep_grammar(grammar: str) -> str:
return grammar.replace("'\\n'", "'\n'")

@staticmethod
def create_grammar():
def create_grammar() -> str:
grammar = SDocGrammarBuilder._prep_grammar(
DOCUMENT_GRAMMAR
+ GRAMMAR_GRAMMAR
Expand All @@ -33,20 +31,20 @@ def create_grammar():
return grammar

@staticmethod
def create_free_text_grammar():
def create_free_text_grammar() -> str:
return SDocGrammarBuilder._prep_grammar(
FREE_TEXT_PARSER_GRAMMAR + TEXT_TYPES_GRAMMAR
)

@staticmethod
def create_grammar_grammar():
def create_grammar_grammar() -> str:
grammar = SDocGrammarBuilder._prep_grammar(
GRAMMAR_WRAPPER + GRAMMAR_GRAMMAR + STRICTDOC_BASIC_TYPE_SYSTEM
)
return grammar

@staticmethod
def create_raw_grammar():
def create_raw_grammar() -> str:
grammar = (
DOCUMENT_GRAMMAR
+ GRAMMAR_GRAMMAR
Expand All @@ -55,5 +53,4 @@ def create_raw_grammar():
+ TEXT_TYPES_GRAMMAR
+ STRICTDOC_BASIC_TYPE_SYSTEM
)

return grammar
31 changes: 17 additions & 14 deletions strictdoc/backend/sdoc/grammar_reader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# mypy: disable-error-code="no-untyped-call,no-untyped-def"
import sys
import traceback
from typing import Optional
Expand All @@ -21,25 +20,29 @@


class SDocGrammarReader:
def __init__(self, path_to_output_root) -> None:
self.path_to_output_root = path_to_output_root
meta_model = metamodel_from_str(
SDocGrammarBuilder.create_grammar_grammar(),
classes=GRAMMAR_MODELS + [DocumentGrammarWrapper],
use_regexp_group=True,
)

@staticmethod
def read(input_string, file_path=None) -> DocumentGrammar:
meta_model = metamodel_from_str(
SDocGrammarBuilder.create_grammar_grammar(),
classes=GRAMMAR_MODELS + [DocumentGrammarWrapper],
use_regexp_group=True,
)
def __init__(self, path_to_output_root: str) -> None:
self.path_to_output_root: str = path_to_output_root

meta_model.register_obj_processors(
@staticmethod
def read(
input_string: str, file_path: Optional[str] = None
) -> DocumentGrammar:
SDocGrammarReader.meta_model.register_obj_processors(
{
"GrammarElement": preserve_source_location_data,
}
)

grammar_wrapper: DocumentGrammarWrapper = meta_model.model_from_str(
input_string, file_name=file_path
grammar_wrapper: DocumentGrammarWrapper = (
SDocGrammarReader.meta_model.model_from_str(
input_string, file_name=file_path
)
)
grammar: DocumentGrammar = grammar_wrapper.grammar
grammar.parent = None
Expand All @@ -52,7 +55,7 @@ def read(input_string, file_path=None) -> DocumentGrammar:

return grammar

def read_from_file(self, file_path) -> DocumentGrammar:
def read_from_file(self, file_path: str) -> DocumentGrammar:
unpickled_content: Optional[DocumentGrammar] = assert_optional_cast(
PickleCache.read_from_cache(file_path, self.path_to_output_root),
DocumentGrammar,
Expand Down
18 changes: 10 additions & 8 deletions strictdoc/backend/sdoc/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,24 @@


class SDReader:
meta_model = metamodel_from_str(
SDocGrammarBuilder.create_grammar(),
classes=DOCUMENT_MODELS,
use_regexp_group=True,
)

def __init__(self, path_to_output_root: str = "NOT_RELEVANT"):
self.path_to_output_root = path_to_output_root

@staticmethod
def _read(input_string, file_path=None):
meta_model = metamodel_from_str(
SDocGrammarBuilder.create_grammar(),
classes=DOCUMENT_MODELS,
use_regexp_group=True,
)

parse_context = ParseContext(path_to_sdoc_file=file_path)
processor = SDocParsingProcessor(parse_context=parse_context)
meta_model.register_obj_processors(processor.get_default_processors())
SDReader.meta_model.register_obj_processors(
processor.get_default_processors()
)

document: SDocDocument = meta_model.model_from_str(
document: SDocDocument = SDReader.meta_model.model_from_str(
input_string, file_name=file_path
)
parse_context.document_reference.set_document(document)
Expand Down

0 comments on commit e001f2e

Please sign in to comment.