%% parse_pvs_result
%   parse the stuff that comes back from pvs batch mode, extract
%   the counter examples and theorems.
% inputs:
%   object:PVS_checker - PVS_checker object
%   result:string - string containing text that comes back from pvs
% outputs:
%   output:structure - results parsed into structure for easier
%       manipulating
%   error:boolean - 0 if no error, 1 if error found during parsing.
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function [output error] = parse_pvs_result(object,result)
found = regexp(result,'\w+(?=[\.]{4,})|(?<=[\.]{4,})\w+','match');
if (isempty(found))
    error = 1;
    output = [];
else
    seqs = [];
    for i = 1:2:size(found,2)-1
        if(~strcmp(char(found(i+1)),'proved'))
            
            found4 = [];
            % These parsing depend on the version of PVS used,
            % which may be a bad idea, although pvs is not
            
            if object.pvs_version <= 4.0
                % find the TCC that was unprovable
                search_str = sprintf('(?<=Proving formula %s[\\n]*)%s.*?(?=[ \\n]*Rerunning step)',char(found(i)),char(found(i)));
            elseif object.pvs_version <= 4.2
                search_str =  sprintf('(?<=Installing rewrite rule \\w+\\s\\s)%s.*?(?=[ \\n]*Rerunning step)',char(found(i)));
            elseif object.pvs_version >= 5.0
                search_str =  sprintf('(?<=Installing rewrite rule.*\\s\\s)%s.*?(?=[ \\n]*Rerunning step)',char(found(i)));
            end
            found2 = regexp(result,char(search_str),'match','once');
          
            
            % find the counter examples
            search_str = sprintf('(?<=%s.*?substitutions:).*?(?=This will undo the proof to: \\n%s)|(?<=%s.*?)No counter-examples(?=.*?%s)',char(found(i)),char(found(i)),char(found(i)),char(found(i)));
            found3 = regexp(result,char(search_str),'match','once');
            if (~strcmp(found3,'No counter-examples'))
                found4 = regexp(found3,'[^\n]*(?===>)|(?<===>)[^\n]*','match');
            end
            seqs = [seqs struct('id', '1234a', 'TCC', found(i), 'seq', found2, 'ce', {found4})];
        end
        
    end
    output = seqs;
    error = 0;
end
end

