Class: IDL::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/ridl/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(backend, options) ⇒ Engine

Returns a new instance of Engine.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ridl/runner.rb', line 44

def initialize(backend, options)
  @backend = backend ? Backend.load(backend) : Backend.null_be
  @initopts = options.merge({
    backend: @backend.name,
    macros: options[:macros].merge({
       __RIDLBE__: @backend.name.to_s,
       __RIDLBE_VER__: @backend.version
    })
  })
  @optparser = init_optparser
  @inputstack = []
  @options = nil
end

Instance Method Details

#backendObject



58
59
60
# File 'lib/ridl/runner.rb', line 58

def backend
  @backend
end

#has_input?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/ridl/runner.rb', line 78

def has_input?
  !@inputstack.empty?
end

#optionsObject



62
63
64
# File 'lib/ridl/runner.rb', line 62

def options
  @options || @initopts
end

#parse(io, opts) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ridl/runner.rb', line 168

def parse(io, opts)
  # parse IDL source
  _parser = ::IDL::Parser.new(opts)
  _parser.yydebug = opts[:debug]

  begin
    _parser.parse(io)
  rescue => ex
    IDL.error(ex.inspect)
    IDL.error(ex.backtrace.join("\n")) unless ex.is_a? IDL::ParseError
    return nil
  ensure
    io.close unless String === io || io == $stdin
  end
  _parser
end

#peek_inputObject



74
75
76
# File 'lib/ridl/runner.rb', line 74

def peek_input
  @inputstack.first
end

#pop_inputObject



70
71
72
# File 'lib/ridl/runner.rb', line 70

def pop_input
  @inputstack.shift
end

#push_input(idlfile, opts) ⇒ Object



66
67
68
# File 'lib/ridl/runner.rb', line 66

def push_input(idlfile, opts)
  @inputstack << [idlfile, opts]
end

#run(argv, runopts = {}) ⇒ Object



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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ridl/runner.rb', line 90

def run(argv, runopts = {})

  # initialize options
  @options = @initopts.merge(runopts)

  # backup current engine (if any)
  cur_engine = Thread.current[:ridl_engine]
  # store currently running engine for current thread
  Thread.current[:ridl_engine] = self

  begin
    # parse arguments
    begin
      @optparser.parse!(argv)
    rescue ArgumentError => ex
      IDL.error(ex.inspect)
      IDL.error(ex.backtrace.join("\n")) if IDL.verbose_level>0
      return false
    end

    if options[:preprocess]

      ## PREPROCESSING
      o = if options[:output].nil?
            $stdout
          else
            File.open(options[:output], 'w+')
          end
      options[:output] = o

      input_base = File.basename(argv.first)
      if (input_base != argv.first)
        options[:xincludepaths] << File.dirname(argv.first)
      end

      return !parse("#include \"#{input_base}\"", options).nil?
    else
      ## collect input files from commandline
      collect_input(argv)

      ## CODE GENERATION
      while has_input?
        # get input from stack
        _idlfile, _opts = pop_input

        _fio = if IO === _idlfile || StringIO === _idlfile
                 _idlfile
               else
                 File.open(_idlfile, 'r')
               end
        raise RuntimeError, 'cannot read from STDOUT' if $stdout == _fio

        # parse IDL source
        IDL.log(1, "RIDL - parsing #{IO === _idlfile ? 'from STDIN': (StringIO === _idlfile ? 'from string' : _idlfile)}")

        unless _parser = parse(_fio, _opts)
          return false
        end

        # process parse result -> code generation
        IDL.log(2, 'RIDL - starting code generation')

        GenFile.transaction  do
          begin
            backend.process_input(_parser, _opts)
          rescue Backend::ProcessStop
            IDL.log(2, "RIDL - processing #{IO === _idlfile ? 'from STDIN': (StringIO === _idlfile ? 'from string' : _idlfile)} stopped with \"#{$!.message}\"")
          end
        end
      end
    end
  ensure
    # restore previous state
    Thread.current[:ridl_engine] = cur_engine
  end
  true
end

#verbose_levelObject



82
83
84
# File 'lib/ridl/runner.rb', line 82

def verbose_level
  options[:verbose]
end

#verbose_level=(l) ⇒ Object



86
87
88
# File 'lib/ridl/runner.rb', line 86

def verbose_level=(l)
  options[:verbose] = l
end