class DrgDSL::PrettyPrinter

Constants

OUTPUT_FORMATS

Attributes

indentation_level[R]
indentation_string[R]
output_format[R]

Public Class Methods

new(output_format: :string, multiline: true) click to toggle source

@param output_format [Symbol] @param multiline [Boolean]

# File lib/drgdsl/pretty_printer.rb, line 33
def initialize(output_format: :string,
               multiline: true)

  @indentation_level = 0
  @indentation_string = '  '

  unless OUTPUT_FORMATS.include?(output_format)
    raise "Unknown output format: #{output_format.inspect}"
  end

  @output_format = output_format
  @multiline = multiline
end
pretty_print(expression, **options) click to toggle source

@param expression [String] @return [String]

# File lib/drgdsl/pretty_printer.rb, line 21
def self.pretty_print(expression, **options)
  ast = Parser.parse(expression)
  ast = ParenCleaner.clean(ast) if options.delete(:remove_redundant_parens)

  output = ast.accept(new(**options))
  output_format = options[:output_format]
  return "<pre>#{output}</pre>" if output_format == :html
  output
end

Public Instance Methods

cache?() click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 55
def cache?
  true
end
cache_key(n) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 59
def cache_key(n)
  [
    n.hash,
    @indentation_level,
    @indentation_string,
    @output_format,
    @multiline
  ].hash
end
multiline?() click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 47
def multiline?
  @multiline
end
visit_nil() click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 51
def visit_nil
  ''
end

Private Instance Methods

ansi_colored(str, type:) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 201
def ansi_colored(str, type:)
  case type.to_sym
  when :keyword
    str.blue
  when :function
    str.green
  when :op
    str.blue
  when :table
    str.magenta
  when :variable
    str.cyan
  when :constant
    str.red
  when :drg, :adrg
    str.yellow
  else
    str
  end
end
deindent!() click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 171
def deindent!
  @indentation_level -= 1
end
escape_html(str) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 197
def escape_html(str)
  CGI.escapeHTML(str)
end
f(str) click to toggle source

@param str [String] @return [String] indented string

# File lib/drgdsl/pretty_printer.rb, line 182
def f(str)
  "#{indentation}#{str}"
end
indent!() click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 167
def indent!
  @indentation_level += 1
end
indentation() click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 175
def indentation
  return '' unless multiline?
  @indentation_string * @indentation_level
end
indented() { || ... } click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 161
def indented
  indent!
  yield
  deindent!
end
span(str, type:) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 192
def span(str, type:)
  class_name = "drgdsl-#{type}"
  %{<span class="#{class_name}">#{escape_html(str)}</span>}
end
visit_AndExpression(n) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 80
def visit_AndExpression(n)
  keyword = wrap('AND', type: :keyword)

  separator = " #{keyword} "
  separator = "\n#{f(keyword)} " if multiline?

  n.expressions.map { |e| visit(e) }.join(separator)
end
visit_BasicExpression(n) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 112
def visit_BasicExpression(n)
  "#{visit(n.variable)} #{visit(n.condition)}"
end
visit_Comparison(n) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 116
def visit_Comparison(n)
  "#{wrap(n.op, type: :op)} #{visit(n.value)} #{visit(n.table_condition)}".strip
end
visit_Constant(n) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 157
def visit_Constant(n)
  wrap(n.value, type: :constant)
end
visit_DateExpression(n) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 149
def visit_DateExpression(n)
  "#{wrap(n.opd, type: :variable)} IN (#{visit(n.left_variable)} #{visit(n.left_condition)}#{n.right_variable && " AND #{visit(n.right_variable)} #{visit(n.right_condition)}"}) #{visit(n.comparison)}".strip
end
visit_Empty(_) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 124
def visit_Empty(_)
  wrap('EMPTY', type: :keyword)
end
visit_Expression(n) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 71
def visit_Expression(n)
  keyword = wrap('OR', type: :keyword)

  separator = " #{keyword} "
  separator = "\n#{f(keyword)} " if multiline?

  n.expressions.map { |e| visit(e) }.join(separator)
end
visit_FunctionCall(n) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 104
def visit_FunctionCall(n)
  wrap(n.name, type: :function)
end
visit_NotExpression(n) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 100
def visit_NotExpression(n)
  "#{wrap('NOT', type: :keyword)} (#{visit(n.expression)})"
end
visit_ParenExpression(n) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 89
def visit_ParenExpression(n)
  return "(#{visit(n.expression)})" unless multiline?

  str = +"(\n"
  indented do
    str << f(visit(n.expression))
  end
  str << "\n"
  str << f(')')
end
visit_SrglrbTableCondition(n) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 145
def visit_SrglrbTableCondition(n)
  "#{visit(n.variable)} #{visit(n.condition)}"
end
visit_TableCondition(n) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 128
def visit_TableCondition(n)
  str = +"#{wrap(n.op.upcase, type: :keyword)} "

  if multiline? && n.tables.count > 4
    str << "(\n"
    indented do
      str << n.tables.map { |t| f(wrap(t.upcase, type: :table)) }.join(",\n")
    end
    str << "\n"
    str << f(')')
  else
    str << "(#{n.tables.map { |t| wrap(t.upcase, type: :table) }.join(', ')})"
  end

  str
end
visit_UnaryCondition(n) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 120
def visit_UnaryCondition(n)
  "#{wrap(n.op.upcase, type: :keyword)} #{visit(n.condition)}"
end
visit_Variable(n) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 153
def visit_Variable(n)
  wrap(n.name.upcase, type: :variable)
end
wrap(str, **options) click to toggle source
# File lib/drgdsl/pretty_printer.rb, line 186
def wrap(str, **options)
  return str if output_string?
  return span(str, type: options[:type]) if output_html?
  return ansi_colored(str, type: options[:type]) if output_bash?
end