Permalink
Browse files

Trying to make all specs pass with ruby18 and ruby19

  • Loading branch information...
1 parent f5a9a91 commit 7222cc17acbedb01a8de165f3a18ff24f0294c1a @vic committed Sep 19, 2011
Showing with 2,557 additions and 3,603 deletions.
  1. +72 −102 lib/akin/grammar.kpeg
  2. +2,194 −2,857 lib/akin/grammar.rb
  3. +11 −7 lib/akin/operator.rb
  4. +13 −9 lib/akin/parser.rb
  5. +2 −2 lib/akin/shuffle.rb
  6. +236 −601 spec/grammar_spec.rb
  7. +3 −3 spec/operator_spec.rb
  8. +23 −19 spec/shuffle_spec.rb
  9. +3 −3 spec/spec_helper.rb
View
@@ -1,34 +1,84 @@
-# -*- ruby -*-
-#
-# This is a KPEG grammar for Akin.
-# If you want to compile it into grammar.rb, you must use vic/kpeg.
-#
+# -*- mode: ruby -*-
%% name = Akin::Grammar
%% { include Akin::Parser }
+root = - block(ctx)?:b - eof {b}
+
+eof = !.
+
+comment = /#!.*?$/
nl = "\n" | "\r\n"
-sp = " " | "\t" | "\\" nl
+ws = " " | "\t"
+
+op = /[\~\!@\#\$%\^\&\|\?\<\>*\/+=:-]/
p = &. ~current_position
+- = (ws | nl | comment)*
+s = (ws | nl | comment | ";")*
+
+brace = <.> &{brace(text)} ~brace(text)
+
+left_brace = <brace:b> &{ text == b.first} ~b
+right_brace(l) = <brace:b> &{ text == l.last } ~l
+
+braced = left_brace:l - (braced_(ctx) | ~nil):a - right_brace(l) {[l] + Array(a)}
+braced_(x) = braced_(x):a - "," - block(x):b {a + Array(b)}
+ | block(x):b ~Array(b)
+
+block(x) = block(x):a ws* nl - block(x):b &{a.pos.column < b.pos.column}
+ ~n(a.pos, :on, a, b)
+ | block_(x):b {b.size > 1 && n(b.first.pos, :block, *b) || b.first}
+block_(x) = block_(x):b - ";" s chain(x):c {Array(c)}:a {b + a}
+ | block_(x):b s chain(x):c {Array(c)}:a
+ &{b.first.pos.column == a.first.pos.column} {b + a}
+ | chain(x):c ~Array(c)
+
+chain(x) = chain_(x):c {c.size > 1 && n(c.first.pos, :chain, *c) || c.first}
+chain_(x) = chain_(x):c - "." &{x.kmsg?} - chain_(x):v {c + v}
+ | chain_(x):c &{c.last.name == :oper} (ws* nl -)?
+ value(x.at(c.first.pos)):v {c + v}
+ | chain_(x):c oper:o {c + [o]}
+ | chain_(x):c ws+
+ value(x.at(c.first.pos)):v {c + v}
+ | value(x)
+
+value(x) = value_(x):v {Array(v)}:a &{a.first.pos.column > x.pos.column} ~a
-sheebang = "#!" /.*?$/
+value_(x) = &{x.kmsg?} kmsg(x)
+ | value_(x):v p:p braced:b !&":" {Array(v) + [n(p, :send, *b)]}
+ | empty(x) | space
+ | literal(x):a (&{x.kmsg?} | !&":") ~a
-w = (sp | sheebang | nl)*
-o = sp* (sheebang | nl)
-t = ";" | sheebang | nl
-n = t | sp | "."
-- = n*
+space = p:p braced:a ~n(p, :space, *a)
-brace = < . . > &{ brace(text) } { brace(text) }
- | < . > &{ brace(text) } { brace(text) }
+empty(x) = p:p braced:a ":" ws* empty_(x):b ~n(p, :empty, *(a+b))
+empty_(x) = (braced_(x) | ~nil):a (ws* nl - block(x) | ~nil):b {Array(a) + Array(b)}
-left_brace = <brace:b> &{ text == b.first} { b }
-right_brace(l) = <brace:b> &{ text == l.last } { l }
-
-literal = float | fixnum | str | regexp
+name = p:p <(!&(ws|nl|brace|op|":"|";"|","|".") .)+> ~n(p, :name, text)
+oper = p:p <".." (op|".")* | op op*> ~n(p, :oper, text)
-symbol(h) = p:p ":" !&":" value(h.keymsg(true)):v ~n(p, :symbol, v)
+keyargs(x) = (keyargs_(x.kmsg!) | ~nil):a (ws* nl - braced_(x)| ~nil):b
+ {Array(a) + Array(b)}
+keyargs_(x) = keyargs_(x):a ws* "," ws* chain(x):c {a + Array(c)}
+ | chain(x):c ~Array(c)
+
+keyw(x) = <name | oper> ":" {[text, nil]}
+keya(x) = <name | oper> braced:a ":" {[text] + a}
+
+keyword(x) = keyw(x) | keya(x)
+part(x) = 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))
+
+parts(x) = parts(x):a - part(x):b {a + [b]}
+ | part(x):a {[a]}
+
+kmsg(x) = parts(x):a ~n(a.first.pos, :kmsg, *a)
+
+literal(x) = symbol(x) | str | float | fixnum | regexp | name | oper
+
+symbol(x) = p:p ":" !&":" value(x.kmsg):v ~n(p, :symbol, v.first)
regexp = p:p quoted(:text, & "/"):b ~n(p, :regexp, text_node(p, b))
@@ -59,94 +109,16 @@ sstr = p:p quoted(:text, & "\""):b ~text_node(p, b)
quoted(t,q) = q quoted_inner(t,q)*:b q {b}
quoted_inner(t,q) = p:p "#{" - block(ctx)?:b - "}" {b}
- | p:p < ("\\" escape | ("\\" q | "\\#" | &!((q | "#{")) .))+ >
+ | p:p < ("\\" escape | ("\\" q | "\\#" | &!((q | "#{")) .))+ >
~n(p, t, text)
mstr = p:p "\"\"\"" mstr_inner*:b "\"\"\"" ~text_node(p, b)
mstr_inner = p:p "#{" - block(h)?:b - "}" {b}
- | p:p < ( "\\" escape | ("\\\"\"\"" |
+ | p:p < ( "\\" escape | ("\\\"\"\"" |
!&("\"\"\"" | "#{") . | . &("\"\"\"")) )+ >
~n(p, :text, text)
-opchr = /[\~\!@\#\$%\^\&\|\?\<\>*\/+=-]/
-oper = < opchr (opchr)* > {text}
-
-operator = p:p oper:o ~n(p, :oper, o)
-
-name = p:p <(&(!(sp|nl|brace|opchr|":"|";"|","|".")) .)+> ~n(p, :name, text)
-
-keyword = < (!&(n | ":" | brace) .)+ > ":" !&(":"|";"|".") {text}
-keyargs = < (!&(n | ":" | brace) .)+ > args:a ":" {[text, a]}
-keypart = keyword | keyargs
-
-value(h) = msg(h)
- | value(h):v args:a ~n(v.pos, :act, v, a.name, *a.args)
- | args:a ~n(a.pos, :act, nil, a.name, *a.args)
- | literal | symbol(h)
- | &{!h.keymsg?} (operator | name) &!":"
- | &{h.keymsg?} (operator | name)
-
-comma_left(h) = block(h):a w "," {a}
-
-comma(h) = comma_left(h):a w comma(h):b { b.unshift a ; b }
- | comma_left(h):a w block(h):b { [a,b] }
- | comma_left(h):a &(sp* ("."|","|t|brace)) {[a]}
-
-tuple(h) = comma(h):c ~n(p, :tuple, *c)
-
-cons_left(h) = expr(h):a sp* "::" !&(":"|";"|".") {a}
-
-cons(h) = cons_left(h):a - chain_val(h):b ~n(p, :cons, a, b)
-
-args = p:p left_brace:l - (comma(ctx) | block(ctx) | {[]}):a - right_brace(l)
- ~n(p, l.join, *Array(a))
-
-msg(h) = &{h.keymsg?} kmsg(h) | emsg(h)
-
-kmsg(h) = kmsg(h):a sp* (&("::") | ":" &(";"|".")) {a}
- | part(h):a w kmsg(h.at?(a.pos)):m ~n(a.pos, :msg, a, *m.args)
- | part(h):a ~n(a.pos, :msg, a)
-
-part(h) = part(h):p o w block(h.at?(p.pos)):e { p.args.push *Array(e) ; p }
- | part(h):p sp+ part_head(h.at?(p.pos)):e { p.args.push *Array(e) ; p }
- | p:p keyword:k sp* "." w chain(h.at?(p)):c ~n(p, k, nil, c)
- | p:p keyargs:k ~n(p, k.first, k.last.name, *k.last.args)
- | p:p keyword:k ~n(p, k, nil)
-
-part_head(h) = !&(keypart|".")
- (ph_comma(h.keymsg(false)) | chain(h.keymsg(false)) | {[]})
-
-ph_comma(h) = chain(h):a w "," - ph_comma(h):b { b.unshift a ; b }
- | chain(h):a w "," - chain(h):b { [a,b] }
-
-emsg(h) = emsg(h):a sp* (&("::") | ":" &(";"|".")) {a}
- | (epart(h) | spart(h)):a ~n(a.pos, :msg, a)
-
-epart(h) = epart(h):p o w block(h.at?(p.pos)):e { p.args.push *Array(e) ; p }
- | epart(h):p sp+ epart_head(h.at?(p.pos)):e { p.args.push *Array(e) ; p }
- | args:a ":" !&":" ~n(a.pos, nil, a.name, *a.args)
-
-spart(h) = spart(h):p o w block(h.at?(p.pos)):e { p.args.push *Array(e) ; p }
- | spart(h):p sp* epart_head(h.at?(p.pos)):e { p.args.push *Array(e) ; p }
- | p:p ":" (sp|nl|sheebang) ~n(p, nil, nil)
-
-epart_head(h) = ph_comma(h) | chain(h.keymsg(false)) | {[]}
-
-expr(h) = value(h):e &{ e.pos.column > h.pos.column } {e}
-
-chain(h) = chain(h):a w &{h.keymsg?} "." - chain(h):b ~chain_cont(a, b)
- | operator:a !&brace o w chain(h):b
- ~n(a.pos, :chain, a, *Array(b.name == :chain && b.args || b))
- | chain_val(h):a sp* chain(h.at(a.pos)):b ~chain_cont(a, b)
- | chain_val(h)
-
-chain_val(h) = cons(h) | expr(h)
-
-block(h) = chain(h):a sp* t - block(h):b
- ~n(a.pos, :block, a, *Array(b.name == :block && b.args || b))
- | chain(h)
-
escape = number_escapes | escapes
escapes = "n" { "\n" }
| "s" { " " }
@@ -201,6 +173,4 @@ number_escapes = /[xX]/ < /[0-9a-fA-F]{1,5}/ > { [text.to_i(16)].pack("U") }
| /[uU]/ < /[0-9a-fA-F]{4}/ > { [text.to_i(16)].pack("U") }
-root = - block(ctx)?:b - eof {b}
-unit = - chain(ctx):c {c}
-eof = !.
+
Oops, something went wrong.

0 comments on commit 7222cc1

Please sign in to comment.