ruby_parser
Advanced tools
+12
-0
@@ -0,1 +1,13 @@ | ||
| === 2.1.0 / 2011-08-15 | ||
| * 2 minor enhancements: | ||
| * Added new accessor canonicalize_conditions to toggle conditional canonicalization (on by default). (confused) | ||
| * Awesome cleanup: Replaced call to append_block by block_append. (Confusion) | ||
| * 2 bug fixes: | ||
| * Fixed handling last line of =begin/=end. (raybaxter) | ||
| * Fixed source line numbers after heredocs. (jbarreneche) | ||
| === 2.0.6 / 2011-02-18 | ||
@@ -2,0 +14,0 @@ |
@@ -202,2 +202,3 @@ class RubyLexer | ||
| src.string[src.pos, src.matched_size] = "\n" | ||
| src.extra_lines_added += 1 | ||
| src.pos += 1 | ||
@@ -731,3 +732,3 @@ else | ||
| unless src.scan(/.*?\n=end\s*(\n|\z)/m) then | ||
| unless src.scan(/.*?\n=end( |\t|\f)*[^(\n|\z)]*(\n|\z)/m) then | ||
| @comments.clear | ||
@@ -734,0 +735,0 @@ rb_compile_error("embedded document meets end of file") |
@@ -32,3 +32,2 @@ require 'stringio' | ||
| # end | ||
| def current_line # HAHA fuck you (HACK) | ||
@@ -38,4 +37,12 @@ string[0..pos][/\A.*__LINE__/m].split(/\n/).size | ||
| def extra_lines_added | ||
| @extra_lines_added ||= 0 | ||
| end | ||
| def extra_lines_added= val | ||
| @extra_lines_added = val | ||
| end | ||
| def lineno | ||
| string[0...pos].count("\n") + 1 | ||
| string[0...pos].count("\n") + 1 - extra_lines_added | ||
| end | ||
@@ -53,2 +60,3 @@ | ||
| warn({:unread_many => caller[0]}.inspect) if ENV['TALLY'] | ||
| self.extra_lines_added += str.count("\n") | ||
| string[pos, 0] = str | ||
@@ -121,3 +129,3 @@ end | ||
| class RubyParser < Racc::Parser | ||
| VERSION = '2.0.6' unless constants.include? "VERSION" # SIGH | ||
| VERSION = '2.1.0' unless constants.include? "VERSION" # SIGH | ||
@@ -127,11 +135,2 @@ attr_accessor :lexer, :in_def, :in_single, :file | ||
| def append_to_block head, tail # FIX: wtf is this?!? switch to block_append | ||
| return head if tail.nil? | ||
| return tail if head.nil? | ||
| head = s(:block, head) unless head.node_type == :block | ||
| head << tail | ||
| head | ||
| end | ||
| def arg_add(node1, node2) # TODO: nuke | ||
@@ -225,5 +224,5 @@ return s(:arglist, node2) unless node1 | ||
| def block_append(head, tail, strip_tail_block=false) | ||
| return head unless tail | ||
| return tail unless head | ||
| def block_append(head, tail) | ||
| return head if tail.nil? | ||
| return tail if head.nil? | ||
@@ -238,12 +237,6 @@ case head[0] | ||
| head = remove_begin(head) | ||
| head = s(:block, head) unless head[0] == :block | ||
| head = s(:block, head) unless head.node_type == :block | ||
| if strip_tail_block and Sexp === tail and tail[0] == :block then | ||
| head.push(*tail.values) | ||
| else | ||
| head << tail | ||
| end | ||
| head.line = line | ||
| head | ||
| head << tail | ||
| end | ||
@@ -345,4 +338,15 @@ | ||
| def initialize | ||
| super | ||
| ## | ||
| # Canonicalize conditionals. Eg: | ||
| # | ||
| # not x ? a : b | ||
| # | ||
| # becomes: | ||
| # | ||
| # x ? b : a | ||
| attr_accessor :canonicalize_conditions | ||
| def initialize(options = {}) | ||
| super() | ||
| self.lexer = RubyLexer.new | ||
@@ -353,2 +357,4 @@ self.lexer.parser = self | ||
| @canonicalize_conditions = true | ||
| self.reset | ||
@@ -546,3 +552,3 @@ end | ||
| c = cond c | ||
| c, t, f = c.last, f, t if c[0] == :not | ||
| c, t, f = c.last, f, t if c[0] == :not and canonicalize_conditions | ||
| s(:if, c, t, f).line(l) | ||
@@ -671,8 +677,4 @@ end | ||
| def new_until block, expr, pre | ||
| expr = (expr.first == :not ? expr.last : s(:not, expr)).line(expr.line) | ||
| new_while block, expr, pre | ||
| end | ||
| def new_while block, expr, pre | ||
| def new_until_or_while type, block, expr, pre | ||
| other = type == :until ? :while : :until | ||
| line = [block && block.line, expr.line].compact.min | ||
@@ -682,6 +684,7 @@ block, pre = block.last, false if block && block[0] == :begin | ||
| expr = cond expr | ||
| result = if expr.first == :not then | ||
| s(:until, expr.last, block, pre) | ||
| result = unless expr.first == :not and canonicalize_conditions then | ||
| s(type, expr, block, pre) | ||
| else | ||
| s(:while, expr, block, pre) | ||
| s(other, expr.last, block, pre) | ||
| end | ||
@@ -693,2 +696,10 @@ | ||
| def new_until block, expr, pre | ||
| new_until_or_while :until, block, expr, pre | ||
| end | ||
| def new_while block, expr, pre | ||
| new_until_or_while :while, block, expr, pre | ||
| end | ||
| def new_xstring str | ||
@@ -695,0 +706,0 @@ if str then |
+3
-5
@@ -8,6 +8,5 @@ # -*- ruby -*- | ||
| Hoe.plugin :racc | ||
| Hoe.plugin :isolate | ||
| Hoe.add_include_dirs("../../ParseTree/dev/test", | ||
| "../../RubyInline/dev/lib", | ||
| "../../sexp_processor/dev/lib") | ||
| Hoe.add_include_dirs "../../sexp_processor/dev/lib" | ||
@@ -19,4 +18,3 @@ Hoe.spec 'ruby_parser' do | ||
| extra_dev_deps << ['ParseTree', '~> 3.0'] | ||
| extra_deps << ['sexp_processor', '~> 3.0'] | ||
| dependency 'sexp_processor', '~> 3.0' | ||
@@ -23,0 +21,0 @@ self.perforce_ignore << "lib/ruby_parser.rb" if plugin? :perforce |
+2
-1
| = ruby_parser | ||
| * http://parsetree.rubyforge.org/ | ||
| home :: https://github.com/seattlerb/ruby_parser | ||
| rdoc :: http://parsetree.rubyforge.org/ruby_parser | ||
@@ -5,0 +6,0 @@ == DESCRIPTION: |
@@ -258,2 +258,7 @@ #!/usr/local/bin/ruby | ||
| def test_yylex_comment_end_space_and_text | ||
| util_lex_token("=begin blah\nblah\n=end blab\n") | ||
| assert_equal "=begin blah\nblah\n=end blab\n", @lex.comments | ||
| end | ||
| def test_yylex_comment_eos | ||
@@ -260,0 +265,0 @@ util_lex_token("# comment") |
+89
-12
@@ -36,4 +36,2 @@ #!/usr/local/bin/ruby | ||
| # puts self.name | ||
| @processor = RubyParser.new | ||
@@ -182,3 +180,3 @@ end | ||
| pt = nil | ||
| exp = rb[2..-1] | ||
| exp = rb.strip + "\n" | ||
@@ -427,9 +425,9 @@ assert_equal pt, @processor.parse(rb) | ||
| "case_splat" => 2, | ||
| "dstr_heredoc_expand" => 2, | ||
| "dstr_heredoc_windoze_sucks" => 2, | ||
| "dstr_heredoc_yet_again" => 2, | ||
| "str_heredoc" => 2, | ||
| "str_heredoc_call" => 2, | ||
| "str_heredoc_empty" => 2, | ||
| "str_heredoc_indent" => 2, | ||
| "dstr_heredoc_expand" => 1, | ||
| "dstr_heredoc_windoze_sucks" => 1, | ||
| "dstr_heredoc_yet_again" => 1, | ||
| "str_heredoc" => 1, | ||
| "str_heredoc_call" => 1, | ||
| "str_heredoc_empty" => 1, | ||
| "str_heredoc_indent" => 1, | ||
| "structure_unused_literal_wwtt" => 3, # yes, 3... odd test | ||
@@ -447,3 +445,3 @@ "undef_block_1" => 2, | ||
| def test_position_info | ||
| def test_position_info_block | ||
| rb = "a = 42\np a" | ||
@@ -472,3 +470,3 @@ pt = s(:block, | ||
| def test_position_info2 | ||
| def test_position_info_defn | ||
| rb = "def x(y)\n p(y)\n y *= 2\n return y;\nend" # TODO: remove () & ; | ||
@@ -494,2 +492,81 @@ pt = s(:defn, :x, s(:args, :y), | ||
| end | ||
| def test_position_info_heredoc | ||
| rb = <<-CODE | ||
| string = <<-HEREDOC | ||
| very long string | ||
| HEREDOC | ||
| puts string | ||
| CODE | ||
| result = @processor.parse rb | ||
| assert_equal 1, result.lasgn.line | ||
| assert_equal 4, result.call.line | ||
| end | ||
| def test_parse_if_not_canonical | ||
| rb = "if not var.nil? then 'foo' else 'bar'\nend" | ||
| pt = s(:if, | ||
| s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist)), | ||
| s(:str, "bar"), | ||
| s(:str, "foo")) | ||
| assert_equal pt, @processor.parse(rb) | ||
| end | ||
| def test_parse_if_not_noncanonical | ||
| rb = "if not var.nil? then 'foo' else 'bar'\nend" | ||
| pt = s(:if, | ||
| s(:not, | ||
| s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist))), | ||
| s(:str, "foo"), | ||
| s(:str, "bar")) | ||
| @processor.canonicalize_conditions = false | ||
| assert_equal pt, @processor.parse(rb) | ||
| end | ||
| def test_parse_while_not_canonical | ||
| rb = "while not var.nil?\n 'foo'\nend" | ||
| pt = s(:until, | ||
| s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist)), | ||
| s(:str, "foo"), true) | ||
| assert_equal pt, @processor.parse(rb) | ||
| end | ||
| def test_parse_while_not_noncanonical | ||
| rb = "while not var.nil?\n 'foo'\nend" | ||
| pt = s(:while, | ||
| s(:not, | ||
| s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist))), | ||
| s(:str, "foo"), true) | ||
| @processor.canonicalize_conditions = false | ||
| assert_equal pt, @processor.parse(rb) | ||
| end | ||
| def test_parse_until_not_canonical | ||
| rb = "until not var.nil?\n 'foo'\nend" | ||
| pt = s(:while, | ||
| s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist)), | ||
| s(:str, "foo"), true) | ||
| assert_equal pt, @processor.parse(rb) | ||
| end | ||
| def test_parse_until_not_noncanonical | ||
| rb = "until not var.nil?\n 'foo'\nend" | ||
| pt = s(:until, | ||
| s(:not, | ||
| s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist))), | ||
| s(:str, "foo"), true) | ||
| @processor.canonicalize_conditions = false | ||
| assert_equal pt, @processor.parse(rb) | ||
| end | ||
| end |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet