class Representer

Overview

Representer allows to be integrated with other programs easilly through its API.

represent = Representer.new
represent.parse_string("def foo\n  1 + 1\nend")
represent.represent
represent.representation
# => "def PLACEHOLDER_1\n  1 + 1\nend"

Defined in:

api.cr

Instance Method Summary

Instance Method Detail

def ast : Crystal::ASTNode #

[View source]
def debug_json : String #

Returns a json formatted String with the debug information.

represent = Representer.new
represent.parse_string("def foo\n  1 + 1\nend")
represent.represent
represent.debug_json
# => "[["PLACEHOLDER_1", "Crystal::Def"]]"

[View source]
def mapping_json : String #

Returns a mapping of the replaced names to the original names. The format returned is a json formatted String.

represent = Representer.new
represent.parse_string("def foo\n  1 + 1\nend")
represent.represent
represent.mapping
# => "{\"PLACEHOLDER_1\":\"foo\"}"

[View source]
def parse_file(file : Path) #

Parses a single file by reading it and then parsing it with the Crystal parser.

path = Path.new("path/to/file")
represent = Representer.new
represent.parse_file(path)

[View source]
def parse_folder(folder : Path, files : Array(String)) #

Parses specified files in a folder by grouping them into a single long String and then parsing that String with the Crystal parser.

path = Path.new("path/to/folder")
represent = Representer.new
represent.parse_folder(path, ["main.cr"])

[View source]
def parse_folder(folder : Path) #

Parses all files in a folder by grouping them into a single long String and then parsing that String with the Crystal parser.

path = Path.new("path/to/folder")
represent = Representer.new
represent.parse_folder(path)

[View source]
def parse_string(content : String) #

Parses a String by parsing it with the Crystal parser.

represent = Representer.new
represent.parse_string("def foo\n  1 + 1\nend")

[View source]
def represent #

Transforms the AST into a representation.


[View source]
def representation : String #

[View source]
def representation_json : String #

Returns a version number for the representation format. The format returned is a json formatted String.

represent = Representer.new
represent.parse_string("def foo\n  1 + 1\nend")
represent.represent
represent.representation_json
# => "{\"version\":1}"

[View source]
def update_counter(new_counter : Int32 = 1) #

Allows changing the value of the counter which is which number of the placeholder item gets. When wanting to represent brand new code, this variable has to be changed to one. When the method is called without any argumments will it defualt to one.

Example:

represent = Representer.new
represent.parse_string("def foo\n  1 + 1\nend")
represent.represent
represent.update_data([] of String)
represent.update_counter(1)
represent.parse_string("def foo\n  1 + 1\nend")
represent.represent

[View source]
def update_data(new_data : Array(String)) #

Allows changing the value of the data variable which is what is holding all names when representation. When wanting to represent brand new code, this variable has to be cleared

Example:

represent = Representer.new
represent.parse_string("def foo\n  1 + 1\nend")
represent.represent
represent.update_data([] of String)
represent.update_counter
represent.parse_string("def foo\n  1 + 1\nend")
represent.represent

[View source]