texinfo.scm 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. ;;;; (texinfo) -- parsing of texinfo into SXML
  2. ;;;;
  3. ;;;; Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
  4. ;;;; Copyright (C) 2004, 2009 Andy Wingo <wingo at pobox dot com>
  5. ;;;; Copyright (C) 2001,2002 Oleg Kiselyov <oleg at pobox dot com>
  6. ;;;;
  7. ;;;; This file is based on SSAX's SSAX.scm.
  8. ;;;;
  9. ;;;; This library is free software; you can redistribute it and/or
  10. ;;;; modify it under the terms of the GNU Lesser General Public
  11. ;;;; License as published by the Free Software Foundation; either
  12. ;;;; version 3 of the License, or (at your option) any later version.
  13. ;;;;
  14. ;;;; This library is distributed in the hope that it will be useful,
  15. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. ;;;; Lesser General Public License for more details.
  18. ;;;;
  19. ;;;; You should have received a copy of the GNU Lesser General Public
  20. ;;;; License along with this library; if not, write to the Free Software
  21. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. ;;; Commentary:
  23. ;;
  24. ;; @subheading Texinfo processing in scheme
  25. ;;
  26. ;; This module parses texinfo into SXML. TeX will always be the
  27. ;; processor of choice for print output, of course. However, although
  28. ;; @code{makeinfo} works well for info, its output in other formats is
  29. ;; not very customizable, and the program is not extensible as a whole.
  30. ;; This module aims to provide an extensible framework for texinfo
  31. ;; processing that integrates texinfo into the constellation of SXML
  32. ;; processing tools.
  33. ;;
  34. ;; @subheading Notes on the SXML vocabulary
  35. ;;
  36. ;; Consider the following texinfo fragment:
  37. ;;
  38. ;;@example
  39. ;; @@deffn Primitive set-car! pair value
  40. ;; This function...
  41. ;; @@end deffn
  42. ;;@end example
  43. ;;
  44. ;; Logically, the category (Primitive), name (set-car!), and arguments
  45. ;; (pair value) are ``attributes'' of the deffn, with the description as
  46. ;; the content. However, texinfo allows for @@-commands within the
  47. ;; arguments to an environment, like @code{@@deffn}, which means that
  48. ;; texinfo ``attributes'' are PCDATA. XML attributes, on the other hand,
  49. ;; are CDATA. For this reason, ``attributes'' of texinfo @@-commands are
  50. ;; called ``arguments'', and are grouped under the special element, `%'.
  51. ;;
  52. ;; Because `%' is not a valid NCName, stexinfo is a superset of SXML. In
  53. ;; the interests of interoperability, this module provides a conversion
  54. ;; function to replace the `%' with `texinfo-arguments'.
  55. ;;
  56. ;;; Code:
  57. ;; Comparison to xml output of texinfo (which is rather undocumented):
  58. ;; Doesn't conform to texinfo dtd
  59. ;; No DTD at all, in fact :-/
  60. ;; Actually outputs valid xml, after transforming %
  61. ;; Slower (although with caching the SXML that problem can go away)
  62. ;; Doesn't parse menus (although menus are shite)
  63. ;; Args go in a dedicated element, FBOFW
  64. ;; Definitions are handled a lot better
  65. ;; Does parse comments
  66. ;; Outputs only significant line breaks (a biggie!)
  67. ;; Nodes are treated as anchors, rather than content organizers (a biggie)
  68. ;; (more book-like, less info-like)
  69. ;; TODO
  70. ;; Integration: help, indexing, plain text
  71. (define-module (texinfo)
  72. #:use-module (sxml simple)
  73. #:use-module (sxml transform)
  74. #:use-module (sxml ssax input-parse)
  75. #:use-module (srfi srfi-1)
  76. #:use-module (srfi srfi-11)
  77. #:use-module (srfi srfi-13)
  78. #:export (call-with-file-and-dir
  79. texi-command-specs
  80. texi-command-depth
  81. texi-fragment->stexi
  82. texi->stexi
  83. stexi->sxml))
  84. ;; Some utilities
  85. (define (parser-error port message . rest)
  86. (apply throw 'parser-error port message rest))
  87. (define (call-with-file-and-dir filename proc)
  88. "Call the one-argument procedure @var{proc} with an input port that
  89. reads from @var{filename}. During the dynamic extent of @var{proc}'s
  90. execution, the current directory will be @code{(dirname
  91. @var{filename})}. This is useful for parsing documents that can include
  92. files by relative path name."
  93. (let ((current-dir (getcwd)))
  94. (dynamic-wind
  95. (lambda () (chdir (dirname filename)))
  96. (lambda ()
  97. (call-with-input-file (basename filename) proc))
  98. (lambda () (chdir current-dir)))))
  99. ;;========================================================================
  100. ;; Reflection on the XML vocabulary
  101. (define texi-command-specs
  102. #;
  103. "A list of (@var{name} @var{content-model} . @var{args})
  104. @table @var
  105. @item name
  106. The name of an @@-command, as a symbol.
  107. @item content-model
  108. A symbol indicating the syntactic type of the @@-command:
  109. @table @code
  110. @item EMPTY-COMMAND
  111. No content, and no @code{@@end} is coming
  112. @item EOL-ARGS
  113. Unparsed arguments until end of line
  114. @item EOL-TEXT
  115. Parsed arguments until end of line
  116. @item INLINE-ARGS
  117. Unparsed arguments ending with @code{#\\@}}
  118. @item INLINE-TEXT
  119. Parsed arguments ending with @code{#\\@}}
  120. @item INLINE-TEXT-ARGS
  121. Parsed arguments ending with @code{#\\@}}
  122. @item ENVIRON
  123. The tag is an environment tag, expect @code{@@end foo}.
  124. @item TABLE-ENVIRON
  125. Like ENVIRON, but with special parsing rules for its arguments.
  126. @item FRAGMENT
  127. For @code{*fragment*}, the command used for parsing fragments of
  128. texinfo documents.
  129. @end table
  130. @code{INLINE-TEXT} commands will receive their arguments within their
  131. bodies, whereas the @code{-ARGS} commands will receive them in their
  132. attribute list.
  133. @code{EOF-TEXT} receives its arguments in its body.
  134. @code{ENVIRON} commands have both: parsed arguments until the end of
  135. line, received through their attribute list, and parsed text until the
  136. @code{@@end}, received in their bodies.
  137. @code{EOF-TEXT-ARGS} receives its arguments in its attribute list, as in
  138. @code{ENVIRON}.
  139. In addition, @code{ALIAS} can alias one command to another. The alias
  140. will never be seen in parsed stexinfo.
  141. There are four @@-commands that are treated specially. @code{@@include}
  142. is a low-level token that will not be seen by higher-level parsers, so
  143. it has no content-model. @code{@@para} is the paragraph command, which
  144. is only implicit in the texinfo source. @code{@@item} has special
  145. syntax, as noted above, and @code{@@entry} is how this parser treats
  146. @code{@@item} commands within @code{@@table}, @code{@@ftable}, and
  147. @code{@@vtable}.
  148. Also, indexing commands (@code{@@cindex}, etc.) are treated specially.
  149. Their arguments are parsed, but they are needed before entering the
  150. element so that an anchor can be inserted into the text before the index
  151. entry.
  152. @item args
  153. Named arguments to the command, in the same format as the formals for a
  154. lambda. Only present for @code{INLINE-ARGS}, @code{EOL-ARGS},
  155. @code{INLINE-TEXT-ARGS}, @code{ENVIRON}, @code{TABLE-ENVIRON} commands.
  156. @end table"
  157. '(;; Special commands
  158. (include #f) ;; this is a low-level token
  159. (para PARAGRAPH)
  160. (item ITEM)
  161. (entry ENTRY . heading)
  162. (noindent EMPTY-COMMAND)
  163. (*fragment* FRAGMENT)
  164. ;; Inline text commands
  165. (*braces* INLINE-TEXT) ;; FIXME: make me irrelevant
  166. (bold INLINE-TEXT)
  167. (sample INLINE-TEXT)
  168. (samp INLINE-TEXT)
  169. (code INLINE-TEXT)
  170. (math INLINE-TEXT)
  171. (kbd INLINE-TEXT)
  172. (key INLINE-TEXT)
  173. (var INLINE-TEXT)
  174. (env INLINE-TEXT)
  175. (file INLINE-TEXT)
  176. (command INLINE-TEXT)
  177. (option INLINE-TEXT)
  178. (dfn INLINE-TEXT)
  179. (cite INLINE-TEXT)
  180. (acro INLINE-TEXT)
  181. (email INLINE-TEXT)
  182. (emph INLINE-TEXT)
  183. (strong INLINE-TEXT)
  184. (sample INLINE-TEXT)
  185. (sc INLINE-TEXT)
  186. (titlefont INLINE-TEXT)
  187. (asis INLINE-TEXT)
  188. (b INLINE-TEXT)
  189. (i INLINE-TEXT)
  190. (r INLINE-TEXT)
  191. (sansserif INLINE-TEXT)
  192. (slanted INLINE-TEXT)
  193. (t INLINE-TEXT)
  194. ;; Inline args commands
  195. (value INLINE-ARGS . (key))
  196. (ref INLINE-ARGS . (node #:opt name section info-file manual))
  197. (xref INLINE-ARGS . (node #:opt name section info-file manual))
  198. (pxref INLINE-TEXT-ARGS
  199. . (node #:opt name section info-file manual))
  200. (url ALIAS . uref)
  201. (uref INLINE-TEXT-ARGS . (url #:opt title replacement))
  202. (anchor INLINE-ARGS . (name))
  203. (dots INLINE-ARGS . ())
  204. (result INLINE-ARGS . ())
  205. (bullet INLINE-ARGS . ())
  206. (copyright INLINE-ARGS . ())
  207. (tie INLINE-ARGS . ())
  208. (image INLINE-ARGS . (file #:opt width height alt-text extension))
  209. ;; Inline parsed args commands
  210. (acronym INLINE-TEXT-ARGS . (acronym #:opt meaning))
  211. ;; EOL args elements
  212. (node EOL-ARGS . (name #:opt next previous up))
  213. (c EOL-ARGS . all)
  214. (comment EOL-ARGS . all)
  215. (setchapternewpage EOL-ARGS . all)
  216. (sp EOL-ARGS . all)
  217. (page EOL-ARGS . ())
  218. (vskip EOL-ARGS . all)
  219. (syncodeindex EOL-ARGS . all)
  220. (contents EOL-ARGS . ())
  221. (shortcontents EOL-ARGS . ())
  222. (summarycontents EOL-ARGS . ())
  223. (insertcopying EOL-ARGS . ())
  224. (dircategory EOL-ARGS . (category))
  225. (top EOL-ARGS . (title))
  226. (printindex EOL-ARGS . (type))
  227. (paragraphindent EOL-ARGS . (indent))
  228. ;; EOL text commands
  229. (*ENVIRON-ARGS* EOL-TEXT)
  230. (itemx EOL-TEXT)
  231. (set EOL-TEXT)
  232. (center EOL-TEXT)
  233. (title EOL-TEXT)
  234. (subtitle EOL-TEXT)
  235. (author EOL-TEXT)
  236. (chapter EOL-TEXT)
  237. (section EOL-TEXT)
  238. (appendix EOL-TEXT)
  239. (appendixsec EOL-TEXT)
  240. (unnumbered EOL-TEXT)
  241. (unnumberedsec EOL-TEXT)
  242. (subsection EOL-TEXT)
  243. (subsubsection EOL-TEXT)
  244. (appendixsubsec EOL-TEXT)
  245. (appendixsubsubsec EOL-TEXT)
  246. (unnumberedsubsec EOL-TEXT)
  247. (unnumberedsubsubsec EOL-TEXT)
  248. (chapheading EOL-TEXT)
  249. (majorheading EOL-TEXT)
  250. (heading EOL-TEXT)
  251. (subheading EOL-TEXT)
  252. (subsubheading EOL-TEXT)
  253. (deftpx EOL-TEXT-ARGS . (category name . attributes))
  254. (defcvx EOL-TEXT-ARGS . (category class name))
  255. (defivarx EOL-TEXT-ARGS . (class name))
  256. (deftypeivarx EOL-TEXT-ARGS . (class data-type name))
  257. (defopx EOL-TEXT-ARGS . (category class name . arguments))
  258. (deftypeopx EOL-TEXT-ARGS . (category class data-type name . arguments))
  259. (defmethodx EOL-TEXT-ARGS . (class name . arguments))
  260. (deftypemethodx EOL-TEXT-ARGS . (class data-type name . arguments))
  261. (defoptx EOL-TEXT-ARGS . (name))
  262. (defvrx EOL-TEXT-ARGS . (category name))
  263. (defvarx EOL-TEXT-ARGS . (name))
  264. (deftypevrx EOL-TEXT-ARGS . (category data-type name))
  265. (deftypevarx EOL-TEXT-ARGS . (data-type name))
  266. (deffnx EOL-TEXT-ARGS . (category name . arguments))
  267. (deftypefnx EOL-TEXT-ARGS . (category data-type name . arguments))
  268. (defspecx EOL-TEXT-ARGS . (name . arguments))
  269. (defmacx EOL-TEXT-ARGS . (name . arguments))
  270. (defunx EOL-TEXT-ARGS . (name . arguments))
  271. (deftypefunx EOL-TEXT-ARGS . (data-type name . arguments))
  272. ;; Indexing commands
  273. (cindex INDEX . entry)
  274. (findex INDEX . entry)
  275. (vindex INDEX . entry)
  276. (kindex INDEX . entry)
  277. (pindex INDEX . entry)
  278. (tindex INDEX . entry)
  279. ;; Environment commands (those that need @end)
  280. (texinfo ENVIRON . title)
  281. (ignore ENVIRON . ())
  282. (ifinfo ENVIRON . ())
  283. (iftex ENVIRON . ())
  284. (ifhtml ENVIRON . ())
  285. (ifxml ENVIRON . ())
  286. (ifplaintext ENVIRON . ())
  287. (ifnotinfo ENVIRON . ())
  288. (ifnottex ENVIRON . ())
  289. (ifnothtml ENVIRON . ())
  290. (ifnotxml ENVIRON . ())
  291. (ifnotplaintext ENVIRON . ())
  292. (titlepage ENVIRON . ())
  293. (menu ENVIRON . ())
  294. (direntry ENVIRON . ())
  295. (copying ENVIRON . ())
  296. (example ENVIRON . ())
  297. (smallexample ENVIRON . ())
  298. (display ENVIRON . ())
  299. (smalldisplay ENVIRON . ())
  300. (verbatim ENVIRON . ())
  301. (format ENVIRON . ())
  302. (smallformat ENVIRON . ())
  303. (lisp ENVIRON . ())
  304. (smalllisp ENVIRON . ())
  305. (cartouche ENVIRON . ())
  306. (quotation ENVIRON . ())
  307. (deftp ENVIRON . (category name . attributes))
  308. (defcv ENVIRON . (category class name))
  309. (defivar ENVIRON . (class name))
  310. (deftypeivar ENVIRON . (class data-type name))
  311. (defop ENVIRON . (category class name . arguments))
  312. (deftypeop ENVIRON . (category class data-type name . arguments))
  313. (defmethod ENVIRON . (class name . arguments))
  314. (deftypemethod ENVIRON . (class data-type name . arguments))
  315. (defopt ENVIRON . (name))
  316. (defvr ENVIRON . (category name))
  317. (defvar ENVIRON . (name))
  318. (deftypevr ENVIRON . (category data-type name))
  319. (deftypevar ENVIRON . (data-type name))
  320. (deffn ENVIRON . (category name . arguments))
  321. (deftypefn ENVIRON . (category data-type name . arguments))
  322. (defspec ENVIRON . (name . arguments))
  323. (defmac ENVIRON . (name . arguments))
  324. (defun ENVIRON . (name . arguments))
  325. (deftypefun ENVIRON . (data-type name . arguments))
  326. (table TABLE-ENVIRON . (formatter))
  327. (itemize TABLE-ENVIRON . (formatter))
  328. (enumerate TABLE-ENVIRON . (start))
  329. (ftable TABLE-ENVIRON . (formatter))
  330. (vtable TABLE-ENVIRON . (formatter))))
  331. (define command-depths
  332. '((chapter . 1) (section . 2) (subsection . 3) (subsubsection . 4)
  333. (top . 0) (unnumbered . 1) (unnumberedsec . 2)
  334. (unnumberedsubsec . 3) (unnumberedsubsubsec . 4)
  335. (appendix . 1) (appendixsec . 2) (appendixsection . 2)
  336. (appendixsubsec . 3) (appendixsubsubsec . 4)))
  337. (define (texi-command-depth command max-depth)
  338. "Given the texinfo command @var{command}, return its nesting level, or
  339. @code{#f} if it nests too deep for @var{max-depth}.
  340. Examples:
  341. @example
  342. (texi-command-depth 'chapter 4) @result{} 1
  343. (texi-command-depth 'top 4) @result{} 0
  344. (texi-command-depth 'subsection 4) @result{} 3
  345. (texi-command-depth 'appendixsubsec 4) @result{} 3
  346. (texi-command-depth 'subsection 2) @result{} #f
  347. @end example"
  348. (let ((depth (and=> (assq command command-depths) cdr)))
  349. (and depth (<= depth max-depth) depth)))
  350. ;; The % is for arguments
  351. (define (space-significant? command)
  352. (memq command
  353. '(example smallexample verbatim lisp smalllisp menu %)))
  354. ;; Like a DTD for texinfo
  355. (define (command-spec command)
  356. (let ((spec (assq command texi-command-specs)))
  357. (cond
  358. ((not spec)
  359. (parser-error #f "Unknown command" command))
  360. ((eq? (cadr spec) 'ALIAS)
  361. (command-spec (cddr spec)))
  362. (else
  363. spec))))
  364. (define (inline-content? content)
  365. (case content
  366. ((INLINE-TEXT INLINE-ARGS INLINE-TEXT-ARGS) #t)
  367. (else #f)))
  368. ;;========================================================================
  369. ;; Lower-level parsers and scanners
  370. ;;
  371. ;; They deal with primitive lexical units (Names, whitespaces, tags) and
  372. ;; with pieces of more generic productions. Most of these parsers must
  373. ;; be called in appropriate context. For example, complete-start-command
  374. ;; must be called only when the @-command start has been detected and
  375. ;; its name token has been read.
  376. ;; Test if a string is made of only whitespace
  377. ;; An empty string is considered made of whitespace as well
  378. (define (string-whitespace? str)
  379. (or (string-null? str)
  380. (string-every char-whitespace? str)))
  381. ;; Like read-text-line, but allows EOF.
  382. (define read-eof-breaks '(*eof* #\return #\newline))
  383. (define (read-eof-line port)
  384. (if (eof-object? (peek-char port))
  385. (peek-char port)
  386. (let* ((line (next-token '() read-eof-breaks
  387. "reading a line" port))
  388. (c (read-char port))) ; must be either \n or \r or EOF
  389. (if (and (eq? c #\return) (eq? (peek-char port) #\newline))
  390. (read-char port)) ; skip \n that follows \r
  391. line)))
  392. (define (skip-whitespace port)
  393. (skip-while '(#\space #\tab #\return #\newline) port))
  394. (define (skip-horizontal-whitespace port)
  395. (skip-while '(#\space #\tab) port))
  396. ;; command ::= Letter+
  397. ;; procedure: read-command PORT
  398. ;;
  399. ;; Read a command starting from the current position in the PORT and
  400. ;; return it as a symbol.
  401. (define (read-command port)
  402. (let ((first-char (peek-char port)))
  403. (or (char-alphabetic? first-char)
  404. (parser-error port "Nonalphabetic @-command char: '" first-char "'")))
  405. (string->symbol
  406. (next-token-of
  407. (lambda (c)
  408. (cond
  409. ((eof-object? c) #f)
  410. ((char-alphabetic? c) c)
  411. (else #f)))
  412. port)))
  413. ;; A token is a primitive lexical unit. It is a record with two fields,
  414. ;; token-head and token-kind.
  415. ;;
  416. ;; Token types:
  417. ;; END The end of a texinfo command. If the command is ended by },
  418. ;; token-head will be #f. Otherwise if the command is ended by
  419. ;; @end COMMAND, token-head will be COMMAND. As a special case,
  420. ;; @bye is the end of a special @texinfo command.
  421. ;; START The start of a texinfo command. The token-head will be a
  422. ;; symbol of the @-command name.
  423. ;; INCLUDE An @include directive. The token-head will be empty -- the
  424. ;; caller is responsible for reading the include file name.
  425. ;; ITEM @item commands have an irregular syntax. They end at the
  426. ;; next @item, or at the end of the environment. For that
  427. ;; read-command-token treats them specially.
  428. (define (make-token kind head) (cons kind head))
  429. (define token? pair?)
  430. (define token-kind car)
  431. (define token-head cdr)
  432. ;; procedure: read-command-token PORT
  433. ;;
  434. ;; This procedure starts parsing of a command token. The current
  435. ;; position in the stream must be #\@. This procedure scans enough of
  436. ;; the input stream to figure out what kind of a command token it is
  437. ;; seeing. The procedure returns a token structure describing the token.
  438. (define (read-command-token port)
  439. (assert-curr-char '(#\@) "start of the command" port)
  440. (let ((peeked (peek-char port)))
  441. (cond
  442. ((memq peeked '(#\! #\: #\. #\? #\@ #\\ #\{ #\}))
  443. ;; @-commands that escape characters
  444. (make-token 'STRING (string (read-char port))))
  445. (else
  446. (let ((name (read-command port)))
  447. (case name
  448. ((end)
  449. ;; got an ending tag
  450. (let ((command (string-trim-both
  451. (read-eof-line port))))
  452. (or (and (not (string-null? command))
  453. (string-every char-alphabetic? command))
  454. (parser-error port "malformed @end" command))
  455. (make-token 'END (string->symbol command))))
  456. ((bye)
  457. ;; the end of the top
  458. (make-token 'END 'texinfo))
  459. ((item)
  460. (make-token 'ITEM 'item))
  461. ((include)
  462. (make-token 'INCLUDE #f))
  463. (else
  464. (make-token 'START name))))))))
  465. ;; procedure+: read-verbatim-body PORT STR-HANDLER SEED
  466. ;;
  467. ;; This procedure must be called after we have read a string
  468. ;; "@verbatim\n" that begins a verbatim section. The current position
  469. ;; must be the first position of the verbatim body. This function reads
  470. ;; _lines_ of the verbatim body and passes them to a STR-HANDLER, a
  471. ;; character data consumer.
  472. ;;
  473. ;; The str-handler is a STR-HANDLER, a procedure STRING1 STRING2 SEED.
  474. ;; The first STRING1 argument to STR-HANDLER never contains a newline.
  475. ;; The second STRING2 argument often will. On the first invocation of the
  476. ;; STR-HANDLER, the seed is the one passed to read-verbatim-body
  477. ;; as the third argument. The result of this first invocation will be
  478. ;; passed as the seed argument to the second invocation of the line
  479. ;; consumer, and so on. The result of the last invocation of the
  480. ;; STR-HANDLER is returned by the read-verbatim-body. Note a
  481. ;; similarity to the fundamental 'fold' iterator.
  482. ;;
  483. ;; Within a verbatim section all characters are taken at their face
  484. ;; value. It ends with "\n@end verbatim(\r)?\n".
  485. ;; Must be called right after the newline after @verbatim.
  486. (define (read-verbatim-body port str-handler seed)
  487. (let loop ((seed seed))
  488. (let ((fragment (next-token '() '(#\newline)
  489. "reading verbatim" port)))
  490. ;; We're reading the char after the 'fragment', which is
  491. ;; #\newline.
  492. (read-char port)
  493. (if (string=? fragment "@end verbatim")
  494. seed
  495. (loop (str-handler fragment "\n" seed))))))
  496. ;; procedure+: read-arguments PORT
  497. ;;
  498. ;; This procedure reads and parses a production ArgumentList.
  499. ;; ArgumentList ::= S* Argument (S* , S* Argument)* S*
  500. ;; Argument ::= ([^@{},])*
  501. ;;
  502. ;; Arguments are the things in braces, i.e @ref{my node} has one
  503. ;; argument, "my node". Most commands taking braces actually don't have
  504. ;; arguments, they process text. For example, in
  505. ;; @emph{@strong{emphasized}}, the emph takes text, because the parse
  506. ;; continues into the braces.
  507. ;;
  508. ;; Any whitespace within Argument is replaced with a single space.
  509. ;; Whitespace around an Argument is trimmed.
  510. ;;
  511. ;; The procedure returns a list of arguments. Afterwards the current
  512. ;; character will be after the final #\}.
  513. (define (read-arguments port stop-char)
  514. (define (split str)
  515. (read-char port) ;; eat the delimiter
  516. (let ((ret (map (lambda (x) (if (string-null? x) #f x))
  517. (map string-trim-both (string-split str #\,)))))
  518. (if (and (pair? ret) (eq? (car ret) #f) (null? (cdr ret)))
  519. '()
  520. ret)))
  521. (split (next-token '() (list stop-char)
  522. "arguments of @-command" port)))
  523. ;; procedure+: complete-start-command COMMAND PORT
  524. ;;
  525. ;; This procedure is to complete parsing of an @-command. The procedure
  526. ;; must be called after the command token has been read. COMMAND is a
  527. ;; TAG-NAME.
  528. ;;
  529. ;; This procedure returns several values:
  530. ;; COMMAND: a symbol.
  531. ;; ARGUMENTS: command's arguments, as an alist.
  532. ;; CONTENT-MODEL: the content model of the command.
  533. ;;
  534. ;; On exit, the current position in PORT will depend on the CONTENT-MODEL.
  535. ;;
  536. ;; Content model Port position
  537. ;; ============= =============
  538. ;; INLINE-TEXT One character after the #\{.
  539. ;; INLINE-TEXT-ARGS One character after the #\{.
  540. ;; INLINE-ARGS The first character after the #\}.
  541. ;; EOL-TEXT The first non-whitespace character after the command.
  542. ;; ENVIRON, TABLE-ENVIRON, EOL-ARGS, EOL-TEXT
  543. ;; The first character on the next line.
  544. ;; PARAGRAPH, ITEM, EMPTY-COMMAND
  545. ;; The first character after the command.
  546. (define (arguments->attlist port args arg-names)
  547. (let loop ((in args) (names arg-names) (opt? #f) (out '()))
  548. (cond
  549. ((symbol? names) ;; a rest arg
  550. (reverse (if (null? in) out (acons names in out))))
  551. ((and (not (null? names)) (eq? (car names) #:opt))
  552. (loop in (cdr names) #t out))
  553. ((null? in)
  554. (if (or (null? names) opt?)
  555. (reverse out)
  556. (parser-error port "@-command expected more arguments:"
  557. args arg-names names)))
  558. ((null? names)
  559. (parser-error port "@-command didn't expect more arguments:" in))
  560. ((not (car in))
  561. (or (and opt? (loop (cdr in) (cdr names) opt? out))
  562. (parser-error "@-command missing required argument"
  563. (car names))))
  564. (else
  565. (loop (cdr in) (cdr names) opt?
  566. (acons (car names)
  567. (if (list? (car in)) (car in) (list (car in)))
  568. out))))))
  569. (define (parse-table-args command port)
  570. (let* ((line (string-trim-both (read-text-line port)))
  571. (length (string-length line)))
  572. (define (get-formatter)
  573. (or (and (not (zero? length))
  574. (eq? (string-ref line 0) #\@)
  575. (let ((f (string->symbol (substring line 1))))
  576. (or (inline-content? (cadr (command-spec f)))
  577. (parser-error
  578. port "@item formatter must be INLINE" f))
  579. f))
  580. (parser-error port "Invalid @item formatter" line)))
  581. (case command
  582. ((enumerate)
  583. (if (zero? length)
  584. '()
  585. `((start
  586. ,(if (or (and (eq? length 1)
  587. (char-alphabetic? (string-ref line 0)))
  588. (string-every char-numeric? line))
  589. line
  590. (parser-error
  591. port "Invalid enumerate start" line))))))
  592. ((itemize)
  593. `((bullet
  594. ,(or (and (eq? length 1) line)
  595. (and (string-null? line) '(bullet))
  596. (list (get-formatter))))))
  597. (else ;; tables of various varieties
  598. `((formatter (,(get-formatter))))))))
  599. (define (complete-start-command command port)
  600. (define (get-arguments type arg-names stop-char)
  601. (arguments->attlist port (read-arguments port stop-char) arg-names))
  602. (let* ((spec (command-spec command))
  603. (command (car spec))
  604. (type (cadr spec))
  605. (arg-names (cddr spec)))
  606. (case type
  607. ((INLINE-TEXT)
  608. (assert-curr-char '(#\{) "Inline element lacks {" port)
  609. (values command '() type))
  610. ((INLINE-ARGS)
  611. (assert-curr-char '(#\{) "Inline element lacks {" port)
  612. (values command (get-arguments type arg-names #\}) type))
  613. ((INLINE-TEXT-ARGS)
  614. (assert-curr-char '(#\{) "Inline element lacks {" port)
  615. (values command '() type))
  616. ((EOL-ARGS)
  617. (values command (get-arguments type arg-names #\newline) type))
  618. ((ENVIRON ENTRY INDEX)
  619. (skip-horizontal-whitespace port)
  620. (values command (parse-environment-args command port) type))
  621. ((TABLE-ENVIRON)
  622. (skip-horizontal-whitespace port)
  623. (values command (parse-table-args command port) type))
  624. ((EOL-TEXT)
  625. (skip-horizontal-whitespace port)
  626. (values command '() type))
  627. ((EOL-TEXT-ARGS)
  628. (skip-horizontal-whitespace port)
  629. (values command (parse-eol-text-args command port) type))
  630. ((PARAGRAPH EMPTY-COMMAND ITEM FRAGMENT)
  631. (values command '() type))
  632. (else ;; INCLUDE shouldn't get here
  633. (parser-error port "can't happen")))))
  634. ;;-----------------------------------------------------------------------------
  635. ;; Higher-level parsers and scanners
  636. ;;
  637. ;; They parse productions corresponding entire @-commands.
  638. ;; Only reads @settitle, leaves it to the command parser to finish
  639. ;; reading the title.
  640. (define (take-until-settitle port)
  641. (or (find-string-from-port? "\n@settitle " port)
  642. (parser-error port "No \\n@settitle found"))
  643. (skip-horizontal-whitespace port)
  644. (and (eq? (peek-char port) #\newline)
  645. (parser-error port "You have a @settitle, but no title")))
  646. ;; procedure+: read-char-data PORT EXPECT-EOF? STR-HANDLER SEED
  647. ;;
  648. ;; This procedure is to read the CharData of a texinfo document.
  649. ;;
  650. ;; text ::= (CharData | Command)*
  651. ;;
  652. ;; The procedure reads CharData and stops at @-commands (or
  653. ;; environments). It also stops at an open or close brace.
  654. ;;
  655. ;; port
  656. ;; a PORT to read
  657. ;; expect-eof?
  658. ;; a boolean indicating if EOF is normal, i.e., the character
  659. ;; data may be terminated by the EOF. EOF is normal
  660. ;; while processing the main document.
  661. ;; preserve-ws?
  662. ;; a boolean indicating if we are within a whitespace-preserving
  663. ;; environment. If #t, suppress paragraph detection.
  664. ;; str-handler
  665. ;; a STR-HANDLER, see read-verbatim-body
  666. ;; seed
  667. ;; an argument passed to the first invocation of STR-HANDLER.
  668. ;;
  669. ;; The procedure returns two results: SEED and TOKEN. The SEED is the
  670. ;; result of the last invocation of STR-HANDLER, or the original seed if
  671. ;; STR-HANDLER was never called.
  672. ;;
  673. ;; TOKEN can be either an eof-object (this can happen only if expect-eof?
  674. ;; was #t), or a texinfo token denoting the start or end of a tag.
  675. ;; read-char-data port expect-eof? preserve-ws? str-handler seed
  676. (define read-char-data
  677. (let* ((end-chars-eof '(*eof* #\{ #\} #\@ #\newline)))
  678. (define (handle str-handler str1 str2 seed)
  679. (if (and (string-null? str1) (string-null? str2))
  680. seed
  681. (str-handler str1 str2 seed)))
  682. (lambda (port expect-eof? preserve-ws? str-handler seed)
  683. (let ((end-chars ((if expect-eof? identity cdr) end-chars-eof)))
  684. (let loop ((seed seed))
  685. (let* ((fragment (next-token '() end-chars "reading char data" port))
  686. (term-char (peek-char port))) ; one of end-chars
  687. (cond
  688. ((eof-object? term-char) ; only if expect-eof?
  689. (values (handle str-handler fragment "" seed) term-char))
  690. ((memq term-char '(#\@ #\{ #\}))
  691. (values (handle str-handler fragment "" seed)
  692. (case term-char
  693. ((#\@) (read-command-token port))
  694. ((#\{) (make-token 'START '*braces*))
  695. ((#\}) (read-char port) (make-token 'END #f)))))
  696. ((eq? term-char #\newline)
  697. ;; Always significant, unless directly before an end token.
  698. (let ((c (peek-next-char port)))
  699. (cond
  700. ((eof-object? c)
  701. (or expect-eof?
  702. (parser-error port "EOF while reading char data"))
  703. (values (handle str-handler fragment "" seed) c))
  704. ((eq? c #\@)
  705. (let* ((token (read-command-token port))
  706. (end? (eq? (token-kind token) 'END)))
  707. (values
  708. (handle str-handler fragment
  709. (if end? "" (if preserve-ws? "\n" " "))
  710. seed)
  711. token)))
  712. ((and (not preserve-ws?) (eq? c #\newline))
  713. ;; paragraph-separator ::= #\newline #\newline+
  714. (skip-while '(#\newline) port)
  715. (skip-horizontal-whitespace port)
  716. (values (handle str-handler fragment "" seed)
  717. (make-token 'PARA 'para)))
  718. (else
  719. (loop (handle str-handler fragment
  720. (if preserve-ws? "\n" " ") seed)))))))))))))
  721. ; procedure+: assert-token TOKEN KIND NAME
  722. ; Make sure that TOKEN is of anticipated KIND and has anticipated NAME
  723. (define (assert-token token kind name)
  724. (or (and (token? token)
  725. (eq? kind (token-kind token))
  726. (equal? name (token-head token)))
  727. (parser-error #f "Expecting @end for " name ", got " token)))
  728. ;;========================================================================
  729. ;; Highest-level parsers: Texinfo to SXML
  730. ;; These parsers are a set of syntactic forms to instantiate a SSAX
  731. ;; parser. The user tells what to do with the parsed character and
  732. ;; element data. These latter handlers determine if the parsing follows a
  733. ;; SAX or a DOM model.
  734. ;; syntax: make-command-parser fdown fup str-handler
  735. ;; Create a parser to parse and process one element, including its
  736. ;; character content or children elements. The parser is typically
  737. ;; applied to the root element of a document.
  738. ;; fdown
  739. ;; procedure COMMAND ARGUMENTS EXPECTED-CONTENT SEED
  740. ;;
  741. ;; This procedure is to generate the seed to be passed to handlers
  742. ;; that process the content of the element. This is the function
  743. ;; identified as 'fdown' in the denotational semantics of the XML
  744. ;; parser given in the title comments to (sxml ssax).
  745. ;;
  746. ;; fup
  747. ;; procedure COMMAND ARGUMENTS PARENT-SEED SEED
  748. ;;
  749. ;; This procedure is called when parsing of COMMAND is finished.
  750. ;; The SEED is the result from the last content parser (or from
  751. ;; fdown if the element has the empty content). PARENT-SEED is the
  752. ;; same seed as was passed to fdown. The procedure is to generate a
  753. ;; seed that will be the result of the element parser. This is the
  754. ;; function identified as 'fup' in the denotational semantics of
  755. ;; the XML parser given in the title comments to (sxml ssax).
  756. ;;
  757. ;; str-handler
  758. ;; A STR-HANDLER, see read-verbatim-body
  759. ;;
  760. ;; The generated parser is a
  761. ;; procedure COMMAND PORT SEED
  762. ;;
  763. ;; The procedure must be called *after* the command token has been read.
  764. (define (read-include-file-name port)
  765. (let ((x (string-trim-both (read-eof-line port))))
  766. (if (string-null? x)
  767. (error "no file listed")
  768. x))) ;; fixme: should expand @value{} references
  769. (define (sxml->node-name sxml)
  770. "Turn some sxml string into a valid node name."
  771. (let loop ((in (string->list (sxml->string sxml))) (out '()))
  772. (if (null? in)
  773. (apply string (reverse out))
  774. (if (memq (car in) '(#\{ #\} #\@ #\,))
  775. (loop (cdr in) out)
  776. (loop (cdr in) (cons (car in) out))))))
  777. (define (index command arguments fdown fup parent-seed)
  778. (case command
  779. ((deftp defcv defivar deftypeivar defop deftypeop defmethod
  780. deftypemethod defopt defvr defvar deftypevr deftypevar deffn
  781. deftypefn defspec defmac defun deftypefun)
  782. (let ((args `((name ,(string-append (symbol->string command) "-"
  783. (cadr (assq 'name arguments)))))))
  784. (fup 'anchor args parent-seed
  785. (fdown 'anchor args 'INLINE-ARGS '()))))
  786. ((cindex findex vindex kindex pindex tindex)
  787. (let ((args `((name ,(string-append (symbol->string command) "-"
  788. (sxml->node-name
  789. (assq 'entry arguments)))))))
  790. (fup 'anchor args parent-seed
  791. (fdown 'anchor args 'INLINE-ARGS '()))))
  792. (else parent-seed)))
  793. (define (make-command-parser fdown fup str-handler)
  794. (lambda (command port seed)
  795. (let visit ((command command) (port port) (sig-ws? #f) (parent-seed seed))
  796. (let*-values (((command arguments expected-content)
  797. (complete-start-command command port)))
  798. (let* ((parent-seed (index command arguments fdown fup parent-seed))
  799. (seed (fdown command arguments expected-content parent-seed))
  800. (eof-closes? (or (memq command '(texinfo para *fragment*))
  801. (eq? expected-content 'EOL-TEXT)))
  802. (sig-ws? (or sig-ws? (space-significant? command)))
  803. (up (lambda (s) (fup command arguments parent-seed s)))
  804. (new-para (lambda (s) (fdown 'para '() 'PARAGRAPH s)))
  805. (make-end-para (lambda (p) (lambda (s) (fup 'para '() p s)))))
  806. (define (port-for-content)
  807. (if (eq? expected-content 'EOL-TEXT)
  808. (call-with-input-string (read-text-line port) identity)
  809. port))
  810. (cond
  811. ((memq expected-content '(EMPTY-COMMAND INLINE-ARGS EOL-ARGS INDEX
  812. EOL-TEXT-ARGS))
  813. ;; empty or finished by complete-start-command
  814. (up seed))
  815. ((eq? command 'verbatim)
  816. (up (read-verbatim-body port str-handler seed)))
  817. (else
  818. (let loop ((port (port-for-content))
  819. (expect-eof? eof-closes?)
  820. (end-para identity)
  821. (need-break? (and (not sig-ws?)
  822. (memq expected-content
  823. '(ENVIRON TABLE-ENVIRON
  824. ENTRY ITEM FRAGMENT))))
  825. (seed seed))
  826. (cond
  827. ((and need-break? (or sig-ws? (skip-whitespace port))
  828. (not (memq (peek-char port) '(#\@ #\})))
  829. (not (eof-object? (peek-char port))))
  830. ;; Even if we have an @, it might be inline -- check
  831. ;; that later
  832. (let ((seed (end-para seed)))
  833. (loop port expect-eof? (make-end-para seed) #f
  834. (new-para seed))))
  835. (else
  836. (let*-values (((seed token)
  837. (read-char-data
  838. port expect-eof? sig-ws? str-handler seed)))
  839. (cond
  840. ((eof-object? token)
  841. (case expect-eof?
  842. ((include #f) (end-para seed))
  843. (else (up (end-para seed)))))
  844. (else
  845. (case (token-kind token)
  846. ((STRING)
  847. ;; this is only @-commands that escape
  848. ;; characters: @}, @@, @{ -- new para if need-break
  849. (let ((seed ((if need-break? end-para identity) seed)))
  850. (loop port expect-eof?
  851. (if need-break? (make-end-para seed) end-para) #f
  852. (str-handler (token-head token) ""
  853. ((if need-break? new-para identity)
  854. seed)))))
  855. ((END)
  856. ;; The end will only have a name if it's for an
  857. ;; environment
  858. (cond
  859. ((memq command '(item entry))
  860. (let ((spec (command-spec (token-head token))))
  861. (or (eq? (cadr spec) 'TABLE-ENVIRON)
  862. (parser-error
  863. port "@item not ended by @end table/enumerate/itemize"
  864. token))))
  865. ((eq? expected-content 'ENVIRON)
  866. (assert-token token 'END command)))
  867. (up (end-para seed)))
  868. ((ITEM)
  869. (cond
  870. ((memq command '(enumerate itemize))
  871. (up (visit 'item port sig-ws? (end-para seed))))
  872. ((eq? expected-content 'TABLE-ENVIRON)
  873. (up (visit 'entry port sig-ws? (end-para seed))))
  874. ((memq command '(item entry))
  875. (visit command port sig-ws? (up (end-para seed))))
  876. (else
  877. (parser-error
  878. port "@item must be within a table environment"
  879. command))))
  880. ((PARA)
  881. ;; examine valid paragraphs?
  882. (loop port expect-eof? end-para (not sig-ws?) seed))
  883. ((INCLUDE)
  884. ;; Recurse for include files
  885. (let ((seed (call-with-file-and-dir
  886. (read-include-file-name port)
  887. (lambda (port)
  888. (loop port 'include end-para
  889. need-break? seed)))))
  890. (loop port expect-eof? end-para need-break? seed)))
  891. ((START) ; Start of an @-command
  892. (let* ((head (token-head token))
  893. (spec (command-spec head))
  894. (head (car spec))
  895. (type (cadr spec))
  896. (inline? (inline-content? type))
  897. (seed ((if (and inline? (not need-break?))
  898. identity end-para) seed))
  899. (end-para (if inline?
  900. (if need-break? (make-end-para seed)
  901. end-para)
  902. identity))
  903. (new-para (if (and inline? need-break?)
  904. new-para identity)))
  905. (loop port expect-eof? end-para (not inline?)
  906. (visit head port sig-ws? (new-para seed)))))
  907. (else
  908. (parser-error port "Unknown token type" token))))))))))))))))
  909. ;; procedure: reverse-collect-str-drop-ws fragments
  910. ;;
  911. ;; Given the list of fragments (some of which are text strings), reverse
  912. ;; the list and concatenate adjacent text strings. We also drop
  913. ;; "unsignificant" whitespace, that is, whitespace in front, behind and
  914. ;; between elements. The whitespace that is included in character data
  915. ;; is not affected.
  916. (define (reverse-collect-str-drop-ws fragments)
  917. (cond
  918. ((null? fragments) ; a shortcut
  919. '())
  920. ((and (string? (car fragments)) ; another shortcut
  921. (null? (cdr fragments)) ; remove single ws-only string
  922. (string-whitespace? (car fragments)))
  923. '())
  924. (else
  925. (let loop ((fragments fragments) (result '()) (strs '())
  926. (all-whitespace? #t))
  927. (cond
  928. ((null? fragments)
  929. (if all-whitespace?
  930. result ; remove leading ws
  931. (cons (apply string-append strs) result)))
  932. ((string? (car fragments))
  933. (loop (cdr fragments) result (cons (car fragments) strs)
  934. (and all-whitespace?
  935. (string-whitespace? (car fragments)))))
  936. (else
  937. (loop (cdr fragments)
  938. (cons
  939. (car fragments)
  940. (cond
  941. ((null? strs) result)
  942. (all-whitespace?
  943. (if (null? result)
  944. result ; remove trailing whitespace
  945. (cons " " result))); replace interstitial ws with
  946. ; one space
  947. (else
  948. (cons (apply string-append strs) result))))
  949. '() #t)))))))
  950. (define (parse-inline-text-args port spec text)
  951. (let lp ((in text) (cur '()) (out '()))
  952. (cond
  953. ((null? in)
  954. (if (and (pair? cur)
  955. (string? (car cur))
  956. (string-whitespace? (car cur)))
  957. (lp in (cdr cur) out)
  958. (let ((args (reverse (if (null? cur)
  959. out
  960. (cons (reverse cur) out)))))
  961. (arguments->attlist port args (cddr spec)))))
  962. ((pair? (car in))
  963. (lp (cdr in) (cons (car in) cur) out))
  964. ((string-index (car in) #\,)
  965. (let* ((parts (string-split (car in) #\,))
  966. (head (string-trim-right (car parts)))
  967. (rev-tail (reverse (cdr parts)))
  968. (last (string-trim (car rev-tail))))
  969. (lp (cdr in)
  970. (if (string-null? last) cur (cons last cur))
  971. (append (cdr rev-tail)
  972. (cons (reverse (if (string-null? head) cur (cons head cur)))
  973. out)))))
  974. (else
  975. (lp (cdr in)
  976. (cons (if (null? cur) (string-trim (car in)) (car in)) cur)
  977. out)))))
  978. (define (make-dom-parser)
  979. (make-command-parser
  980. (lambda (command args content seed) ; fdown
  981. '())
  982. (lambda (command args parent-seed seed) ; fup
  983. (let* ((seed (reverse-collect-str-drop-ws seed))
  984. (spec (command-spec command))
  985. (command (car spec)))
  986. (if (eq? (cadr spec) 'INLINE-TEXT-ARGS)
  987. (cons (list command (cons '% (parse-inline-text-args #f spec seed)))
  988. parent-seed)
  989. (acons command
  990. (if (null? args) seed (acons '% args seed))
  991. parent-seed))))
  992. (lambda (string1 string2 seed) ; str-handler
  993. (if (string-null? string2)
  994. (cons string1 seed)
  995. (cons* string2 string1 seed)))))
  996. (define parse-environment-args
  997. (let ((parser (make-dom-parser)))
  998. ;; duplicate arguments->attlist to avoid unnecessary splitting
  999. (lambda (command port)
  1000. (let* ((args (cdar (parser '*ENVIRON-ARGS* port '())))
  1001. (spec (command-spec command))
  1002. (command (car spec))
  1003. (arg-names (cddr spec)))
  1004. (cond
  1005. ((not arg-names)
  1006. (if (null? args) '()
  1007. (parser-error port "@-command doesn't take args" command)))
  1008. ((eq? arg-names #t)
  1009. (list (cons 'arguments args)))
  1010. (else
  1011. (let loop ((args args) (arg-names arg-names) (out '()))
  1012. (cond
  1013. ((null? arg-names)
  1014. (if (null? args) (reverse! out)
  1015. (parser-error port "@-command didn't expect more args"
  1016. command args)))
  1017. ((symbol? arg-names)
  1018. (reverse! (acons arg-names args out)))
  1019. ((null? args)
  1020. (parser-error port "@-command expects more args"
  1021. command arg-names))
  1022. ((and (string? (car args)) (string-index (car args) #\space))
  1023. => (lambda (i)
  1024. (let ((rest (substring/shared (car args) (1+ i))))
  1025. (if (zero? i)
  1026. (loop (cons rest (cdr args)) arg-names out)
  1027. (loop (cons rest (cdr args)) (cdr arg-names)
  1028. (cons (list (car arg-names)
  1029. (substring (car args) 0 i))
  1030. out))))))
  1031. (else
  1032. (loop (cdr args) (cdr arg-names)
  1033. (if (and (pair? (car args)) (eq? (caar args) '*braces*))
  1034. (acons (car arg-names) (cdar args) out)
  1035. (cons (list (car arg-names) (car args)) out))))))))))))
  1036. (define (parse-eol-text-args command port)
  1037. ;; perhaps parse-environment-args should be named more
  1038. ;; generically.
  1039. (parse-environment-args command port))
  1040. ;; procedure: texi-fragment->stexi STRING
  1041. ;;
  1042. ;; A DOM parser for a texinfo fragment STRING.
  1043. ;;
  1044. ;; The procedure returns an SXML tree headed by the special tag,
  1045. ;; *fragment*.
  1046. (define (texi-fragment->stexi string-or-port)
  1047. "Parse the texinfo commands in @var{string-or-port}, and return the
  1048. resultant stexi tree. The head of the tree will be the special command,
  1049. @code{*fragment*}."
  1050. (define (parse port)
  1051. (postprocess (car ((make-dom-parser) '*fragment* port '()))))
  1052. (if (input-port? string-or-port)
  1053. (parse string-or-port)
  1054. (call-with-input-string string-or-port parse)))
  1055. ;; procedure: texi->stexi PORT
  1056. ;;
  1057. ;; This is an instance of a SSAX parser above that returns an SXML
  1058. ;; representation of the texinfo document ready to be read at PORT.
  1059. ;;
  1060. ;; The procedure returns an SXML tree. The port points to the
  1061. ;; first character after the @bye, or to the end of the file.
  1062. (define (texi->stexi port)
  1063. "Read a full texinfo document from @var{port} and return the parsed
  1064. stexi tree. The parsing will start at the @code{@@settitle} and end at
  1065. @code{@@bye} or EOF."
  1066. (let ((parser (make-dom-parser)))
  1067. (take-until-settitle port)
  1068. (postprocess (car (parser 'texinfo port '())))))
  1069. (define (car-eq? x y) (and (pair? x) (eq? (car x) y)))
  1070. (define (make-contents tree)
  1071. (define (lp in out depth)
  1072. (cond
  1073. ((null? in) (values in (cons 'enumerate (reverse! out))))
  1074. ((and (pair? (cdr in)) (texi-command-depth (caadr in) 4))
  1075. => (lambda (new-depth)
  1076. (let ((node-name (and (car-eq? (car in) 'node)
  1077. (cadr (assq 'name (cdadar in))))))
  1078. (cond
  1079. ((< new-depth depth)
  1080. (values in (cons 'enumerate (reverse! out))))
  1081. ((> new-depth depth)
  1082. (let ((out-cdr (if (null? out) '() (cdr out)))
  1083. (out-car (if (null? out) (list 'item) (car out))))
  1084. (let*-values (((new-in new-out) (lp in '() (1+ depth))))
  1085. (lp new-in
  1086. (cons (append out-car (list new-out)) out-cdr)
  1087. depth))))
  1088. (else ;; same depth
  1089. (lp (cddr in)
  1090. (cons
  1091. `(item (para
  1092. ,@(if node-name
  1093. `((ref (% (node ,node-name))))
  1094. (cdadr in))))
  1095. out)
  1096. depth))))))
  1097. (else (lp (cdr in) out depth))))
  1098. (let*-values (((_ contents) (lp tree '() 1)))
  1099. `((chapheading "Table of Contents") ,contents)))
  1100. (define (trim-whitespace str trim-left? trim-right?)
  1101. (let* ((left-space? (and (not trim-left?)
  1102. (string-prefix? " " str)))
  1103. (right-space? (and (not trim-right?)
  1104. (string-suffix? " " str)))
  1105. (tail (append! (string-tokenize str)
  1106. (if right-space? '("") '()))))
  1107. (string-join (if left-space? (cons "" tail) tail))))
  1108. (define (postprocess tree)
  1109. (define (loop in out state first? sig-ws?)
  1110. (cond
  1111. ((null? in)
  1112. (values (reverse! out) state))
  1113. ((string? (car in))
  1114. (loop (cdr in)
  1115. (cons (if sig-ws? (car in)
  1116. (trim-whitespace (car in) first? (null? (cdr in))))
  1117. out)
  1118. state #f sig-ws?))
  1119. ((pair? (car in))
  1120. (case (caar in)
  1121. ((set)
  1122. (if (null? (cdar in)) (error "@set missing arguments" in))
  1123. (if (string? (cadar in))
  1124. (let ((i (string-index (cadar in) #\space)))
  1125. (if i
  1126. (loop (cdr in) out
  1127. (acons (substring (cadar in) 0 i)
  1128. (cons (substring (cadar in) (1+ i)) (cddar in))
  1129. state)
  1130. #f sig-ws?)
  1131. (loop (cdr in) out (acons (cadar in) (cddar in) state)
  1132. #f sig-ws?)))
  1133. (error "expected a constant to define for @set" in)))
  1134. ((value)
  1135. (loop (fold-right cons (cdr in)
  1136. (or (and=>
  1137. (assoc (cadr (assq 'key (cdadar in))) state) cdr)
  1138. (error "unknown value" (cdadar in) state)))
  1139. out
  1140. state #f sig-ws?))
  1141. ((copying)
  1142. (loop (cdr in) out (cons (car in) state) #f sig-ws?))
  1143. ((insertcopying)
  1144. (loop (fold-right cons (cdr in)
  1145. (or (cdr (assoc 'copying state))
  1146. (error "copying isn't set yet")))
  1147. out
  1148. state #f sig-ws?))
  1149. ((contents)
  1150. (loop (cdr in) (fold cons out (make-contents tree)) state #f sig-ws?))
  1151. (else
  1152. (let*-values (((kid-out state)
  1153. (loop (car in) '() state #t
  1154. (or sig-ws? (space-significant? (caar in))))))
  1155. (loop (cdr in) (cons kid-out out) state #f sig-ws?)))))
  1156. (else ; a symbol
  1157. (loop (cdr in) (cons (car in) out) state #t sig-ws?))))
  1158. (call-with-values
  1159. (lambda () (loop tree '() '() #t #f))
  1160. (lambda (out state) out)))
  1161. ;; Replace % with texinfo-arguments.
  1162. (define (stexi->sxml tree)
  1163. "Transform the stexi tree @var{tree} into sxml. This involves
  1164. replacing the @code{%} element that keeps the texinfo arguments with an
  1165. element for each argument.
  1166. FIXME: right now it just changes % to @code{texinfo-arguments} -- that
  1167. doesn't hang with the idea of making a dtd at some point"
  1168. (pre-post-order
  1169. tree
  1170. `((% . ,(lambda (x . t) (cons 'texinfo-arguments t)))
  1171. (*text* . ,(lambda (x t) t))
  1172. (*default* . ,(lambda (x . t) (cons x t))))))
  1173. ;;; arch-tag: 73890afa-597c-4264-ae70-46fe7756ffb5
  1174. ;;; texinfo.scm ends here