localhostdotdev
localhostdotdev

Reputation: 1895

how to write a simple peg grammar for a liquid-like templating language?

edit: you can follow the progress here: https://github.com/simple-updates/template

I'm using peg.js and trying to write something that could interpret a template like:

hello {{ "world" }}
{% if a %}
  good {{ a }}
{% else %}
  bad
{% endif %}

I've tried many things but let's say this is my starting point:

Template
  = ws markup ws

ws = " "*

open_interpolation = "{{"
close_interpolation = "}}"
open_tag = "{%"
close_tag = "%}"

char = . // ?

markup =
  (open_tag tag:char* close_tag)
  { return { 'tag': tag.join('') } } /
  (open_interpolation interpolation:char* close_interpolation)
  { return { 'interpolation': interpolation.join('') } } /
  chars:char*
  { return { 'chars': chars.join('') } }

when I try on the string {{ test }} for instance it will just interpret it as chars instead of an interpolation.

any idea of how I could do it?

(obviously it would be more complex with nested "markups")

Upvotes: 2

Views: 407

Answers (2)

Bart Kiers
Bart Kiers

Reputation: 170178

How about something like this as a start:

Template
 = Atom*

Atom
 = IfTag
 / Interpolation
 / [^{]
 / !"{%" !"{{" "{"

Interpolation
 = "{{" _ Expression _ "}}"

IfTag
 = If Template ( Else Template )? EndIf

If
 = "{%" _ "if" _ Expression _ "%}"

Else
 = "{%" _ "else" _ "%}"

EndIf
 = "{%" _ "endif" _ "%}"

Expression
 = "\"" [^"]* "\""
 / [a-zA-Z]+
 / [0-9]+

_
 = [ \t\n\r]*

The tricky part here is the !"{%" !"{{" "{" alternative of the Atom production, which reads as follows:

When no "{%" and "{{" can be seen ahead of the current position, match a single "{"

Upvotes: 1

localhostdotdev
localhostdotdev

Reputation: 1895

ok I'm not at the nested part of anything much yet but I got my tags/interpolations working

the key is those !not_something value

current grammar:

{
    function j(value) {
        return value.join('')
    }
}

Template
  = ws markup:markup ws { return markup }

ws = " "*

open_interpolation = "{{"
close_interpolation = "}}"
open_tag = "{%"
close_tag = "%}"

value = .

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

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

not_open_tag_or_interpolation =
    !open_interpolation !open_tag value:value { return value }

markup =
    (
      open_interpolation interpolation:not_close_interpolation+ close_interpolation {
          return { 'interpolation': j(interpolation) }
      } /
      open_tag tag:not_close_tag+ close_tag {
          return { 'tag': j(tag) }
      } /
      value:not_open_tag_or_interpolation+ { return j(value) }
    )+

Upvotes: 0

Related Questions