Class: Lisp::Format::State
Overview
This class represents the state of a given Formatter. It keeps track of output gathered and arguments left to be processed.
Instance Attribute Summary collapse
-
#case_conv ⇒ Object
Returns the value of attribute case_conv.
Instance Method Summary collapse
-
#args_left ⇒ Object
Get the number of arguments left to process.
-
#args_move(n = 1, relative = true) ⇒ Object
Move
nsteps forward or backward depending on sign amongst the arguments. -
#col ⇒ Object
Retrieve the current output column.
-
#initialize(args, output) ⇒ State
constructor
Create a state from arguments and a destination output.
-
#latest_output ⇒ Object
Retrieve the latest output buffer.
-
#next_arg ⇒ Object
Get the current argument and move forward one argument.
-
#output(str) ⇒ Object
Delegates output to the top-most output buffer.
-
#pop_output ⇒ Object
Pop and return the latest output buffer.
-
#previous_arg ⇒ Object
Get the previously returned argument, without moving.
-
#push_back_arg ⇒ Object
Push back the previously returned argument.
-
#push_output ⇒ Object
Push a new Output buffer to collect output in.
Constructor Details
#initialize(args, output) ⇒ State
Create a state from arguments and a destination output
144 145 146 147 148 149 |
# File 'lib/carat/lisp-format.rb', line 144 def initialize(args, output) @args = args @arg_pos = 0 @outputs = [output] @case_conv = nil end |
Instance Attribute Details
#case_conv ⇒ Object
Returns the value of attribute case_conv.
209 210 211 |
# File 'lib/carat/lisp-format.rb', line 209 def case_conv @case_conv end |
Instance Method Details
#args_left ⇒ Object
Get the number of arguments left to process
205 206 207 |
# File 'lib/carat/lisp-format.rb', line 205 def args_left @args.size - @arg_pos end |
#args_move(n = 1, relative = true) ⇒ Object
Move n steps forward or backward depending on sign amongst the arguments. Movement is relative or absolute depending on the boolean value of relative.
179 180 181 182 183 184 185 |
# File 'lib/carat/lisp-format.rb', line 179 def args_move(n = 1, relative = true) @arg_pos = relative ? @arg_pos + n : n raise IndexError.new, 'too few arguments' if @arg_pos > @args.size raise IndexError.new, 'cannot move past first argument' if @arg_pos < 0 end |
#col ⇒ Object
Retrieve the current output column.
167 168 169 |
# File 'lib/carat/lisp-format.rb', line 167 def col @outputs.last.col end |
#latest_output ⇒ Object
Retrieve the latest output buffer.
172 173 174 |
# File 'lib/carat/lisp-format.rb', line 172 def latest_output @outputs.last end |
#next_arg ⇒ Object
Get the current argument and move forward one argument.
188 189 190 191 |
# File 'lib/carat/lisp-format.rb', line 188 def next_arg args_move(1, true) @args[@arg_pos - 1] end |
#output(str) ⇒ Object
Delegates output to the top-most output buffer.
162 163 164 |
# File 'lib/carat/lisp-format.rb', line 162 def output(str) @outputs.last << str end |
#pop_output ⇒ Object
Pop and return the latest output buffer.
157 158 159 |
# File 'lib/carat/lisp-format.rb', line 157 def pop_output @outputs.pop end |
#previous_arg ⇒ Object
Get the previously returned argument, without moving.
194 195 196 197 |
# File 'lib/carat/lisp-format.rb', line 194 def previous_arg args_move(-1, relative) next_arg end |
#push_back_arg ⇒ Object
Push back the previously returned argument.
200 201 202 |
# File 'lib/carat/lisp-format.rb', line 200 def push_back_arg args_move(-1, true) end |
#push_output ⇒ Object
Push a new Output buffer to collect output in.
152 153 154 |
# File 'lib/carat/lisp-format.rb', line 152 def push_output @outputs << Output.new('', @outputs.last.col) end |