Commit b416a402 authored by eckhart's avatar eckhart
Browse files

- - compile.Compiler.compile: None-test can be turned off object variable, not config var needed!

parent 3335167d
...@@ -43,7 +43,7 @@ from DHParser.transform import TransformationFunc ...@@ -43,7 +43,7 @@ from DHParser.transform import TransformationFunc
from DHParser.parse import Grammar from DHParser.parse import Grammar
from DHParser.error import adjust_error_locations, is_error, Error from DHParser.error import adjust_error_locations, is_error, Error
from DHParser.log import log_parsing_history, log_ST, is_logging, logfile_basename from DHParser.log import log_parsing_history, log_ST, is_logging, logfile_basename
from DHParser.toolkit import load_if_file, get_config_value from DHParser.toolkit import load_if_file
__all__ = ('CompilerError', 'Compiler', 'compile_source', 'visitor_name') __all__ = ('CompilerError', 'Compiler', 'compile_source', 'visitor_name')
...@@ -111,7 +111,7 @@ class Compiler: ...@@ -111,7 +111,7 @@ class Compiler:
self.source = '' self.source = ''
self.tree = ROOTNODE_PLACEHOLDER # type: RootNode self.tree = ROOTNODE_PLACEHOLDER # type: RootNode
self.context = [] # type: List[Node] self.context = [] # type: List[Node]
self._None_check = get_config_value('raise_error_on_None_return') self._None_check = True # type: bool
self._dirty_flag = False self._dirty_flag = False
def __call__(self, root: RootNode, source: str = '') -> Any: def __call__(self, root: RootNode, source: str = '') -> Any:
...@@ -173,10 +173,12 @@ class Compiler: ...@@ -173,10 +173,12 @@ class Compiler:
self.context.append(node) self.context.append(node)
result = compiler(node) result = compiler(node)
self.context.pop() self.context.pop()
if result is None and self._None_check and \ if result is None and self._None_check:
compiler.__annotations__['return'] not in (None, 'None'): raise CompilerError('Method on_%s returned `None` instead of a valid compilation '
raise CompilerError('Method on_%s returned `None` instead of a ' 'compilation result! Turn this check of by adding '
'valid compilation result!' % elem) '"self._None_check = False" to the _reset()-Method of your'
'compiler class, in case on_%s actually SHOULD return None.'
% elem)
return result return result
......
...@@ -111,21 +111,6 @@ CONFIG_PRESET['default_serialization'] = SXPRESSION_SERIALIZATION ...@@ -111,21 +111,6 @@ CONFIG_PRESET['default_serialization'] = SXPRESSION_SERIALIZATION
CONFIG_PRESET['flatten_sxpr_threshold'] = 120 CONFIG_PRESET['flatten_sxpr_threshold'] = 120
########################################################################
#
# compiler configuration
#
########################################################################
# Turn checks for None-return of compiler-methods (a common mistake) in
# the compile.Compiler class on. This can lead to problems in cases
# where compile functions legitimately return `None` as value. See the
# examples/json for an example. In this case the configuration variable
# 'raise_error_on_None_return' should be set to False.
# Possible values: True, False Default value: True
CONFIG_PRESET['raise_error_on_None_return'] = True
######################################################################## ########################################################################
# #
# ebnf compiler configuration # ebnf compiler configuration
......
...@@ -68,7 +68,7 @@ Match test "entry" for parser "entry" failed: ...@@ -68,7 +68,7 @@ Match test "entry" for parser "entry" failed:
organization = {Wikipedia} organization = {Wikipedia}
} }
6:68: Error (1010): '}' ~ expected, "%E2\%80\%9" found! 6:68: Error (1010): '}' ~ expected, "%E2\%80\%9..." found!
6:69: Error (1040): Parser stopped before end! trying to recover but stopping history recording at this point. 6:69: Error (1040): Parser stopped before end! trying to recover but stopping history recording at this point.
7:1: Error (1020): Parser did not match! 7:1: Error (1020): Parser did not match!
Most advanced: 6, 68: entry->:ZeroOrMore->:Series->content->:Series->text->:Alternative->CONTENT_STRING->:Alternative->:Series->:Lookahead->/(?i)%/; MATCH; "%" Most advanced: 6, 68: entry->:ZeroOrMore->:Series->content->:Series->text->:Alternative->CONTENT_STRING->:Alternative->:Series->:Lookahead->/(?i)%/; MATCH; "%"
......
...@@ -59,7 +59,7 @@ class EBNFGrammar(Grammar): ...@@ -59,7 +59,7 @@ class EBNFGrammar(Grammar):
r"""Parser for an EBNF source file. r"""Parser for an EBNF source file.
""" """
expression = Forward() expression = Forward()
source_hash__ = "0a4657b403c537e8305c9ff3d3c90472" source_hash__ = "9a341fad79f7e8eb46f1a657b553bec7"
static_analysis_pending__ = [True] static_analysis_pending__ = [True]
parser_initialization__ = ["upon instantiation"] parser_initialization__ = ["upon instantiation"]
resume_rules__ = {} resume_rules__ = {}
...@@ -70,7 +70,7 @@ class EBNFGrammar(Grammar): ...@@ -70,7 +70,7 @@ class EBNFGrammar(Grammar):
EOF = NegativeLookahead(RegExp('.')) EOF = NegativeLookahead(RegExp('.'))
whitespace = Series(RegExp('~'), dwsp__) whitespace = Series(RegExp('~'), dwsp__)
regexp = Series(RegExp('/(?:(?<!\\\\)\\\\(?:/)|[^/])*?/'), dwsp__) regexp = Series(RegExp('/(?:(?<!\\\\)\\\\(?:/)|[^/])*?/'), dwsp__)
plaintext = Series(RegExp('`(?:(?<!\\\\)\\\\`|[^"])*?`'), dwsp__) plaintext = Series(RegExp('`(?:(?<!\\\\)\\\\`|[^`])*?`'), dwsp__)
literal = Alternative(Series(RegExp('"(?:(?<!\\\\)\\\\"|[^"])*?"'), dwsp__), Series(RegExp("'(?:(?<!\\\\)\\\\'|[^'])*?'"), dwsp__)) literal = Alternative(Series(RegExp('"(?:(?<!\\\\)\\\\"|[^"])*?"'), dwsp__), Series(RegExp("'(?:(?<!\\\\)\\\\'|[^'])*?'"), dwsp__))
symbol = Series(RegExp('(?!\\d)\\w+'), dwsp__) symbol = Series(RegExp('(?!\\d)\\w+'), dwsp__)
option = Series(Series(Token("["), dwsp__), expression, Series(Token("]"), dwsp__), mandatory=1) option = Series(Series(Token("["), dwsp__), expression, Series(Token("]"), dwsp__), mandatory=1)
......
This diff is collapsed.
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