Commit 017c4fd7 authored by di68kap's avatar di68kap
Browse files

- configuration: default values for serialization now constants rather than "magic" strings

parent 30e6dd80
...@@ -32,7 +32,12 @@ program and before any DHParser-function is invoked. ...@@ -32,7 +32,12 @@ program and before any DHParser-function is invoked.
from typing import Dict, Hashable, Any from typing import Dict, Hashable, Any
__all__ = ('CONFIG_PRESET',) __all__ = ('CONFIG_PRESET',
'XML_SERIALIZATION',
'SXPRESSION_SERIALIZATION',
'COMPACT_SERIALIZATION',
'JSON_SERIALIZATION',
'SERIALIZATIONS')
CONFIG_PRESET = dict() # type: Dict[Hashable, Any] CONFIG_PRESET = dict() # type: Dict[Hashable, Any]
...@@ -77,15 +82,25 @@ CONFIG_PRESET['max_parser_dropouts'] = 3 ...@@ -77,15 +82,25 @@ CONFIG_PRESET['max_parser_dropouts'] = 3
# Possible values are: # Possible values are:
# 'XML' - output as XML # 'XML' - output as XML
# 'S-expression' - output as S-expression, i.e. a list-like format # 'S-expression' - output as S-expression, i.e. a list-like format
# 'compact' - compact tree output, i.e. children a represented # 'compact' - compact tree output, i.e. children a represented on
# on indented lines with no opening or closing tags, # indented lines with no opening or closing tags, brackets
# brackets etc. # etc.
# Default values: "compact" for concrete syntax trees and "XML" for # 'json' - output in JSON-format. This is probably the least
# abstract syntax trees and "S-expression" for any # readable representation, but useful for serialization, for
# other kind of tree. # example, to return syntax trees from remote procedure calls.
CONFIG_PRESET['cst_serialization'] = "compact" # Default values: "compact" for concrete syntax trees and "XML" for abstract
CONFIG_PRESET['ast_serialization'] = "XML" # syntax trees and "S-expression" for any other kind of tree.
CONFIG_PRESET['default_serialization'] = "S-expression" XML_SERIALIZATION = "XML"
SXPRESSION_SERIALIZATION = "S-expression"
COMPACT_SERIALIZATION = "compact"
JSON_SERIALIZATION = "json"
SERIALIZATIONS = frozenset({XML_SERIALIZATION, SXPRESSION_SERIALIZATION,
COMPACT_SERIALIZATION, JSON_SERIALIZATION})
CONFIG_PRESET['cst_serialization'] = COMPACT_SERIALIZATION
CONFIG_PRESET['ast_serialization'] = XML_SERIALIZATION
CONFIG_PRESET['default_serialization'] = SXPRESSION_SERIALIZATION
# Defines the maximum line length for flattened S-expressions. # Defines the maximum line length for flattened S-expressions.
# Below this threshold S-expressions will be returned in flattened # Below this threshold S-expressions will be returned in flattened
......
...@@ -28,6 +28,8 @@ import copy ...@@ -28,6 +28,8 @@ import copy
import json import json
from typing import Callable, cast, Iterator, List, AbstractSet, Set, Union, Tuple, Optional, Dict from typing import Callable, cast, Iterator, List, AbstractSet, Set, Union, Tuple, Optional, Dict
from DHParser.configuration import SERIALIZATIONS, XML_SERIALIZATION, SXPRESSION_SERIALIZATION, \
COMPACT_SERIALIZATION, JSON_SERIALIZATION
from DHParser.error import Error, ErrorCode, linebreaks, line_col from DHParser.error import Error, ErrorCode, linebreaks, line_col
from DHParser.stringview import StringView from DHParser.stringview import StringView
from DHParser.toolkit import get_config_value, re from DHParser.toolkit import get_config_value, re
...@@ -837,16 +839,17 @@ def serialize(node: Node, how: str = 'default') -> str: ...@@ -837,16 +839,17 @@ def serialize(node: Node, how: str = 'default') -> str:
elif switch == 'default': elif switch == 'default':
switch = get_config_value('default_serialization').lower() switch = get_config_value('default_serialization').lower()
if switch == 's-expression': if switch == SXPRESSION_SERIALIZATION.lower():
return node.as_sxpr(flatten_threshold=get_config_value('flatten_sxpr_threshold')) return node.as_sxpr(flatten_threshold=get_config_value('flatten_sxpr_threshold'))
elif switch == 'xml': elif switch == XML_SERIALIZATION.lower():
return node.as_xml() return node.as_xml()
elif switch == 'json': elif switch == JSON_SERIALIZATION.lower():
return node.as_json() return node.as_json()
elif switch == 'compact': elif switch == COMPACT_SERIALIZATION.lower():
return node.as_sxpr(compact=True) return node.as_sxpr(compact=True)
else: else:
raise ValueError('Unknown serialization %s, %s' % (how, switch)) raise ValueError('Unknown serialization %s. Allowed values are either: %s or : %s'
% (how, "'ast', 'cst', 'default'", ", ".join(list(SERIALIZATIONS))))
class FrozenNode(Node): class FrozenNode(Node):
......
...@@ -105,7 +105,7 @@ def set_config_value(key: Hashable, value: Any): ...@@ -105,7 +105,7 @@ def set_config_value(key: Hashable, value: Any):
Changes a configuration value thread-safely. The configuration Changes a configuration value thread-safely. The configuration
value will be set only for the current thread. In order to value will be set only for the current thread. In order to
set configuration values for any new thread, add the key and value set configuration values for any new thread, add the key and value
to CONFIG_PRESET, before the thread is started. to CONFIG_PRESET, before any thread accessing config values is started.
:param key: the key (an immutable, usually a string) :param key: the key (an immutable, usually a string)
:param value: the value :param value: the value
""" """
......
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