%% parse_inputs
%    parse an input string and return the list of inputs as a
%    matrix with accompaning type or indicating an error
% inputs:
%   input_string:string - string containing input string, comma
%   delimited list of variables, each variable may or may not be
%   followed by a colon then a type string.
% outputs:
%   revised_input:matrix - matrix containing parsed list, ex.
%   [{'x' 'int'};{'y'};{ 'z:{x:real|x>1}' }]
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function revised_input = parse_inputs(input_string)
revised_input = [];
input_string2 = reshape(input_string',1,size(input_string,1)*size(input_string,2));
inputs = regexprep(input_string2,'\n','');
inputs = regexp(inputs,',','split');

% need to be careful here because pvs dependant types can have multiple
% :'s in them

% find the first :, any thing before this is considered the
% variable any thing following is considered the type
c_locations = regexp(inputs,':','start');
for i= 1:size(inputs,2)
    sub_input = [];
    if size(c_locations{i},2) == 0
        sub_input{1} = inputs{i};
        new_inputs{i} = sub_input;
    else
        sub_input{1} = inputs{i}(1:c_locations{i}(1)-1);
        sub_input{2} = inputs{i}(c_locations{i}(1)+1:end);
        new_inputs{i} = sub_input;
        %new_inputs{i}(1) = inputs{i}(1:c_locations{i}(1))
        %new_inputs{i}(2) = inputs{i}(c_locations{i}(1):end)
    end
end

%inputs = regexp(inputs,':','split')

for i=1:size(new_inputs,2)
    new_inputs{i}(1) = regexprep(new_inputs{i}(1),'\s','');
    
    valid = regexp(new_inputs{i}(1),'[a-zA-Z][_a-zA-Z0-9]*','match');
    if isempty(char(new_inputs{i}(1))) || length(valid{1}(1)) == 0 || ~strcmp(new_inputs{i}(1),valid{1}(1))
        new_inputs{i}(2) = {'error'};
        %revised_input = cat(2,revised_input,[char(inputs{i}(1)); 'error'])
    end
end
if isempty(revised_input)
    revised_input = new_inputs;
end
end