erlydtl_parser.yrl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. %%% -*- mode: erlang -*- ------------------------------------------------------------------
  2. %%% File: erlydtl_parser.erl
  3. %%% @author Roberto Saccon <rsaccon@gmail.com> [http://rsaccon.com]
  4. %%% @author Evan Miller <emmiller@gmail.com>
  5. %%% @copyright 2008 Roberto Saccon, Evan Miller
  6. %%% @doc Template language grammar
  7. %%% @reference See <a href="http://erlydtl.googlecode.com" target="_top">http://erlydtl.googlecode.com</a> for more information
  8. %%% @end
  9. %%%
  10. %%% The MIT License
  11. %%%
  12. %%% Copyright (c) 2007 Roberto Saccon, Evan Miller
  13. %%%
  14. %%% Permission is hereby granted, free of charge, to any person obtaining a copy
  15. %%% of this software and associated documentation files (the "Software"), to deal
  16. %%% in the Software without restriction, including without limitation the rights
  17. %%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. %%% copies of the Software, and to permit persons to whom the Software is
  19. %%% furnished to do so, subject to the following conditions:
  20. %%%
  21. %%% The above copyright notice and this permission notice shall be included in
  22. %%% all copies or substantial portions of the Software.
  23. %%%
  24. %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. %%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  30. %%% THE SOFTWARE.
  31. %%%
  32. %%% @since 2007-11-11 by Roberto Saccon, Evan Miller
  33. %%%-------------------------------------------------------------------
  34. Nonterminals
  35. Elements
  36. Literal
  37. ValueBraced
  38. Value
  39. Values
  40. Variable
  41. Filter
  42. FilterArg
  43. AutoEscapeBlock
  44. AutoEscapeBraced
  45. EndAutoEscapeBraced
  46. BlockBlock
  47. BlockBraced
  48. EndBlockBraced
  49. CommentTag
  50. CommentBlock
  51. CommentBraced
  52. EndCommentBraced
  53. CycleTag
  54. CycleNames
  55. CycleNamesCompat
  56. ExtendsTag
  57. IncludeTag
  58. NowTag
  59. FirstofTag
  60. FilterBlock
  61. FilterBraced
  62. EndFilterBraced
  63. Filters
  64. ForBlock
  65. ForBraced
  66. EmptyBraced
  67. EndForBraced
  68. ForExpression
  69. ForGroup
  70. IfBlock
  71. IfBraced
  72. ElifBlock
  73. ElifBraced
  74. IfExpression
  75. ElseBraced
  76. EndIfBraced
  77. IfChangedBlock
  78. IfChangedBraced
  79. EndIfChangedBraced
  80. IfEqualBlock
  81. IfEqualBraced
  82. IfEqualExpression
  83. EndIfEqualBraced
  84. IfNotEqualBlock
  85. IfNotEqualBraced
  86. IfNotEqualExpression
  87. EndIfNotEqualBraced
  88. CustomTag
  89. CustomArgs
  90. Args
  91. RegroupTag
  92. SpacelessBlock
  93. SSITag
  94. BlockTransBlock
  95. BlockTransContent
  96. TransTag
  97. TransArgs
  98. TransText
  99. TemplatetagTag
  100. Templatetag
  101. WidthRatioTag
  102. WithBlock
  103. WithBraced
  104. EndWithBraced
  105. CallTag
  106. CallWithTag
  107. LoadTag
  108. LoadArgs
  109. Unot.
  110. Terminals
  111. and_keyword
  112. as_keyword
  113. autoescape_keyword
  114. block_keyword
  115. blocktrans_keyword
  116. by_keyword
  117. call_keyword
  118. close_tag
  119. close_var
  120. comment_tag
  121. comment_keyword
  122. context_keyword
  123. cycle_keyword
  124. elif_keyword
  125. else_keyword
  126. empty_keyword
  127. endautoescape_keyword
  128. endblock_keyword
  129. endblocktrans_keyword
  130. endcomment_keyword
  131. endfilter_keyword
  132. endfor_keyword
  133. endif_keyword
  134. endifchanged_keyword
  135. endifequal_keyword
  136. endifnotequal_keyword
  137. endregroup_keyword
  138. endspaceless_keyword
  139. endwith_keyword
  140. extends_keyword
  141. filter_keyword
  142. firstof_keyword
  143. for_keyword
  144. from_keyword
  145. identifier
  146. if_keyword
  147. ifchanged_keyword
  148. ifequal_keyword
  149. ifnotequal_keyword
  150. in_keyword
  151. include_keyword
  152. load_keyword
  153. noop_keyword
  154. not_keyword
  155. now_keyword
  156. number_literal
  157. only_keyword
  158. or_keyword
  159. open_tag
  160. open_var
  161. parsed_keyword
  162. regroup_keyword
  163. reversed_keyword
  164. spaceless_keyword
  165. ssi_keyword
  166. string_literal
  167. string
  168. templatetag_keyword
  169. openblock_keyword
  170. closeblock_keyword
  171. openvariable_keyword
  172. closevariable_keyword
  173. openbrace_keyword
  174. closebrace_keyword
  175. opencomment_keyword
  176. closecomment_keyword
  177. trans_keyword
  178. widthratio_keyword
  179. with_keyword
  180. ',' '|' '=' ':' '.'
  181. '==' '!='
  182. '>=' '<='
  183. '>' '<'
  184. '(' ')'
  185. '_'.
  186. Rootsymbol
  187. Elements.
  188. %% Operator precedences for the E non terminal
  189. Left 100 or_keyword.
  190. Left 110 and_keyword.
  191. Nonassoc 300 '==' '!=' '>=' '<=' '>' '<'.
  192. Unary 600 Unot.
  193. Elements -> '$empty' : [].
  194. Elements -> Elements string : '$1' ++ ['$2'].
  195. Elements -> Elements AutoEscapeBlock : '$1' ++ ['$2'].
  196. Elements -> Elements BlockBlock : '$1' ++ ['$2'].
  197. Elements -> Elements BlockTransBlock : '$1' ++ ['$2'].
  198. Elements -> Elements CallTag : '$1' ++ ['$2'].
  199. Elements -> Elements CallWithTag : '$1' ++ ['$2'].
  200. Elements -> Elements CommentBlock : '$1' ++ ['$2'].
  201. Elements -> Elements CommentTag : '$1' ++ ['$2'].
  202. Elements -> Elements CustomTag : '$1' ++ ['$2'].
  203. Elements -> Elements CycleTag : '$1' ++ ['$2'].
  204. Elements -> Elements ExtendsTag : '$1' ++ ['$2'].
  205. Elements -> Elements FilterBlock : '$1' ++ ['$2'].
  206. Elements -> Elements FirstofTag : '$1' ++ ['$2'].
  207. Elements -> Elements ForBlock : '$1' ++ ['$2'].
  208. Elements -> Elements IfBlock : '$1' ++ ['$2'].
  209. Elements -> Elements IfEqualBlock : '$1' ++ ['$2'].
  210. Elements -> Elements IfNotEqualBlock : '$1' ++ ['$2'].
  211. Elements -> Elements IfChangedBlock : '$1' ++ ['$2'].
  212. Elements -> Elements IncludeTag : '$1' ++ ['$2'].
  213. Elements -> Elements LoadTag : '$1' ++ ['$2'].
  214. Elements -> Elements NowTag : '$1' ++ ['$2'].
  215. Elements -> Elements RegroupTag : '$1' ++ ['$2'].
  216. Elements -> Elements SpacelessBlock : '$1' ++ ['$2'].
  217. Elements -> Elements SSITag : '$1' ++ ['$2'].
  218. Elements -> Elements TemplatetagTag : '$1' ++ ['$2'].
  219. Elements -> Elements TransTag : '$1' ++ ['$2'].
  220. Elements -> Elements ValueBraced : '$1' ++ ['$2'].
  221. Elements -> Elements WidthRatioTag : '$1' ++ ['$2'].
  222. Elements -> Elements WithBlock : '$1' ++ ['$2'].
  223. ValueBraced -> open_var Value close_var : '$2'.
  224. Value -> Value '|' Filter : {apply_filter, '$1', '$3'}.
  225. Value -> '_' '(' Value ')' : {trans, '$3'}.
  226. Value -> Variable : '$1'.
  227. Value -> Literal : '$1'.
  228. Values -> Value : ['$1'].
  229. Values -> Value Values : ['$1'|'$2'].
  230. Filter -> identifier FilterArg : {'$1', '$2'}.
  231. FilterArg -> '$empty' : [].
  232. FilterArg -> ':' Variable : ['$2'].
  233. FilterArg -> ':' Literal : ['$2'].
  234. Variable -> identifier : {variable, '$1'}.
  235. Variable -> Variable '.' identifier : {attribute, {'$3', '$1'}}.
  236. Variable -> Variable '.' Literal : {attribute, {'$3', '$1'}}.
  237. Literal -> string_literal : '$1'.
  238. Literal -> number_literal : '$1'.
  239. AutoEscapeBlock -> AutoEscapeBraced Elements EndAutoEscapeBraced : {autoescape, '$1', '$2'}.
  240. AutoEscapeBraced -> open_tag autoescape_keyword identifier close_tag : '$3'.
  241. EndAutoEscapeBraced -> open_tag endautoescape_keyword close_tag.
  242. BlockBlock -> BlockBraced Elements EndBlockBraced : {block, '$1', '$2'}.
  243. BlockBraced -> open_tag block_keyword identifier close_tag : '$3'.
  244. EndBlockBraced -> open_tag endblock_keyword close_tag.
  245. ExtendsTag -> open_tag extends_keyword string_literal close_tag : {extends, '$3'}.
  246. IncludeTag -> open_tag include_keyword string_literal close_tag : {include, '$3', []}.
  247. IncludeTag -> open_tag include_keyword string_literal with_keyword Args close_tag : {include, '$3', '$5'}.
  248. IncludeTag -> open_tag include_keyword string_literal only_keyword close_tag : {include_only, '$3', []}.
  249. IncludeTag -> open_tag include_keyword string_literal with_keyword Args only_keyword close_tag : {include_only, '$3', '$5'}.
  250. LoadTag -> open_tag load_keyword LoadArgs close_tag : {load_libs, '$3'}.
  251. LoadTag -> open_tag load_keyword LoadArgs from_keyword identifier close_tag : {load_from_lib, '$3', '$5'}.
  252. LoadArgs -> '$empty' : [].
  253. LoadArgs -> identifier LoadArgs : ['$1'|'$2'].
  254. NowTag -> open_tag now_keyword string_literal close_tag : {date, now, '$3'}.
  255. CommentBlock -> CommentBraced Elements EndCommentBraced : {comment, '$2'}.
  256. CommentBraced -> open_tag comment_keyword close_tag.
  257. EndCommentBraced -> open_tag endcomment_keyword close_tag.
  258. CommentTag -> comment_tag : '$1'.
  259. CycleTag -> open_tag cycle_keyword CycleNamesCompat close_tag : {cycle_compat, '$3'}.
  260. CycleTag -> open_tag cycle_keyword CycleNames close_tag : {cycle, '$3'}.
  261. CycleNames -> Value : ['$1'].
  262. CycleNames -> CycleNames Value : '$1' ++ ['$2'].
  263. CycleNamesCompat -> identifier ',' : ['$1'].
  264. CycleNamesCompat -> CycleNamesCompat identifier ',' : '$1' ++ ['$2'].
  265. CycleNamesCompat -> CycleNamesCompat identifier : '$1' ++ ['$2'].
  266. FilterBlock -> FilterBraced Elements EndFilterBraced : {filter, '$1', '$2'}.
  267. FilterBraced -> open_tag filter_keyword Filters close_tag : '$3'.
  268. EndFilterBraced -> open_tag endfilter_keyword close_tag.
  269. Filters -> Filter : ['$1'].
  270. Filters -> Filter '|' Filters : ['$1'|'$3'].
  271. FirstofTag -> open_tag firstof_keyword Values close_tag : {firstof, '$3'}.
  272. ForBlock -> ForBraced Elements EndForBraced : {for, '$1', '$2'}.
  273. ForBlock -> ForBraced Elements EmptyBraced Elements EndForBraced : {for, '$1', '$2', '$4'}.
  274. EmptyBraced -> open_tag empty_keyword close_tag.
  275. ForBraced -> open_tag for_keyword ForExpression close_tag : '$3'.
  276. EndForBraced -> open_tag endfor_keyword close_tag.
  277. ForExpression -> ForGroup in_keyword Value : {'in', '$1', '$3', false}.
  278. ForExpression -> ForGroup in_keyword Value reversed_keyword : {'in', '$1', '$3', true}.
  279. ForGroup -> identifier : ['$1'].
  280. ForGroup -> ForGroup ',' identifier : '$1' ++ ['$3'].
  281. IfBlock -> IfBraced Elements ElseBraced Elements EndIfBraced : {'ifelse', '$1', '$2', '$4'}.
  282. IfBlock -> IfBraced Elements EndIfBraced : {'if', '$1', '$2'}.
  283. IfBlock -> IfBraced Elements ElifBlock : {'if', '$1', '$2', ['$3']}.
  284. ElifBlock -> ElifBraced Elements ElseBraced Elements EndIfBraced : {'ifelse', '$1', '$2', '$4'}.
  285. ElifBlock -> ElifBraced Elements EndIfBraced : {'if', '$1', '$2'}.
  286. ElifBlock -> ElifBraced Elements ElifBlock : {'if', '$1', '$2', ['$3']}.
  287. IfBraced -> open_tag if_keyword IfExpression close_tag : '$3'.
  288. ElifBraced -> open_tag elif_keyword IfExpression close_tag : '$3'.
  289. IfExpression -> Value in_keyword Value : {'expr', "in", '$1', '$3'}.
  290. IfExpression -> Value not_keyword in_keyword Value : {'expr', "not", {'expr', "in", '$1', '$4'}}.
  291. IfExpression -> Value '==' Value : {'expr', "eq", '$1', '$3'}.
  292. IfExpression -> Value '!=' Value : {'expr', "ne", '$1', '$3'}.
  293. IfExpression -> Value '>=' Value : {'expr', "ge", '$1', '$3'}.
  294. IfExpression -> Value '<=' Value : {'expr', "le", '$1', '$3'}.
  295. IfExpression -> Value '>' Value : {'expr', "gt", '$1', '$3'}.
  296. IfExpression -> Value '<' Value : {'expr', "lt", '$1', '$3'}.
  297. IfExpression -> '(' IfExpression ')' : '$2'.
  298. IfExpression -> Unot : '$1'.
  299. IfExpression -> IfExpression or_keyword IfExpression : {'expr', "or", '$1', '$3'}.
  300. IfExpression -> IfExpression and_keyword IfExpression : {'expr', "and", '$1', '$3'}.
  301. IfExpression -> Value : '$1'.
  302. Unot -> not_keyword IfExpression : {expr, "not", '$2'}.
  303. ElseBraced -> open_tag else_keyword close_tag.
  304. EndIfBraced -> open_tag endif_keyword close_tag.
  305. IfChangedBlock -> IfChangedBraced Elements ElseBraced Elements EndIfChangedBraced : {ifchangedelse, '$1', '$2', '$4'}.
  306. IfChangedBlock -> IfChangedBraced Elements EndIfChangedBraced : {ifchanged, '$1', '$2'}.
  307. IfChangedBraced -> open_tag ifchanged_keyword close_tag.
  308. IfChangedBraced -> open_tag ifchanged_keyword Values close_tag : '$3'.
  309. EndIfChangedBraced -> open_tag endifchanged_keyword close_tag.
  310. IfEqualBlock -> IfEqualBraced Elements ElseBraced Elements EndIfEqualBraced : {ifequalelse, '$1', '$2', '$4'}.
  311. IfEqualBlock -> IfEqualBraced Elements EndIfEqualBraced : {ifequal, '$1', '$2'}.
  312. IfEqualBraced -> open_tag ifequal_keyword IfEqualExpression Value close_tag : ['$3', '$4'].
  313. IfEqualExpression -> Value : '$1'.
  314. EndIfEqualBraced -> open_tag endifequal_keyword close_tag.
  315. IfNotEqualBlock -> IfNotEqualBraced Elements ElseBraced Elements EndIfNotEqualBraced : {ifnotequalelse, '$1', '$2', '$4'}.
  316. IfNotEqualBlock -> IfNotEqualBraced Elements EndIfNotEqualBraced : {ifnotequal, '$1', '$2'}.
  317. IfNotEqualBraced -> open_tag ifnotequal_keyword IfNotEqualExpression Value close_tag : ['$3', '$4'].
  318. IfNotEqualExpression -> Value : '$1'.
  319. EndIfNotEqualBraced -> open_tag endifnotequal_keyword close_tag.
  320. RegroupTag -> open_tag regroup_keyword Value by_keyword Value as_keyword identifier close_tag : {regroup, {'$3', '$5', '$7'}}.
  321. RegroupTag -> open_tag endregroup_keyword close_tag : end_regroup.
  322. SpacelessBlock -> open_tag spaceless_keyword close_tag Elements open_tag endspaceless_keyword close_tag : {spaceless, '$4'}.
  323. SSITag -> open_tag ssi_keyword Value close_tag : {ssi, '$3'}.
  324. SSITag -> open_tag ssi_keyword string_literal parsed_keyword close_tag : {ssi_parsed, '$3'}.
  325. BlockTransBlock -> open_tag blocktrans_keyword close_tag BlockTransContent open_tag endblocktrans_keyword close_tag : {blocktrans, [], '$4'}.
  326. BlockTransBlock -> open_tag blocktrans_keyword with_keyword Args close_tag BlockTransContent open_tag endblocktrans_keyword close_tag : {blocktrans, '$4', '$6'}.
  327. BlockTransContent -> '$empty' : [].
  328. BlockTransContent -> BlockTransContent open_var identifier close_var : '$1' ++ [{variable, '$3'}].
  329. BlockTransContent -> BlockTransContent string : '$1' ++ ['$2'].
  330. %% TODO: {% plural %}
  331. TemplatetagTag -> open_tag templatetag_keyword Templatetag close_tag : {templatetag, '$3'}.
  332. Templatetag -> openblock_keyword : '$1'.
  333. Templatetag -> closeblock_keyword : '$1'.
  334. Templatetag -> openvariable_keyword : '$1'.
  335. Templatetag -> closevariable_keyword : '$1'.
  336. Templatetag -> openbrace_keyword : '$1'.
  337. Templatetag -> closebrace_keyword : '$1'.
  338. Templatetag -> opencomment_keyword : '$1'.
  339. Templatetag -> closecomment_keyword : '$1'.
  340. TransTag -> open_tag trans_keyword TransArgs close_tag : '$3'.
  341. TransTag -> open_tag trans_keyword TransArgs as_keyword identifier close_tag : {scope_as, '$5', ['$3']}.
  342. TransTag -> open_tag trans_keyword TransArgs noop_keyword close_tag : element(2, '$3').
  343. TransArgs -> TransText : {trans, '$1'}.
  344. TransArgs -> TransText context_keyword string_literal: {trans, '$1', '$3'}.
  345. TransText -> string_literal : '$1'.
  346. TransText -> Variable : '$1'.
  347. WidthRatioTag -> open_tag widthratio_keyword Value Value number_literal close_tag : {widthratio, '$3', '$4', '$5'}.
  348. WithBlock -> WithBraced Elements EndWithBraced : {with, '$1', '$2'}.
  349. WithBraced -> open_tag with_keyword Args close_tag : '$3'.
  350. EndWithBraced -> open_tag endwith_keyword close_tag.
  351. CustomTag -> open_tag identifier CustomArgs close_tag : {tag, '$2', '$3'}.
  352. CustomArgs -> '$empty' : [].
  353. CustomArgs -> identifier '=' Value CustomArgs : [{'$1', '$3'}|'$4'].
  354. CustomArgs -> Value CustomArgs : ['$1'|'$2'].
  355. Args -> '$empty' : [].
  356. Args -> Args identifier '=' Value : '$1' ++ [{'$2', '$4'}].
  357. CallTag -> open_tag call_keyword identifier close_tag : {call, '$3'}.
  358. CallWithTag -> open_tag call_keyword identifier with_keyword Value close_tag : {call, '$3', '$5'}.
  359. Erlang code.
  360. %% vim: syntax=erlang