Class: Protobug::Compiler::Builder::Statement
- Inherits:
-
Object
- Object
- Protobug::Compiler::Builder::Statement
- Defined in:
- lib/protobug/compiler/builder.rb,
lib/protobug/compiler/builder_gen.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Instance Method Summary collapse
- #_class ⇒ Object
- #_def ⇒ Object
- #_module ⇒ Object
- #append(item) ⇒ Object
- #block(&blk) ⇒ Object
- #call(*args, &blk) ⇒ Object
- #comment(text) ⇒ Object
- #compact? ⇒ Boolean
- #dot(name) ⇒ Object
- #empty ⇒ Object
- #empty? ⇒ Boolean
- #identifier(name) ⇒ Object
- #index(*items, &blk) ⇒ Object
-
#initialize ⇒ Statement
constructor
A new instance of Statement.
- #list(*items, &blk) ⇒ Object
- #literal(content) ⇒ Object
- #op(operator) ⇒ Object
- #parens(item, &blk) ⇒ Object
-
#render(q) ⇒ Object
rubocop:disable Naming/MethodParameterName.
Constructor Details
#initialize ⇒ Statement
Returns a new instance of Statement.
23 24 25 |
# File 'lib/protobug/compiler/builder.rb', line 23 def initialize @items = [] end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
21 22 23 |
# File 'lib/protobug/compiler/builder.rb', line 21 def items @items end |
Instance Method Details
#_class ⇒ Object
7 8 9 |
# File 'lib/protobug/compiler/builder_gen.rb', line 7 def _class append Token.new(type: :keyword, content: "class") end |
#_def ⇒ Object
11 12 13 |
# File 'lib/protobug/compiler/builder_gen.rb', line 11 def _def append Token.new(type: :keyword, content: "def") end |
#_module ⇒ Object
15 16 17 |
# File 'lib/protobug/compiler/builder_gen.rb', line 15 def _module append Token.new(type: :keyword, content: "module") end |
#append(item) ⇒ Object
80 81 82 83 |
# File 'lib/protobug/compiler/builder.rb', line 80 def append(item) items << item self end |
#block(&blk) ⇒ Object
43 44 45 46 47 |
# File 'lib/protobug/compiler/builder.rb', line 43 def block(&blk) append Group.new( name: "block", items: [], close: "end", multi: true, indent: 2 ).tap(&blk) end |
#call(*args, &blk) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/protobug/compiler/builder_gen.rb', line 48 def call(*args, &blk) append Group.new( name: :call, items: args, open: "(", close: ")", separator: ", ", indent: 2 ).tap(&blk) end |
#comment(text) ⇒ Object
27 28 29 |
# File 'lib/protobug/compiler/builder.rb', line 27 def comment(text) append Comment.new(text) end |
#compact? ⇒ Boolean
89 |
# File 'lib/protobug/compiler/builder.rb', line 89 def compact? = false |
#dot(name) ⇒ Object
49 50 51 |
# File 'lib/protobug/compiler/builder.rb', line 49 def dot(name) append(Token.new(type: :delimiter, content: ".")).identifier(name) end |
#empty ⇒ Object
31 32 33 |
# File 'lib/protobug/compiler/builder.rb', line 31 def empty append Token.new(type: :empty, content: "") end |
#empty? ⇒ Boolean
85 86 87 |
# File 'lib/protobug/compiler/builder.rb', line 85 def empty? items.none? { |item| !item.empty? } end |
#identifier(name) ⇒ Object
35 36 37 |
# File 'lib/protobug/compiler/builder.rb', line 35 def identifier(name) append Token.new(type: :identifier, content: name) end |
#index(*items, &blk) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/protobug/compiler/builder_gen.rb', line 38 def index(*items, &blk) append Group.new( name: :index, items: items, open: "[", close: "]", separator: "," ).tap(&blk) end |
#list(*items, &blk) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/protobug/compiler/builder_gen.rb', line 29 def list(*items, &blk) append Group.new( name: :list, items: items, separator: ",", indent: 2 ).tap(&blk) end |
#literal(content) ⇒ Object
39 40 41 |
# File 'lib/protobug/compiler/builder.rb', line 39 def literal(content) append Token.new(type: :literal, content: content) end |
#op(operator) ⇒ Object
53 54 55 |
# File 'lib/protobug/compiler/builder.rb', line 53 def op(operator) append Token.new(type: :operator, content: operator) end |
#parens(item, &blk) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/protobug/compiler/builder_gen.rb', line 19 def parens(item, &blk) append Group.new( name: :parens, items: [item], open: "(", close: ")", indent: 2 ).tap(&blk) end |
#render(q) ⇒ Object
rubocop:disable Naming/MethodParameterName
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/protobug/compiler/builder.rb', line 57 def render(q) # rubocop:disable Naming/MethodParameterName first = true prev = nil q.group do items.each do |item| next if item.empty? if !first && !item.compact? && !prev&.compact? if (item.is_a?(Token) && item.type == :operator) || (prev.is_a?(Token) && prev.type == :operator) || item.is_a?(Comment) q.text " " elsif !(item.is_a?(Group) && item.name == :call) && !(item.is_a?(Group) && item.multi) q.fill_breakable " " end end first = false unless item.compact? || prev&.compact? prev = item item.render(q) end end end |