Permalink
Browse files

Moved parser methods into a module and started operator shuffling.

  • Loading branch information...
1 parent 7f7055a commit c293fc5c240b1fc05e1ef1bec68fb82cea87b684 @vic committed Aug 16, 2011
Showing with 169 additions and 198 deletions.
  1. +6 −3 lib/akin.rb
  2. +1 −92 lib/akin/grammar.kpeg
  3. +1 −91 lib/akin/grammar.rb
  4. +111 −0 lib/akin/parser.rb
  5. +19 −0 lib/akin/shuffle.rb
  6. +10 −9 spec/grammar_spec.rb
  7. +14 −0 spec/shuffle_spec.rb
  8. +7 −3 spec/spec_helper.rb
View
9 lib/akin.rb
@@ -1,5 +1,8 @@
-module Akin
-end
+%w{
-require File.expand_path('../akin/grammar', __FILE__)
+ parser
+ grammar
+ shuffle
+
+}.each { |f| require File.expand_path("../akin/#{f}", __FILE__) }
View
93 lib/akin/grammar.kpeg
@@ -5,98 +5,7 @@
#
%% name = Akin::Grammar
-
-%% {
- BRACES_ALIST = [
- ['(', ')'],
- ['{', '}'],
- ['[', ']'],
- ['\(', '\)'],
- ['\{', '\}'],
- ['\[', '\]']
- ]
-
- def braces
- @braces ||= BRACES_ALIST.dup
- end
-
- def brace(text)
- braces.assoc(text) || braces.rassoc(text)
- end
-
- def node(*a, &b)
- Node.new(*a, &b)
- end
-
- alias_method :n, :node
-
- def current_position(o = pos)
- Position.new(current_line(o), current_column(o))
- end
-
- def h
- Position.new(0, 0)
- end
-
- class Position
- attr_accessor :line, :column
- def initialize(line = nil, column = nil)
- @line, @column = line, column
- end
- def |(other)
- if @line.nil? || @line.zero?
- other
- else
- self
- end
- end
- def incr(line = 0, column = 1)
- self.class.new @line + line, @column + column
- end
- end
-
- class Node
- def initialize(position = Position.new, name = nil, *args)
- @pos, @name, @args = position, name, args
- end
-
- attr_reader :pos, :name, :args
-
- def first
- @args.first
- end
-
- def last
- @args.last
- end
-
- def [](idx)
- @args[idx]
- end
-
- def sexp
- sexp = []
- sexp << name if name
- sexp.push *args.map { |a| a.respond_to?(:sexp) && a.sexp || a }
- sexp
- end
- end
-
- def text_node(p, parts)
- parts = parts.compact
- return node(p, :text, "") if parts.empty?
- ary = parts.dup
- m = ary.shift
- if ary.empty?
- unless m.name == :text
- m = node(p, :chain, m, n(p, :name, "to_s"))
- end
- return m
- end
- node(p, :chain, m, *ary.map { |a| [n(p, :oper, "++"), a] }.flatten)
- end
-
-}
+%% { include Akin::Parser }
nl = "\n" | "\r\n"
sp = " " | "\t" | "\\" nl
View
92 lib/akin/grammar.rb
@@ -359,97 +359,7 @@ def self.rule_info(name, rendered)
#
-
- BRACES_ALIST = [
- ['(', ')'],
- ['{', '}'],
- ['[', ']'],
- ['\(', '\)'],
- ['\{', '\}'],
- ['\[', '\]']
- ]
-
- def braces
- @braces ||= BRACES_ALIST.dup
- end
-
- def brace(text)
- braces.assoc(text) || braces.rassoc(text)
- end
-
- def node(*a, &b)
- Node.new(*a, &b)
- end
-
- alias_method :n, :node
-
- def current_position(o = pos)
- Position.new(current_line(o), current_column(o))
- end
-
- def h
- Position.new(0, 0)
- end
-
- class Position
- attr_accessor :line, :column
- def initialize(line = nil, column = nil)
- @line, @column = line, column
- end
- def |(other)
- if @line.nil? || @line.zero?
- other
- else
- self
- end
- end
- def incr(line = 0, column = 1)
- self.class.new @line + line, @column + column
- end
- end
-
- class Node
- def initialize(position = Position.new, name = nil, *args)
- @pos, @name, @args = position, name, args
- end
-
- attr_reader :pos, :name, :args
-
- def first
- @args.first
- end
-
- def last
- @args.last
- end
-
- def [](idx)
- @args[idx]
- end
-
- def sexp
- sexp = []
- sexp << name if name
- sexp.push *args.map { |a| a.respond_to?(:sexp) && a.sexp || a }
- sexp
- end
- end
-
- def text_node(p, parts)
- parts = parts.compact
- return node(p, :text, "") if parts.empty?
- ary = parts.dup
- m = ary.shift
- if ary.empty?
- unless m.name == :text
- m = node(p, :chain, m, n(p, :name, "to_s"))
- end
- return m
- end
- node(p, :chain, m, *ary.map { |a| [n(p, :oper, "++"), a] }.flatten)
- end
-
-
+ include Akin::Parser
def setup_foreign_grammar; end
View
111 lib/akin/parser.rb
@@ -0,0 +1,111 @@
+module Akin
+
+ module Parser
+
+ BRACES_ALIST = [
+ ['(', ')'],
+ ['{', '}'],
+ ['[', ']'],
+ ['\(', '\)'],
+ ['\{', '\}'],
+ ['\[', '\]']
+ ]
+
+ def braces
+ @braces ||= BRACES_ALIST.dup
+ end
+
+ def brace(text)
+ braces.assoc(text) || braces.rassoc(text)
+ end
+
+ def node(*a, &b)
+ Node.new(*a, &b)
+ end
+
+ alias_method :n, :node
+
+ def current_position(o = pos)
+ Position.new(current_line(o), current_column(o))
+ end
+
+ def h
+ Position.new(0, 0)
+ end
+
+ class Position
+ attr_accessor :line, :column
+
+ def initialize(line = nil, column = nil)
+ @line, @column = line, column
+ end
+
+ def |(other)
+ if @line.nil? || @line.zero?
+ other
+ else
+ self
+ end
+ end
+
+ def incr(line = 0, column = 1)
+ self.class.new @line + line, @column + column
+ end
+
+ def at(line = 0, column = 0)
+ self.class.new line, column
+ end
+ end
+
+ class Node
+ def initialize(position = Position.new, name = nil, *args)
+ @pos, @name, @args = position, name, args
+ end
+
+ attr_reader :pos, :name, :args
+
+ def first
+ @args.first
+ end
+
+ def last
+ @args.last
+ end
+
+ def [](idx)
+ @args[idx]
+ end
+
+ def sexp
+ sexp = []
+ sexp << name if name
+ sexp.push *args.map { |a| a.respond_to?(:sexp) && a.sexp || a }
+ sexp
+ end
+
+ def with(name = nil, *args)
+ self.class.new pos, name || @name, *args
+ end
+
+ def shuffle
+ Shuffle.shuffle(self)
+ end
+ end
+
+ def text_node(p, parts)
+ parts = parts.compact
+ return node(p, :text, "") if parts.empty?
+ ary = parts.dup
+ m = ary.shift
+ if ary.empty?
+ unless m.name == :text
+ m = node(p, :chain, m, n(p, :name, "to_s"))
+ end
+ return m
+ end
+ node(p, :chain, m, *ary.map { |a| [n(p, :oper, "++"), a] }.flatten)
+ end
+
+ end # Parser
+
+end
View
19 lib/akin/shuffle.rb
@@ -0,0 +1,19 @@
+module Akin
+ module Shuffle
+ extend self
+
+ def shuffle(node)
+ send "shuffle_#{node.name}", node
+ end
+
+ def nothing(node)
+ node
+ end
+
+ alias_method :shuffle_name, :nothing
+ alias_method :shuffle_text, :nothing
+ alias_method :shuffle_fixnum, :nothing
+ alias_method :shuffle_float, :nothing
+
+ end
+end
View
19 spec/grammar_spec.rb
@@ -467,8 +467,10 @@
[:oper, "+"], [:name, "b"],
[:oper, "-"], [:name, "c"]]
end
+ end
- it 'allows operators arguments to be on nested line' do
+ describe 'nested args' do
+ it 'for operators can be continued on next line' do
code = <<-CODE
a +
b -
@@ -481,7 +483,7 @@
[:oper, "-"], [:name, "c"]]
end
- it 'allows nested block' do
+ it 'can be indicated on next line' do
code = <<-CODE
a foo
bar
@@ -492,7 +494,7 @@
[:act, [:name, "foo"], "()", [:name, "bar"]]]
end
- it 'allows nested block' do
+ it 'are not parsed if found dot terminator' do
code = <<-CODE
a foo .
bar
@@ -504,7 +506,7 @@
[:name, "bar"]]
end
- it 'allows nested block' do
+ it 'are not parsed if found semicolon terminator' do
code = <<-CODE
a foo ;
bar
@@ -515,7 +517,7 @@
[:name, "bar"]]
end
- it 'allows nested block with args' do
+ it 'appends nested block to activation args' do
code = <<-CODE
a foo(baz)
bar
@@ -526,7 +528,7 @@
[:act, [:name, "foo"], "()", [:name, "baz"], [:name, "bar"]]]
end
- it 'allows nested block with comma' do
+ it 'appends args from end' do
code = <<-CODE
a foo bar, baz
CODE
@@ -536,7 +538,7 @@
[:act, [:name, "foo"], "()", [:name, "bar"], [:name, "baz"]]]
end
- it 'allows nested block with comma' do
+ it 'allows args to have many lines' do
code = <<-CODE
a foo bar,
baz
@@ -547,7 +549,7 @@
[:act, [:name, "foo"], "()", [:name, "bar"], [:name, "baz"]]]
end
- it 'allows nested block with comma' do
+ it 'allows nested block with curly act' do
code = <<-CODE
a foo{} bar,
baz,
@@ -559,5 +561,4 @@
[:act, [:name, "foo"], "{}", [:name, "bar"], [:name, "baz"], [:name, "bat"]]]
end
end
-
end
View
14 spec/shuffle_spec.rb
@@ -0,0 +1,14 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+describe 'Akin operator shuffling' do
+ include_context 'grammar'
+
+ describe 'name' do
+ it 'should return same node' do
+ n = c('hi')
+ n.shuffle.should be(n)
+ end
+ end
+
+end
+
View
10 spec/spec_helper.rb
@@ -6,7 +6,7 @@ def o(line = 0, column = 0)
Akin::Grammar::Position.new(line, column)
end
- def c(code, rule = :script, fail_on_error = false, *args)
+ def c(code, rule = :root, fail_on_error = false, *args)
parser = Akin::Grammar.new(code)
method = "_"+rule.to_s.gsub('-', '_hyphen_')
args.push(o) if parser.method(method).arity > 0 && args.empty?
@@ -15,9 +15,13 @@ def c(code, rule = :script, fail_on_error = false, *args)
ok && parser.result
end
- def s(code, rule = :script, *args)
+ def s(code, rule = :root, *args)
n = c(code, rule, true, *args)
n.sexp if n
end
-
+
+ def n(code, rule = :root, *args)
+ n = c(code, rule, true, *args)
+ n.shuffle if n
+ end
end

0 comments on commit c293fc5

Please sign in to comment.