Alexander, you will notice I pulled a couple lambdas to the top, and added a test routine at the bottom take those out ... then the code you sent isn't shorter. I am also expecting to have to add more code to the intermediate points. I like seeing those named. I think it is easy to read. ... What advantage do you see in using match?
So I have been thinking about this, have come to this conclusion, what do you think: In general grammar driven parsing is elegant against well formed input data. However a complication enters because the error grammar explodes in size in comparison. Where as if one builds a match with cascading let (naming the parts) and cond (doing the match), all end conditions are exposed and can be handled. As an example, I will have to check the tag to see if it aliases against tags the parser is using, so that people can't send in poison xml statement. The alternative would be to first run the xml through a well formed checker, but that would just move the complication to the checker, just so the parse can look pretty. In addition, I'm concerned about the stream behavior of racket's match - something I've promised to work on. Notice, what happens if you try to match pattern delineated by paren literals within nested parens, because it does a greedy match, and it will wait for the end of stream to complete that match using the outside most right paren. So I don't understand match behavior enough yet to be completely comfortable with it yet. Independent, in this case I suspect I like the cascading cond/let match. Be glad to hear your thoughts on this. Be glad to learn more about match. On Wed, Jul 29, 2015 at 10:56 PM, Alexander D. Knauth <alexan...@knauth.org> wrote: > Would it be easier using match? > > (define (xexpr->tok-tree an-xexpr) > (match as-xexpr > ['() > '()] > [(not (cons _ _)) > (tok-make ...)] > [(list tag) > (list tag)] > [(list-rest tag (? is-at-list at-list) r2) > ....] > ....)) > > On Jul 29, 2015, at 7:56 AM, Thomas Lynch < > thomas.ly...@reasoningtechnology.com> wrote: > > 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. > > > -- 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.