24.09., 9:00 - 11:00: Due to updates GitLab will be unavailable for some minutes between 09:00 and 11:00.

jsonCompiler.py 9.25 KB
Newer Older
eckhart's avatar
eckhart committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/python3

#######################################################################
#
# SYMBOLS SECTION - Can be edited. Changes will be preserved.
#
#######################################################################


import collections
from functools import partial
import os
import sys

di68kap's avatar
di68kap committed
15
sys.path.extend(['../../', '../', './'])
eckhart's avatar
eckhart committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

try:
    import regex as re
except ImportError:
    import re
from DHParser import logging, is_filename, load_if_file, \
    Grammar, Compiler, nil_preprocessor, PreprocessorToken, Whitespace, DropWhitespace, \
    Lookbehind, Lookahead, Alternative, Pop, Token, DropToken, Synonym, AllOf, SomeOf, \
    Unordered, Option, NegativeLookbehind, OneOrMore, RegExp, Retrieve, Series, Capture, \
    ZeroOrMore, Forward, NegativeLookahead, Required, mixin_comment, compile_source, \
    grammar_changed, last_value, counterpart, PreprocessorFunc, is_empty, \
    Node, TransformationFunc, TransformationDict, transformation_factory, traverse, \
    remove_children_if, move_adjacent, normalize_whitespace, is_anonymous, matches_re, \
    reduce_single_child, replace_by_single_child, replace_or_reduce, remove_whitespace, \
    replace_by_children, remove_empty, remove_tokens, flatten, is_insignificant_whitespace, \
    collapse, collapse_if, replace_content, WHITESPACE_PTYPE, TOKEN_PTYPE, \
    remove_nodes, remove_content, remove_brackets, change_tag_name, remove_anonymous_tokens, \
    keep_children, is_one_of, not_one_of, has_content, apply_if, remove_first, remove_last, \
    remove_anonymous_empty, keep_nodes, traverse_locally, strip, lstrip, rstrip, \
    replace_content, replace_content_by, forbid, assert_content, remove_infix_operator, \
36 37 38
    error_on, recompile_grammar, left_associative, lean_left, set_config_value, \
    get_config_value, XML_SERIALIZATION, SXPRESSION_SERIALIZATION, COMPACT_SERIALIZATION, \
    JSON_SERIALIZATION, CONFIG_PRESET, GLOBALS
eckhart's avatar
eckhart committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64


#######################################################################
#
# PREPROCESSOR SECTION - Can be edited. Changes will be preserved.
#
#######################################################################

def jsonPreprocessor(text):
    return text, lambda i: i

def get_preprocessor() -> PreprocessorFunc:
    return jsonPreprocessor


#######################################################################
#
# PARSER SECTION - Don't edit! CHANGES WILL BE OVERWRITTEN!
#
#######################################################################

class jsonGrammar(Grammar):
    r"""Parser for a json source file.
    """
    element = Forward()
    value = Forward()
65
    source_hash__ = "abc885159d84633d94e82c75709c9684"
eckhart's avatar
eckhart committed
66 67 68
    static_analysis_pending__ = [True]
    parser_initialization__ = ["upon instantiation"]
    resume_rules__ = {}
69
    COMMENT__ = r'(?:\/\/|#).*'
eckhart's avatar
eckhart committed
70 71
    WHITESPACE__ = r'\s*'
    WSP_RE__ = mixin_comment(whitespace=WHITESPACE__, comment=COMMENT__)
72
    dwsp__ = DropWhitespace(WSP_RE__)
eckhart's avatar
eckhart committed
73
    EOF = NegativeLookahead(RegExp('.'))
74 75 76
    EXP = Option(Series(Alternative(DropToken("E"), DropToken("e")), Option(Alternative(DropToken("+"), DropToken("-"))), RegExp('[0-9]+')))
    FRAC = Option(Series(DropToken("."), RegExp('[0-9]+')))
    INT = Alternative(Series(Option(DropToken("-")), RegExp('[0-9]')), RegExp('[1-9][0-9]+'))
eckhart's avatar
eckhart committed
77 78 79
    HEX = RegExp('[0-9a-fA-F]')
    ESCAPE = Alternative(RegExp('\\\\[/bnrt\\\\]'), Series(RegExp('\\\\u'), HEX, HEX, HEX, HEX))
    CHARACTERS = ZeroOrMore(Alternative(RegExp('[^"\\\\]+'), ESCAPE))
80 81 82 83 84 85 86 87
    null = Series(Token("null"), dwsp__)
    bool = Alternative(Series(RegExp('true'), dwsp__), Series(RegExp('false'), dwsp__))
    number = Series(INT, FRAC, EXP, dwsp__)
    string = Series(DropToken('"'), CHARACTERS, DropToken('"'), dwsp__)
    array = Series(Series(DropToken("["), dwsp__), Option(Series(value, ZeroOrMore(Series(Series(DropToken(","), dwsp__), value)))), Series(DropToken("]"), dwsp__))
    member = Series(string, Series(DropToken(":"), dwsp__), element)
    object = Series(Series(DropToken("{"), dwsp__), Option(Series(member, ZeroOrMore(Series(Series(DropToken(","), dwsp__), member)))), Series(DropToken("}"), dwsp__))
    value.set(Alternative(object, array, string, number, bool, null))
eckhart's avatar
eckhart committed
88
    element.set(Synonym(value))
89
    json = Series(dwsp__, element, EOF)
eckhart's avatar
eckhart committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    root__ = json
    
def get_grammar() -> jsonGrammar:
    """Returns a thread/process-exclusive jsonGrammar-singleton."""
    try:
        grammar = GLOBALS.json_00000001_grammar_singleton
    except AttributeError:
        GLOBALS.json_00000001_grammar_singleton = jsonGrammar()
        if hasattr(get_grammar, 'python_src__'):
            GLOBALS.json_00000001_grammar_singleton.python_src__ = get_grammar.python_src__
        grammar = GLOBALS.json_00000001_grammar_singleton
    return grammar


#######################################################################
#
# AST SECTION - Can be edited. Changes will be preserved.
#
#######################################################################

json_AST_transformation_table = {
    # AST Transformations for the json-grammar
    "<": flatten,
113 114 115
    "json": [remove_nodes('EOF'), replace_by_single_child],
    "element": [replace_by_single_child],
    "value": [replace_by_single_child],
eckhart's avatar
eckhart committed
116 117 118
    "object": [],
    "member": [],
    "array": [],
119 120 121 122
    "string": [collapse],
    "number": [collapse],
    "bool": [],
    "null": [],
eckhart's avatar
eckhart committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    "CHARACTERS": [],
    "ESCAPE": [],
    "HEX": [],
    "INT": [],
    "FRAC": [],
    "EXP": [],
    "EOF": [],
    "*": replace_by_single_child
}


def CreatejsonTransformer() -> TransformationFunc:
    """Creates a transformation function that does not share state with other
    threads or processes."""
    return partial(traverse, processing_table=json_AST_transformation_table.copy())

def get_transformer() -> TransformationFunc:
    """Returns a thread/process-exclusive transformation function."""
    try:
        transformer = GLOBALS.json_00000001_transformer_singleton
    except AttributeError:
        GLOBALS.json_00000001_transformer_singleton = CreatejsonTransformer()
        transformer = GLOBALS.json_00000001_transformer_singleton
    return transformer


#######################################################################
#
# COMPILER SECTION - Can be edited. Changes will be preserved.
#
#######################################################################

class jsonCompiler(Compiler):
    """Compiler for the abstract-syntax-tree of a json source file.
    """

    def __init__(self):
        super(jsonCompiler, self).__init__()

162 163
    def reset(self):
        super().reset()
164 165
        self._None_check = False

166 167
    def on_object(self, node):
        return dict(self.compile(child) for child in node.children)
eckhart's avatar
eckhart committed
168

169 170
    def on_member(self, node) -> tuple:
        return (self.compile(node.children[0]), self.compile(node.children[1]))
eckhart's avatar
eckhart committed
171

172 173
    def on_array(self, node) -> list:
        return [self.compile(child) for child in node.children]
eckhart's avatar
eckhart committed
174

175 176
    def on_string(self, node) -> str:
        return node.content
eckhart's avatar
eckhart committed
177

178 179
    def on_number(self, node) -> float:
        return float(node.content)
eckhart's avatar
eckhart committed
180

181 182
    def on_bool(self, node) -> bool:
        return True if node.content == "true" else False
eckhart's avatar
eckhart committed
183

184 185
    def on_null(self, node) -> None:
        return None
eckhart's avatar
eckhart committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242


def get_compiler() -> jsonCompiler:
    """Returns a thread/process-exclusive jsonCompiler-singleton."""
    try:
        compiler = GLOBALS.json_00000001_compiler_singleton
    except AttributeError:
        GLOBALS.json_00000001_compiler_singleton = jsonCompiler()
        compiler = GLOBALS.json_00000001_compiler_singleton
    return compiler


#######################################################################
#
# END OF DHPARSER-SECTIONS
#
#######################################################################


def compile_src(source, log_dir=''):
    """Compiles ``source`` and returns (result, errors, ast).
    """
    with logging(log_dir):
        compiler = get_compiler()
        result_tuple = compile_source(source, get_preprocessor(),
                                      get_grammar(),
                                      get_transformer(), compiler)
    return result_tuple


if __name__ == "__main__":
    # recompile grammar if needed
    grammar_path = os.path.abspath(__file__).replace('Compiler.py', '.ebnf')
    if os.path.exists(grammar_path):
        if not recompile_grammar(grammar_path, force=False,
                                  notify=lambda:print('recompiling ' + grammar_path)):
            error_file = os.path.basename(__file__).replace('Compiler.py', '_ebnf_ERRORS.txt')
            with open(error_file, encoding="utf-8") as f:
                print(f.read())
            sys.exit(1)
    else:
        print('Could not check whether grammar requires recompiling, '
              'because grammar was not found at: ' + grammar_path)

    if len(sys.argv) > 1:
        # compile file
        file_name, log_dir = sys.argv[1], ''
        if file_name in ['-d', '--debug'] and len(sys.argv) > 2:
            file_name, log_dir = sys.argv[2], 'LOGS'
        result, errors, _ = compile_src(file_name, log_dir)
        if errors:
            cwd = os.getcwd()
            rel_path = file_name[len(cwd):] if file_name.startswith(cwd) else file_name
            for error in errors:
                print(rel_path + ':' + str(error))
            sys.exit(1)
        else:
243
            print(result.as_sxpr() if isinstance(result, Node) else result)
eckhart's avatar
eckhart committed
244 245
    else:
        print("Usage: jsonCompiler.py [FILENAME]")