Class: CTioga2::Commands::InterpreterString

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/commands/strings.rb

Overview

All variables and arguments are only strings. There is no type in ctioga, at least not on the variable/strings level. Ruby functions of course work with typed objects, but all user input is taken to be a string and is converted using the CTioga2::Metabuilder::Type system.

A string is however not a simple Ruby String, as it can contain elements that are expanded, and it needs to provide a way to be split into substrings for arguments/options processing.

A string is composed of an array of [type, value, … ? ] arrays, where type can have the following meanings:

  • :unquoted : an unquoted string (can be split)

  • :unquoted_variable : an unquoted variable (replacement text can be split)

  • :quoted : a quoted string (cannot be split)

  • :quoted_variable : a quoted variable (cannot be split)

Defined Under Namespace

Classes: LexicalAnalyzer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents = []) ⇒ InterpreterString

Returns a new instance of InterpreterString.



241
242
243
# File 'lib/ctioga2/commands/strings.rb', line 241

def initialize(contents = [])
  @contents = contents
end

Instance Attribute Details

#contentsObject

The array of the aforementioned [type, value, …] arrays



224
225
226
# File 'lib/ctioga2/commands/strings.rb', line 224

def contents
  @contents
end

Class Method Details

.parse_until_unquoted(io, term, eoerror = true) ⇒ Object

Read the given io stream until an unquoted element of term is found. Returns the parsed InterpreterString. The terminating element is pushed back onto the stream.

If io encounters EOF before the parsing is finished, an UnterminatedString exception is raised, unless the eoerror is false.

This is the central function of the parsing of files.



235
236
237
238
239
# File 'lib/ctioga2/commands/strings.rb', line 235

def self.parse_until_unquoted(io, term, eoerror = true)
  string = InterpreterString.new
  string.contents = LexicalAnalyzer.new(io, term).parse(eoerror)
  return string
end

Instance Method Details

#expand_and_split(re, interpreter) ⇒ Object

Splits the string, after expansion, in the unquoted parts, where re matches, and returns the corresponding array of strings.

An empty expanded string expands to a null array.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/ctioga2/commands/strings.rb', line 262

def expand_and_split(re, interpreter)
  pre_expanded = expand_all_variables(interpreter)
  retval = []
  cur_str = ""
  for type, value in pre_expanded.contents
    case type
    when :quoted
      cur_str += value
    when :unquoted
      tmp = value.split(re, -1)
      cur_str += tmp[0] if tmp.size > 0
      # Push splitted stuff here:
      while tmp.size > 1
        retval << cur_str
        tmp.shift
        cur_str = tmp[0]
      end
    end
  end
  retval << cur_str
  if (retval.size == 1) && (retval.first == "")
    return []
  end
  return retval
end

#expand_to_string(interpreter) ⇒ Object

Fully expand the InterpreterString to obtain a String object. interpreter is the Interpreter object in which the expansion takes place.



248
249
250
251
252
253
254
255
# File 'lib/ctioga2/commands/strings.rb', line 248

def expand_to_string(interpreter)
  pre_expanded = expand_all_variables(interpreter)
  retval = ""
  for type, value in pre_expanded.contents 
    retval += value
  end
  return retval
end