Class: MarshalParser::CLI::Commands::AST

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/marshal-parser/cli/commands.rb

Instance Method Summary collapse

Instance Method Details

#call(**options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/marshal-parser/cli/commands.rb', line 58

def call(**options)
  if options[:require]
    require options[:require]
  end

  dump = \
    if options[:file]
      File.read(options[:file])
    elsif options[:evaluate]
      Marshal.dump(eval(options[:evaluate]))
    else
      $stdin.read
    end

  lexer = Lexer.new(dump)
  lexer.run

  parser = Parser.new(lexer)
  ast = parser.parse

  renderer = \
    if options[:annotate]
      width = options[:width] ? Integer(options[:width]) : 50
      MarshalParser::Formatters::AST::Renderers::RendererWithAnnotations.new(indent_size: 2, width: width)
    else
      MarshalParser::Formatters::AST::Renderers::Renderer.new(indent_size: 2)
    end

  formatter = \
    if options[:"only-tokens"]
      MarshalParser::Formatters::AST::OnlyTokens.new(ast, dump, renderer)
    elsif options[:compact]
      MarshalParser::Formatters::AST::SExpressionCompact.new(ast, dump, renderer)
    else
      MarshalParser::Formatters::AST::SExpression.new(ast, dump, renderer)
    end

  puts formatter.string

  if options[:symbols]
    symbols = parser.symbols
    puts ""
    puts "Symbols table:"
    puts MarshalParser::Formatters::Symbols::Table.new(symbols).string
  end
end