abstract class Crystal::Macros::ASTNode

Overview

This is the base class of all AST nodes. This methods are available to all AST nodes.

Direct Known Subclasses

Defined in:

compiler/crystal/macros.cr

Instance Method Summary

Instance Method Detail

def !=(other : ASTNode) : BoolLiteral #

Returnstrue if this node's textual representation is not the same as theother node.


def ==(other : ASTNode) : BoolLiteral #

Returnstrue if this node's textual representation is the same as theother node.


def class_name : StringLiteral #

Returns aStringLiteral that contains this node's name.

macro test
  {{ "foo".class_name }}
end

puts test # => prints StringLiteral

def column_number : StringLiteral | NilLiteral #

Returns the column numberwhere this node begins. Might returnnil if the location is not known.

The first column number in a line is1.


def doc : StringLiteral #

Returns aStringLiteral that contains the documentation comments attached to this node, or an empty string if there are none.

WARNING The return valuewill be an empty stringwhen executed outside of thecrystal docs command.


def doc_comment : MacroId #

Returns aMacroId that contains the documentation comments attached to this node, or an emptyMacroId if there are none. Each line is prefixedwith a# character to allow the output to be used directlywithin another node's documentation comment.

A common use case is combining this methodwith the@caller macro instance variable in order to allowmerging macro expansion and call comments.

WARNING The return valuewill be emptywhen executed outside of thecrystal docs command.


def end_column_number : StringLiteral | NilLiteral #

Returns the column numberwhere this node ends. Might returnnil if the location is not known.

The first column number in a line is1.


def end_line_number : StringLiteral | NilLiteral #

Returns the line numberwhere this node ends. Might returnnil if the location is not known.

The first line number in a file is1.


def filename : StringLiteral | NilLiteral #

Returns the filenamewhere this node is located. Might returnnil if the location is not known.


def id : MacroId #

Returns this node as aMacroId. Usefulwhen you need an identifier out of aStringLiteral,SymbolLiteral,Var orCall.

macro define_method(name, content)
  def {{name.id}}
    {{content}}
  end
end

define_method :foo, 1
define_method "bar", 2
define_method baz, 3

puts foo # => prints 1
puts bar # => prints 2
puts baz # => prints 3

def is_a?(type : TypeNode) : BoolLiteral #

Returnstrue if this node's type is the giventype or any of its subclasses.

type always refers to an AST node type, never a type in the program.

{{ 1.is_a?(NumberLiteral) }} # => true
{{ 1.is_a?(BoolLiteral) }}   # => false
{{ 1.is_a?(ASTNode) }}       # => true
{{ 1.is_a?(Int32) }}         # => false

NOTE This is a pseudo-method provided directly by the Crystal compiler. It cannot be redefined nor overridden.


def line_number : StringLiteral | NilLiteral #

Returns the line numberwhere this node begins. Might returnnil if the location is not known.

The first line number in a file is 1.


def nil? : BoolLiteral #

Returnstrue if this node is aNilLiteral orNop.

NOTE This is a pseudo-method provided directly by the Crystal compiler. It cannot be redefined nor overridden.


def raise(message) : NoReturn #

Gives a compile-time errorwith the givenmessage. Thiswill highlight this node in the error message.


def stringify : StringLiteral #

Returns aStringLiteral that contains this node's textual representation. Note that invoking stringify on a string literalwill return aStringLiteral that contains a string literal.

macro test
  {{ "foo".stringify }}
end

puts test # prints "foo" (including the double quotes)

def symbolize : SymbolLiteral #

Returns aSymbolLiteral that contains this node's textual representation.

{{ "foo".id.symbolize }} # => :foo

def warning(message : StringLiteral) : NilLiteral #

Emits a compile-timewarningwith the givenmessage. Thiswill highlight this node in thewarning message.