Skip to content

Instantly share code, notes, and snippets.

@localhostdotdev
Created May 12, 2019 16:05
Show Gist options
  • Save localhostdotdev/ba7b38b1b4a1c6ba6c799ee3c63c1fa6 to your computer and use it in GitHub Desktop.
Save localhostdotdev/ba7b38b1b4a1c6ba6c799ee3c63c1fa6 to your computer and use it in GitHub Desktop.

Revisions

  1. localhostdotdev created this gist May 12, 2019.
    34 changes: 34 additions & 0 deletions template.pegjs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    { function j(string) { return string.join('') } }

    template =
    (
    interpolation /
    tag /
    $(!interpolation !tag .)
    )+

    ws = (" " / "\n")*
    value = [^ ]

    // {{ this }}
    open_interpolation = "{{"
    close_interpolation = "}}"

    not_close_interpolation =
    ws !close_interpolation m:value ws { return m }

    interpolation =
    open_interpolation m:not_close_interpolation+ close_interpolation { return j(m) }

    // {% this %}
    open_tag = "{%"
    close_tag = "%}"

    not_close_tag =
    ws !close_tag m:value ws { return m }

    tag_start = !tag_end open_tag m:not_close_tag+ close_tag { return j(m) }

    tag_end = open_tag ws "end" not_close_tag+ close_tag

    tag = tag_start template tag_end