forked from bastibe/MatlabCodeAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze_file.m
290 lines (256 loc) · 11.2 KB
/
analyze_file.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
function blocks = analyze_file(filename, tokenlist)
%ANALYZE_FILE analyzes TOKENLIST and extracts information about BLOCKS
% in FILENAME. TOKENLIST is assumed to be the content of FILENAME.
%
% Returns a struct array with fields:
% - name: the function name
% - body: the tokens that make up the body of the function
% - nesting: how deeply is this block nested within other blocks
% - children: other blocks nested within this block
% (again as a struct array)
% - variables: variables defined in this block, or properties if the
% block is a class.
% - arguments: function arguments of this block (if a function)
% - returns: return variable names of this block (if a function)
% - type: one of 'Function', 'Nested Function', 'Subfunction',
% 'Class', or 'Script'.
% - filename: the FILENAME.
% (c) 2016, Bastian Bechtold
% This code is licensed under the terms of the BSD 3-clause license
beginnings = check_settings('beginnings');
blocks = struct('name', {}, 'body', {}, 'nesting', {}, ...
'children', {}, 'variables', {}, ...
'arguments', {}, 'returns', {}, ...
'type', {}, 'filename', {});
function_stack = struct('start', {}, 'nesting', {}, 'children', {});
nesting = 0;
is_first_block = true;
main_type = '';
for current_pos = 1:length(tokenlist)
current_token = tokenlist(current_pos);
% count the 'end's to figure out function extents:
if current_token.isEqual('keyword', beginnings)
nesting = nesting + 1;
elseif current_token.isEqual('keyword', 'end')
nesting = nesting - 1;
end
% determine file type (Script, Function, or Class):
if isempty(main_type) && ...
~current_token.hasType({'linebreak', 'comment'})
if current_token.isEqual('keyword', 'function')
main_type = 'Function';
elseif current_token.isEqual('keyword', 'classdef')
main_type = 'Class';
else
main_type = 'Script';
end
end
% pre-compute intermediate values for better readability:
is_end_of_block = current_token.isEqual('keyword', 'end') && ...
~isempty(function_stack) && ...
nesting == function_stack(end).nesting;
is_end_of_function_file = current_pos == length(tokenlist) && ...
~isempty(function_stack);
is_end_of_other_file = current_pos == length(tokenlist) && ...
any(strcmp(main_type, {'Script' 'Class'}));
% build a stack of function definitions:
% We don't know where these functions end, yet. As soon as we
% know the end, it will get appended to the block list. For
% now, only record where the function starts.
if current_token.isEqual('keyword', 'function')
% include any leading space in the function body, so that
% later analysis steps can figure out the initial
% indentation of the function:
if current_pos > 1 && tokenlist(current_pos-1).hasType('space')
function_start = current_pos - 1;
else
function_start = current_pos;
end
% save the new function on the function stack:
stack_frame = struct('start', function_start, ...
'nesting', nesting-1, ...
'children', []);
function_stack = [function_stack stack_frame]; %#ok
elseif is_end_of_block || is_end_of_function_file
function_body = ...
tokenlist(function_stack(end).start:current_pos);
% determine function type (Top-Level, Nested, or Subfunction):
if nesting > 0 && current_pos ~= length(tokenlist)
block_type = 'Nested Function';
elseif is_first_block
block_type = main_type;
is_first_block = false;
else
block_type = 'Subfunction';
end
% build block struct:
new_block = struct( ...
'name', get_funcname(function_body), ...
'body', function_body, ...
'nesting', function_stack(end).nesting, ...
'children', function_stack(end).children, ...
'variables', {get_funcvariables(function_body)}, ...
'arguments', {get_funcarguments(function_body)}, ...
'returns', {get_funcreturns(function_body)}, ...
'type', block_type, 'filename', filename);
% update function stack with new block struct:
function_stack(end) = [];
if nesting > 0 && ~isempty(function_stack)
if isempty(function_stack(end).children)
function_stack(end).children = new_block;
else
function_stack(end).children = ...
[function_stack(end).children new_block];
end
else
blocks = [blocks new_block]; %#ok
end
elseif is_end_of_other_file
% in classes, variables contains properties:
if strcmp(main_type, 'Script')
variables = {get_variables(tokenlist)};
else
variables = {get_properties(tokenlist)};
end
blocks = struct('name', Token('special', filename, 0, 0), ...
'body', tokenlist, ...
'nesting', 0, ...
'children', blocks, ...
'variables', variables, ...
'arguments', [], ...
'returns', [], ...
'type', main_type, ...
'filename', filename);
end
end
end
function variables = get_properties(tokenlist)
%GET_PROPERTIES extracts all assigned property VARIABLES from TOKENLIST
% returns an object array of Tokens.
variables = Token.empty;
in_properties = false; % true whenever the loop is inside a properties
% block.
is_first = false; % true whenever the loop is between a line break and
% the beginning of the line's content.
for pos = 1:length(tokenlist)
token = tokenlist(pos);
if token.isEqual('keyword', 'properties')
in_properties = true;
is_first = false;
elseif in_properties && token.isEqual('keyword', 'end')
in_properties = false;
end
if token.hasType('linebreak')
is_first = true;
elseif token.hasType('identifier') && is_first && in_properties
variables = [variables token]; %#ok
is_first = false;
end
end
end
function variables = get_funcvariables(tokenlist)
%GET_FUNCVARIABLES extracts all assigned VARIABLES from TOKENLIST
%
% See also: get_variables
% skip the function declaration:
end_declaration = search_token('pair', ')', tokenlist, 1, +1);
variables = get_variables(tokenlist(end_declaration+1:end));
end
function variables = get_variables(tokenlist)
%GET_VARIABLES extracts all assigned VARIABLES from TOKENLIST
% Variables are things on the left hand side of equal signs which are not
% enclosed in braces.
variables = containers.Map();
for token_idx = 1:length(tokenlist)
token = tokenlist(token_idx);
if token.isEqual('punctuation', '=')
start = search_token('linebreak', [], tokenlist, token_idx, -1);
lhs_tokens = tokenlist(start:token_idx);
% all non-nested identifiers are assigned variable names
nesting = 0;
for this_token = lhs_tokens
if this_token.isEqual('pair', {'{' '('})
nesting = nesting + 1;
elseif this_token.isEqual('pair', {'}' ')'})
nesting = nesting - 1;
elseif this_token.hasType('identifier') && ...
nesting == 0 && ...
~variables.isKey(this_token.text)
variables(this_token.text) = this_token;
end
end
end
end
variables = variables.values();
variables = [variables{:}]; % convert to object array
if ~isempty(variables)
% sort by column:
[~, sort_idx] = sort([variables.col]);
variables = variables(sort_idx);
% sort by line (this preserves column ordering for variables
% on the same line):
[~, sort_idx] = sort([variables.line]);
variables = variables(sort_idx);
end
end
function name = get_funcname(tokenlist)
%GET_FUNCNAME analyzes TOKENLIST to find function name
% NAME is a Token
pos = search_token('pair', '(', tokenlist, 1, +1);
pos = search_token('identifier', [], tokenlist, pos, -1);
name = tokenlist(pos);
end
function arguments = get_funcarguments(tokenlist)
%GET_FUNCARGUMENTS analyzes TOKENLIST to find function return values
% ARGUMENTS is an object array of Tokens.
start = search_token('pair', '(', tokenlist, 1, +1);
stop = search_token('pair', ')', tokenlist, start, +1);
arguments = tokenlist(start+1:stop-1);
% extract all identifiers:
arguments = arguments(strcmp({arguments.type}, 'identifier'));
end
function returns = get_funcreturns(tokenlist)
%GET_FUNCRETURNS analyzes TOKENLIST to find function return values
% RETURNS is an object array of Tokens.
start = search_token('keyword', 'function', tokenlist, 1, +1);
pos = search_token('pair', '(', tokenlist, start, +1);
stop = search_token('identifier', [], tokenlist, pos, -1);
returns = tokenlist(start+1:stop-1);
% extract all identifiers:
returns = returns(strcmp({returns.type}, 'identifier'));
end
function token_idx = search_token(token_type, token_text, tokenlist, token_idx, increment)
%SEARCH_TOKEN search TOKENLIST for token with TOKEN_TYPE and TOKEN_TEXT
% starting from TOKEN_IDX and stepping with INCREMENT.
%
% To search for any Token with a given TOKEN_TYPE, leave TOKEN_TEXT empty
% To search for any Token with a given TOKEN_TEXT, leave TOKEN_TYPE empty
% Set INCREMENT to 1 for forward searching and -1 for backward searching
%
% Returns the TOKEN_IDX of the first matching token.
if ~isempty(token_type) && ~isempty(token_text)
while ~tokenlist(token_idx).isEqual(token_type, token_text)
if token_idx + increment < 1 || ...
token_idx + increment > length(tokenlist)
break
end
token_idx = token_idx + increment;
end
elseif ~isempty(token_text)
while ~tokenlist(token_idx).hasText(token_text)
if token_idx + increment < 1 || ...
token_idx + increment > length(tokenlist)
break
end
token_idx = token_idx + increment;
end
elseif ~isempty(token_type)
while ~tokenlist(token_idx).hasType(token_type)
if token_idx + increment < 1 || ...
token_idx + increment > length(tokenlist)
break
end
token_idx = token_idx + increment;
end
end
end