Home About The Codist RSS Feed

Building a Web Framework My Way - building a parser
Oct 05, 2006 14:59 perm link Readers: 341

I wrote the parser for the language I came up with yesterday; it turned out to be fairly easy to do given a couple of limitations I can live with.

Using a single regular expression and a bit of code I can turn almost any set of my language tokens into a node tree (like a DOM tree but way simpler). The only issues are that the open brackets [ ( { have to butt up against their tag name, and I can't have them in the content without escaping.

I can always replace the parser with antlr later. It works fine for now.

The next step is to generate XHTML from the node tree, which is a bit more work. The goal is to always generate valid XHTML from valid (whatever the heck this language is called).

This is what the tree looks like (almost the same input as the previous post:

tag_html
  tag_head
    tag_title
    tag_link
      "/static/base.css"
  tag_body
    tag_div(id=container)
      tag_div(id=header)
        tag_h1
          "{blog.title}"
        tag_h3
          "{blog.subtitle}"
      tag_div(id=wrapper)
        tag_div(id=content)
          command_loop(COMMAND=post|articles)
            tag_h4(id=article)
              "{post.title}"
              tag_span
                "{post.time}"
              "test"
              tag_span
                "morecrap"
              "here"
            tag_div
              "{post.content}"
            command_if(COMMAND=post.hascomments)
              command_loop(COMMAND=i | post.comments)
                tag_div
                  "{i.comment}"
      tag_div(id=navigation)
        tag_h4
          "Home"
        tag_li
          tag_a
            "Home|{blog.homeurl}"
        tag_h4
          "Tags/RSS"
        tag_li
          tag_a
            "RSS|{blog.rssfeed}"
          tag_img
            "rss.png"
        tag_h4
          "Recent posts"
        tag_ul
          command_loop(COMMAND=i | recentposts)
            tag_li
              tag_a
                "{i.truncatedtitle}|{i.permurl}"
              "{i.time}"
        tag_h4
          "Recent comments"
        tag_ul
          command_loop(COMMAND=i | recentcomments)
            tag_li
              tag_a
                "{i.truncatedcomment}|{i.permurl}"
              "{i.time}"
      tag_div(id=extra)
      tag_div(id=footer)
        tag_p
          "contents copyright © 2006 by andrew wulf ♦ 
		email codist at biit dot com ♦ arlington, texas,
      usa"
          tag_br
          "check out my cd at"
          tag_a
            "consonance music|http://consonancemusic.com"

My Tags:

Name:


Optional URL:


Comment:


Save Cancel

Copyright © 2007 By Andrew Wulf