Skip to content

Instantly share code, notes, and snippets.

@bluven
Created June 15, 2020 00:51
Show Gist options
  • Save bluven/054e59be17d758ccce76bdf432c9d08c to your computer and use it in GitHub Desktop.
Save bluven/054e59be17d758ccce76bdf432c9d08c to your computer and use it in GitHub Desktop.
a EBNF description of go text/template' grammar
Template = {Text | Action} .
Action = CommentAction | PipelineAction | IfAction | RangeAction | TemplateAction | BlockAction | WithAction .
CommentAction = LD "/*" Text "*/" RD .
PipelineAction = LD ( Pipeline | VarDeclarePipeline | VarAssignPipeline ) RD .
IfAction = LD "if" Pipeline RD Template [ LD "else" ["if" Pipeline] RD Template ] End .
RangeAction = LD "range" ( Pipeline | VarDeclarePipeline ) RD Template [ LD "else" RD Template] End .
TemplateAction = LD "template" Name [Pipeline] RD .
BlockAction = LD "block" Name [Pipeline] RD .
WithAction = LD "with" Pipeline RD Template [ LD "else" RD Template] End .
Pipeline = Argument { "|" Argument } | MethodCall | FuncCall { Argument } .
VarDeclarePipeline = Variable ["," Variable] ":=" Pipeline
VarAssignPipeline = Variable "=" Pipeline
Argument = Constant | "nil" | Variable | Fields . | FuncCall | ("(" Argument ")" [Fields] ) .
Variable = "$" [Name] .
Fields = [Variable] "." Name [ "." Name] .
FuncCall = Name .
MethodCall = ( Variable | Fields ) { Argument }
End = LD "end" RD .
LD = "{{" ["-"] .
RD = ["-"] "}}" .
Name = Letter { Letter | Digit } .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment