File: xdoc_index.js

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (267 lines) | stat: -rw-r--r-- 9,360 bytes parent folder | download | duplicates (2)
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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
; XDOC Documentation System for ACL2
; Copyright (C) 2009-2013 Centaur Technology
;
; Contact:
;   Centaur Technology Formal Verification Group
;   7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
;   http://www.centtech.com/
;
; License: (An MIT/X11-style license)
;
;   Permission is hereby granted, free of charge, to any person obtaining a
;   copy of this software and associated documentation files (the "Software"),
;   to deal in the Software without restriction, including without limitation
;   the rights to use, copy, modify, merge, publish, distribute, sublicense,
;   and/or sell copies of the Software, and to permit persons to whom the
;   Software is furnished to do so, subject to the following conditions:
;
;   The above copyright notice and this permission notice shall be included in
;   all copies or substantial portions of the Software.
;
;   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;   DEALINGS IN THE SOFTWARE.
;
; Original author: Jared Davis <jared@centtech.com>
*/

// --------------------------------------------------------------------------
//
//                          MAIN DATA STRUCTURES
//
// --------------------------------------------------------------------------
//
// The XDOC database is split up into two files: XINDEX and XDATA.  Both of
// these files are generated ahead of time by the ACL2 code for XDOC.
//    - xindex is smaller and contains just the navigation data
//    - xdata is larger and has the full "long" strings for each topic
//
// We load these files lazily to make the page seem to appear faster!  This
// means you have to sort of be aware of when the data becomes available.  We
// load xindex first, then once it's complete we load xdata.  The formats of
// both files are described below.

// Public:
//
//    xindexReady() -> Bool  -- Has the xindex been loaded and initialized?
//                               May be called at any time.
//
//    xindexInit();     -- Prepare the index for use.
//                          Must only be called after loading xindex.js
//                          Must be called before using the below functions
//
//    allKeys() -> [array of all valid keys]
//
//    uid_to_key        : UID -> KEY
//
//    topicExists      : KEY -> bool
//    topicUid         : KEY -> unique integer identifier
//    topicName        : KEY -> xml encoded nice topic name
//    topicRawname     : KEY -> non-encoded symbol-name (no package)
//    topicShort       : KEY -> xml encoded short topic description
//    topicParentKeys : KEY -> [array of KEYS of parents]
//    topicChildKeys  : KEY -> [array of KEYS of children]
//    topicSuborder    : KEY -> [array of KEYS of children]
//
//
// Implementation details:
//
// The file xindex.js contains most of the XDOC database (the metadata like
// topic names, parents, keys---everything but the :long data).  This is a
// lot of data so we load it lazily.  Once xindex.js is loaded, we just end
// up with the "xindex" variable loaded.
//
// "xindex" contains the data in a somewhat compressed form.  To reduce its
// size, we leave out some information that we can reconstruct, e.g., the
// children of each topic.  So there's a little work we need to decode the
// xindex and turn it into a suitable table.
//
// We want to separate the format of xindex.js from the rest of the viewer
// application, and hence be able to easily change the format.
//
// The xindex holds an array of entries.  The UID for each topic is implicitly
// just its position in the array.
//
// Format of each entry (xdoc/save-fancy.lisp:json-encode-index-entry)
//
//        0     1      2         3         4
//      [key, name, rawname, parent-uids, short]
//  OR  [key, name, rawname, parent-uids, short, suborder-uids]
//
// We translate the xindex into an "xhash" of the form:
//
//             0    1      2         3          4           5       6          7             8
//    KEY -> [uid, name, rawname, parentuids, parentkeys, short, childkeys, suborder-uids, suborder-keys].

class XDocIndex {
    _xhash = new Map();

    /**
     * Load raw xindex data into this object
     * @param {Array} xindex The xindex data, of the form described in the above comment.
     */
    loadFromXindex(xindex) {
        // Fill in most of the xhash directly from the xindex
        for(const [uid, entry] of xindex.entries()) {
            const key = entry[0];
            const name = entry[1];
            const rawname = entry[2];
            const parentuids = entry[3];
            const shortstr = entry[4];
            const suborder = entry[5];
            this._xhash.set(key, [uid,name,rawname,parentuids,[],shortstr,[],suborder,[]]);
        }

        // Fill in the parent_keys by resolving all parent uids
        const xl = xindex.length;
        for(const entry of this._xhash.values()) {
            const parentuids = entry[3];
            for(const uid of parentuids) {
                const parentkey = (0 <= uid && uid < xl)
                                ? xindex[uid][0]
                                : "XDOC____ERROR-BROKEN-PARENT";
                entry[4].push(parentkey);
            }
        }

        // Fill in suborder_keys by resolving all suborder uids
        for(const entry of this._xhash.values()) {
            const subuids = entry[7];
            if(subuids) {
                for(const uid of subuids) {
                    if (0 <= uid && uid < xl) {
                        const subkey = xindex[uid][0];
                        entry[8].push(subkey);
                    }
                }
            }
        }

        // Fill in all child_keys by cross-referencing parents
        for(const child_key of this._xhash.keys()) {
            const parent_keys = this.topicParentKeys(child_key);
            for(const parent_key of parent_keys) {
                // It's incorrect, but possible for a child topic to list parents
                // that don't exist, so we have to make sure it really exists:
                if (this.topicExists(parent_key)) {
                    const parent_node = this._xhash.get(parent_key);
                    parent_node[6].push(child_key);
                }
            }
        }
    }

    allKeys() {
        return this._xhash.keys();
    }

    topicExists(key) {
        return this._xhash.has(key);
    }

    getTopicField(key, field, defaultValue) {
        return this.topicExists(key) ? this._xhash.get(key)[field] : defaultValue;
    }

    topicUid(key) {
        return this.getTopicField(key, 0, null);
    }

    topicName(key) {
        return this.getTopicField(key, 1, `Error: Key ${key} not found`);
    }

    topicRawname(key) {
        return this.getTopicField(key, 2, `Error: Key ${key} not found`);
    }

    topicParentKeys(key) {
        return this.getTopicField(key, 4, []);
    }

    topicShort(key) {
        return this.getTopicField(key, 5, `Error: Key ${key} not found`);
    }

    topicChildKeys(key) {
        return this.getTopicField(key, 6, []);
    }

    topicSuborder(key) {
        return this.getTopicField(key, 8, []);
    }
}

const XD_PNAMES = 0;
const XD_FROM = 1;
const XD_BASEPKG = 2;
const XD_LONG = 3;
const LAST_XD_IDX = XD_LONG;

// The XDATA table has the following format:
//
//   xdata:         KEY -> {"pnames"  : [array of xml encoded nice parent names],
//                          "from"    : "xml encoded string for topic location",
//                          "basepkg" : "base package name (not encoded)",
//                          "long"    : "xml encoded long topic description"}
//
// Except that we represent each entry with an array, instead of a hash, to
// save a tiny amount of space.
// An entry in this structure can be either:
// - a regular XData entry (a length-4 list)
// - a "missing topic" XData entry
// - an error XData entry
class XDocData {
    _xhash = new Map();

    loadFromXdata(xdata) {
        for(const [key, data] of Object.entries(xdata)) {
            this.add(key, data);
        }
    }

    add(key, data) {
        this._xhash.set(key, data);
    }

    addError(key, data) {
        this._xhash.set(key, [[], data, data, data]);
    }

    addMissing(key) {
        this.addError(key, "Error: no such topic.");
    }

    allKeys() {
        return this._xhash.keys();
    }

    topicExists(key) {
        return this._xhash.has(key);
    }

    getTopicField(key, field, defaultValue) {
        return this.topicExists(key) ? this._xhash.get(key)[field] : defaultValue;
    }

    topicParentNames(key) {
        return this.getTopicField(key, XD_PNAMES, []);
    }

    topicFrom(key) {
        return this.getTopicField(key, XD_FROM, `Error: Key ${key} not found`);
    }

    topicBasepkg(key) {
        return this.getTopicField(key, XD_BASEPKG, `Error: Key ${key} not found`);
    }

    topicLong(key) {
        return this.getTopicField(key, XD_LONG, `Error: Key ${key} not found`);
    }
}