summaryrefslogtreecommitdiffstats
path: root/util/cmake/json_parser.py
blob: f8e0fa6017f0ed3b07947c8b1ab613c9ce66d9fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
# Copyright (C) 2019 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

import pyparsing as pp  # type: ignore
import json
import re
from helper import _set_up_py_parsing_nicer_debug_output

_set_up_py_parsing_nicer_debug_output(pp)


class QMakeSpecificJSONParser:
    def __init__(self, *, debug: bool = False) -> None:
        self.debug = debug
        self.grammar = self.create_py_parsing_grammar()

    def create_py_parsing_grammar(self):
        # Keep around all whitespace.
        pp.ParserElement.setDefaultWhitespaceChars("")

        def add_element(name: str, value: pp.ParserElement):
            nonlocal self
            if self.debug:
                value.setName(name)
                value.setDebug()
            return value

        # Our grammar is pretty simple. We want to remove all newlines
        # inside quoted strings, to make the quoted strings JSON
        # compliant. So our grammar should skip to the first quote while
        # keeping everything before it as-is, process the quoted string
        # skip to the next quote, and repeat that until the end of the
        # file.

        EOF = add_element("EOF", pp.StringEnd())
        SkipToQuote = add_element("SkipToQuote", pp.SkipTo('"'))
        SkipToEOF = add_element("SkipToEOF", pp.SkipTo(EOF))

        def remove_newlines_and_whitespace_in_quoted_string(tokens):
            first_string = tokens[0]
            replaced_string = re.sub(r"\n[ ]*", " ", first_string)
            return replaced_string

        QuotedString = add_element(
            "QuotedString", pp.QuotedString(quoteChar='"', multiline=True, unquoteResults=False)
        )
        QuotedString.setParseAction(remove_newlines_and_whitespace_in_quoted_string)

        QuotedTerm = add_element("QuotedTerm", pp.Optional(SkipToQuote) + QuotedString)
        Grammar = add_element("Grammar", pp.OneOrMore(QuotedTerm) + SkipToEOF)

        return Grammar

    def parse_file_using_py_parsing(self, file: str):
        print(f'Pre processing "{file}" using py parsing to remove incorrect newlines.')
        try:
            with open(file, "r") as file_fd:
                contents = file_fd.read()

            parser_result = self.grammar.parseString(contents, parseAll=True)
            token_list = parser_result.asList()
            joined_string = "".join(token_list)

            return joined_string
        except pp.ParseException as pe:
            print(pe.line)
            print(" " * (pe.col - 1) + "^")
            print(pe)
            raise pe

    def parse(self, file: str):
        pre_processed_string = self.parse_file_using_py_parsing(file)
        print(f'Parsing "{file}" using json.loads().')
        json_parsed = json.loads(pre_processed_string)
        return json_parsed