matlab.c 3.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/*
*
*   Copyright (c) 2000-2001, Darren Hiebert
*
*   This source code is released for free distribution under the terms of the
*   GNU General Public License.
*
*   This module contains functions for generating tags for Matlab scripts.
*   The tags 'function' and 'struct' are parsed.
*	Author Roland Baudin <roland65@free.fr>
*/

/*
*   INCLUDE FILES
*/
#include "general.h"	/* must always come first */

#include <string.h>

#include "parse.h"
#include "read.h"
#include "vstring.h"

/*
*   DATA DEFINITIONS
*/
typedef enum {
    K_FUNCTION,
	K_STRUCT
} MatlabKind;

static kindOption MatlabKinds [] = {
    { TRUE, 'f', "function", "Functions" },
	{ TRUE, 's', "struct", "Structures" },
};

/*
*   FUNCTION DEFINITIONS
*/

static void findMatlabTags (void)
{
    vString *name = vStringNew ();
    const unsigned char *line;
	const unsigned char *p;

    while ((line = fileReadLine ()) != NULL)
    {
		int i, ic;

		if (line [0] == '\0'  ||  line [0] == '%')
			continue;

		/* search if the line has a comment */
		for (ic = 0  ;  line [ic] != '\0'  &&  line [ic]!='%'  ;  ++ic)
			;

		/* function tag */

		/* read first word */
		for (i = 0  ;  line [i] != '\0'  &&  ! isspace (line [i])  ;  ++i)
			;

		if (strncmp ((const char *) line, "function", (size_t) 8) == 0)
		{
			const unsigned char *cp = line + i;
			const unsigned char *ptr = cp;
			boolean eq=FALSE;

			while (isspace ((int) *cp))
				++cp;

			/* search for '=' character in the line */
			while (*ptr != '\0')
			{
				if (*ptr == '=')
				{
					eq=TRUE;
					break;
				}
				ptr++;
			}

			/* '=' was found => get the right most part of the line after '=' and before '%' */
			if (eq)
			{
				ptr++;
				while (isspace ((int) *ptr))
					++ptr;

				while (*ptr != '\0' && *ptr != '%')
				{
					vStringPut (name, (int) *ptr);
					++ptr;
				}
			}

			/* '=' was not found => get the right most part of the line after
			 * 'function' and before '%' */
			else
			{
				while (*cp != '\0' && *cp != '%')
				{
					vStringPut (name, (int) *cp);
					++cp;
				}
			}

			vStringTerminate (name);
			makeSimpleTag (name, MatlabKinds, K_FUNCTION);
			vStringClear (name);
		}

		/* struct tag */

		/* search if the line contains the keyword 'struct' */
		p=(const unsigned char*) strstr ((const char*) line, "struct");

		/* and avoid the part after the '%' if any */
		if ( p != NULL && ic>0 && p < line+ic)
		{
			const unsigned char *cp = line;

			/* get the left most part of the line before '=' */
			while (*cp != '\0' && !isspace(*cp) && *cp != '=' )
			{
				vStringPut (name, (int) *cp);
				++cp;
			}

			vStringTerminate (name);
			makeSimpleTag (name, MatlabKinds, K_STRUCT);
			vStringClear (name);
		}
    }
    vStringDelete (name);
}

extern parserDefinition* MatlabParser (void)
{
    static const char *const extensions [] = { "m", NULL };
    parserDefinition* def = parserNew ("Matlab");
    def->kinds      = MatlabKinds;
    def->kindCount  = KIND_COUNT (MatlabKinds);
    def->extensions = extensions;
    def->parser     = findMatlabTags;
    return def;
}

/* vi:set tabstop=8 shiftwidth=4: */