Class: Lisp::Format::State

Inherits:
Object show all
Defined in:
lib/carat/lisp-format.rb

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

Instance Method Summary collapse

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_convObject

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_leftObject

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.

Raises:



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

#colObject

Retrieve the current output column.



167
168
169
# File 'lib/carat/lisp-format.rb', line 167

def col
  @outputs.last.col
end

#latest_outputObject

Retrieve the latest output buffer.



172
173
174
# File 'lib/carat/lisp-format.rb', line 172

def latest_output
  @outputs.last
end

#next_argObject

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_outputObject

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_argObject

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_argObject

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_outputObject

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