Class: MilkTea::PrettyPrinter::BaseFormatter
- Inherits:
-
Object
- Object
- MilkTea::PrettyPrinter::BaseFormatter
show all
- Defined in:
- lib/milk_tea/core/pretty_printer/base_formatter.rb
Constant Summary
collapse
- INDENT =
" "
Instance Method Summary
collapse
Constructor Details
Returns a new instance of BaseFormatter.
8
9
10
11
|
# File 'lib/milk_tea/core/pretty_printer/base_formatter.rb', line 8
def initialize
@lines = []
@indent = 0
end
|
Instance Method Details
#binding_name(name, linkage_name) ⇒ Object
45
46
47
48
49
|
# File 'lib/milk_tea/core/pretty_printer/base_formatter.rb', line 45
def binding_name(name, linkage_name)
return linkage_name if name.nil? || name.empty? || name == linkage_name
"#{name} as #{linkage_name}"
end
|
#blank_line ⇒ Object
22
23
24
|
# File 'lib/milk_tea/core/pretty_printer/base_formatter.rb', line 22
def blank_line
@lines << "" unless @lines.empty? || @lines.last.empty?
end
|
#capture_lines ⇒ Object
Renders into the line buffer via the given block, then extracts and
returns those lines (removing them from the buffer). Used to embed a
multi-line block (e.g. a proc body) inside an expression string while
keeping correct absolute indentation.
37
38
39
40
41
42
43
|
# File 'lib/milk_tea/core/pretty_printer/base_formatter.rb', line 37
def capture_lines
start = @lines.length
yield
captured = @lines[start..] || []
@lines.slice!(start..)
captured
end
|
#finish ⇒ Object
13
14
15
|
# File 'lib/milk_tea/core/pretty_printer/base_formatter.rb', line 13
def finish
@lines.join("\n") + "\n"
end
|
#line(text = "") ⇒ Object
17
18
19
20
|
# File 'lib/milk_tea/core/pretty_printer/base_formatter.rb', line 17
def line(text = "")
content = "#{INDENT * @indent}#{text}"
@lines << content.rstrip
end
|
#precedence(operator) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/milk_tea/core/pretty_printer/base_formatter.rb', line 51
def precedence(operator)
case operator
when "or"
10
when "and"
20
when "|"
30
when "^"
35
when "&"
40
when "==", "!="
50
when "<", "<=", ">", ">="
55
when "<<", ">>"
60
when "+", "-"
65
when "*", "/", "%"
70
else
0
end
end
|
#with_indent ⇒ Object
26
27
28
29
30
31
|
# File 'lib/milk_tea/core/pretty_printer/base_formatter.rb', line 26
def with_indent
@indent += 1
yield
ensure
@indent -= 1
end
|
#wrap(text, parent_precedence, current_precedence) ⇒ Object
78
79
80
81
82
|
# File 'lib/milk_tea/core/pretty_printer/base_formatter.rb', line 78
def wrap(text, parent_precedence, current_precedence)
return text if current_precedence >= parent_precedence
"(#{text})"
end
|