Skip to content
Browse files

Added cell node for accessing an object cell

  • Loading branch information...
1 parent 5e3df7e commit ddee133e59c1210d9677d9d5816ad8edbc974cfd @vic committed Sep 21, 2011
Showing with 66 additions and 4 deletions.
  1. +4 −2 lib/akin/grammar.kpeg
  2. +54 −2 lib/akin/grammar.rb
  3. +1 −0 lib/akin/shuffle.rb
  4. +7 −0 spec/grammar_spec.rb
View
6 lib/akin/grammar.kpeg
@@ -76,13 +76,15 @@ parts(x) = parts(x):a - part(x):b {a + [b]}
kmsg(x) = parts(x):a ~n(a.first.pos, :kmsg, *a)
-literal(x) = symbol(x) | infix | str | float | fixnum | regexp | name | oper
+literal(x) = symbol(x) | infix | cell | str | float | fixnum | regexp | name | oper
symbol(x) = p:p ":" !&":" value(x.kmsg):v ~n(p, :symbol, v.first)
infix_ = <"#"+> !&(brace|"!") {text.size} | {0}
infix = p:p infix_:l <name|oper> infix_:r &{ l+r > 0 }
- ~n(p, :infix, text, l, r)
+ ~n(p, :infix, text, l, r)
+
+cell = p:p "\\" <name|oper> ~n(p, :cell, text)
regexp = p:p quoted(:text, & "/"):b ~n(p, :regexp, text_node(p, b))
View
56 lib/akin/grammar.rb
@@ -2239,7 +2239,7 @@ def _kmsg(x)
set_failed_rule :_kmsg unless _tmp
return _tmp
end
-
+
# literal = (symbol(x) | infix | cell | str | float | fixnum | regexp | name | oper)
def _literal(x)
@@ -2250,6 +2250,9 @@ def _literal(x)
self.pos = _save
_tmp = apply(:_infix)
break if _tmp
+ self.pos = _save
+ _tmp = apply(:_cell)
+ break if _tmp
self.pos = _save
_tmp = apply(:_str)
break if _tmp
@@ -2450,6 +2453,54 @@ def _infix
set_failed_rule :_infix unless _tmp
return _tmp
end
+
+ # cell = p:p "\\" < (name | oper) > {n(p, :cell, text)}
+ def _cell
+
+ _save = self.pos
+ while true # sequence
+ _tmp = apply(:_p)
+ p = @result
+ unless _tmp
+ self.pos = _save
+ break
+ end
+ _tmp = match_string("\\")
+ unless _tmp
+ self.pos = _save
+ break
+ end
+ _text_start = self.pos
+
+ _save1 = self.pos
+ while true # choice
+ _tmp = apply(:_name)
+ break if _tmp
+ self.pos = _save1
+ _tmp = apply(:_oper)
+ break if _tmp
+ self.pos = _save1
+ break
+ end # end choice
+
+ if _tmp
+ text = get_text(_text_start)
+ end
+ unless _tmp
+ self.pos = _save
+ break
+ end
+ @result = begin; n(p, :cell, text); end
+ _tmp = true
+ unless _tmp
+ self.pos = _save
+ end
+ break
+ end # end sequence
+
+ set_failed_rule :_cell unless _tmp
+ return _tmp
+ end
# regexp = p:p quoted(:text, &"/"):b {n(p, :regexp, text_node(p, b))}
def _regexp
@@ -4740,10 +4791,11 @@ def _number_escapes
Rules[:_keya] = rule_info("keya", "< (name | oper) > braced:a \":\" {[text] + a}")
Rules[:_keyword] = rule_info("keyword", "(keyw(x) | keya(x))")
Rules[:_part] = rule_info("part", "(p:p keyword(x):a &(ws* keyword(x)) {n(p, :part, *a)} | p:p keyword(x):a ws* \".\" - empty_(x):b {n(p, :part, *(a+b))} | p:p keyword(x):a ws* keyargs(x.in(x.pos.minor(p))):b {n(p, :part, *(a+b))})")
- Rules[:_parts] = rule_info("parts", "(parts(x):a - part(x):b {a + [b]} | part(x):a {[a]})")
+ Rules[:_parts] = rule_info("parts", "(parts(x):a - part(x):b {a + [b]} | part(x):a {[a]})")
Rules[:_kmsg] = rule_info("kmsg", "parts(x):a {n(a.first.pos, :kmsg, *a)}")
Rules[:_literal] = rule_info("literal", "(symbol(x) | infix | cell | str | float | fixnum | regexp | name | oper)")
Rules[:_symbol] = rule_info("symbol", "p:p \":\" !(&\":\") value(x.kmsg):v {n(p, :symbol, v.first)}")
+ Rules[:_infix_] = rule_info("infix_", "(< \"\#\"+ > !(&(brace | \"!\")) {text.size} | {0})")
Rules[:_infix] = rule_info("infix", "p:p infix_:l < (name | oper) > infix_:r &{ l+r > 0 } {n(p, :infix, text, l, r)}")
Rules[:_cell] = rule_info("cell", "p:p \"\\\\\" < (name | oper) > {n(p, :cell, text)}")
Rules[:_regexp] = rule_info("regexp", "p:p quoted(:text, &\"/\"):b {n(p, :regexp, text_node(p, b))}")
View
1 lib/akin/shuffle.rb
@@ -33,6 +33,7 @@ def nothing(node)
alias_method :shuffle_fixnum, :nothing
alias_method :shuffle_float, :nothing
alias_method :shuffle_oper, :nothing
+ alias_method :shuffle_cell, :nothing
def shuffle_block(node)
node.with(:block, *shuffle(node.args))
View
7 spec/grammar_spec.rb
@@ -351,4 +351,11 @@
end
end
+ describe 'cell' do
+ it 'allows any name to be referenced as cell' do
+ s('a \mod c').should ==
+ [:chain, [:name, "a"], [:cell, "mod"], [:name, "c"]]
+ end
+ end
+
end

0 comments on commit ddee133

Please sign in to comment.
Something went wrong with that request. Please try again.