Class: CTioga2::Commands::Parsers::OldFileParser

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/ctioga2/commands/parsers/old-file.rb

Overview

This class is in charge of parsing a “old style” command file.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

context, counts, debug, error, fatal, #format_exception, #identify, info, init_logger, log_to, logger, set_level, #spawn, warn

Class Method Details

.run_command_file(file, interpreter) ⇒ Object

Runs a command file targeting the given interpreter.



44
45
46
# File 'lib/ctioga2/commands/parsers/old-file.rb', line 44

def self.run_command_file(file, interpreter)
  OldFileParser.new.run_command_file(file, interpreter)
end

.run_commands(strings, interpreter) ⇒ Object

Runs the given command strings



49
50
51
# File 'lib/ctioga2/commands/parsers/old-file.rb', line 49

def self.run_commands(strings, interpreter)
  OldFileParser.new.run_commands(strings, interpreter)
end

Instance Method Details

#parse_io_object(io, interpreter) ⇒ Object

Parses a given io object, sending commands/variable definitions to the given interpreter.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ctioga2/commands/parsers/old-file.rb', line 67

def parse_io_object(io, interpreter)
  # The process is simple: we look for symbols and
  # corresponding syntax element: parentheses or assignments

  ## @todo It would be really great if assignments could be
  ## made conditional (a bit like in makefiles)
  while(1)
    symbol = up_to_next_symbol(io)
    break if not symbol
    
    while(1)
      c = io.getc
      if ! c              # EOF
        raise ParserSyntaxError, "Expecting something after symbol #{symbol}"
      end
      ch = c.chr
      if ch =~ /\s/      # blank...
        next
      elsif ch == '('    # beginning of a function call
        # Parse string:
        str = InterpreterString.parse_until_unquoted(io,")")
        # Now, we need to split str.
        args = str.expand_and_split(/\s*,\s*/, interpreter)

        cmd = interpreter.get_command(symbol)
        real_args = args.slice!(0, cmd.argument_number)
        # And now the options:
        options = {}

        # Problem: the space on the right of the = sign is
        # *significant*. 
        for o in args
          if o =~ /^\s*\/?([\w-]+)\s*=(.*)/
            if cmd.has_option? $1
              options[$1] = $2
            else
              error { 
                "Command #{cmd.name} does not take option #{$1}" 
              }
            end
          end
        end

        interpreter.context.parsing_file(symbol, io) # Missing line number
        interpreter.run_command(cmd, real_args, options)
        io.getc         # Slurp up the )
        break
      elsif ch == ':'   # Assignment
        c = io.getc
        if ! c          # EOF
          raise ParserSyntaxError, "Expecting = after :"
        end
        ch = c.chr
        if ch != '='
          raise ParserSyntaxError, "Expecting = after :"
        end
        str = InterpreterString.parse_until_unquoted(io,"\n", false)
        interpreter.variables.define_variable(symbol, str, 
                                              interpreter)
        break
      elsif ch == '='
        str = InterpreterString.parse_until_unquoted(io,"\n", false)
        interpreter.variables.define_variable(symbol, str, nil) 
        break
      else
        raise UnexpectedCharacter, "Did not expect #{ch} after #{symbol}"
      end
    end
  end
end

#run_command_file(file, interpreter) ⇒ Object

Runs a command file targeting the given interpreter.



54
55
56
57
# File 'lib/ctioga2/commands/parsers/old-file.rb', line 54

def run_command_file(file, interpreter)
  f = Utils::open(file)
  parse_io_object(f, interpreter)
end

#run_commands(strings, interpreter) ⇒ Object

Runs the given command strings



60
61
62
63
# File 'lib/ctioga2/commands/parsers/old-file.rb', line 60

def run_commands(strings, interpreter)
  io = StringIO.new(strings)
  parse_io_object(io, interpreter)
end