Re: parsing entities

Andy Wingo <[email protected]> Fri, 06 Jun 2008 16:22:21 +0200
Newsgroups gmane.lisp.scheme.ssax-sxml
Message-ID <[email protected]>
On Fri 06 Jun 2008 12:17, "minh thu" <[email protected]> writes:

> I'd like to parse r5rs into SXML. My aproach is to use the Texinfo
> file from http://swissnet.ai.mit.edu/~jaffer/Scheme.html,
> generating xml from it (makeinfo --xml) then using ssax:xml->sxml.

Another option would be to use the texinfo parser from guile-lib:

Docs: http://home.gna.org/guile-lib/doc/ref/texinfo/
Code: bzr get http://arch.gna.org/guile-lib/guile-lib.bzr guile-lib

It produces "stexinfo", which differs slightly from sxml, as noted in
the documentation.

> The paser tells me it skips the dtd declaration then yells
> "[wf-entdeclared] broken for period".
> period is one entity declared in the dtd.

You should enlarge the set of predefined character entities, e.g:

(define *character-entities* '((agrave . 224)
                               (laquo . 171)
                               (mdash . 8212)
                               (nbsp . 160)
                               (raquo . 187)
                               (uacute . 250)))

(define (unichar->utf-8 u)
  (define (byte header mask shift)
    (integer->char (logior header (logand mask (ash u shift)))))
  (cond
   ((< u #x000000) (error "bad unicode code point" u))
   ((< u #x000080) (string (integer->char u)))
   ((< u #x000800) (string (byte #b11000000 #b11111 -6)
                           (byte #b10000000 #b111111 0)))
   ((< u #x00d800) (string (byte #b11100000 #b1111 -12)
                           (byte #b10000000 #b111111 -6)
                           (byte #b10000000 #b111111 0)))
   ((< u #x00e000) (error "bad unicode code point" u))
   ((< u #x010000) (string (byte #b11100000 #b1111 -12)
                           (byte #b10000000 #b111111 -6)
                           (byte #b10000000 #b111111 0)))
   ((< u #x110000) (string (byte #b11110000 #b111 -18)
                           (byte #b10000000 #b111111 -12)
                           (byte #b10000000 #b111111 -6)
                           (byte #b10000000 #b111111 0)))
   (else (error "bad unicode code point" u))))

(for-each
 (lambda (pair)
   (set! ssax:predefined-parsed-entities
         (assoc-set! ssax:predefined-parsed-entities
                     (car pair) (unichar->utf-8 (cdr pair)))))
 *character-entities*)

Of course this assumes that your scheme uses utf-8 and that its strings
are byte strings, not character strings. But that's how the one I use
is. Anyway, should be enough information.

Cheers,

Andy
-- 
http://wingolog.org/

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php