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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
/*
* Copyright (c) 2011, Colomban Wendling <colomban@geany.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 M4.
*/
#include "general.h" /* must always come first */
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include "entry.h"
#include "htable.h"
#include "keyword.h"
#include "m4.h"
#include "parse.h"
#include "read.h"
#include "vstring.h"
enum M4Kind {
M4_MACRO_KIND,
M4_MACROFILE_KIND,
};
enum M4MacroRole {
M4_MACRO_ROLE_UNDEF,
};
enum M4MacrofileRole {
M4_MACROFILE_ROLE_INCLUDED,
M4_MACROFILE_ROLE_SILENTLY_INCLUDED,
};
static roleDefinition M4MacroRoles [] = {
{ true, "undef", "undefined" },
};
static roleDefinition M4MacrofileRoles [] = {
{ true, "included", "included macro" },
{ true, "sincluded", "silently included macro" },
};
static kindDefinition M4Kinds[] = {
{ true, 'd', "macro", "macros",
.referenceOnly = false, ATTACH_ROLES(M4MacroRoles) },
{ true, 'I', "macrofile", "macro files",
.referenceOnly = true, ATTACH_ROLES(M4MacrofileRoles) },
};
typedef enum {
KEYWORD_define,
KEYWORD_undefine,
KEYWORD_include,
KEYWORD_sinclude,
KEYWORD_changequote,
} m4KeywordId;
/* TODO: ideally "m4_" prefix keywords should be
installed and handled in Autoconf parser. */
static const keywordTable m4KeywordTable[] = {
#define ENTRY(K) \
{ #K, KEYWORD_##K }, \
{ "m4_" #K, KEYWORD_##K }
ENTRY(define),
ENTRY(undefine),
ENTRY(include),
ENTRY(sinclude),
ENTRY(changequote),
};
/* Quote handling */
/* TODO: Characters are assumed for quoting.
However, m4 allows strings. */
static char m4QuoteOpen = 0;
static char m4QuoteClose = 0;
extern void setM4Quotes(char openQuote, char closeQuote)
{
m4QuoteOpen = openQuote;
m4QuoteClose = closeQuote;
}
/* gets the close quote corresponding to openQuote.
* return 0 if openQuote is not a valid open quote */
static int getCloseQuote(int openQuote)
{
if (openQuote == m4QuoteOpen)
{
return m4QuoteClose;
}
return 0;
}
static void skipQuotes(int c)
{
unsigned int depth = 0;
int openQuote = 0, closeQuote = 0;
closeQuote = getCloseQuote(c);
if (! closeQuote)
return;
else
openQuote = c;
for (; c != EOF; c = getcFromInputFile())
{
if (c == closeQuote)
depth --;
else if (c == openQuote)
depth ++;
if (depth == 0)
break;
}
}
/* parser */
#define IS_WORD(c) (isalnum(c) || (c) == '_')
/* reads a possibly quoted word. word characters are those passing IS_WORD() */
static void readQuotedWord(vString *const name)
{
unsigned int depth = 0;
int openQuote = 0, closeQuote = 0;
int c = getcFromInputFile();
closeQuote = getCloseQuote(c);
if (closeQuote != 0)
{
openQuote = c;
depth ++;
c = getcFromInputFile();
}
for (; c != EOF; c = getcFromInputFile())
{
/* don't allow embedded NULs, and prevents to match when quote == 0 (aka none) */
if (c == 0)
break;
/* close before open to support open and close characters to be the same */
else if (c == closeQuote)
depth --;
else if (c == openQuote)
depth ++;
else if (IS_WORD(c) || depth > 0)
vStringPut(name, c);
else
{
ungetcToInputFile(c);
break;
}
}
}
static bool skipLineEnding(int c)
{
if (c == '\n')
return true;
else if (c == '\r')
{
/* try to eat the `\n' of a `\r\n' sequence */
c = getcFromInputFile();
if (c != '\n')
ungetcToInputFile(c);
return true;
}
return false;
}
static void skipToCharacter(int ch, bool oneLine)
{
int c;
while ((c = getcFromInputFile()) != EOF)
{
if (c == ch)
break;
else if (oneLine && skipLineEnding(c))
break;
}
}
static void skipLine(int c)
{
for (; c != EOF; c = getcFromInputFile())
{
if (skipLineEnding(c))
break;
}
}
static m4Subparser * maySwitchLanguage (const char* token)
{
subparser *tmp;
m4Subparser *m4found = NULL;
foreachSubparser (tmp, false)
{
m4Subparser *m4tmp = (m4Subparser *)tmp;
enterSubparser(tmp);
if (m4tmp->probeLanguage
&& m4tmp->probeLanguage (m4tmp, token))
{
chooseExclusiveSubparser (tmp, NULL);
m4found = m4tmp;
}
leaveSubparser();
if (m4found)
break;
}
return m4found;
}
/* reads everything in a macro argument
* return true if there are more args, false otherwise */
extern bool readM4MacroArgument(vString *const arg)
{
int c;
/* discard leading blanks */
while ((c = getcFromInputFile()) != EOF && isspace(c))
;
for (; c != EOF; c = getcFromInputFile())
{
if (c == ',' || c == ')')
{
ungetcToInputFile(c);
return c == ',';
}
else if (getCloseQuote(c) != 0)
{
ungetcToInputFile(c);
readQuotedWord(arg);
}
else
vStringPut(arg, c);
}
return false;
}
static void handleM4Changequote(void)
{
vString *const arg = vStringNew();
char args[2] = {0,0};
int i, n = (sizeof(args) / sizeof(args[0]));
bool more = true;
for (i = 0; more && i < n; i++)
{
const char *v;
vStringClear(arg);
more = readM4MacroArgument(arg);
if (more)
getcFromInputFile();
v = vStringValue(arg);
if (! v[0] || v[1])
break;
else
args[i] = *v;
}
if (! more)
{
if (args[0] && args[1])
setM4Quotes (args[0], args[1]);
else if (args[1])
setM4Quotes (args[0], '\'');
else if (args[0])
setM4Quotes ('\0', '\0');
else
setM4Quotes ('`', '\'');
}
vStringDelete(arg);
}
static bool doesQuoteStart (int c)
{
return (c == m4QuoteOpen);
}
static bool doesLineCommentStart (m4Subparser *m4, int c, char *token)
{
if (m4 && m4->doesLineCommentStart)
{
bool r;
enterSubparser ((subparser *)m4);
r = m4->doesLineCommentStart (m4, c, token);
leaveSubparser ();
if (r)
return true;
}
return (strcmp(token, "dnl") == 0);
}
static bool doesStringLiteralStart (m4Subparser *m4, int c)
{
if (m4 && m4->doesStringLiteralStart)
{
bool r;
enterSubparser ((subparser *)m4);
r = m4->doesStringLiteralStart (m4, c);
leaveSubparser ();
return r;
}
return false;
}
static int notifyNewMacro (m4Subparser *m4, const char *token)
{
int index;
enterSubparser ((subparser *)m4);
index = m4->newMacroNotify (m4, token);
leaveSubparser ();
return index;
}
/* tag creation */
static int makeM4RefTag(int kind, const vString *const name, int role)
{
tagEntryInfo e;
if (vStringLength(name) <= 0)
return CORK_NIL;
initRefTagEntry (&e, vStringValue(name), kind, role);
return makeTagEntry(&e);
}
static int makeM4Tag (int kind, int role)
{
int index = CORK_NIL;
vString *name = NULL;
if (kind == M4_MACRO_KIND)
{
if (role == ROLE_DEFINITION_INDEX)
{
name = vStringNew();
readM4MacroArgument(name);
index = makeM4RefTag (kind, name, role);
}
else if (role == M4_MACRO_ROLE_UNDEF)
{
name = vStringNew();
while (true)
{
bool more = readM4MacroArgument(name);
/* TODO: The cork indexes are thrown away here.
`end' field cannot be attached to multiple
indexes. */
makeM4RefTag (kind, name, role);
vStringClear (name);
if (more)
getcFromInputFile ();
else
break;
}
}
}
else if (kind == M4_MACROFILE_KIND)
{
name = vStringNew();
readM4MacroArgument(name);
index = makeM4RefTag (kind, name, role);
}
if (name)
vStringDelete (name);
return index;
}
struct newMacroResult
{
int index;
bool consumed;
};
static struct newMacroResult newMacroM4 (const char* token)
{
static langType lang = LANG_IGNORE;
struct newMacroResult result = {
.index = CORK_NIL,
.consumed = false,
};
int keyword;
int role = ROLE_DEFINITION_INDEX;
int kind = -1;
if (lang == LANG_IGNORE)
lang = getNamedLanguage ("M4", 0);
keyword = lookupKeyword (token, lang);
switch (keyword)
{
case KEYWORD_NONE:
break;
case KEYWORD_define:
kind = M4_MACRO_KIND;
role = ROLE_DEFINITION_INDEX;
result.consumed = true;
break;
case KEYWORD_undefine:
kind = M4_MACRO_KIND;
role = M4_MACRO_ROLE_UNDEF;
result.consumed = true;
break;
case KEYWORD_include:
kind = M4_MACROFILE_KIND;
role = M4_MACROFILE_ROLE_INCLUDED;
result.consumed = true;
break;
case KEYWORD_sinclude:
kind = M4_MACROFILE_KIND;
role = M4_MACROFILE_ROLE_SILENTLY_INCLUDED;
result.consumed = true;
break;
case KEYWORD_changequote:
handleM4Changequote ();
result.consumed = true;
break;
}
if (kind == -1)
return result;
if ((! isXtagEnabled (XTAG_REFERENCE_TAGS))
&& (role != ROLE_DEFINITION_INDEX))
return result;
result.index = makeM4Tag (kind, role);
return result;
}
/* parser instance */
static void findM4Tags(void)
{
m4Subparser *sub;
vString *const token = vStringNew();
int c;
int index = CORK_NIL;
setM4Quotes ('`', '\'');
sub = (m4Subparser *)getSubparserRunningBaseparser();
if (sub)
chooseExclusiveSubparser ((subparser *)sub, NULL);
while ((c = getcFromInputFile()) != EOF)
{
if (doesLineCommentStart (sub, c, vStringValue (token)))
skipLine(c);
else if (doesQuoteStart (c))
skipQuotes(c);
else if (doesStringLiteralStart (sub, c))
skipToCharacter(c, false);
else if (c == '(' && vStringLength(token) > 0) /* catch a few macro calls */
{
struct newMacroResult r;
if (!sub)
sub = maySwitchLanguage (vStringValue (token));
r = newMacroM4 (vStringValue (token));
if (r.consumed)
index = r.index;
else if (sub)
index = notifyNewMacro (sub, vStringValue (token));
}
vStringClear(token);
if (IS_WORD(c))
{
ungetcToInputFile(c);
readQuotedWord(token);
}
else if (c == ')')
{
tagEntryInfo *e = getEntryInCorkQueue (index);
if (e)
e->extensionFields.endLine = getInputLineNumber ();
index = CORK_NIL;
}
}
vStringDelete(token);
}
extern parserDefinition* M4Parser (void)
{
static const char *const extensions [] = { "m4",
"spt", /* used in `selinux-policy' */
NULL };
parserDefinition* const def = parserNew("M4");
def->kindTable = M4Kinds;
def->kindCount = ARRAY_SIZE(M4Kinds);
def->extensions = extensions;
def->parser = findM4Tags;
def->useCork = CORK_QUEUE;
def->keywordTable = m4KeywordTable;
def->keywordCount = ARRAY_SIZE (m4KeywordTable);
return def;
}
|