Module: DBC

Defined in:
lib/dbc/dbc.rb

Defined Under Namespace

Classes: Cache, OCLParser, Parser

Constant Summary collapse

NONE =
0
PRE =
1
ALL =
2

Class Method Summary collapse

Class Method Details

.format_conditions(conditions) ⇒ Object

DBC.parse



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dbc/dbc.rb', line 73

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*(?:inv|pre|post|context).+)[ \t]*\*\/\Z/m
	$1
end

.parse(source) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dbc/dbc.rb', line 54

def DBC.parse(source)
	tokens = CTokenizer::SkipMacros.new(source)
	ctype_parser = CType::Parser.new()
	
	until tokens.empty?
		context = ctype_parser.parse(tokens)
		f_body = false
		unless tokens.scope == 0
			yield([], f_body)
			until tokens.scope == 0 or tokens.empty?
				# get back to group zero
				tokens.shift
			end
			f_body = true
		end
		yield(context, f_body)
	end
end

.parse_docs(tokens) ⇒ Object

kinda ugly…



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
# File 'lib/dbc/dbc.rb', line 90

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[0] == :COMMENT and tokens.start_of_line? and str = DBC.get_ocl(t[1])
			context = nil
			# take out context information
			str.gsub!(/^[ \t]*context[ \t]+(.+)$/) do
				if context
					source.error("multiple contexts given: '#{context.to_s}' and '#{$1}'")
				end
				context = ctype_parser.parse($1 << ';')
				context = context.first
				'' # replace with an empty string
			end
			conditions = []
			str.scan(/^[ \t]*(pre|post|inv)[ \t]*([^:]*):|\Z/) do
				conditions.push([$1, $2]) if $1
			end
			statements = str.split(/^[ \t]*(?:pre|post|inv)[^:]*:|\Z/)
			statements.shift # remove first element
			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
					outstr << t[1]
					break if t[1] == '{'
				end
			end
			outstr << doc_str
		else
			outstr << t[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