Module: DBC

Defined in:
lib/dbc/dbc.rb

Defined Under Namespace

Classes: Parser

Constant Summary collapse

NONE =
0
PRE =
1
ALL =
2

Class Method Summary collapse

Class Method Details

.format_conditions(conditions) ⇒ Object

class Parser



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dbc/dbc.rb', line 95

def DBC.format_conditions(conditions)
  outstr = ''
  conditions.each do |type, label, conds|
    outstr << case type
      when 'pre' then '\\pre'
      when 'post' then '\\post'
      when 'inv' then '\\invariant'
    end << ' '
    if label and not label.empty?
      outstr << label << ': '
    end
    outstr << "\\code\n" << conds << "\n\\endcode\n"
  end
  outstr
end

.get_ocl(str) ⇒ Object



24
25
26
27
# File 'lib/dbc/dbc.rb', line 24

def DBC.get_ocl(str)
  str =~ /\A\/\*\*[ \t]*[\r\n]+(\s*\*?\s*(?:inv|pre|post|context).+)[ \t]*\*\/\Z/m
  $1
end

.parse_docs(tokens) ⇒ Object

kinda ugly…



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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/dbc/dbc.rb', line 112

def DBC.parse_docs(tokens)
  tokens = CTokenizer::SkipMacros.new(tokens)
  ctype_parser = CType::Parser.new()
  outstr = ''
  
  until tokens.empty?
    t = tokens.shift
    # note: comments don't affect start_of_line?
    if t.at(0) == :COMMENT and \
        tokens.start_of_line? and \
        str = DBC.get_ocl(t.at(1))
      context = nil
      # take out context information
      str.gsub!(/^[ \t]*\*?[ \t]*context[ \t]+(.+)$/) do
        if context
          source.error("multiple contexts given: '#{context}' and '#{$1}'")
        end
        context = ctype_parser.parse($1 << ';')
        context = context.first
        '' # replace with an empty string
      end

       # remove all leading '*'s when there is at least one
       # leading '*' in front of the ocl command
      if str =~ /^[ \t]*\*[ \t]*(?:pre|post|inv)[^:]*:/
        str.gsub!(/^([ \t]*)\*/, '\1')
       end
               
      conditions = []
      str.scan(/^[ \t]*(pre|post|inv)[ \t]*([^:]*):|\Z/) do
        conditions.push([$1, $2]) if $1 # skip 'end of string'
      end

      statements = str.split(/^[ \t]*(?:pre|post|inv)[^:]*:|\Z/)
      statements.shift # first element is whitespace before first condition

      #raise "check failed" if conditions.length != statements.length
      conditions.zip(statements) do |cond, stmt|
        cond.push(stmt)
      end
      # condition is an array of arrays like so:
      # [ ['pre', 'msg', 'statment'], ['post', 'msg', 'stmt'], ...]

      doc_str = ''
      # open Doxygen tag
      doc_str << '/*!'
      if context
        doc_str << if context.function?
          '\\fn' << context.to_init_s << "\n"
        else
          context = context.to_s
          if context =~ /\A(?:struct|union|enum)\W/
            '\\' << context << "\n"
          else
            '\\typedef' << context << "\n"
          end
        end
      end
      doc_str << DBC.format_conditions(conditions) << '*/'
      # if no context is given and we have function
      # try to insert the tag after then next opening braket
      if conditions.first[0] =~ /pre|post/ and not context
        until tokens.empty?
          t = tokens.shift.at(1)
          outstr << t
          break if t == '{'
        end
      end
      outstr << doc_str
    else
      outstr << t.at(1)
    end
  end
  outstr
end

.valid_check_level?(c) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
# File 'lib/dbc/dbc.rb', line 15

def DBC.valid_check_level?(c)
  case c
    when NONE, PRE, ALL
      true
    else
      false
  end
end