class DrgDSL::ParenCleaner
Public Class Methods
clean(ast, remove_outer_parens: true)
click to toggle source
@param ast [Node] @return [Node] ast without redundant ParenExpressions
# File lib/drgdsl/paren_cleaner.rb, line 18 def self.clean(ast, remove_outer_parens: true) # If the whole expression is wrapped in parentheses, we can start # visiting the first child. if ast.type == 'ParenExpression' && remove_outer_parens ast = ast.expression end ast.accept(new) end
remove_redundant_parens(expression)
click to toggle source
@param expression [String] @return [String] pretty printed expression without any redundant
parentheses.
# File lib/drgdsl/paren_cleaner.rb, line 10 def self.remove_redundant_parens(expression) ast = Parser.parse(expression) reduced_ast = clean(ast) reduced_ast.to_s end
Private Instance Methods
visit_AndExpression(n)
click to toggle source
# File lib/drgdsl/paren_cleaner.rb, line 34 def visit_AndExpression(n) Ast::AndExpression.new(n.expressions.map { |e| visit(e) }) end
visit_Expression(n)
click to toggle source
# File lib/drgdsl/paren_cleaner.rb, line 30 def visit_Expression(n) Ast::Expression.new(n.expressions.map { |e| visit(e) }) end
visit_ParenExpression(n)
click to toggle source
# File lib/drgdsl/paren_cleaner.rb, line 38 def visit_ParenExpression(n) types = %w( ParenExpression DrgLink BasicExpression AndExpression NotExpression FunctionCall Empty TableCondition ) return visit(n.expression) if types.include?(n.expression.type) if n.expression.type == 'Expression' return Ast::ParenExpression.new(visit(n.expression)) end n end