class Ameba::AST::MacroCodeScanner

Overview

Scans the raw text of a macro body (its MacroLiteral nodes), telling the code apart from the comments and the literals.

Macro bodies are kept as a raw MacroLiteral text, split at macro expressions and control nodes, so i.e. a comment or a string literal can span multiple nodes - hence the scanner keeps its state between the subsequent #scan calls, which must follow the order of the nodes.

scanner = MacroCodeScanner.new
scanner.scan(%(foo "bar" # baz\n)) # => "foo            \n"

Defined in:

ameba/ast/macro_code_scanner.cr

Instance Method Summary

Instance Method Detail

def after_value! : Nil #

Marks the end of a value placed in between the scanned macro literals, i.e. the output of a {{ ... }} macro expression, so that the following / or % are treated as operators, not literal delimiters.


[View source]
def clone : self #

Returns a deep copy of the scanner, i.e. to scan alternative macro branches starting from the same lexical state.


[View source]
def scan(literal : String) : String #

Returns the macro literal with all of the comments, string, char, command, symbol, percent, regex and heredoc literals blanked out, while keeping the code within their interpolations.

The returned string has the same number of characters and lines as literal, as the blanked out characters (but newlines) are replaced with spaces.


[View source]