image thumbnail

gettok: a pedestrian matlab constructs decoder

by

 

16 Feb 2002 (Updated )

a decoder/tokenizer for MATLAB constructs

o=stok(ivar)
% O = STOK(IN)
%
%	  a pedestrian GETTOK wrapper
%	  creates a gui with two panels
%
% IN	: a valid MATLAB variable|construct (struct|cell|object|...)
% O	: output structure
%	  see: help GETTOK for explanations
%
% PANEL   TAG / contents
%--------------------------------------------------------------------------------
% upper
%	  ENTRY
%	     nr of token
%	  CONSTRUCTOR SYNTAX
%	     shows the MATLAB syntax to retrieve the contents of the construct
%	  CONTENTS DESCRIPTOR
%	     shows a brief summary of the CONSTRUCTOR's contents
%
%	  selection
%	     shows brief help: 1st line
%	     shows version information: 2nd line
%	     shows the CONTENTS of the selecte entry in the lower panel
%	     note  the output format is determined by FORMAT
%
% lower
%	  SYNOPSIS
%	     shows a summary of the input and the selected entry
%
%	  selection
%	     shows the CONSTRUCTOR at the command prompt (for copy/paste)
%--------------------------------------------------------------------------------
%
% EXAMPLES
%	  STOKDEMO
%	  GETTOK -h

% created:
%	us	14-Apr-2005
% modified:
%	us	25-Apr-2005 09:06:54	/ TMW
%--------------------------------------------------------------------------------
function o=stok(ivar)

		sver='25-Apr-2005 09:06:54';
		w=[];
	if	nargout
		o.sver=sver;
	end
	if	~nargin
		help(mfilename);
		return;
	end

		sflg=1;
		inam=inputname(1);
		vnam=inam;
		icls=class(ivar);
	if	isempty(ivar)
		VAR.val=ivar;
	elseif	ischar(ivar)
		VAR.val=ivar;
	elseif	isnumeric(ivar)
		VAR.val=ivar;
	else
		sflg=0;
		VAR=ivar;
	end
		del1=': ';
		del2='  =  ';
		nflg='-n';
		dcls=inam;
	if	isempty(inam)
		nflg='-nx';
		vnam='VAR';
		inam=char('?'.*ones(size(vnam)));
		dcls='unnamed   (assigned to VAR)';
	end
		opt={
			'-f',-3,...
			'-q',...
			'-sd','@STR >','<',...
			nflg,inam
		};

		[u,v,w]=gettok(VAR,opt{:});
		w.sver=sver;
		par.sflg=sflg;
		par.inam=inam;
		par.vnam=vnam;
		par.ides=sprintf('%s:   %s',icls,dcls);
		par.rdes=sprintf('%s',dcls);
		par.ver=vtxt(w);

	if	w.n
	if	par.sflg || w.opt.hasval
		w.field=strrep(w.field,[vnam '.val'],vnam);
	end
		ent=num2str((1:w.n).');
		fmt=max(length('ENTRY'),size(ent,2));
		ent=[repmat(' ',size(ent,1),fmt-size(ent,2)),ent];
		e=strvcat('ENTRY','-',ent);
		u=strvcat('CONSTRUCTOR SYNTAX','-',char(w.field));
		v=strvcat('CONTENTS DESCRIPTOR','-',char(v));
		del1=repmat(del1,size(u,1),1);
		del2=repmat(del2,size(u,1),1);
		txt=[e,del1,u,del2,v];
		txt(2,:)=repmat('-',1,size(txt,2));
		txt(1,end+1:end+7)=' (help)';
		txt(2,end-6:end  )=' (info)';

		clf;
		set(gcf,'visible','off');
		set(gcf,'toolbar','none');
		set(gcf,'menubar','none');
		set(gcf,'numbertitle','off');
		set(gcf,'name',['STOK source>     ' par.ides]);

		uh=[uicontrol;uicontrol];
		set(uh,'units','normalized');
		set(uh,'style','listbox');
		set(uh,'max',1);
		set(uh,'value',1);
		set(uh,'foregroundcolor',[0 0 .50]);
		set(uh,'backgroundcolor',[1 1 .75]);
		set(uh,'horizontalalignment','left');
		set(uh,'fontsize',9);
		set(uh,'fontname','courier new');

		set(uh(1),'position',...
			[0 .5 1 .5]+.005*[1 1 -2 -2]);
		set(uh(1),'callback',{@lcb,ivar,VAR,w,par,uh(2)});
% R14.SP2: this may take a long time...
		set(uh(1),'string',txt);

		set(uh(2),'position',...
			[0 .0 1 .5]+.005*[1 1 -2 -1]);
		set(uh(2),'userdata',[]);
		set(uh(2),'callback',{@fcb,w,par,uh(2)});
		set(uh(2),'string',htxt);
		set(gcf,'visible','on');
		shg;
	end
	if	nargout
		o=w;
	end
		return;
%--------------------------------------------------------------------------------
% CALLBACK ROUTINES
%--------------------------------------------------------------------------------
function	lcb(h,e,ivar,VAR,w,par,uh)

		v=get(h,'value')-2;
		set(uh,'userdata',[]);
		set(uh,'value',1);

	if	v > 0 && v <= w.n
		com=w.field{v};
	if	par.sflg
		VAR=ivar;
		com='VAR;';
	else
		com=['VAR' com(length(par.inam)+1:end) ';'];
	end

% use <eval> to demonstrate
% the validity of the constructor

		t=get_whos(w,VAR);
	if	strcmp(w.desc{v}(1:2),'@E')
		r=sprintf(...
			['******     valid  empty CONSTRUCTOR\n',...
			 '******     cannot evaluate CONTENTS\n\n',...
			 'e.g.\n',...
			 '           astruct = struct(''afield'',{}); %% VALID\n',...
			 '           avalue  = astruct.afield       %% ERROR\n\n\n',...
			 '%s'],...
			  par.ver);
		r=strread(r.','%s','delimiter','\n','whitespace','');
		s.class='?????';
		s.bytes=0;
		s.size=0;
		s.tsize='?????';
	else
	try
% TEST: catch output
%		com=com(end:-1:1);
		SYNTAX=eval(com);
		s=get_whos(w,SYNTAX);
		r=evalc('SYNTAX');
		r=strread(r.','%s','delimiter','','whitespace','');
	catch
		com=com(1:end-1);
		r=sprintf(...
			['UNEXPECTED ERROR\n\n',...
			 'constructor:\n\n',...
			 '          <%s>\n\n',...
			 'descriptor :\n\n',...
			 '          <%s>\n\n\n',...
			 '%s\n\n\n',...
			 'please, contact the author'],...
			  com,w.desc{v},par.ver);
		r=strread(r.','%s','delimiter','\n','whitespace','');
		set(uh,'string',char(r));
		set(uh,'userdata',w.field{v});
		return;
	end
	end
		txt=            sprintf('SOURCE >   %s',par.rdes);
		txt=str2mat(txt,sprintf('class  :   %s',t.class));
		txt=str2mat(txt,sprintf('dim    :   %s',t.tsize));
		txt=str2mat(txt,sprintf('bytes  :   %-1d',t.bytes));
		txt=str2mat(txt,sprintf('entries:   %-1d',w.n));
		txt=str2mat(txt,sprintf('format :   %s',get(0,'format')));
		txt=str2mat(txt,'-');
		deln=size(txt,1);
		txt=str2mat(txt,sprintf('entry  :   %-1d',v));
		txt=str2mat(txt,sprintf('DESC   :   %s',w.desc{v}));
		txt=str2mat(txt,sprintf('class  :   %s',s.class));
		txt=str2mat(txt,sprintf('dim    :   %s',s.tsize));
		txt=str2mat(txt,sprintf('bytes  :   %-1d',s.bytes));
		txt=str2mat(txt,sprintf('SYNTAX <   %s',w.field{v}));
		txt=str2mat(txt,'-');
		dele=size(txt,1);
		r=str2mat(txt,char(r));
		r(deln,:)=repmat('-',1,size(r,2));
		r(dele,:)=txt(deln);
		set(uh,'string',r);
		set(uh,'userdata',w.field{v});
	elseif v == 0
		r=sprintf('VERSION\n\n%s',par.ver);
		r=strread(r.','%s','delimiter','\n','whitespace','');
		set(uh,'string',r);
	else
		set(uh,'string',htxt);
		return;
	end
		return;
%--------------------------------------------------------------------------------
function	fcb(h,e,w,par,uh)

		udat=get(uh,'userdata');
	if	~isempty(udat)
		txt=sprintf('%s\nconstructor = %s',par.ides,udat);
		disp(txt);
	end
		set(uh,'value',1);
		return;
%--------------------------------------------------------------------------------
% UTILITIES / HELP
%--------------------------------------------------------------------------------
function	t=get_whos(w,var)

		t=whos('var');
		t.tsize=sprintf('%-1d: %s',numel(t.size),num2str(t.size,'%-1d '));
		t.tsize(end)='';

	if	w.mver < 7
		return;
	end

		ext='';
	if	t.global
		ext=[ext 'global.'];	% this should NEVER happen!
	end
	if	t.sparse
		ext=[ext 'sparse.'];
	end
	if	t.complex
		ext=[ext 'complex.'];
	end
	if	~isempty(ext)
		t.class=sprintf('%s%s',ext,t.class);
	end
		return
%--------------------------------------------------------------------------------
function	txt=vtxt(w)

		txt=sprintf(...
			['MATLAB     release:    %s [%s]\n',...
			 'MATLAB     version:    %g\n',...
			 'GETTOK     version:    %s\n',...
			 'STOK       version:    %s'],...
			  w.mrel,computer,w.mver,w.version,w.sver);
		return;
%--------------------------------------------------------------------------------
function	txt=htxt

		txt={
'PANEL'
'       upper :    select first  line to show this help'
'                  select second line to show version information'
'                  select an  entry to display its CONTENTS    in the lower panel'
'       lower :    select any item  to display the CONSTRUCTOR at the command prompt'
'                  note:  the output format is determined by   FORMAT'
' '
'CONSTRUCTOR SYNTAX'
'                  shows  the MATLAB syntax to retrieve the contents of the construct'
'                  note:  an empty or unnamed input is assigned to VAR'
'CONTENTS DESCRIPTOR'
'                  shows  a brief summary of the CONSTRUCTOR''s contents'
'       STRING     @STR >string< [b]'
'       OTHER      @type.nri(size:class) [b]'
'       -type :    @C      cell'
'                  @E      valid  empty constructor'
'                          cannot evaluate contents'
'                  @N      numerical'
'                  @O      object'
'                  @S      structure'
'                  @T      char array'
'       -nri  :    nr      of instance of type'
'       -size :    size    of contents'
'       -class:    class   of contents'
'       -b    :    nr      of bytes'
		};
		txt=char(txt);
		return;
%--------------------------------------------------------------------------------'

Contact us