class CTioga2::Commands::InterpreterString

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:

Attributes

contents[RW]

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

Public Class Methods

new(contents = []) click to toggle source
# File lib/ctioga2/commands/strings.rb, line 241
def initialize(contents = [])
  @contents = contents
end
parse_until_unquoted(io, term, eoerror = true) click to toggle source

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.

# 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

Public Instance Methods

expand_and_split(re, interpreter) click to toggle source

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.

# 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) click to toggle source

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

# 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

Protected Instance Methods

expand_all_variables(interpreter) click to toggle source

Returns a new InterpreterString object with all variables and function calls expanded. interpreter is the Interpreter in which the expansion takes place.

# File lib/ctioga2/commands/strings.rb, line 293
def expand_all_variables(interpreter)
  c = []
  for type, value, args in @contents
    case type
    when :quoted_variable
      c << [:quoted, interpreter.variables.
            expand_variable(value, interpreter)]
    when :unquoted_variable
      c << [:unquoted, interpreter.variables.
            expand_variable(value, interpreter)]
    when :quoted_funcall
      c << [:quoted, interpreter.
            call_function(value, args)]
    when :unquoted_funcall
      c << [:unquoted, interpreter.
            call_function(value, args)]
    else
      c << [type, value]
    end
  end
  return InterpreterString.new(c)
end