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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/*
* Copyright (c) 2001-2002, Nick Hibma <n_hibma@van-laarhoven.org>
*
* This source code is released for free distribution under the terms of the
* GNU General Public License version 2 or (at your option) any later version.
*
* This module contains functions for generating tags for YACC language files.
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include <string.h>
#include "parse.h"
#include "routines.h"
#include "read.h"
#include "mio.h"
#include "promise.h"
enum yaccParsingArea {
YACC_TOP_LEVEL,
YACC_C_PROLOGUE,
YACC_UNION,
YACC_GRAMMAR,
YACC_C_EPILOGUE,
};
static struct yaccParserState {
bool not_in_grammar_rules;
enum yaccParsingArea area;
} parserState;
static tagRegexTable yaccTagRegexTable [] = {
{"^([A-Za-z][A-Za-z_0-9]+)[ \t]*:", "\\1",
"l,label,labels", NULL, &parserState.not_in_grammar_rules },
{"^([A-Za-z][A-Za-z_0-9]+)[ \t]*$", "\\1",
"l,label,labels", NULL, &parserState.not_in_grammar_rules },
};
static bool change_section (const char *line CTAGS_ATTR_UNUSED,
const regexMatch *matches CTAGS_ATTR_UNUSED,
unsigned int count CTAGS_ATTR_UNUSED,
void *data)
{
struct yaccParserState *state = data;
state->not_in_grammar_rules = !state->not_in_grammar_rules;
if (state->area == YACC_GRAMMAR)
state->area = YACC_C_EPILOGUE;
else
state->area = YACC_GRAMMAR;
return true;
}
static bool enter_c_prologue (const char *line CTAGS_ATTR_UNUSED,
const regexMatch *matches CTAGS_ATTR_UNUSED,
unsigned int count CTAGS_ATTR_UNUSED,
void *data)
{
struct yaccParserState *state = data;
state->area = YACC_C_PROLOGUE;
return true;
}
static bool leave_c_prologue (const char *line CTAGS_ATTR_UNUSED,
const regexMatch *matches CTAGS_ATTR_UNUSED,
unsigned int count CTAGS_ATTR_UNUSED,
void *data)
{
struct yaccParserState *state = data;
state->area = YACC_TOP_LEVEL;
return true;
}
static bool enter_union (const char *line CTAGS_ATTR_UNUSED,
const regexMatch *matches CTAGS_ATTR_UNUSED,
unsigned int count CTAGS_ATTR_UNUSED,
void *data)
{
struct yaccParserState *state = data;
if (state->area == YACC_TOP_LEVEL)
state->area = YACC_UNION;
return true;
}
static bool leave_union (const char *line CTAGS_ATTR_UNUSED,
const regexMatch *matches CTAGS_ATTR_UNUSED,
unsigned int count CTAGS_ATTR_UNUSED,
void *data)
{
struct yaccParserState *state = data;
if (state->area == YACC_UNION)
state->area = YACC_TOP_LEVEL;
return true;
}
static void make_promise_for_epilogue (void)
{
const unsigned char *tmp;
long endCharOffset;
unsigned long c_start;
unsigned long c_source_start;
unsigned long c_end;
/* We are at line with %% so the next line is the start of epilogue */
c_start = getInputLineNumber () + 1;
c_source_start = getSourceLineNumber() + 1;
/* Skip the lines for finding the EOF. */
endCharOffset = 0;
while ((tmp = readLineFromInputFile ()))
{
/* We want to get strlen() of the last line only but because
* readLineFromInputFile() invalidates the previous value, get
* endCharOffset here while the tmp variable is valid. */
endCharOffset = strlen ((const char *)tmp);
}
/* If `last' is too long, strlen returns a too large value
for the positive area of `endCharOffset'. */
if (endCharOffset < 0)
endCharOffset = 0;
c_end = getInputLineNumber ();
makePromise ("C", c_start, 0, c_end, endCharOffset, c_source_start);
}
static void initializeYaccParser (langType language)
{
/*
%{ ...
C language
%}
TOKE DEFINITIONS
%%
SYNTAX
%%
C language
*/
addLanguageCallbackRegex (language, "^%\\{", "{exclusive}", enter_c_prologue, NULL, &parserState);
addLanguageCallbackRegex (language, "^%\\}", "{exclusive}", leave_c_prologue, NULL, &parserState);
addLanguageCallbackRegex (language, "^%%", "{exclusive}", change_section, NULL, &parserState);
addLanguageCallbackRegex (language, "^%union", "{exclusive}", enter_union, NULL, &parserState);
addLanguageCallbackRegex (language, "^}", "{exclusive}", leave_union, NULL, &parserState);
}
static void runYaccParser (void)
{
enum yaccParsingArea last_area;
unsigned long c_input = 0;
unsigned long c_source = 0;
parserState.not_in_grammar_rules = true;
c_input = 0;
c_source = 0;
parserState.area = YACC_TOP_LEVEL;
last_area = parserState.area;
while (readLineFromInputFile () != NULL)
{
if (last_area == YACC_TOP_LEVEL &&
parserState.area == YACC_C_PROLOGUE)
{
if (readLineFromInputFile ())
{
c_input = getInputLineNumber ();
c_source = getSourceLineNumber ();
}
}
else if (last_area == YACC_C_PROLOGUE
&& parserState.area == YACC_TOP_LEVEL)
{
unsigned long c_end = getInputLineNumber ();
makePromise ("C", c_input, 0, c_end, 0, c_source);
c_input = 0;
c_source = 0;
}
else if (last_area == YACC_TOP_LEVEL
&& parserState.area == YACC_UNION)
{
c_input = getInputLineNumber ();
c_source = getInputLineNumber ();
}
else if (last_area == YACC_UNION
&& parserState.area == YACC_TOP_LEVEL)
{
unsigned long c_end = getInputLineNumber ();
makePromise ("C", c_input, strlen ("%"), c_end, strlen ("}"),
c_source);
c_input = 0;
c_source = 0;
}
else if (parserState.area == YACC_C_EPILOGUE)
{
make_promise_for_epilogue ();
}
last_area = parserState.area;
}
}
extern parserDefinition* YaccParser (void)
{
static const char *const extensions [] = { "y", NULL };
parserDefinition* const def = parserNew ("YACC");
def->extensions = extensions;
def->initialize = initializeYaccParser;
def->method = METHOD_REGEX;
def->parser = runYaccParser;
def->tagRegexTable = yaccTagRegexTable;
def->tagRegexCount = ARRAY_SIZE (yaccTagRegexTable);
return def;
}
|