-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compiler: add locations to all expanded macro arguments #7008
Merged
RX14
merged 3 commits into
crystal-lang:master
from
makenowjust:feature/add-location-to-all-expanded-macro-args
Dec 4, 2018
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ module Crystal | |
class MacroInterpreter < Visitor | ||
getter last : ASTNode | ||
property free_vars : Hash(String, TypeVar)? | ||
property macro_expansion_pragmas : Hash(Int32, Array(Lexer::LocPragma))? = nil | ||
|
||
def self.new(program, scope : Type, path_lookup : Type, a_macro : Macro, call, a_def : Def? = nil, in_macro = false) | ||
vars = {} of String => ASTNode | ||
|
@@ -99,13 +100,17 @@ module Crystal | |
node.exp.accept self | ||
|
||
if node.output? | ||
# In the caseof {{yield}}, we want to paste the block's body | ||
# retaining the original node's location, so error messages | ||
# are shown in the block instead of in the generated macro source | ||
is_yield = node.exp.is_a?(Yield) && [email protected]_a?(Nop) | ||
@str << " #<loc:push>begin " if is_yield | ||
@last.to_s(@str, emit_loc_pragma: is_yield, emit_doc: is_yield) | ||
@str << " end#<loc:pop> " if is_yield | ||
if (loc = @last.location) && loc.filename.is_a?(String) || is_yield | ||
macro_expansion_pragmas = @macro_expansion_pragmas ||= {} of Int32 => Array(Lexer::LocPragma) | ||
(macro_expansion_pragmas[@str.pos.to_i32] ||= [] of Lexer::LocPragma) << Lexer::LocPushPragma.new | ||
@str << "begin " if is_yield | ||
@last.to_s(@str, macro_expansion_pragmas: macro_expansion_pragmas, emit_doc: true) | ||
@str << " end" if is_yield | ||
(macro_expansion_pragmas[@str.pos.to_i32] ||= [] of Lexer::LocPragma) << Lexer::LocPopPragma.new | ||
else | ||
@last.to_s(@str) | ||
end | ||
end | ||
|
||
false | ||
|
@@ -161,7 +166,7 @@ module Crystal | |
@last.to_s(str) | ||
end | ||
end | ||
end).at(node) | ||
end) | ||
false | ||
end | ||
|
||
|
@@ -512,33 +517,33 @@ module Crystal | |
end | ||
|
||
def visit(node : TupleLiteral) | ||
@last = TupleLiteral.map(node.elements) { |element| accept element }.at(node) | ||
@last = TupleLiteral.map(node.elements) { |element| accept element } | ||
false | ||
end | ||
|
||
def visit(node : ArrayLiteral) | ||
@last = ArrayLiteral.map(node.elements) { |element| accept element }.at(node) | ||
@last = ArrayLiteral.map(node.elements) { |element| accept element } | ||
false | ||
end | ||
|
||
def visit(node : HashLiteral) | ||
@last = | ||
HashLiteral.new(node.entries.map do |entry| | ||
HashLiteral::Entry.new(accept(entry.key), accept(entry.value)) | ||
end).at(node) | ||
end) | ||
false | ||
end | ||
|
||
def visit(node : NamedTupleLiteral) | ||
@last = | ||
NamedTupleLiteral.new(node.entries.map do |entry| | ||
NamedTupleLiteral::Entry.new(entry.key, accept(entry.value)) | ||
end).at(node) | ||
end) | ||
false | ||
end | ||
|
||
def visit(node : Nop | NilLiteral | BoolLiteral | NumberLiteral | CharLiteral | StringLiteral | SymbolLiteral | RangeLiteral | RegexLiteral | MacroId | TypeNode | Def) | ||
@last = node | ||
@last = node.clone_without_location | ||
false | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove the location of these new nodes? (here and below)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For preventing to report macro location as error location.
However, I forgot they are used to report macro interpreter error, so this is wrong.EDIT(2018-10-31): No. There is nowhere to use these locations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sry but I still don't get it, could you give an example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bew You have this code:
And you will get such a error with bad location when evaluated macro node has its location:
I think now it is wrong that evaluated macro node has location.
@asterite You added locations to evaluated macro node in #5597, and you said:
However, what is theory? I'm wondering when these locations are used and useful.
I think it is creepy that evaluated macro node has location because it is just a value except passed arguments. And I think these locations are never used.