/usr/share/acl2-8.0dfsg/books/xdoc/autolink.lisp is in acl2-books-source 8.0dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | ; XDOC Documentation System for ACL2
; Copyright (C) 2009-2015 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>
; autolink.lisp -- automatically insert links into pretty-printed s-expressions
(in-package "XDOC")
(include-book "fmt-to-str")
(include-book "names")
(local (include-book "misc/assert" :dir :system))
(set-state-ok t)
(program)
; Convention: X is a string we are traversing, N is our current position in the
; string, and XL is the length of the string. The imagined guard is:
;
; (declare (xargs :guard (and (stringp x)
; (natp n)
; (natp xl)
; (= xl (length x))
; (<= n xl))))
;
; We could do a lot of this in logic mode, but there doesn't seem to be much
; point to that.
(defun error-context (x n xl) ;; ==> STRING
;; Tries to show what text is near an error.
(declare (type string x))
(b* ((min (nfix (- n 40)))
(max (min (+ n 20) xl)))
(subseq x min max)))
(defun skip-past-ws (x n xl) ;; ==> N-PRIME
(declare (type string x))
(cond ((eql xl n)
n)
((member (char x n) '(#\Space #\Tab #\Newline #\Page))
(skip-past-ws x (+ 1 n) xl))
(t
n)))
; What a pain. We have to implement a symbol parser.
(defun parse-symbol-name-part (x n xl
bar-escape-p ; have we read an opening bar?
slash-escape-p ; have we just read a backslash?
some-chars-p ; have we read any chars at all yet?
nice-error-msg-p ; do we care about nice error msgs?
acc)
;; ==> (MV ERROR NAME N-PRIME)
(declare (type string x))
; This tries to read just one part of a symbol name (i.e., the package part,
; or the name part.)
(b* (((when (= xl n))
;; End of string? Error if we were escaped, or if we have not actually
;; read some characters yet. Otherwise, it was okay.
(b* ((result (str::rchars-to-string acc))
((when (or bar-escape-p slash-escape-p (not some-chars-p)))
(mv (if nice-error-msg-p
(str::cat "Near " (error-context x n xl)
": unexpected end of string while reading symbol. "
"Characters read so far: " result)
"Symbol Parse Error")
result n)))
(mv nil result n)))
(n+1 (+ n 1))
(char (char x n))
((when slash-escape-p)
;; Slash escape is on, so just add next char verbatim and turn off
;; slash escape.
(parse-symbol-name-part x n+1 xl bar-escape-p nil t nice-error-msg-p (cons char acc)))
((when (eql char #\|))
;; Bar just toggles bar-escaped-ness.
(parse-symbol-name-part x n+1 xl (not bar-escape-p) nil t nice-error-msg-p acc))
((when (eql char #\\))
;; Slash starts a slash-escape.
(parse-symbol-name-part x n+1 xl bar-escape-p t t nice-error-msg-p acc))
((when bar-escape-p)
;; Bar-escape is on and not a special char. Read verbatim through it's
;; turned off.
(parse-symbol-name-part x n+1 xl t nil t nice-error-msg-p (cons char acc)))
((when (member char '(#\Space #\( #\) #\Newline #\Tab #\Page #\: #\, #\' #\`)))
;; Whitespace, paren, colon, comma, quote, backquote, outside of a bar
;; escape; end of symbol. We can stop as long as we've actually read
;; some characters.
(if some-chars-p
(mv nil (str::rchars-to-string acc) n)
(mv (if nice-error-msg-p
(str::cat "Near " (error-context x n xl)
": expected to read some part of a symbol, but found "
(implode (list char)) ".")
"Symbol Parse Error")
"" n)))
((when (or (and (char<= #\a char) (char<= char #\z))))
;; lowercase letters outside of bar escape get capitalized
(parse-symbol-name-part x n+1 xl nil nil t nice-error-msg-p
(cons (acl2::char-upcase char) acc))))
;; Otherwise add the char verbatim
(parse-symbol-name-part x n+1 xl nil nil t nice-error-msg-p (cons char acc))))
(defun parse-symbol (x n xl
base-pkg ; default package to intern into
kpa ; (known-package-alist state)
nice-error-msg-p)
;; ==> (MV ERROR SYMBOL N-PRIME)
(declare (type string x))
; This extends parse-symbol-name-part to read both parts. We support keywords,
; etc. This is definitely not going to handle everything in Common Lisp, but
; whatever.
(b* (((when (= xl n))
(mv (if nice-error-msg-p
(str::cat "Near " (error-context x n xl)
": end of string while trying to parse a symbol.")
"Symbol Parse Error")
nil n))
(char (char x n))
((when (eql char #\:))
;; Starts with a colon. Maybe it's keyword symbol?
(b* (((mv error name n)
(parse-symbol-name-part x (+ n 1) xl nil nil nil nice-error-msg-p nil)))
(if error
(mv error nil n)
(mv nil (intern-in-package-of-symbol name :keyword) n))))
;; Could start with a package name, or with the symbol name (for symbols
;; in the base pkg). Either way, we need to read a symbol name part.
((mv error part1 n)
(parse-symbol-name-part x n xl nil nil nil nice-error-msg-p nil))
((when error)
(mv error nil n))
((when (and (< (+ n 1) xl)
(eql (char x n) #\:)
(eql (char x (+ n 1)) #\:)))
;; Found "::" after the first part, so it's a package name.
(b* (((unless (assoc-equal part1 kpa))
(mv (if nice-error-msg-p
(str::cat "Near " (error-context x n xl)
": not a known package: " part1 ".")
"Symbol Parse Error")
nil n))
((mv error part2 n)
(parse-symbol-name-part x (+ n 2) xl nil nil nil nice-error-msg-p nil))
((when error)
(mv error nil n))
;; Things look pretty good here. One weird thing we will try to
;; detect is if there are extra colons, e.g., foo::bar::baz should
;; be disallowed. We really want a whitespace or paren or quote
;; or something
((when (and (< n xl)
(eql (char x n) #\:)))
(mv (if nice-error-msg-p
(str::cat "Near " (error-context x n xl)
": Three layers of colons in symbol name?")
"Symbol Parse Error")
nil n)))
(mv nil (intern$ part2 part1) n)))
;; Didn't find a :: after part1.
((when (and (< n xl)
(eql (char x n) #\:)))
(mv (if nice-error-msg-p
(str::cat "Near " (error-context x n xl)
": Lone colon after symbol name?")
"Symbol Parse Error")
nil n)))
;; We seem to have an okay package name, but no ::, so put it into the base
;; package.
(mv nil (intern-in-package-of-symbol part1 base-pkg) n)))
(encapsulate
()
(logic) ;; since otherwise local stuff gets skipped
(local
(defun test (x expect-errmsg expect-result expect-n-prime)
(declare (xargs :mode :program))
(b* ((known-pkgs (pairlis$ '("KEYWORD" "ACL2" "XDOC") nil))
((mv errmsg result n-prime)
(parse-symbol x 0 (length x) 'acl2::foo known-pkgs t)))
(cw "Errmsg: Found ~x0, expected ~x1~%" errmsg expect-errmsg)
(cw "Result: Found ~x0, expected ~x1~%" result expect-result)
(cw "N-prime: Found ~x0, expected ~x1~%" n-prime expect-n-prime)
(and (or (iff errmsg expect-errmsg) (cw "Errmsg failed!~%"))
(or expect-errmsg
(equal result expect-result)
(cw "Result failed!~%"))
(or expect-errmsg
(equal n-prime expect-n-prime)
(cw "N-Prime failed!~%"))))))
(local
(progn
;; Things that should work
(assert! (test "foo" nil 'acl2::foo 3))
(assert! (test "bar" nil 'acl2::bar 3))
(assert! (test "acl2::bar)" nil 'acl2::bar 9))
(assert! (test "xdoc::bar)" nil 'xdoc::bar 9))
(assert! (test "xdoc::|foo|)" nil 'xdoc::|foo| 11))
(assert! (test "xdoc::bar12 " nil 'xdoc::bar12 11))
(assert! (test ":foo)" nil :foo 4))
(assert! (test ":|foo|)" nil :|foo| 6))
(assert! (test ":||" nil :|| 3))
(assert! (test "acl2::bar|:|)" nil 'acl2::bar|:| 12))
;; Things that should fail
(assert! (test ":" t nil nil)) ;; lone colon, not ok
(assert! (test "||:" t nil nil)) ;; ending colon, not ok
(assert! (test "::|foo|)" t nil nil)) ;; starting colons w/o pkgname, not ok
(assert! (test "acl2:::bar)" t nil nil)) ;; three colons, not ok
(assert! (test "acl2::bar:" t nil nil)) ;; extra colon, not ok
;; Uh... bug? feature?
(assert! (test "123" nil 'acl2::|123| 3)))))
(defun autolink-and-encode (x n xl topics base-pkg kpa acc) ;; ==> ACC
; Main routine for autolinking and HTML encoding s-expressions. X typically
; has a pretty-printed S-expression that we want to turn into an XDOC <code>
; segment. TOPICS is a fast alist whose keys are the known topic names, for
; fast lookups of whether a symbol is a topic.
;
; We walk over the string and look for symbols following open-parens; this
; limitation is meant to reduce the amount of symbol parsing we have to do and
; should be good enough to insert links to function calls. Whenever we find a
; symbol that is a documented topic, we insert a link to it. We also HTML
; encode the string in the process.
(b* (((when (int= n xl))
acc)
(char1 (char x n))
((when (eql char1 #\<)) ;; --> "<" in reverse
(autolink-and-encode x (+ 1 n) xl topics base-pkg kpa
(list* #\; #\t #\l #\& acc)))
((when (eql char1 #\>)) ;; --> ">" in reverse
(autolink-and-encode x (+ 1 n) xl topics base-pkg kpa
(list* #\; #\t #\g #\& acc)))
((when (eql char1 #\&)) ;; --> "&" in reverse
(autolink-and-encode x (+ 1 n) xl topics base-pkg kpa
(list* #\; #\p #\m #\a #\& acc)))
((when (eql char1 #\")) ;; --> """ in reverse
(autolink-and-encode x (+ 1 n) xl topics base-pkg kpa
(list* #\; #\t #\o #\u #\q #\& acc)))
((unless (eql char1 #\())
;; Anything else except an open paren, we aren't going to do anything
;; special with. This way we don't have to call parse-symbol most of
;; the time.
(autolink-and-encode x (+ 1 n) xl topics base-pkg kpa (cons char1 acc)))
(acc (cons char1 acc))
((mv err symbol n-prime) (parse-symbol x (+ 1 n) xl base-pkg kpa nil))
((when err)
;; Failed to parse a valid symbol after it, so that's fine, maybe we hit
;; a quoted thing like '((1 . 2)) or whatever. Just keep going.
(autolink-and-encode x (+ 1 n) xl topics base-pkg kpa acc))
(look (hons-get symbol topics))
((unless look)
;; Nope, not a documented topic, so that's fine, just leave the paren
;; there and keep on encoding things without inserting a link.
(autolink-and-encode x (+ 1 n) xl topics base-pkg kpa acc))
;; Finally, the interesting case. We just found something like
;; "(append". We want to convert this into, e.g.,
;; (<see topic='[url-for-append]'>append</see>
;; and then keep going with our encoding.
(acc
;; <see topic="
(list* #\" #\= #\c #\i #\p #\o #\t #\Space #\e #\e #\s #\< acc))
(acc (file-name-mangle symbol acc))
(acc
;; ">
(list* #\> #\" acc))
;; Subtle: normally the "xl" argument should be the length of the string
;; here, but we only want to encode the part of the name that we read.
;; So, use the new N-PRIME returned by the symbol parser for the XL part
;; to stop early.
(acc (simple-html-encode-str x (+ 1 n) n-prime acc))
(acc
;; </see>
(list* #\> #\e #\e #\s #\/ #\< acc)))
;; Finally recur...
(autolink-and-encode x n-prime xl topics base-pkg kpa acc)))
(encapsulate
()
(logic)
(local (defun test (str expect)
(declare (xargs :mode :program))
(b* ((topics '(acl2::f g h foo bar baz + - xdoc::top1 xdoc::top2)) ;; just for testing
(alist (make-fast-alist (pairlis$ topics nil)))
(known-pkgs (pairlis$ '("ACL2" "KEYWORD" "XDOC") nil))
(acc (autolink-and-encode str 0 (length str) alist 'acl2::foo known-pkgs nil))
(- (fast-alist-free alist))
(result (str::rchars-to-string acc)))
(or (equal result expect)
(cw "Result: ~x0~%" result)
(cw "Expected: ~x0~%" expect)))))
(local
(progn
(assert! (test "foo"
;; -->
"foo"))
(assert! (test "foo & bar"
;; -->
"foo & bar"))
(assert! (test "foo & bar <baz>"
;; -->
"foo & bar <baz>"))
(assert! (test "(foo (+ 1 2))"
;; note that foo doesn't get processed: xdoc::foo is among the topics, but
;; since the base-pkg is acl2, the foo here is acl2::foo.
"(foo (<see topic=\"COMMON-LISP_____B2\">+</see> 1 2))"))
(assert! (test "(xdoc::foo (f 1 2))"
;; -->
"(<see topic=\"XDOC____FOO\">xdoc::foo</see> (<see topic=\"ACL2____F\">f</see> 1 2))")))))
(defun xml-ppr-obj-aux
(x ; object to pretty-print
topics-fal ; fast alist binding all xdoc topic names to [irrelevant]
base-pkg ; base package to print from
state
acc)
(b* ((kpa (known-package-alist state))
(str (fmt-to-str x base-pkg))
(acc (autolink-and-encode str 0 (length str) topics-fal base-pkg kpa acc)))
acc))
(defun xml-ppr-obj-fn (x topics-fal base-pkg state)
(str::rchars-to-string
(xml-ppr-obj-aux x topics-fal base-pkg state nil)))
(defmacro xml-ppr-obj (x &key
(state 'state)
(topics-fal 'nil)
(base-pkg ''acl2::foo))
`(b* ((acc (xml-ppr-obj-aux ,x ,topics-fal ,base-pkg ,state nil))
(ret (str::rchars-to-string acc)))
ret))
(local
(progn
(assert! (equal (xml-ppr-obj '(f 1 2)
:topics-fal (make-fast-alist '((f . nil))))
"(<see topic=\"XDOC____F\">xdoc::f</see> 1 2)"))
(assert! (equal (xml-ppr-obj '(f 1 2)
:topics-fal (make-fast-alist '((f . nil)))
:base-pkg 'xdoc::foo)
"(<see topic=\"XDOC____F\">f</see> 1 2)"))))
|