view m-toolbox/classes/@LTPDAworkbench/parse.m @ 40:977eb37f31cb database-connection-manager

User friendlier errors from utils.mysql.connect
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 18:04:03 +0100
parents f0afece42f48
children
line wrap: on
line source

% PARSE an LTPDA command
%
% Try to parse out:
%   output vars
%   input vars
%   method name
%   plists
%   comment at end of line
%
% $Id: parse.m,v 1.2 2010/08/06 19:10:49 ingo Exp $
%
function out = parse(cmd)
  
  cmd = strtrim(cmd);
  
  % get comment
  st = regexp(cmd, '%', 'split');
  if numel(st)>1
    cmt = st{2};
  else
    cmt = '';
  end
  
  % get method
  str = st{1};
  mtdend = length(str);
  for kk=1:length(str)
    if str(kk) == '('
      mtdend = kk-1;
      break;
    end
  end
  mtdstart = 1;
  for kk=1:mtdend
    if str(kk) == '='
      mtdstart = kk+1;
      break;
    end
  end
  method = strtrim(regexp(str(mtdstart:mtdend), '\w*', 'match'));
  method = method{1};
  
  % get output
  output = '';
  for kk=1:mtdstart
    if str(kk) == '='
      output = str(1:kk-1);
      break;
    end
  end
  output = strrep(output, '[', '');
  output = strrep(output, ']', '');
  if ~isempty(output)
    outvars = regexp(output, ',*\s*', 'split');
  else
    outvars = {};
  end
  
  pls = {};
  pl = '';
  rest = strrep(cmd, pl, '');
  plstart = regexp(rest, 'plist\(', 'start');
  while ~isempty(plstart)
    % look for the opening (
    for kk=plstart(1):length(rest)
      if rest(kk) == '('
        plstart = kk;
        break
      end
    end
    % now look for the ending ) for this plist
    nexp = 1;
    kk=plstart+1;
    while nexp~=0
      if rest(kk) == '('
        nexp = nexp + 1;
      elseif rest(kk) == ')'
        nexp = nexp - 1;
      end
      kk=kk+1;
    end
    plend = kk-1;
    pl = ['plist' rest(plstart:plend)];
    rest = strrep(rest, pl, '');
    plstart = regexp(rest, 'plist\(', 'start');
    pls = [pls {pl}];
  end
  
  % input vars
  invars = regexp(rest, '\((.*)\)', 'tokens');
  if ~isempty(invars)
    invars = regexp(invars{1}, '\s*,\s*', 'split');
    invars = invars{1};
    for kk=1:numel(invars)
      % remove leading [ and trailing ]
      iv = strtrim(invars{kk});
      if ~isempty(iv)
        if invars{kk}(1) == '[' && invars{kk}(end) == ']'
          invars{kk} = invars{kk}(2:end-1);
        end
      else
        invars{kk} = iv;
      end
    end
  end
  out.outvars = strtrim(outvars(cellfun(@(x) ~isempty(x),outvars)));
  out.method  = strtrim(method);
  out.invars  = strtrim(invars(cellfun(@(x) ~isempty(x),invars)));
  out.pls     = strtrim(pls(cellfun(@(x) ~isempty(x),pls)));
  out.comment = strtrim(cmt);
  
end