⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checksyntax.m

📁 toolbox of BVQX, This is the access between BV and matlab. It will help you to analysis data from BV
💻 M
字号:
function [errormsg] = checksyntax(snippet)
% checksyntax  - checks a snippet of code for syntax validity
%
% FORMAT:       [errmsg] = checksyntax(snippet)
%       OR      [errmsg] = checksyntax(filename)
%
% Input fields:
%
%       snippet     CR/LF separated lines of code (or single line)
%       filename    filename of the M-file to test
%
% Output fields:
%
%       errmsg      if requested (nargout > 0), any error detected
%                   by the MATLAB parser is returned, otherwise
%                   printed to stdout
%
% See also eval, try, catch.

% Version:  v0.5c
% Build:    6120415
% Date:     Dec-04 2006, 3:15 PM CET
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% enough arguments ?
if nargin < 1
    error( ...
        'BVQXtools:TooFewArguments', ...
        'Too few arguments. Try ''help %s''.', ...
        mfilename ...
    );
end

% decide between snippet and filename
if ~any(snippet==';') && ...
   ~any(snippet==',') && ...
   ~any(snippet==' ') && ...
    exist(snippet,'file') == 2

    % filename
    filecont = splittocell(asciiread(snippet), char([13,10]), 1, 1);

    % check lines first
    for r = length(filecont):-1:1
        tlc = filecont{r};

        % check first non space char
        tlr = find(tlc~=char(9) & tlc~=char(32));

        % remove comment lines
        if isempty(tlr) || ...
            tlc(tlr(1)) == '%'
            filecont(r) = [];
            continue;
        end

        % check for function declaration
        tlc = tlc(tlr(1):end);
        if length(tlc)>9 && ...
            strcmp(tlc(1:8), 'function') && ...
           (tlc(9) == char(9) || tlc(9) == char(32))
            tlc = ['FUNC' 'TION_DEF__ ' tlc(10:end)];
        end

        % put back in filecont
        filecont{r} = rcomment(tlc);
    end

    % parse functions specifically
    snippets  = splittocell(gluetostring(filecont, char(10)), ...
        ['FUNC' 'TION_DEF__ ']);
    ierrormsg = '';
    hasfuncs  = '';

    % sub code particles but first one not
    if ~isempty(snippets) && ...
        isempty(snippets{1})
        snippets = snippets(2:end);
        hasfuncs = 'function ';

    % no particles
    elseif ~isempty(snippets) && ...
        length(snippets) == 1
        ierrormsg = checksyntax(snippets{1});
        snippets  = cell(0);

    % regular particles
    elseif length(snippets) > 1
        firstsnip = splittocell(snippets{1},char([13,10]),1, 1);
        for lc = 1:length(firstsnip)
            iscomm = find(firstsnip{lc}=='%');
            if ~isempty(iscomm)
                firstsnip{lc} = firstsnip{lc}(1:iscomm(1)-1);
            end
            if ~isempty(deblank(firstsnip{lc}))
                ierrormsg = ...
                    'A function declaration may not appear in a script M-file.';
                snippets  = {''};
                break;
            end
        end
        snippets = snippets(2:end);

    % otherwise nothing to check
    else
        snippets = cell(0);
    end

    % check snippets
    if ~isempty(snippets)
        for snc = 1:length(snippets)
            ierrormsg = checksyntax([hasfuncs snippets{snc}]);
            if ~isempty(ierrormsg)
                sniplines = splittocell(snippets{snc}, char([13,10]), 1, 1);
                ierrormsg = [ierrormsg ' (in function ' sniplines{1} ')'];
                break;
            end
        end
    end

% code directly given (also used for filewise calls)
else

    % with functions
    ffound = strfind(snippet, 'function');

    % function found and at beginning
    if ~isempty(ffound) && ...
        ffound(1) < 2

        % split to lines
        snippet = splittocell(snippet,char([13,10]),1, 1);
        lc = 1;

        % go to next function
        while isempty(strfind(snippet{lc},'function'))
            lc = lc + 1;
        end

        % get function line and snippet
        fline   = snippet{lc};
        snippet = gluetostring(snippet((lc+1):end), char(10));

        % find argument delimiters
        flinea  = find(fline == '(');
        flinee  = find(fline == ')');

        % check for valid function declaration
        if ~isempty(flinea) && ...
           ~isempty(flinee) && ...
            flinee(end) > flinea(end)

            % get arguments
            fargs = splittocell(fline((flinea(end)+1):(flinee(end)-1)), ...
                [', ' char(9)], 1, 1);

            % check arguments
            for fac = length(fargs):-1:1
                if isempty(fargs{fac})
                    fargs(fac) = [];
                end
            end
        else

            % otherwise say 0 arguments
            fargs = cell(0);
        end

        % check arguments syntax by setting all args to [];
        snippet = ['varargin=cell(0);varargout=cell(0);' sprintf('%s=[];',fargs{:}) char(10) ...
                   snippet char(10)  'clear varargin varargout' sprintf(' %s',fargs{:}) ';'];
    end

    % try the actual code by never true if
    try
        eval(['if 0==1, ' snippet char(10) char(10) ' end']);
        ierrormsg = '';
    catch
        ierrormsg = lasterr;
    end
end

% only print error if raised
if nargout > 0
    errormsg = ierrormsg;
else
    disp(['Reported: ' ierrormsg]);
end


% internal function to remove comment
function cremoved = rcomment(cremoved)
    pct = find(cremoved~=char(9) & cremoved~=char(32));
    if isempty(pct) || ...
        cremoved(pct(1)) == '%'
        cremoved = '';
    else
        pcc = find(cremoved == '%');
        if isempty(pcc)
            return;
        end
        if ~any(cremoved == '''')
            cremoved = cremoved(1:pcc(1)-1);
        end
    end
% end of function cremoved

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -