Commit d702fc24 authored by eckhart's avatar eckhart

Corrections of mypy-TypeErrors

parent 4b248f94
......@@ -39,7 +39,7 @@ import os
import re
from DHParser.preprocess import strip_tokens, with_source_mapping, PreprocessorFunc
from DHParser.syntaxtree import Node, RootNode, StrictResultType
from DHParser.syntaxtree import Node, RootNode, ZOMBIE_ROOTNODE, StrictResultType
from DHParser.transform import TransformationFunc
from DHParser.parse import Grammar
from DHParser.error import adjust_error_locations, is_error, Error
......@@ -97,7 +97,7 @@ class Compiler:
self._reset()
def _reset(self):
self.tree = None # type: Optional[RootNode]
self.tree = ZOMBIE_ROOTNODE # type: RootNode
self.context = [] # type: List[Node]
self._dirty_flag = False
......@@ -116,7 +116,7 @@ class Compiler:
result = self.compile(root)
return result
def set_grammar_name(self, grammar_name: str="", grammar_source: str=""):
def set_grammar_name(self, grammar_name: str = "", grammar_source: str = ""):
"""
Changes the grammar's name and the grammar's source.
......@@ -219,7 +219,7 @@ def compile_source(source: str,
parser: Grammar, # str -> Node (concrete syntax tree (CST))
transformer: TransformationFunc, # Node (CST) -> Node (abstract syntax tree (AST))
compiler: Compiler, # Node (AST) -> Any
preserve_ast: bool = False) -> Tuple[Any, List[Error], Node]:
preserve_ast: bool = False) -> Tuple[Optional[Any], List[Error], Optional[Node]]:
"""
Compiles a source in four stages:
1. Pre-Processing (if needed)
......@@ -259,7 +259,7 @@ def compile_source(source: str,
source_mapping = lambda i: i
else:
source_text, source_mapping = with_source_mapping(preprocessor(original_text))
syntax_tree = parser(source_text)
syntax_tree = parser(source_text) # type: RootNode
if is_logging():
log_ST(syntax_tree, log_file_name + '.cst')
log_parsing_history(parser, log_file_name)
......
......@@ -247,6 +247,7 @@ EBNF_AST_transformation_table = {
def EBNFTransform() -> TransformationFunc:
return partial(traverse, processing_table=EBNF_AST_transformation_table.copy())
def get_ebnf_transformer() -> TransformationFunc:
global thread_local_EBNF_transformer_singleton
try:
......@@ -550,11 +551,11 @@ class EBNFCompiler(Compiler):
# add EBNF grammar to the doc string of the parser class
article = 'an ' if self.grammar_name[0:1] in "AaEeIiOoUu" else 'a ' # what about 'hour', 'universe' etc.?
declarations = ['class ' + self.grammar_name +
'Grammar(Grammar):',
'r"""Parser for ' + article + self.grammar_name +
' source file' +
(', with this grammar:' if self.grammar_source else '.')]
declarations = ['class ' + self.grammar_name
+ 'Grammar(Grammar):',
'r"""Parser for ' + article + self.grammar_name
+ ' source file'
+ (', with this grammar:' if self.grammar_source else '.')]
definitions.append(('parser_initialization__', '"upon instantiation"'))
if self.grammar_source:
definitions.append(('source_hash__',
......
......@@ -55,7 +55,7 @@ import os
from DHParser.error import line_col
from DHParser.stringview import StringView
from DHParser.syntaxtree import Node, WHITESPACE_PTYPE
from DHParser.syntaxtree import Node
from DHParser.toolkit import is_filename, escape_control_characters, typing
from typing import List, Tuple, Union
......@@ -236,7 +236,7 @@ class HistoryRecord:
def __str__(self):
return '%4i, %2i: %s; %s; "%s"' % self.as_tuple()
def as_tuple(self) -> Snapshot:
def as_tuple(self) -> 'Snapshot':
"""
Returns history record formatted as a snapshot tuple.
"""
......@@ -294,7 +294,6 @@ class HistoryRecord:
def status(self) -> str:
return self.FAIL if self.node is None else \
('"%s"' % self.err_msg()) if self.node.errors else self.MATCH
# has_errors(self.node._errors)
@property
def excerpt(self):
......@@ -344,8 +343,8 @@ class HistoryRecord:
remaining = -1
result = None
for record in history:
if (record.status == HistoryRecord.MATCH and
(record.remaining < remaining or remaining < 0)):
if (record.status == HistoryRecord.MATCH
and (record.remaining < remaining or remaining < 0)):
result = record
remaining = record.remaining
return result
......@@ -376,7 +375,7 @@ LOG_SIZE_THRESHOLD = 10000 # maximum number of history records to log
LOG_TAIL_THRESHOLD = 500 # maximum number of history recors for "tail log"
def log_parsing_history(grammar, log_file_name: str = '', html: bool=True) -> None:
def log_parsing_history(grammar, log_file_name: str = '', html: bool = True) -> None:
"""
Writes a log of the parsing history of the most recently parsed document.
......@@ -415,8 +414,7 @@ def log_parsing_history(grammar, log_file_name: str = '', html: bool=True) -> No
if not is_logging():
raise AssertionError("Cannot log history when logging is turned off!")
# assert self.history__, \
# "Parser did not yet run or logging was turned off when running parser!"
if not log_file_name:
name = grammar.__class__.__name__
log_file_name = name[:-7] if name.lower().endswith('grammar') else name
......@@ -424,35 +422,23 @@ def log_parsing_history(grammar, log_file_name: str = '', html: bool=True) -> No
log_file_name = log_file_name[:-4]
full_history = ['<h1>Full parsing history of "%s"</h1>' % log_file_name] # type: List[str]
# match_history = ['<h1>Match history of parsing "%s"</h1>' % log_file_name] # type: List[str]
# errors_only = ['<h1>Errors when parsing "%s"</h1>' % log_file_name] # type: List[str]
if len(grammar.history__) > LOG_SIZE_THRESHOLD:
warning =('Sorry, man, %iK history records is just too many! '
'Only looking at the last %iK records.'
% (len(grammar.history__)//1000, LOG_SIZE_THRESHOLD//1000))
% (len(grammar.history__) // 1000, LOG_SIZE_THRESHOLD // 1000))
html_warning = '<p><strong>' + warning + '</strong></p>'
full_history.append(html_warning)
# match_history.append(html_warning)
# errors_only.append(html_warning)
lead_in = '\n'. join(['<table>', HistoryRecord.COLGROUP, HistoryRecord.HEADINGS])
full_history.append(lead_in)
# match_history.append(lead_in)
# errors_only.append(lead_in)
for record in grammar.history__[-LOG_SIZE_THRESHOLD:]:
line = record.as_html_tr() if html else str(record)
append_line(full_history, line)
# if record.node and record.node.parser.ptype != WHITESPACE_PTYPE:
# append_line(match_history, line)
# if record.node.errors:
# append_line(errors_only, line)
write_log(full_history, log_file_name + '_full')
if len(full_history) > LOG_TAIL_THRESHOLD + 10:
heading = '<h1>Last 500 records of parsing history of "%s"</h1>' % log_file_name + lead_in
write_log([heading] + full_history[-LOG_TAIL_THRESHOLD:], log_file_name + '_full.tail')
# write_log(match_history, log_file_name + '_match')
# if (len(errors_only) > 3 or (len(grammar.history__) <= LOG_SIZE_THRESHOLD
# and len(errors_only) > 2)):
# write_log(errors_only, log_file_name + '_errors')
......@@ -40,7 +40,7 @@ from DHParser.stringview import StringView, EMPTY_STRING_VIEW
from DHParser.syntaxtree import Node, RootNode, ParserBase, WHITESPACE_PTYPE, \
TOKEN_PTYPE, ZOMBIE_PARSER
from DHParser.toolkit import sane_parser_name, escape_control_characters, re, typing
from typing import Callable, cast, Dict, DefaultDict, List, Set, Tuple, Union, Optional
from typing import Callable, cast, List, Tuple, Set, Dict, DefaultDict, Union, Optional
__all__ = ('Parser',
......@@ -271,7 +271,7 @@ class Parser(ParserBase):
the `reset()`-method of the parent class must be called from the
`reset()`-method of the derived class."""
self.visited = dict() # type: Dict[int, Tuple[Optional[Node], StringView]]
self.recursion_counter = defaultdict(lambda :0) # type: DefaultDict[int, int]
self.recursion_counter = defaultdict(lambda: 0) # type: DefaultDict[int, int]
self.cycle_detection = set() # type: Set[Callable]
def __call__(self, text: StringView) -> Tuple[Optional[Node], StringView]:
......@@ -293,7 +293,10 @@ class Parser(ParserBase):
@property
def grammar(self) -> 'Grammar':
if self._grammar:
return self._grammar
else:
raise AssertionError('Grammar has not yet been set!')
@grammar.setter
def grammar(self, grammar: 'Grammar'):
......@@ -301,8 +304,9 @@ class Parser(ParserBase):
self._grammar = grammar
self._grammar_assigned_notifier()
else:
assert self._grammar == grammar, \
"Parser has already been assigned to a different Grammar object!"
if self._grammar != grammar:
raise AssertionError("Parser has already been assigned"
"to a different Grammar object!")
def _grammar_assigned_notifier(self):
"""A function that notifies the parser object that it has been
......@@ -564,12 +568,6 @@ class Grammar:
def __init__(self, root: Parser = None) -> None:
# if not hasattr(self.__class__, 'parser_initialization__'):
# self.__class__.parser_initialization__ = "pending"
# if not hasattr(self.__class__, 'wspL__'):
# self.wspL__ = ''
# if not hasattr(self.__class__, 'wspR__'):
# self.wspR__ = ''
self.all_parsers__ = set() # type: Set[ParserBase]
self._dirty_flag__ = False # type: bool
self.history_tracking__ = False # type: bool
......@@ -650,7 +648,7 @@ class Grammar:
parser.grammar = self
def __call__(self, document: str, start_parser="root__", track_history=False) -> Node:
def __call__(self, document: str, start_parser="root__", track_history=False) -> RootNode:
"""
Parses a document with with parser-combinators.
......
......@@ -288,12 +288,12 @@ class StringView(collections.abc.Sized):
return self.fullstring.split(sep)
else:
pieces = []
l = len(sep)
length = len(sep)
k = 0
i = self.find(sep, k)
while i >= 0:
pieces.append(self.text[self.begin + k: self.begin + i])
k = i + l
k = i + length
i = self.find(sep, k)
pieces.append(self.text[self.begin + k: self.end])
return pieces
......
This diff is collapsed.
......@@ -328,8 +328,8 @@ def main():
file_path = input('Please enter a file path for compilation > ')
if os.path.exists(file_path) and os.path.isfile(file_path):
compiler_suite = input('Compiler suite or ENTER (for ebnf) > ')
if (not compiler_suite or (os.path.exists(compiler_suite)
and os.path.isfile(compiler_suite))):
if not compiler_suite or (os.path.exists(compiler_suite)
and os.path.isfile(compiler_suite)):
_errors = compile_on_disk(file_path, compiler_suite)
if _errors:
print('\n\n'.join(str(err) for err in _errors))
......
......@@ -11,7 +11,7 @@ import time
def run_tests(command):
testtype = 'DOCTEST' if command.find('doctest') >= 0 else 'UNITTEST'
filename = command[command.rfind(' ')+1:]
filename = command[command.rfind(' ') + 1:]
print('\n' + testtype + ' ' + filename)
os.system(command)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment