Skip to content

Commit

Permalink
Don't remove docs in block yielding of macro (#3778)
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust authored and asterite committed Dec 26, 2016
1 parent 7e9be8a commit 225ff0d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
9 changes: 7 additions & 2 deletions spec/compiler/parser/to_s_spec.cr
@@ -1,8 +1,12 @@
require "../../spec_helper"

private def expect_to_s(original, expected = original, file = __FILE__, line = __LINE__)
private def expect_to_s(original, expected = original, emit_doc = false, file = __FILE__, line = __LINE__)
it "does to_s of #{original.inspect}", file, line do
Parser.parse(original).to_s.should eq(expected), file, line
str = IO::Memory.new expected.bytesize
parser = Parser.new original
parser.wants_doc = emit_doc
parser.parse.to_s(str, emit_doc: emit_doc)
str.to_s.should eq(expected), file, line
end
end

Expand Down Expand Up @@ -88,4 +92,5 @@ describe "ASTNode#to_s" do
expect_to_s "macro foo\n{% @type %}\nend"
expect_to_s "macro foo\n\\{%@type %}\nend"
expect_to_s "enum A : B\nend"
expect_to_s "# doc\ndef foo\nend", emit_doc: true
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/macros/interpreter.cr
Expand Up @@ -103,7 +103,7 @@ module Crystal
# are shown in the block instead of in the generated macro source
is_yield = node.exp.is_a?(Yield) && !@last.is_a?(Nop)
@str << " #<loc:push>begin " if is_yield
@last.to_s(@str, emit_loc_pragma: is_yield)
@last.to_s(@str, emit_loc_pragma: is_yield, emit_doc: is_yield)
@str << " end#<loc:pop> " if is_yield
end

Expand Down
1 change: 1 addition & 0 deletions src/compiler/crystal/syntax/ast.cr
Expand Up @@ -40,6 +40,7 @@ module Crystal
clone = clone_without_location
clone.location = location
clone.end_location = end_location
clone.doc = doc
clone
end

Expand Down
37 changes: 20 additions & 17 deletions src/compiler/crystal/syntax/to_s.cr
Expand Up @@ -7,37 +7,40 @@ module Crystal
to_s(io)
end

def to_s(io, emit_loc_pragma = false)
visitor = ToSVisitor.new(io, emit_loc_pragma: emit_loc_pragma)
def to_s(io, emit_loc_pragma = false, emit_doc = false)
visitor = ToSVisitor.new(io, emit_loc_pragma: emit_loc_pragma, emit_doc: emit_doc)
self.accept visitor
end
end

class ToSVisitor < Visitor
@str : IO

def initialize(@str = IO::Memory.new, @emit_loc_pragma = false)
def initialize(@str = IO::Memory.new, @emit_loc_pragma = false, @emit_doc = false)
@indent = 0
@inside_macro = 0
@inside_lib = false
end

def visit_any(node)
return true unless @emit_loc_pragma

location = node.location
return true unless location

filename = location.filename
return true unless filename.is_a?(String)
if @emit_doc && (doc = node.doc) && !doc.empty?
doc.each_line(chomp: false) do |line|
append_indent
@str << "# "
@str << line
end
@str.puts
end

@str << "#<loc:"
filename.inspect(@str)
@str << ","
@str << location.line_number
@str << ","
@str << location.column_number
@str << ">"
if @emit_loc_pragma && (loc = node.location) && loc.filename.is_a?(String)
@str << "#<loc:"
loc.filename.inspect(@str)
@str << ","
@str << loc.line_number
@str << ","
@str << loc.column_number
@str << ">"
end

true
end
Expand Down

0 comments on commit 225ff0d

Please sign in to comment.