image thumbnail
from farg: a pedestrian M-file parser showing all used functions' syntax by us
parses an M-file searching for functions and prints their exact, literal syntax

farghtml

Contents

introduction

% FARG parses an M-file for FUNCTION tags and extracts their
%    syntax
% these functions/calls are collected
%	- M = main function
%	- S = subfunctions
%	- N = nested functions
%	- A = anonymous functions
%	- E = eval class calls
%	- X = unresolved calls
%
% formats of the run-time output, which optionally may be saved
% in a character string
%
%	MLINT error-free function:
%
%				T	= type of function/call
%				    C	= McCabe/cyclomatic complexity
%
%	function#   |   line#:	T   C	syntax
%	--------------------------------|--|--|--|--------------------
%	1		____x:	+	M		= main
%	x		____x:	-	S		= subfunction
%	x		____x:	.	   N		= nested function
%	x		____x:	@	      A		= anonymous function
%	x		____x:	!	      E		= eval class call
%	x		____x:	?	         X	= unresolved call
%
%	function with fatal syntax error(s):
%
%	line#:	column#	= <offending syntax>
%	---------------------------------------
%	____x:	y	= <MLINT error message>
%
% NOTE
%	- clicking on the underlined LINE entry opens the
%	  function in the editor at the corresponding line
%	- whitespaces and continuation statements are removed
%	  for better readability of the function syntax
%	- EOL markers may be indicated by <;>s in FARG_ANONYMOUS
%	  definitions, which still yield a valid, executable
%	  syntax when copy/pasted into the command window (see demo)
%	- definitions are indented according to the function
%	  type for easy reading
%	- current EVAL class calls are eval|evalc|evalin|feval
%	- FARG_ANONYMOUS functions are extracted from SCRIPTs
%	- FARG_ANONYMOUS functions are shown in the full context
%	  of their surrounding statement
%	- if the lex parser encounters fatal errors, it
%	  will stop processing and print a list of the
%	  offending syntax
%
%	  see also: mlint, depfun, depdir, which, functions
%
% because the LINE open syntax is available only in the
% command window output, this feature is turned of during
% the demonstration by the [-l] option (see help below)

example files

	flst={
		'fargtest'	% test function with ugly syntax
		'fargscpt'	% test script with ugly syntax
		'farg'		% the program itself
		'amp1dae'		% MATLAB stock function in the demo folder
		'fargpart'	% test script with ANONYMOUS using parentheses
		'fargerrr'	% test script with syntax errors
	};

content of function FARGTEST

	dbtype(flst{1},'13:38');
13    % - main function					%#ok
14    function	[k,varargout	]=	fargtest(...	%#ok
15    			a, b,	...			%#ok
16    			varargin...			%#ok
17    		)					%#ok
18    		fh1=@(a,  b,   c    )...		%#ok
19    			a.*b.*c;,,,x=1:10;		%#ok
20    		x=pi*sind(30);;;,fh2 = {@(x, y)  ...	%#ok
21    			x  *  y([2 : 3])};x=1:10;	%#ok
22    		fhx={					%#ok
23    			@(x) sind(x)			%#ok
24    			@(z) cosd(z)			%#ok
25    		};					%#ok
26    		x=fhx{2}(180*ccc);			%#ok
27    % - nested functions					%#ok
28    function	v=...					%#ok
29    		nest1(...				%#ok
30    			a)				%#ok
31    		subfun1;
32    		fargtest;
33    end
34    function	v=nest2(b)				%#ok
35    		v=cellfun(@(x) any(x),ccc,'uni',false);
36    		vv=nest1(pi);				%#ok
37    		fhl(2,3,4);
38    end

% NOTE
% - various continuation statements
% - surrounding of ANONYMOUS functions

FARG run-time output of function FARGTEST

	farg(flst{1},'-l');
% -or-	farg fargtest -l
% parsing...          F:\usr\matlab\tmp\fex\afarg\ver2010\fargtest.m
% done                0.2660 sec
                                
% MATLAB version  :   7.10.0.499 (R2010a)                                   
% FARG   version  :   21-Jun-2010 02:16:38                                  
% run    date     :   21-Jun-2010 02:16:42                                  
%                                                                           
% FILE            :   F:\usr\matlab\tmp\fex\afarg\ver2010\fargtest.m        
% - Pcode         :                                                         
% - type          :   FUNCTION                                              
% - date          :   21-Jun-2010                                           
% - time          :      02:16:38                                           
% - size          :          1241   bytes                                   
% - LEX tokens    :           334                                           
%   - lines       :            48                                           
%   - comments    :            38 /           79.17 %                       
%   - empty       :             1 /           2.08 %                        
%   - warnings    :             0                                           
%   - complexity  :             1   max                                     
% - calls         :            39                                           
%   - stock/user  :             6 / unique    4                             
% - functions     :            18                                           
%   - main        : +           1 / recursion 1                             
%   - subroutines : -           1                                           
%   - nested      : .           2                                           
%   - anonymous   : @           7                                           
%   - eval        : !           0                                           
%   - unresolved  : ?           6                                           
%                                                                           
% FUNCTIONS                                                                 
%     #|line      : T  C  syntax                                            
% ------------------------|--|--|--|--------------------------------------- 
%     1|        14: +  1  [k,varargout]=fargtest(a,b,varargin)              
%     2|        18: @           fh1=@(a,b,c) a.*b.*c                        
%     3|        20: @           fh2={@(x,y) x*y([2:3])}                     
%     4|        23: @           @(x) sind(x)                                
%     5|        24: @           @(z) cosd(z)                                
%     6|        26: ?              ccc [source?]                            
%     7|        28: .  1     v=nest1(a)                                     
%     8|        32: +     fargtest                                          
%     9|        34: .  1     v=nest2(b)                                     
%    10|        35: @           v=cellfun(@(x) any(x),ccc,'uni',false)      
%    11|        35: ?              ccc [source?]                            
%    12|        37: ?              fhl                                      
%    13|        39: ?              fhl                                      
%    14|        42: -  1  [ai,bi,varargout]=subfun1(ao,bo,varargin)         
%    15|        44: ?              a                                        
%    16|        44: ?              bb                                       
%    17|        45: @           afun(a,bb)={@(x,y) x.*f(y,y([1,5]))}        
%    18|        46: @           bfun(a([1:4]))=@(x) sscanf(x','%s:%d').'.*10
% NOTE
% - file type: FUNCTION
% - two headers during run-time, only the second part is saved (see below)
% - whitespaces and continuation statements are removed
% - indentation according to the function type
% - ANONYMOUS (@) functions are shown in their full context

content of script FARGSCPT

	dbtype(flst{2},'12:16');
12    	c=cellstr('a'),fh(1)=...					%#ok
13    		cellfun(@(x) sscanf(x','%s:%d'),c,'uni',...		%#ok
14    			false);clear c;					%#ok
15    	y=zeros(2,10);fh(2)={{{@(x,y) x.'*...				%#ok
16    		y(:,[1,2,5:end])}}};disp(y);

FARG run-time output of script FARGSCPT

	farg(flst{2},'-l')
% parsing...          F:\usr\matlab\tmp\fex\afarg\ver2010\fargscpt.m
% done                0.0630 sec
                                
% MATLAB version  :   7.10.0.499 (R2010a)                                           
% FARG   version  :   21-Jun-2010 02:16:38                                          
% run    date     :   21-Jun-2010 02:16:42                                          
%                                                                                   
% FILE            :   F:\usr\matlab\tmp\fex\afarg\ver2010\fargscpt.m                
% - Pcode         :                                                                 
% - type          :   SCRIPT                                                        
% - date          :   21-Jun-2010                                                   
% - time          :      02:16:38                                                   
% - size          :           473   bytes                                           
% - LEX tokens    :           113                                                   
%   - lines       :            16                                                   
%   - comments    :            14 /           87.50 %                               
%   - empty       :             1 /           6.25 %                                
%   - warnings    :             0                                                   
%   - complexity  :           NaN   max                                             
% - calls         :            14                                                   
%   - stock/user  :             6 / unique    6                                     
% - functions     :             2                                                   
%   - main        : +           0 / recursion 0                                     
%   - subroutines : -           0                                                   
%   - nested      : .           0                                                   
%   - anonymous   : @           2                                                   
%   - eval        : !           0                                                   
%   - unresolved  : ?           0                                                   
%                                                                                   
% FUNCTIONS                                                                         
%     #|line      : T  C  syntax                                                    
% ------------------------|--|--|--|----------------------------------------------- 
%     1|        13: @           fh(1)=cellfun(@(x) sscanf(x','%s:%d'),c,'uni',false)
%     2|        15: @           fh(2)={{{@(x,y) x.'*y(:,[1,2,5:end])}}}             
% NOTE
% - file type: SCRIPT

FARG saving run-time output of function FARG

% NOTE
% - no run-time output: [-s] option
% - output is saved in p
%
	p=farg(flst{3},'-s','-l');
%
% show content of output (p)
%
	disp(p);
% MATLAB version  :   7.10.0.499 (R2010a)                                                                                               
% FARG   version  :   21-Jun-2010 02:16:38                                                                                              
% run    date     :   21-Jun-2010 02:16:43                                                                                              
%                                                                                                                                       
% FILE            :   F:\usr\matlab\tmp\fex\afarg\ver2010\farg.m                                                                        
% - Pcode         :                                                                                                                     
% - type          :   FUNCTION                                                                                                          
% - date          :   21-Jun-2010                                                                                                       
% - time          :      02:16:38                                                                                                       
% - size          :         33072   bytes                                                                                               
% - LEX tokens    :         10382                                                                                                       
%   - lines       :          1334                                                                                                       
%   - comments    :           284 /           21.29 %                                                                                   
%   - empty       :           102 /           7.65 %                                                                                    
%   - warnings    :             0                                                                                                       
%   - complexity  :            22   max                                                                                                 
% - calls         :           465                                                                                                       
%   - stock/user  :           209 / unique    67                                                                                        
% - functions     :            29                                                                                                       
%   - main        : +           1 / recursion 0                                                                                         
%   - subroutines : -          17                                                                                                       
%   - nested      : .           0                                                                                                       
%   - anonymous   : @          11                                                                                                       
%   - eval        : !           0                                                                                                       
%   - unresolved  : ?           0                                                                                                       
%                                                                                                                                       
% FUNCTIONS                                                                                                                             
%     #|line      : T   C  syntax                                                                                                       
% -------------------------|--|--|--|-------------------------------------------------------------------------------------------------- 
%     1|       153: +   9  [p,pp]=farg(varargin)                                                                                        
%     2|       201: -  22  [p,par]=FARG_ini_par(magic,fver,varargin)                                                                    
%     3|       282: @            par.rexfh=@(x) regexp(x,par.rexmod,'match')                                                            
%     4|       316: @            par.scom=@(x) textscan(x,'%d/%d(%d):%[^:]:%s')                                                         
%     5|       445: -   5  [s,nl]=FARG_read(fnam,mode)                                                                                  
%     6|       483: -   1  [p,par]=FARG_get_file(p,par)                                                                                 
%     7|       512: -   7  par=FARG_chk_lint(par)                                                                                       
%     8|       527: @            fnc=@(x,y,z) sprintf(['%s%5d>',fmt,':%s'],par.bol,x(1),y(1),z(1,:))                                    
%     9|       531: @            fnc=@(x,y,z) sprintf(['%s%5d>',fmt,':%s'],par.bol,x(1),par.wnam,y(1),y(1),z(1,:))                      
%    10|       536: @            par.lint.err=cellfun(@(x,y,z) fnc(x,y,z),num2cell(1:numel(err)),{err.line},{err.message},'uni',false).'
%    11|       551: @            par.lint.ncyc=cellfun(@(x) sscanf(x,'%d'),par.lint.cyc(:,2))                                           
%    12|       599: -   6  [p,par]=FARG_get_calls(p,par)                                                                                
%    13|       610: @            bx=cellfun(@(x) sscanf(x,'%*2s%d%d%*s'),par.call(ix),'uni',false)                                      
%    14|       613: @            ex=cellfun(@(x) sscanf(x,'%*2s%d%d%*s'),par.call(find(ix)+1),'uni',false)                              
%    15|       651: -  13  [p,par]=FARG_get_class(p,par,mode)                                                                           
%    16|       719: -  19  [p,par]=FARG_get_entries(p,par)                                                                              
%    17|       824: @            p.s=@(varargin) FARG_show_entries(p,varargin{:})                                                       
%    18|       842: -   8  [p,par]=FARG_chk_entries(p,par)                                                                              
%    19|       924: @            p.U.fd(us)=cellfun(@(x) sprintf('%s[source?]',x),p.U.fn(us),'uni',false)                               
%    20|       931: -   5  [p,par]=FARG_upd_entries(p,par,fe,ur,uk)                                                                     
%    21|       951: -   2  [p,par,sr,ixb]=FARG_add_entries(p,par,sr,fe,ixb)                                                             
%    22|       969: -   7  [p,par]=FARG_set_entries(p,par,s,sr,ixb)                                                                     
%    23|      1015: @            sd=cellfun(@(a,b) sprintf(fmt,a,b),sf,sd,'uni',false)                                                  
%    24|      1030: -  13  p=FARG_get_context(p,par,fe,isclosed)                                                                        
%    25|      1091: -   3  ss=FARG_set_context(p,par,fe)                                                                                
%    26|      1126: -   6  s=FARG_set_bracket(s)                                                                                        
%    27|      1165: -  12  [p,par,s]=FARG_set_text(p,par,mode)                                                                          
%    28|      1279: -   2  FARG_sdisp(par,txt)                                                                                          
%    29|      1286: -   8  s=FARG_show_entries(p,varargin)                                                                              
% NOTE
% - the first part of the run-time output is not saved
% - each line is preceded by MATLAB comment delimiters
%   this allows of easy copy/pasting into an other M-file

FARG run-time output of stock function AMP1DEA

	farg(flst{4},'-l');
% parsing...          F:\usr\r2010a\toolbox\matlab\demos\amp1dae.m
% done                0.0470 sec
                                
% MATLAB version  :   7.10.0.499 (R2010a)                         
% FARG   version  :   21-Jun-2010 02:16:38                        
% run    date     :   21-Jun-2010 02:16:44                        
%                                                                 
% FILE            :   F:\usr\r2010a\toolbox\matlab\demos\amp1dae.m
% - Pcode         :                                               
% - type          :   FUNCTION                                    
% - date          :   21-Jun-2005                                 
% - time          :      15:24:08                                 
% - size          :          2639   bytes                         
% - LEX tokens    :           555                                 
%   - lines       :            89                                 
%   - comments    :            34 /           38.20 %             
%   - empty       :            14 /           15.73 %             
%   - warnings    :             0                                 
%   - complexity  :             1   max                           
% - calls         :            24                                 
%   - stock/user  :            10 / unique    10                  
% - functions     :             3                                 
%   - main        : +           1 / recursion 0                   
%   - subroutines : -           0                                 
%   - nested      : .           1                                 
%   - anonymous   : @           1                                 
%   - eval        : !           0                                 
%   - unresolved  : ?           0                                 
%                                                                 
% FUNCTIONS                                                       
%     #|line      : T  C  syntax                                  
% ------------------------|--|--|--|--------------------          
%     1|         1: +  1  amp1dae                                 
%     2|        31: @           Ue=@(t) 0.4*sin(200*pi*t)         
%     3|        74: .  1     dudt=f(t,u)                          

content of script FARGPART

	dbtype(flst{5},'12:29');
12    a=pi;b=a;c='c';a=...
13    	@(x) {...
14    	x.a,x.(b).c
15    
16    	patch(a,b,c),foo(a)
17    	cellfun(@(x) x,b,'uni',false),goo(a)
18    	};
19    b=@(x) [
20    	
21    
22    x,x
23    x,x
24    
25    ];
26    c=...
27    	@(x) (...
28    1:x...
29    );

FARG run-time output of script FARGPART

	farg(flst{5},'-l');
% parsing...          F:\usr\matlab\tmp\fex\afarg\ver2010\fargpart.m
% done                0.0320 sec
                                
% MATLAB version  :   7.10.0.499 (R2010a)                                                                    
% FARG   version  :   21-Jun-2010 02:16:38                                                                   
% run    date     :   21-Jun-2010 02:16:44                                                                   
%                                                                                                            
% FILE            :   F:\usr\matlab\tmp\fex\afarg\ver2010\fargpart.m                                         
% - Pcode         :                                                                                          
% - type          :   SCRIPT                                                                                 
% - date          :   21-Jun-2010                                                                            
% - time          :      02:16:38                                                                            
% - size          :           468   bytes                                                                    
% - LEX tokens    :           126                                                                            
%   - lines       :            29                                                                            
%   - comments    :            10 /           34.48 %                                                        
%   - empty       :             5 /           17.24 %                                                        
%   - warnings    :             0                                                                            
%   - complexity  :           NaN   max                                                                      
% - calls         :             5                                                                            
%   - stock/user  :             1 / unique    1                                                              
% - functions     :             3                                                                            
%   - main        : +           0 / recursion 0                                                              
%   - subroutines : -           0                                                                            
%   - nested      : .           0                                                                            
%   - anonymous   : @           3                                                                            
%   - eval        : !           0                                                                            
%   - unresolved  : ?           0                                                                            
%                                                                                                            
% FUNCTIONS                                                                                                  
%     #|line      : T  C  syntax                                                                             
% ------------------------|--|--|--|------------------------------------------------------------------------ 
%     1|        13: @           a=@(x) {x.a,x.(b).c;;patch(a,b,c),foo(a);cellfun(@(x)x,b,'uni',false),goo(a)}
%     2|        19: @           b=@(x) [;;;x,x;x,x]                                                          
%     3|        27: @           c=@(x) (1:x)                                                                 
% NOTE
% - empty lines (EOL) are indicated by <;>
% - these yields ugly yet valid constructs
%   when copy/pasted into the command window

	b=@(x) [;;;x,x;x,x]			%#ok
	b(10)
b = 

    @(x)[x,x;x,x]


ans =

    10    10
    10    10

content of erroneous script FARGERRR

	dbtype(flst{6},'12:13');
12    		a=_1;		%#ok: invalid character <_>
13    		b=@(x sind(x);	%#ok: parenthesis imbalance around here

FARG run-time output of erroneous script FARGERRR

% NOTE
% - if the parser encounters a fatal syntax error, it stops processing
%   and prints a list of the offending syntax
% - the output will be displayed in the command window even if the
%   no-print [-s] option is selected!
%
	p=farg(flst{6},'-s','-l');
% parsing...          F:\usr\matlab\tmp\fex\afarg\ver2010\fargerrr.m
% done                0.0150 sec                                    
% LEX errors     1                                                  
% -----------------------------------------------------------------

% line          12:   5 = <@>
                                      
% NOTE
% - the result (p) contains the error message(s)
%
	disp(p);
% parsing...          F:\usr\matlab\tmp\fex\afarg\ver2010\fargerrr.m
% done                0.0150 sec                                    
% LEX errors     1                                                  
% -----------------------------------------------------------------

% line          12:   5 = <@>
                                      

FARG help

	help farg;
% -or-	farg
 FARG	to look up function syntax in an M-file
 
 	FARG parses an M-file for FUNCTION tags
 	and extracts their syntax
 	these functions/calls are collected
 	- M = main function
 	- S = subfunctions
 	- N = nested functions
 	- A = anonymous functions
 	- E = eval class calls
 	- X = unresolved calls
 
 SYNTAX
 -------------------------------------------------------------------------------
 	 E    = FARG
 			returns the current FARG engine structure in E
 	        FARG FNAM  OPT1  ...  OPTx
 	 P    = FARG(FNAM,'OPT1',...,'OPTx')
 	[P,E] = FARG(...);
 
 INPUT
 -------------------------------------------------------------------------------
  FNAM	:	name of an M-file or P-file including a full or partial path
 		- a P-file must have a corresponding M-file or an error is
 		  generated
 
  OPT		description
  ----------------------------------------------------------------
     -h	:	show file synopsis only
     -e	:	show eval class calls
     -w	:	show warnings
     -l	:	do NOT make LINE entries open the file (see below)
 		- use this option if your output is NOT directed
 		  to the command window, e.g., a html file
 		- a copy/paste from the command window will remove
 		  the lineopen syntax automatically
     -s	:	do NOT print run-time output
 		- this option is turned off automatically if
 		  the file has fatal syntax errors
     -d	:	save lex parser engine
 
 OUTPUT
 -------------------------------------------------------------------------------
  E	:	FARG engine parameters
  P	:	runtime output saved in a character string in this format
 
 	MLINT error-free function:
 
 				T	= type of function/call
 				    C	= McCabe/cyclomatic complexity
 
 	function#   |   line#:	T   C	syntax
 	--------------------------------|--|--|--|--------------------
 	1		____x:	+	M		= main
 	x		____x:	-	S		= subfunction
 	x		____x:	.	   N		= nested function
 	x		____x:	@	      A		= anonymous function
 	x		____x:	!	      E		= eval class call
 	x		____x:	?	         X	= unresolved call
 
 	function with fatal syntax error(s):
 
 	line#:	column#	= <offending syntax>
 	---------------------------------------
 	____x:	y	= <MLINT error message>
 
 NOTE
 -------------------------------------------------------------------------------
 	- clicking on the underlined LINE entry opens the
 	  function in the editor at the corresponding line
 	- whitespaces and continuation statements are removed
 	  for better readability of the function syntax
 	- EOL markers may be indicated by <;>s in FARG_ANONYMOUS
 	  definitions, which still yield a valid, executable
 	  syntax when copy/pasted into the command window (see demo)
 	- definitions are indented according to the function
 	  type for easy reading
 	- current EVAL class calls are eval|evalc|evalin|feval
 	- FARG_ANONYMOUS functions are extracted from SCRIPTs
 	- FARG_ANONYMOUS functions are shown in the full context
 	  of their surrounding statement
 	- if the lex parser encounters fatal errors, it
 	  will stop processing and print a list of the
 	  offending syntax
 
 	  see also: mlint, depfun, depdir, which, functions
 
 EXAMPLE
 -------------------------------------------------------------------------------
 	farg amp1dae	% a MATLAB stock function from the demo folder
 
  % MATLAB version  :   7.8.0.347 (R2009a)                          
  % FARG   version  :   21-Jun-2010 02:16:38                        
  % run    date     :   21-Jun-2010 02:16:38                        
  %                                                                 
  % FILE            :   F:\usr\r2009a\toolbox\matlab\demos\amp1dae.m
  % - Pcode         :                                               
  % - type          :   FUNCTION                                    
  % - date          :   21-Jun-2005                                 
  % - time          :      15:24:08                                 
  % - size          :          2639   bytes                         
  % - LEX tokens    :           554                                 
  %   - lines       :            88                                 
  %   - comments    :            34 /           38.64 %             
  %   - empty       :            14 /           15.91 %             
  %   - warnings    :             0                                 
  %   - complexity  :             1   max                           
  % - calls         :            24                                 
  %   - stock/user  :            10 / unique    10                  
  % - functions     :             3                                 
  %   - main        : +           1 / recursion 0                   
  %   - subroutines : -           0                                 
  %   - nested      : .           1                                 
  %   - anonymous   : @           1                                 
  %   - eval        : !           0                                 
  %   - unresolved  : ?           0                                 
  %                                                                 
  % FUNCTIONS                                                       
  %     #|line      : T  C  syntax                                  
  % ------------------------|--|--|--|---------------------         
  %     1|         1: +  1  amp1dae                                 
  %     2|        31: @           Ue=@(t) 0.4*sin(200*pi*t)         
  %     3|        74: .  1     dudt=f(t,u)                          

Contact us at files@mathworks.com