aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/bookmaker/fiddleParser.cpp
blob: 990ff201fbfb1650290deb0eb21b24c3fe40847c (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "bookmaker.h"

bool FiddleBase::parseFiddles() {
    if (fStack.empty()) {
        return false;
    }
    JsonStatus* status = &fStack.back();
    while (status->fIter != status->fObject.end()) {
        const char* blockName = status->fIter.memberName();
        Definition* example = nullptr;
        string textString;
        if (!status->fObject.isObject()) {
            return this->reportError<bool>("expected object");
        }
        for (auto iter = status->fIter->begin(); status->fIter->end() != iter; ++iter) {
            const char* memberName = iter.memberName();
            if (!strcmp("compile_errors", memberName)) {
                if (!iter->isArray()) {
                    return this->reportError<bool>("expected array");
                }
                if (iter->size()) {
                    return this->reportError<bool>("fiddle compiler error");
                }
                continue;
            }
            if (!strcmp("runtime_error", memberName)) {
                if (!iter->isString()) {
                    return this->reportError<bool>("expected string 1");
                }
                if (iter->asString().length()) {
                    return this->reportError<bool>("fiddle runtime error");
                }
                continue;
            }
            if (!strcmp("fiddleHash", memberName)) {
                if (!iter->isString()) {
                    return this->reportError<bool>("expected string 2");
                }
                example = this->findExample(blockName);
                if (!example) {
                    return this->reportError<bool>("missing example");
                }
                if (example->fHash.length() && example->fHash != iter->asString()) {
                    return example->reportError<bool>("mismatched hash");
                }
                example->fHash = iter->asString();
                continue;
            }
            if (!strcmp("text", memberName)) {
                if (!iter->isString()) {
                    return this->reportError<bool>("expected string 3");
                }
                textString = iter->asString();
                continue;
            }
            return this->reportError<bool>("unexpected key");
        }
        if (!example) {
            return this->reportError<bool>("missing fiddleHash");
        }
        size_t strLen = textString.length();
        if (strLen) {
            if (fTextOut
                    && !this->textOut(example, textString.c_str(), textString.c_str() + strLen)) {
                return false;
            }
        } else if (fPngOut && !this->pngOut(example)) {
            return false;
        }
        status->fIter++;
    }
    return true;
}

bool FiddleParser::textOut(Definition* example, const char* stdOutStart,
        const char* stdOutEnd) {
    bool foundStdOut = false;
    for (auto& textOut : example->fChildren) {
        if (MarkType::kStdOut != textOut->fMarkType) {
            continue;
        }
        foundStdOut = true;
        bool foundVolatile = false;
        for (auto& stdOutChild : textOut->fChildren) {
                if (MarkType::kVolatile == stdOutChild->fMarkType) {
                    foundVolatile = true;
                    break;
                }
        }
        TextParser bmh(textOut);
        EscapeParser fiddle(stdOutStart, stdOutEnd);
        do {
            bmh.skipWhiteSpace();
            fiddle.skipWhiteSpace();
            const char* bmhEnd = bmh.trimmedLineEnd();
            const char* fiddleEnd = fiddle.trimmedLineEnd();
            ptrdiff_t bmhLen = bmhEnd - bmh.fChar;
            SkASSERT(bmhLen > 0);
            ptrdiff_t fiddleLen = fiddleEnd - fiddle.fChar;
            SkASSERT(fiddleLen > 0);
            if (bmhLen != fiddleLen) {
                if (!foundVolatile) {
                    bmh.reportError("mismatched stdout len\n");
                }
            } else  if (strncmp(bmh.fChar, fiddle.fChar, fiddleLen)) {
                if (!foundVolatile) {
                    SkDebugf("%.*s\n", fiddleLen, fiddle.fChar);
                    bmh.reportError("mismatched stdout text\n");
                }
            }
            bmh.skipToLineStart();
            fiddle.skipToLineStart();
        } while (!bmh.eof() && !fiddle.eof());
        if (!foundStdOut) {
            bmh.reportError("bmh %s missing stdout\n");
        } else if (!bmh.eof() || !fiddle.eof()) {
            if (!foundVolatile) {
                bmh.reportError("%s mismatched stdout eof\n");
            }
        }
    }
    return true;
}