summaryrefslogtreecommitdiff
path: root/test/racc/assets/cadenza.y
blob: 1940ead225103ad2690b7f209585e49fd0d7fd95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# This grammar is released under an MIT license
# Author: William Howard (http://github.com/whoward)
# Source: https://github.com/whoward/cadenza/blob/master/src/cadenza.y

class Cadenza::RaccParser

/* expect this many shift/reduce conflicts */
expect 37

rule
  target
    : document
    | /* none */ { result = nil }
    ;

  parameter_list
    : logical_expression                     { result = [val[0]] }
    | parameter_list ',' logical_expression  { result = val[0].push(val[2]) }
    ;

  /* this has a shift/reduce conflict but since Racc will shift in this case it is the correct behavior */
  primary_expression
    : IDENTIFIER                   { result = VariableNode.new(val[0].value) }
    | IDENTIFIER parameter_list    { result = VariableNode.new(val[0].value, val[1]) }
    | INTEGER                      { result = ConstantNode.new(val[0].value) }
    | REAL                         { result = ConstantNode.new(val[0].value) }
    | STRING                       { result = ConstantNode.new(val[0].value) }
    | '(' filtered_expression ')'   { result = val[1] }
    ;

  multiplicative_expression
    : primary_expression
    | multiplicative_expression '*' primary_expression { result = OperationNode.new(val[0], "*", val[2]) }
    | multiplicative_expression '/' primary_expression { result = OperationNode.new(val[0], "/", val[2]) }
    ;

  additive_expression
    : multiplicative_expression
    | additive_expression '+' multiplicative_expression { result = OperationNode.new(val[0], "+", val[2]) }
    | additive_expression '-' multiplicative_expression { result = OperationNode.new(val[0], "-", val[2]) }
    ;

  boolean_expression
    : additive_expression
    | boolean_expression OP_EQ additive_expression { result = OperationNode.new(val[0], "==", val[2]) }
    | boolean_expression OP_NEQ additive_expression { result = OperationNode.new(val[0], "!=", val[2]) }
    | boolean_expression OP_LEQ additive_expression { result = OperationNode.new(val[0], "<=", val[2]) }
    | boolean_expression OP_GEQ additive_expression { result = OperationNode.new(val[0], ">=", val[2]) }
    | boolean_expression '>' additive_expression  { result = OperationNode.new(val[0], ">", val[2]) }
    | boolean_expression '<' additive_expression  { result = OperationNode.new(val[0], "<", val[2]) }
    ;

  inverse_expression
    : boolean_expression
    | NOT boolean_expression { result = BooleanInverseNode.new(val[1]) }
    ;

  logical_expression
    : inverse_expression
    | logical_expression AND inverse_expression { result = OperationNode.new(val[0], "and", val[2]) }
    | logical_expression OR inverse_expression { result = OperationNode.new(val[0], "or", val[2]) }
    ;

  filter
    : IDENTIFIER                    { result = FilterNode.new(val[0].value) }
    | IDENTIFIER ':' parameter_list { result = FilterNode.new(val[0].value, val[2]) }
    ;

  filter_list
    : filter { result = [val[0]] }
    | filter_list '|' filter { result = val[0].push(val[2]) }
    ;

  filtered_expression
    : logical_expression
    | logical_expression '|' filter_list { result = FilteredValueNode.new(val[0], val[2]) }
    ;

  inject_statement
    : VAR_OPEN filtered_expression VAR_CLOSE { result = val[1] }
    ;

  if_tag
    : STMT_OPEN IF logical_expression STMT_CLOSE { open_scope!; result = val[2] }
    | STMT_OPEN UNLESS logical_expression STMT_CLOSE { open_scope!; result = BooleanInverseNode.new(val[2]) }
    ;

  else_tag
    : STMT_OPEN ELSE STMT_CLOSE { result = close_scope!; open_scope! }
    ;

  end_if_tag
    : STMT_OPEN ENDIF STMT_CLOSE { result = close_scope! }
    | STMT_OPEN ENDUNLESS STMT_CLOSE { result = close_scope! }
    ;

  if_block
    : if_tag end_if_tag { result = IfNode.new(val[0], val[1]) }
    | if_tag document end_if_tag { result = IfNode.new(val[0], val[2]) }
    | if_tag else_tag document end_if_tag { result = IfNode.new(val[0], val[1], val[3]) }
    | if_tag document else_tag end_if_tag { result = IfNode.new(val[0], val[2], val[3]) }
    | if_tag document else_tag document end_if_tag { result = IfNode.new(val[0], val[2], val[4]) }
    ;

  for_tag
    : STMT_OPEN FOR IDENTIFIER IN filtered_expression STMT_CLOSE { open_scope!; result = [val[2].value, val[4]] }
    ;

  end_for_tag
    : STMT_OPEN ENDFOR STMT_CLOSE { result = close_scope! }
    ;

  /* this has a shift/reduce conflict but since Racc will shift in this case it is the correct behavior */
  for_block
    : for_tag end_for_tag { result = ForNode.new(VariableNode.new(val[0].first), val[0].last, val[1]) }
    | for_tag document end_for_tag { result = ForNode.new(VariableNode.new(val[0].first), val[0].last, val[2]) }
    ;

  block_tag
    : STMT_OPEN BLOCK IDENTIFIER STMT_CLOSE { result = open_block_scope!(val[2].value) }
    ;

  end_block_tag
    : STMT_OPEN ENDBLOCK STMT_CLOSE { result = close_block_scope! }
    ;

  /* this has a shift/reduce conflict but since Racc will shift in this case it is the correct behavior */
  block_block
    : block_tag end_block_tag { result = BlockNode.new(val[0], val[1]) }
    | block_tag document end_block_tag { result = BlockNode.new(val[0], val[2]) }
    ;

  generic_block_tag
    : STMT_OPEN IDENTIFIER STMT_CLOSE { open_scope!; result = [val[1].value, []] }
    | STMT_OPEN IDENTIFIER parameter_list STMT_CLOSE { open_scope!; result = [val[1].value, val[2]] }
    ;

  end_generic_block_tag
    : STMT_OPEN END STMT_CLOSE { result = close_scope! }
    ;

  generic_block
    : generic_block_tag document end_generic_block_tag { result = GenericBlockNode.new(val[0].first, val[2], val[0].last) }
    ;

  extends_statement
    : STMT_OPEN EXTENDS STRING STMT_CLOSE { result = val[2].value }
    | STMT_OPEN EXTENDS IDENTIFIER STMT_CLOSE { result = VariableNode.new(val[2].value) }
    ;

  document_component
    : TEXT_BLOCK { result = TextNode.new(val[0].value) }
    | inject_statement
    | if_block
    | for_block
    | generic_block
    | block_block
    ;

  document
    : document_component { push val[0] }
    | document document_component { push val[1] }
    | extends_statement  { document.extends = val[0] }
    | document extends_statement { document.extends = val[1] }
    ;

---- header ----
# racc_parser.rb : generated by racc

---- inner ----