lib/hotcell/parser.y in hotcell-0.2.0 vs lib/hotcell/parser.y in hotcell-0.3.0
- old
+ new
@@ -44,19 +44,19 @@
left GT GTE LT LTE
nonassoc EQUAL INEQUAL
left AND
left OR
nonassoc RANGE
- right TERNARY
+ right QUESTION
right ASSIGN
nonassoc COMMA COLON
left SEMICOLON NEWLINE
preclow
start document
rule
- document: document document_unit { pospoppush(2); val[0].children.push(val[1]) }
- | document_unit { result = build Joiner, :JOINER, val[0], position: pospoppush(1) }
+ document: document document_unit { pospoppush(2); val[0].push(val[1]) }
+ | document_unit { result = [val[0]] }
document_unit: template | tag | block_tag | command_tag
template: TEMPLATE { result = val[0] }
tag: TOPEN TCLOSE {
result = build Tag, :TAG,
@@ -153,33 +153,50 @@
| expr LT expr { result = build Expression, :LT, val[0], val[2], position: pospoppush(3) }
| expr LTE expr { result = build Expression, :LTE, val[0], val[2], position: pospoppush(3) }
| expr EQUAL expr { result = build Expression, :EQUAL, val[0], val[2], position: pospoppush(3) }
| expr INEQUAL expr { result = build Expression, :INEQUAL, val[0], val[2], position: pospoppush(3) }
| NOT expr { result = build Expression, :NOT, val[1], position: pospoppush(2) }
+ | expr QUESTION expr COLON expr { result = build Expression, :TERNARY, val[0], val[2], val[4], position: pospoppush(5) }
| IDENTIFER ASSIGN expr { result = build Assigner, val[0], val[2], position: pospoppush(3) }
| expr PERIOD method { pospoppush(3); val[2].children[0] = val[0]; result = val[2] }
| expr AOPEN arguments ACLOSE {
result = build Summoner, '[]', val[0], *val[2], position: pospoppush(4)
}
| POPEN PCLOSE { pospoppush(2); result = nil }
| POPEN sequence PCLOSE {
- position = pospoppush(3)
result = case val[1].size
when 1
val[1][0]
else
- build Sequencer, :SEQUENCE, *val[1].flatten, position: position
+ build Sequencer, :SEQUENCE, *val[1].flatten, position: pospoppush(3)
end
}
| value
- value: const | number | string | range | array | hash | method
+ value: const | number | string | dstring | regexp | range | array | hash | method
const: NIL | TRUE | FALSE
number: INTEGER | FLOAT
- string: STRING | REGEXP
+ string: STRING
+ regexp: REGEXP
+ dstring: DOPEN dstring_content DCLOSE { result = build Expression, :DSTRING, *val[1], position: pospoppush(3) }
+ | DOPEN DCLOSE { pospoppush(2); result = '' }
+ dstring_content: STRING { result = [val[0]] }
+ | dstring_interpolation { result = [val[0]] }
+ | dstring_content STRING { pospoppush(2); val[0].push(val[1]); result = val[0] }
+ | dstring_content dstring_interpolation { pospoppush(2); val[0].push(val[1]); result = val[0] }
+ dstring_interpolation: IOPEN sequence ICLOSE {
+ result = case val[1].size
+ when 1
+ val[1][0]
+ else
+ build Sequencer, :SEQUENCE, *val[1].flatten, position: pospoppush(3)
+ end
+ }
+ | IOPEN ICLOSE { pospoppush(2); result = nil }
+
range: expr RANGE expr {
result = build Expression, val[1] == '..' ? :RANGE : :ERANGE,
val[0], val[2], position: pospoppush(3)
}
@@ -258,14 +275,11 @@
mode = tag.gsub(/^{{/, '').first
TAG_MODES[mode] || default
end
def parse
- if @tokens.size == 0
- build Joiner, :JOINER, position: 0
- else
- do_parse
- end
+ children = @tokens.size.zero? ? [] : do_parse
+ build Joiner, :JOINER, *children, position: 0
end
def next_token
@position = @position + 1
tcurr = @tokens[@position]