I wrote primitive conversion routines to bring the xexpr or Neil's xexpr
into ... oh gosh, my parser token format, which by coincidence is very
close.  Just playing with this now .. In my target format token children
are always other tokens.  All values given as attributes in value tokens.
I use an empty list if there are no attributes.  The conversion routines
are very simple, though I'm just playing with this, there will be other
cases I missed, I suspect:

 (define (xexpr->tok-tree an-xexpr)
    (define (is-at-list e)
      (and
        (pair? e)
        (pair? (car e))))
    (cond
      [(null? an-xexpr) an-xexpr]
      [(not (pair? an-xexpr)) (tok-make 'tok:value `((value ,an-xexpr)))] ;
actually diff toks for diff types
      [else
        (let(
              [tag (car an-xexpr)]
              [r1  (cdr an-xexpr)]
              )
          (cond
            [(null? r1) an-xexpr]
            [else
              (let(
                    [first-element (car r1)]
                    [r2 (cdr r1)]
                    )
                (cond
                  [(is-at-list first-element) (cons tag (cons first-element
(map xexpr->tok-tree r2)))]
                  [else (cons tag (cons '() (map xexpr->tok-tree r1)))]
                  ))]))]))

(define (test-xexpr-tok-tree-0)
  (equal?
    (xexpr->tok-tree
      '(html (head (title "My Title"))
         (body ((bgcolor "white"))
           (h1 "My Heading")
           (p ((style "default")) "This is a paragraph.")
           (p "This is another paragraph."))))
    '(html
       ()
       (head () (title () (tok:value ((value "My Title")))))
       (body
         ((bgcolor "white"))
         (h1 () (tok:value ((value "My Heading"))))
         (p ((style "default")) (tok:value ((value "This is a
paragraph."))))
         (p () (tok:value ((value "This is another paragraph.")))))
       )))



On Wed, Jul 29, 2015 at 7:05 AM, Matthew Butterick <m...@mbtype.com> wrote:

> Yes, more or less. In an X-expression, an attribute list is the only
> element that's a list made of sublists. A list of embedded X-expressions,
> OTOH, will start with a symbol. To look at it another way,
>
> (cons symbol (list xexpr ...))
>
>
> really amounts to
>
> (list symbol xexpr ...)
>
>
> which is just
>
> (list symbol (list (list symbol string) ...) xexpr ...)
>
>
> but without the attribute list, cf.
>
> '(p "foo" "bar")
>
> '(p ((style "default")) "foo" "bar")
>
> A recurring annoyance in X-expressions is distinguishing these two cases
> on input, because the second element can be either an attribute list or
> nested X-expression. You can use `xexpr-drop-empty-attributes` to force an
> attribute list (even empty). My `txexpr` package also has utilities for
> handling them.
>
>
> Here is the syntax for an xexp from xexp? in the reference:
>
>   xexpr = string  | (list
> <http://pkg-build.racket-lang.org/doc/reference/pairs.html#%28def._%28%28quote._~23~25kernel%29._list%29%29>
>  symbol (list
> <http://pkg-build.racket-lang.org/doc/reference/pairs.html#%28def._%28%28quote._~23~25kernel%29._list%29%29>
>  (list
> <http://pkg-build.racket-lang.org/doc/reference/pairs.html#%28def._%28%28quote._~23~25kernel%29._list%29%29>
>  symbol string) ...) xexpr ...)  | (cons
> <http://pkg-build.racket-lang.org/doc/reference/pairs.html#%28def._%28%28quote._~23~25kernel%29._cons%29%29>
>  symbol (list
> <http://pkg-build.racket-lang.org/doc/reference/pairs.html#%28def._%28%28quote._~23~25kernel%29._list%29%29>
>  xexpr ...))  | symbol  | valid-char?
> <http://pkg-build.racket-lang.org/doc/xml/index.html#%28def._%28%28lib._xml%2Fmain..rkt%29._valid-char~3f%29%29>
>   | cdata  | misc
> And in this latter syntax, how is the attribute list distinguished from a
> list of embedded xexps? Is it due to the nesting in the attribute list?
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to