| 1 |
/* |
| 2 |
* Copyright (c) 2009, Vincent Berthoux |
| 3 |
* |
| 4 |
* This source code is released for free distribution under the terms of the |
| 5 |
* GNU General Public License. |
| 6 |
* |
| 7 |
* This module contains functions for generating tags for Objective Caml |
| 8 |
* language files. |
| 9 |
*/ |
| 10 |
/* |
| 11 |
* INCLUDE FILES |
| 12 |
*/ |
| 13 |
#include "general.h" /* must always come first */ |
| 14 |
|
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
#include "keyword.h" |
| 18 |
#include "entry.h" |
| 19 |
#include "options.h" |
| 20 |
#include "read.h" |
| 21 |
#include "routines.h" |
| 22 |
#include "vstring.h" |
| 23 |
|
| 24 |
/* To get rid of unused parameter warning in |
| 25 |
* -Wextra */ |
| 26 |
#ifdef UNUSED |
| 27 |
#elif defined(__GNUC__) |
| 28 |
# define UNUSED(x) UNUSED_ ## x __attribute__((unused)) |
| 29 |
#elif defined(__LCLINT__) |
| 30 |
# define UNUSED(x) /*@unused@*/ x |
| 31 |
#else |
| 32 |
# define UNUSED(x) x |
| 33 |
#endif |
| 34 |
#define OCAML_MAX_STACK_SIZE 256 |
| 35 |
|
| 36 |
typedef enum { |
| 37 |
K_CLASS, /* Ocaml class, relatively rare */ |
| 38 |
K_METHOD, /* class method */ |
| 39 |
K_MODULE, /* Ocaml module OR functor */ |
| 40 |
K_VAR, |
| 41 |
K_TYPE, /* name of an OCaml type */ |
| 42 |
K_FUNCTION, |
| 43 |
K_CONSTRUCTOR, /* Constructor of a sum type */ |
| 44 |
K_RECORDFIELD, |
| 45 |
K_EXCEPTION |
| 46 |
} ocamlKind; |
| 47 |
|
| 48 |
static kindOption OcamlKinds[] = { |
| 49 |
{TRUE, 'c', "class", "classes"}, |
| 50 |
{TRUE, 'm', "method", "Object's method"}, |
| 51 |
{TRUE, 'M', "module", "Module or functor"}, |
| 52 |
{TRUE, 'v', "var", "Global variable"}, |
| 53 |
{TRUE, 't', "type", "Type name"}, |
| 54 |
{TRUE, 'f', "function", "A function"}, |
| 55 |
{TRUE, 'C', "Constructor", "A constructor"}, |
| 56 |
{TRUE, 'r', "Record field", "A 'structure' field"}, |
| 57 |
{TRUE, 'e', "Exception", "An exception"} |
| 58 |
}; |
| 59 |
|
| 60 |
typedef enum { |
| 61 |
OcaKEYWORD_and, |
| 62 |
OcaKEYWORD_begin, |
| 63 |
OcaKEYWORD_class, |
| 64 |
OcaKEYWORD_do, |
| 65 |
OcaKEYWORD_done, |
| 66 |
OcaKEYWORD_else, |
| 67 |
OcaKEYWORD_end, |
| 68 |
OcaKEYWORD_exception, |
| 69 |
OcaKEYWORD_for, |
| 70 |
OcaKEYWORD_functor, |
| 71 |
OcaKEYWORD_fun, |
| 72 |
OcaKEYWORD_if, |
| 73 |
OcaKEYWORD_in, |
| 74 |
OcaKEYWORD_let, |
| 75 |
OcaKEYWORD_value, |
| 76 |
OcaKEYWORD_match, |
| 77 |
OcaKEYWORD_method, |
| 78 |
OcaKEYWORD_module, |
| 79 |
OcaKEYWORD_mutable, |
| 80 |
OcaKEYWORD_object, |
| 81 |
OcaKEYWORD_of, |
| 82 |
OcaKEYWORD_rec, |
| 83 |
OcaKEYWORD_sig, |
| 84 |
OcaKEYWORD_struct, |
| 85 |
OcaKEYWORD_then, |
| 86 |
OcaKEYWORD_try, |
| 87 |
OcaKEYWORD_type, |
| 88 |
OcaKEYWORD_val, |
| 89 |
OcaKEYWORD_virtual, |
| 90 |
OcaKEYWORD_while, |
| 91 |
OcaKEYWORD_with, |
| 92 |
|
| 93 |
OcaIDENTIFIER, |
| 94 |
Tok_PARL, /* '(' */ |
| 95 |
Tok_PARR, /* ')' */ |
| 96 |
Tok_BRL, /* '[' */ |
| 97 |
Tok_BRR, /* ']' */ |
| 98 |
Tok_CurlL, /* '{' */ |
| 99 |
Tok_CurlR, /* '}' */ |
| 100 |
Tok_Prime, /* '\'' */ |
| 101 |
Tok_Pipe, /* '|' */ |
| 102 |
Tok_EQ, /* '=' */ |
| 103 |
Tok_Val, /* string/number/poo */ |
| 104 |
Tok_Op, /* any operator recognized by the language */ |
| 105 |
Tok_semi, /* ';' */ |
| 106 |
Tok_comma, /* ',' */ |
| 107 |
Tok_To, /* '->' */ |
| 108 |
Tok_Sharp, /* '#' */ |
| 109 |
Tok_Backslash, /* '\\' */ |
| 110 |
|
| 111 |
Tok_EOF /* END of file */ |
| 112 |
} ocamlKeyword; |
| 113 |
|
| 114 |
typedef struct sOcaKeywordDesc { |
| 115 |
const char *name; |
| 116 |
ocamlKeyword id; |
| 117 |
} ocaKeywordDesc; |
| 118 |
|
| 119 |
typedef ocamlKeyword ocaToken; |
| 120 |
|
| 121 |
static const ocaKeywordDesc OcamlKeywordTable[] = { |
| 122 |
{ "and" , OcaKEYWORD_and }, |
| 123 |
{ "begin" , OcaKEYWORD_begin }, |
| 124 |
{ "class" , OcaKEYWORD_class }, |
| 125 |
{ "do" , OcaKEYWORD_do }, |
| 126 |
{ "done" , OcaKEYWORD_done }, |
| 127 |
{ "else" , OcaKEYWORD_else }, |
| 128 |
{ "end" , OcaKEYWORD_end }, |
| 129 |
{ "exception" , OcaKEYWORD_exception }, |
| 130 |
{ "for" , OcaKEYWORD_for }, |
| 131 |
{ "fun" , OcaKEYWORD_fun }, |
| 132 |
{ "function" , OcaKEYWORD_fun }, |
| 133 |
{ "functor" , OcaKEYWORD_functor }, |
| 134 |
{ "in" , OcaKEYWORD_in }, |
| 135 |
{ "let" , OcaKEYWORD_let }, |
| 136 |
{ "match" , OcaKEYWORD_match }, |
| 137 |
{ "method" , OcaKEYWORD_method }, |
| 138 |
{ "module" , OcaKEYWORD_module }, |
| 139 |
{ "mutable" , OcaKEYWORD_mutable }, |
| 140 |
{ "object" , OcaKEYWORD_object }, |
| 141 |
{ "of" , OcaKEYWORD_of }, |
| 142 |
{ "rec" , OcaKEYWORD_rec }, |
| 143 |
{ "sig" , OcaKEYWORD_sig }, |
| 144 |
{ "struct" , OcaKEYWORD_struct }, |
| 145 |
{ "then" , OcaKEYWORD_then }, |
| 146 |
{ "try" , OcaKEYWORD_try }, |
| 147 |
{ "type" , OcaKEYWORD_type }, |
| 148 |
{ "val" , OcaKEYWORD_val }, |
| 149 |
{ "value" , OcaKEYWORD_value }, /* just to handle revised syntax */ |
| 150 |
{ "virtual" , OcaKEYWORD_virtual }, |
| 151 |
{ "while" , OcaKEYWORD_while }, |
| 152 |
{ "with" , OcaKEYWORD_with }, |
| 153 |
|
| 154 |
{ "or" , Tok_Op }, |
| 155 |
{ "mod " , Tok_Op }, |
| 156 |
{ "land " , Tok_Op }, |
| 157 |
{ "lor " , Tok_Op }, |
| 158 |
{ "lxor " , Tok_Op }, |
| 159 |
{ "lsl " , Tok_Op }, |
| 160 |
{ "lsr " , Tok_Op }, |
| 161 |
{ "asr" , Tok_Op }, |
| 162 |
{ "->" , Tok_To }, |
| 163 |
{ "true" , Tok_Val }, |
| 164 |
{ "false" , Tok_Val } |
| 165 |
}; |
| 166 |
|
| 167 |
static langType Lang_Ocaml; |
| 168 |
|
| 169 |
boolean exportLocalInfo = FALSE; |
| 170 |
|
| 171 |
/*////////////////////////////////////////////////////////////////// |
| 172 |
//// lexingInit */ |
| 173 |
typedef struct _lexingState { |
| 174 |
vString *name; /* current parsed identifier/operator */ |
| 175 |
const unsigned char *cp; /* position in stream */ |
| 176 |
} lexingState; |
| 177 |
|
| 178 |
/* array of the size of all possible value for a char */ |
| 179 |
boolean isOperator[1 << (8 * sizeof (char))] = { FALSE }; |
| 180 |
|
| 181 |
static void initKeywordHash ( void ) |
| 182 |
{ |
| 183 |
const size_t count = sizeof (OcamlKeywordTable) / sizeof (ocaKeywordDesc); |
| 184 |
size_t i; |
| 185 |
|
| 186 |
for (i = 0; i < count; ++i) |
| 187 |
{ |
| 188 |
addKeyword (OcamlKeywordTable[i].name, Lang_Ocaml, |
| 189 |
(int) OcamlKeywordTable[i].id); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/* definition of all the operator in OCaml, |
| 194 |
* /!\ certain operator get special treatment |
| 195 |
* in regards of their role in OCaml grammar : |
| 196 |
* '|' ':' '=' '~' and '?' */ |
| 197 |
static void initOperatorTable ( void ) |
| 198 |
{ |
| 199 |
isOperator['!'] = TRUE; |
| 200 |
isOperator['$'] = TRUE; |
| 201 |
isOperator['%'] = TRUE; |
| 202 |
isOperator['&'] = TRUE; |
| 203 |
isOperator['*'] = TRUE; |
| 204 |
isOperator['+'] = TRUE; |
| 205 |
isOperator['-'] = TRUE; |
| 206 |
isOperator['.'] = TRUE; |
| 207 |
isOperator['/'] = TRUE; |
| 208 |
isOperator[':'] = TRUE; |
| 209 |
isOperator['<'] = TRUE; |
| 210 |
isOperator['='] = TRUE; |
| 211 |
isOperator['>'] = TRUE; |
| 212 |
isOperator['?'] = TRUE; |
| 213 |
isOperator['@'] = TRUE; |
| 214 |
isOperator['^'] = TRUE; |
| 215 |
isOperator['~'] = TRUE; |
| 216 |
isOperator['|'] = TRUE; |
| 217 |
} |
| 218 |
|
| 219 |
/*////////////////////////////////////////////////////////////////////// |
| 220 |
//// Lexing */ |
| 221 |
static boolean isNum (char c) |
| 222 |
{ |
| 223 |
return c >= '0' && c <= '9'; |
| 224 |
} |
| 225 |
static boolean isLowerAlpha (char c) |
| 226 |
{ |
| 227 |
return c >= 'a' && c <= 'z'; |
| 228 |
} |
| 229 |
|
| 230 |
static boolean isUpperAlpha (char c) |
| 231 |
{ |
| 232 |
return c >= 'A' && c <= 'Z'; |
| 233 |
} |
| 234 |
|
| 235 |
static boolean isAlpha (char c) |
| 236 |
{ |
| 237 |
return isLowerAlpha (c) || isUpperAlpha (c); |
| 238 |
} |
| 239 |
|
| 240 |
static boolean isIdent (char c) |
| 241 |
{ |
| 242 |
return isNum (c) || isAlpha (c) || c == '_' || c == '\''; |
| 243 |
} |
| 244 |
|
| 245 |
static boolean isSpace (char c) |
| 246 |
{ |
| 247 |
return c == ' ' || c == '\t' || c == '\r' || c == '\n'; |
| 248 |
} |
| 249 |
|
| 250 |
static void eatWhiteSpace (lexingState * st) |
| 251 |
{ |
| 252 |
const unsigned char *cp = st->cp; |
| 253 |
while (isSpace (*cp)) |
| 254 |
cp++; |
| 255 |
|
| 256 |
st->cp = cp; |
| 257 |
} |
| 258 |
|
| 259 |
static void eatString (lexingState * st) |
| 260 |
{ |
| 261 |
boolean lastIsBackSlash = FALSE; |
| 262 |
boolean unfinished = TRUE; |
| 263 |
const unsigned char *c = st->cp + 1; |
| 264 |
|
| 265 |
while (unfinished) |
| 266 |
{ |
| 267 |
/* end of line should never happen. |
| 268 |
* we tolerate it */ |
| 269 |
if (c == NULL || c[0] == '\0') |
| 270 |
break; |
| 271 |
else if (*c == '"' && !lastIsBackSlash) |
| 272 |
unfinished = FALSE; |
| 273 |
else |
| 274 |
lastIsBackSlash = *c == '\\'; |
| 275 |
|
| 276 |
c++; |
| 277 |
} |
| 278 |
|
| 279 |
st->cp = c; |
| 280 |
} |
| 281 |
|
| 282 |
static void eatComment (lexingState * st) |
| 283 |
{ |
| 284 |
boolean unfinished = TRUE; |
| 285 |
boolean lastIsStar = FALSE; |
| 286 |
const unsigned char *c = st->cp + 2; |
| 287 |
|
| 288 |
while (unfinished) |
| 289 |
{ |
| 290 |
/* we've reached the end of the line.. |
| 291 |
* so we have to reload a line... */ |
| 292 |
if (c == NULL || *c == '\0') |
| 293 |
{ |
| 294 |
st->cp = fileReadLine (); |
| 295 |
/* WOOPS... no more input... |
| 296 |
* we return, next lexing read |
| 297 |
* will be null and ok */ |
| 298 |
if (st->cp == NULL) |
| 299 |
return; |
| 300 |
c = st->cp; |
| 301 |
} |
| 302 |
/* we've reached the end of the comment */ |
| 303 |
else if (*c == ')' && lastIsStar) |
| 304 |
unfinished = FALSE; |
| 305 |
/* here we deal with imbricated comment, which |
| 306 |
* are allowed in OCaml */ |
| 307 |
else if (c[0] == '(' && c[1] == '*') |
| 308 |
{ |
| 309 |
st->cp = c; |
| 310 |
eatComment (st); |
| 311 |
|
| 312 |
c = st->cp; |
| 313 |
if (c == NULL) |
| 314 |
return; |
| 315 |
|
| 316 |
lastIsStar = FALSE; |
| 317 |
c++; |
| 318 |
} |
| 319 |
/* OCaml has a rule which says : |
| 320 |
* |
| 321 |
* "Comments do not occur inside string or character literals. |
| 322 |
* Nested comments are handled correctly." |
| 323 |
* |
| 324 |
* So if we encounter a string beginning, we must parse it to |
| 325 |
* get a good comment nesting (bug ID: 3117537) |
| 326 |
*/ |
| 327 |
else if (*c == '"') |
| 328 |
{ |
| 329 |
st->cp = c; |
| 330 |
eatString (st); |
| 331 |
c = st->cp; |
| 332 |
} |
| 333 |
else |
| 334 |
{ |
| 335 |
lastIsStar = '*' == *c; |
| 336 |
c++; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
st->cp = c; |
| 341 |
} |
| 342 |
|
| 343 |
static void readIdentifier (lexingState * st) |
| 344 |
{ |
| 345 |
const unsigned char *p; |
| 346 |
vStringClear (st->name); |
| 347 |
|
| 348 |
/* first char is a simple letter */ |
| 349 |
if (isAlpha (*st->cp) || *st->cp == '_') |
| 350 |
vStringPut (st->name, (int) *st->cp); |
| 351 |
|
| 352 |
/* Go till you get identifier chars */ |
| 353 |
for (p = st->cp + 1; isIdent (*p); p++) |
| 354 |
vStringPut (st->name, (int) *p); |
| 355 |
|
| 356 |
st->cp = p; |
| 357 |
|
| 358 |
vStringTerminate (st->name); |
| 359 |
} |
| 360 |
|
| 361 |
static ocamlKeyword eatNumber (lexingState * st) |
| 362 |
{ |
| 363 |
while (isNum (*st->cp)) |
| 364 |
st->cp++; |
| 365 |
return Tok_Val; |
| 366 |
} |
| 367 |
|
| 368 |
/* Operator can be defined in OCaml as a function |
| 369 |
* so we must be ample enough to parse them normally */ |
| 370 |
static ocamlKeyword eatOperator (lexingState * st) |
| 371 |
{ |
| 372 |
int count = 0; |
| 373 |
const unsigned char *root = st->cp; |
| 374 |
|
| 375 |
vStringClear (st->name); |
| 376 |
|
| 377 |
while (isOperator[st->cp[count]]) |
| 378 |
{ |
| 379 |
vStringPut (st->name, st->cp[count]); |
| 380 |
count++; |
| 381 |
} |
| 382 |
|
| 383 |
vStringTerminate (st->name); |
| 384 |
|
| 385 |
st->cp += count; |
| 386 |
if (count <= 1) |
| 387 |
{ |
| 388 |
switch (root[0]) |
| 389 |
{ |
| 390 |
case '|': |
| 391 |
return Tok_Pipe; |
| 392 |
case '=': |
| 393 |
return Tok_EQ; |
| 394 |
default: |
| 395 |
return Tok_Op; |
| 396 |
} |
| 397 |
} |
| 398 |
else if (count == 2 && root[0] == '-' && root[1] == '>') |
| 399 |
return Tok_To; |
| 400 |
else |
| 401 |
return Tok_Op; |
| 402 |
} |
| 403 |
|
| 404 |
/* The lexer is in charge of reading the file. |
| 405 |
* Some of sub-lexer (like eatComment) also read file. |
| 406 |
* lexing is finished when the lexer return Tok_EOF */ |
| 407 |
static ocamlKeyword lex (lexingState * st) |
| 408 |
{ |
| 409 |
int retType; |
| 410 |
/* handling data input here */ |
| 411 |
while (st->cp == NULL || st->cp[0] == '\0') |
| 412 |
{ |
| 413 |
st->cp = fileReadLine (); |
| 414 |
if (st->cp == NULL) |
| 415 |
return Tok_EOF; |
| 416 |
} |
| 417 |
|
| 418 |
if (isAlpha (*st->cp)) |
| 419 |
{ |
| 420 |
readIdentifier (st); |
| 421 |
retType = lookupKeyword (vStringValue (st->name), Lang_Ocaml); |
| 422 |
|
| 423 |
if (retType == -1) /* If it's not a keyword */ |
| 424 |
{ |
| 425 |
return OcaIDENTIFIER; |
| 426 |
} |
| 427 |
else |
| 428 |
{ |
| 429 |
return retType; |
| 430 |
} |
| 431 |
} |
| 432 |
else if (isNum (*st->cp)) |
| 433 |
return eatNumber (st); |
| 434 |
else if (isSpace (*st->cp)) |
| 435 |
{ |
| 436 |
eatWhiteSpace (st); |
| 437 |
return lex (st); |
| 438 |
} |
| 439 |
/* OCaml permit the definition of our own operators |
| 440 |
* so here we check all the consecuting chars which |
| 441 |
* are operators to discard them. */ |
| 442 |
else if (isOperator[*st->cp]) |
| 443 |
return eatOperator (st); |
| 444 |
else |
| 445 |
switch (*st->cp) |
| 446 |
{ |
| 447 |
case '(': |
| 448 |
if (st->cp[1] == '*') /* ergl, a comment */ |
| 449 |
{ |
| 450 |
eatComment (st); |
| 451 |
return lex (st); |
| 452 |
} |
| 453 |
else |
| 454 |
{ |
| 455 |
st->cp++; |
| 456 |
return Tok_PARL; |
| 457 |
} |
| 458 |
|
| 459 |
case ')': |
| 460 |
st->cp++; |
| 461 |
return Tok_PARR; |
| 462 |
case '[': |
| 463 |
st->cp++; |
| 464 |
return Tok_BRL; |
| 465 |
case ']': |
| 466 |
st->cp++; |
| 467 |
return Tok_BRR; |
| 468 |
case '{': |
| 469 |
st->cp++; |
| 470 |
return Tok_CurlL; |
| 471 |
case '}': |
| 472 |
st->cp++; |
| 473 |
return Tok_CurlR; |
| 474 |
case '\'': |
| 475 |
st->cp++; |
| 476 |
return Tok_Prime; |
| 477 |
case ',': |
| 478 |
st->cp++; |
| 479 |
return Tok_comma; |
| 480 |
case '=': |
| 481 |
st->cp++; |
| 482 |
return Tok_EQ; |
| 483 |
case ';': |
| 484 |
st->cp++; |
| 485 |
return Tok_semi; |
| 486 |
case '"': |
| 487 |
eatString (st); |
| 488 |
return Tok_Val; |
| 489 |
case '_': |
| 490 |
st->cp++; |
| 491 |
return Tok_Val; |
| 492 |
case '#': |
| 493 |
st->cp++; |
| 494 |
return Tok_Sharp; |
| 495 |
case '\\': |
| 496 |
st->cp++; |
| 497 |
return Tok_Backslash; |
| 498 |
|
| 499 |
default: |
| 500 |
st->cp++; |
| 501 |
break; |
| 502 |
} |
| 503 |
|
| 504 |
/* default return if nothing is recognized, |
| 505 |
* shouldn't happen, but at least, it will |
| 506 |
* be handled without destroying the parsing. */ |
| 507 |
return Tok_Val; |
| 508 |
} |
| 509 |
|
| 510 |
/*////////////////////////////////////////////////////////////////////// |
| 511 |
//// Parsing */ |
| 512 |
typedef void (*parseNext) (vString * const ident, ocaToken what); |
| 513 |
|
| 514 |
/********** Helpers */ |
| 515 |
/* This variable hold the 'parser' which is going to |
| 516 |
* handle the next token */ |
| •517 |
static parseNext toDoNext; |
| 518 |
|
| 519 |
/* Special variable used by parser eater to |
| 520 |
* determine which action to put after their |
| 521 |
* job is finished. */ |
| •522 |
static parseNext comeAfter; |
| 523 |
|
| 524 |
/* If a token put an end to current delcaration/ |
| 525 |
* statement */ |
| •526 |
static ocaToken terminatingToken; |
| 527 |
|
| 528 |
/* Token to be searched by the different |
| 529 |
* parser eater. */ |
| •530 |
static ocaToken waitedToken; |
| 531 |
|
| 532 |
/* name of the last class, used for |
| 533 |
* context stacking. */ |
| •534 |
static vString *lastClass; |
| 535 |
|
| •536 |
static vString *voidName; |
| 537 |
|
| 538 |
typedef enum _sContextKind { |
| 539 |
ContextStrong, |
| 540 |
ContextSoft |
| 541 |
} contextKind; |
| 542 |
|
| 543 |
typedef enum _sContextType { |
| 544 |
ContextType, |
| 545 |
ContextModule, |
| 546 |
ContextClass, |
| 547 |
ContextValue, |
| 548 |
ContextFunction, |
| 549 |
ContextMethod, |
| 550 |
ContextBlock |
| 551 |
} contextType; |
| 552 |
|
| 553 |
typedef struct _sOcamlContext { |
| 554 |
contextKind kind; /* well if the context is strong or not */ |
| 555 |
contextType type; |
| 556 |
parseNext callback; /* what to do when a context is pop'd */ |
| 557 |
vString *contextName; /* name, if any, of the surrounding context */ |
| 558 |
} ocamlContext; |
| 559 |
|
| 560 |
/* context stack, can be used to output scope information |
| 561 |
* into the tag file. */ |
| •562 |
static ocamlContext stack[OCAML_MAX_STACK_SIZE]; |
| 563 |
/* current position in the tag */ |
| •564 |
static int stackIndex; |
| 565 |
|
| 566 |
/* special function, often recalled, so putting it here */ |
| 567 |
static void globalScope (vString * const ident, ocaToken what); |
| 568 |
|
| 569 |
/* Return : index of the last named context if one |
| 570 |
* is found, -1 otherwise */ |
| 571 |
static int getLastNamedIndex ( void ) |
| 572 |
{ |
| 573 |
int i; |
| 574 |
|
| 575 |
for (i = stackIndex - 1; i >= 0; --i) |
| 576 |
{ |
| 577 |
if (vStringLength (stack[i].contextName) > 0) |
| 578 |
{ |
| 579 |
return i; |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
return -1; |
| 584 |
} |
| 585 |
|
| 586 |
static const char *contextDescription (contextType t) |
| 587 |
{ |
| 588 |
switch (t) |
| 589 |
{ |
| 590 |
case ContextFunction: |
| 591 |
return "function"; |
| 592 |
case ContextMethod: |
| 593 |
return "method"; |
| 594 |
case ContextValue: |
| 595 |
return "value"; |
| 596 |
case ContextModule: |
| 597 |
return "Module"; |
| 598 |
case ContextType: |
| 599 |
return "type"; |
| 600 |
case ContextClass: |
| 601 |
return "class"; |
| 602 |
case ContextBlock: |
| 603 |
return "begin/end"; |
| 604 |
} |
| 605 |
|
| 606 |
return NULL; |
| 607 |
} |
| 608 |
|
| 609 |
static char contextTypeSuffix (contextType t) |
| 610 |
{ |
| 611 |
switch (t) |
| 612 |
{ |
| 613 |
case ContextFunction: |
| 614 |
case ContextMethod: |
| 615 |
case ContextValue: |
| 616 |
case ContextModule: |
| 617 |
return '/'; |
| 618 |
case ContextType: |
| 619 |
return '.'; |
| 620 |
case ContextClass: |
| 621 |
return '#'; |
| 622 |
case ContextBlock: |
| 623 |
return ' '; |
| 624 |
} |
| 625 |
|
| 626 |
return '$'; |
| 627 |
} |
| 628 |
|
| 629 |
/* Push a new context, handle null string */ |
| 630 |
static void pushContext (contextKind kind, contextType type, parseNext after, |
| 631 |
vString const *contextName) |
| 632 |
{ |
| 633 |
int parentIndex; |
| 634 |
|
| 635 |
if (stackIndex >= OCAML_MAX_STACK_SIZE) |
| 636 |
{ |
| 637 |
verbose ("OCaml Maximum depth reached"); |
| 638 |
return; |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
stack[stackIndex].kind = kind; |
| 643 |
stack[stackIndex].type = type; |
| 644 |
stack[stackIndex].callback = after; |
| 645 |
|
| 646 |
parentIndex = getLastNamedIndex (); |
| 647 |
if (contextName == NULL) |
| 648 |
{ |
| 649 |
vStringClear (stack[stackIndex++].contextName); |
| 650 |
return; |
| 651 |
} |
| 652 |
|
| 653 |
if (parentIndex >= 0) |
| 654 |
{ |
| 655 |
vStringCopy (stack[stackIndex].contextName, |
| 656 |
stack[parentIndex].contextName); |
| 657 |
vStringPut (stack[stackIndex].contextName, |
| 658 |
contextTypeSuffix (stack[parentIndex].type)); |
| 659 |
|
| 660 |
vStringCat (stack[stackIndex].contextName, contextName); |
| 661 |
} |
| 662 |
else |
| 663 |
vStringCopy (stack[stackIndex].contextName, contextName); |
| 664 |
|
| 665 |
stackIndex++; |
| 666 |
} |
| 667 |
|
| 668 |
static void pushStrongContext (vString * name, contextType type) |
| 669 |
{ |
| 670 |
pushContext (ContextStrong, type, &globalScope, name); |
| 671 |
} |
| 672 |
|
| 673 |
static void pushSoftContext (parseNext continuation, |
| 674 |
vString * name, contextType type) |
| 675 |
{ |
| 676 |
pushContext (ContextSoft, type, continuation, name); |
| 677 |
} |
| 678 |
|
| 679 |
static void pushEmptyContext (parseNext continuation) |
| 680 |
{ |
| 681 |
pushContext (ContextSoft, ContextValue, continuation, NULL); |
| 682 |
} |
| 683 |
|
| 684 |
/* unroll the stack until the last named context. |
| 685 |
* then discard it. Used to handle the : |
| 686 |
* let f x y = ... |
| 687 |
* in ... |
| 688 |
* where the context is reseted after the in. Context may have |
| 689 |
* been really nested before that. */ |
| 690 |
static void popLastNamed ( void ) |
| 691 |
{ |
| 692 |
int i = getLastNamedIndex (); |
| 693 |
|
| 694 |
if (i >= 0) |
| 695 |
{ |
| 696 |
stackIndex = i; |
| 697 |
toDoNext = stack[i].callback; |
| 698 |
vStringClear (stack[i].contextName); |
| 699 |
} |
| 700 |
else |
| 701 |
{ |
| 702 |
/* ok, no named context found... |
| 703 |
* (should not happen). */ |
| 704 |
stackIndex = 0; |
| 705 |
toDoNext = &globalScope; |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
/* pop a context without regarding it's content |
| 710 |
* (beside handling empty stack case) */ |
| 711 |
static void popSoftContext ( void ) |
| 712 |
{ |
| 713 |
if (stackIndex <= 0) |
| 714 |
{ |
| 715 |
toDoNext = &globalScope; |
| 716 |
} |
| 717 |
else |
| 718 |
{ |
| 719 |
stackIndex--; |
| 720 |
toDoNext = stack[stackIndex].callback; |
| 721 |
vStringClear (stack[stackIndex].contextName); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
/* Reset everything until the last global space. |
| 726 |
* a strong context can be : |
| 727 |
* - module |
| 728 |
* - class definition |
| 729 |
* - the initial global space |
| 730 |
* - a _global_ delcaration (let at global scope or in a module). |
| 731 |
* Created to exit quickly deeply nested context */ |
| 732 |
static contextType popStrongContext ( void ) |
| 733 |
{ |
| 734 |
int i; |
| 735 |
|
| 736 |
for (i = stackIndex - 1; i >= 0; --i) |
| 737 |
{ |
| 738 |
if (stack[i].kind == ContextStrong) |
| 739 |
{ |
| 740 |
stackIndex = i; |
| 741 |
toDoNext = stack[i].callback; |
| 742 |
vStringClear (stack[i].contextName); |
| 743 |
return stack[i].type; |
| 744 |
} |
| 745 |
} |
| 746 |
/* ok, no strong context found... */ |
| 747 |
stackIndex = 0; |
| 748 |
toDoNext = &globalScope; |
| 749 |
return -1; |
| 750 |
} |
| 751 |
|
| 752 |
/* Ignore everything till waitedToken and jump to comeAfter. |
| 753 |
* If the "end" keyword is encountered break, doesn't remember |
| 754 |
* why though. */ |
| 755 |
static void tillToken (vString * const UNUSED (ident), ocaToken what) |
| 756 |
{ |
| 757 |
if (what == waitedToken) |
| 758 |
toDoNext = comeAfter; |
| 759 |
else if (what == OcaKEYWORD_end) |
| 760 |
{ |
| 761 |
popStrongContext (); |
| 762 |
toDoNext = &globalScope; |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
/* Ignore everything till a waitedToken is seen, but |
| 767 |
* take care of balanced parentheses/bracket use */ |
| 768 |
static void contextualTillToken (vString * const UNUSED (ident), ocaToken what) |
| 769 |
{ |
| 770 |
static int parentheses = 0; |
| 771 |
static int bracket = 0; |
| 772 |
static int curly = 0; |
| 773 |
|
| 774 |
switch (what) |
| 775 |
{ |
| 776 |
case Tok_PARL: |
| 777 |
parentheses--; |
| 778 |
break; |
| 779 |
case Tok_PARR: |
| 780 |
parentheses++; |
| 781 |
break; |
| 782 |
case Tok_CurlL: |
| 783 |
curly--; |
| 784 |
break; |
| 785 |
case Tok_CurlR: |
| 786 |
curly++; |
| 787 |
break; |
| 788 |
case Tok_BRL: |
| 789 |
bracket--; |
| 790 |
break; |
| 791 |
case Tok_BRR: |
| 792 |
bracket++; |
| 793 |
break; |
| 794 |
|
| 795 |
default: /* other token are ignored */ |
| 796 |
break; |
| 797 |
} |
| 798 |
|
| 799 |
if (what == waitedToken && parentheses == 0 && bracket == 0 && curly == 0) |
| 800 |
toDoNext = comeAfter; |
| 801 |
|
| 802 |
else if (what == OcaKEYWORD_end) |
| 803 |
{ |
| 804 |
popStrongContext (); |
| 805 |
toDoNext = &globalScope; |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
/* Wait for waitedToken and jump to comeAfter or let |
| 810 |
* the globalScope handle declarations */ |
| 811 |
static void tillTokenOrFallback (vString * const ident, ocaToken what) |
| 812 |
{ |
| 813 |
if (what == waitedToken) |
| 814 |
toDoNext = comeAfter; |
| 815 |
else |
| 816 |
globalScope (ident, what); |
| 817 |
} |
| 818 |
|
| 819 |
/* ignore token till waitedToken, or give up if find |
| 820 |
* terminatingToken. Use globalScope to handle new |
| 821 |
* declarations. */ |
| 822 |
static void tillTokenOrTerminatingOrFallback (vString * const ident, |
| 823 |
ocaToken what) |
| 824 |
{ |
| 825 |
if (what == waitedToken) |
| 826 |
toDoNext = comeAfter; |
| 827 |
else if (what == terminatingToken) |
| 828 |
toDoNext = globalScope; |
| 829 |
else |
| 830 |
globalScope (ident, what); |
| 831 |
} |
| 832 |
|
| 833 |
/* ignore the next token in the stream and jump to the |
| 834 |
* given comeAfter state */ |
| 835 |
static void ignoreToken (vString * const UNUSED (ident), ocaToken UNUSED (what)) |
| 836 |
{ |
| 837 |
toDoNext = comeAfter; |
| 838 |
} |
| 839 |
|
| 840 |
/********** Grammar */ |
| 841 |
/* the purpose of each function is detailled near their |
| 842 |
* implementation */ |
| 843 |
|
| 844 |
static void killCurrentState ( void ) |
| 845 |
{ |
| 846 |
|
| 847 |
/* Tracking the kind of previous strong |
| 848 |
* context, if it doesn't match with a |
| 849 |
* really strong entity, repop */ |
| 850 |
switch (popStrongContext ()) |
| 851 |
{ |
| 852 |
|
| 853 |
case ContextValue: |
| 854 |
popStrongContext (); |
| 855 |
break; |
| 856 |
case ContextFunction: |
| 857 |
popStrongContext (); |
| 858 |
break; |
| 859 |
case ContextMethod: |
| 860 |
popStrongContext (); |
| 861 |
break; |
| 862 |
|
| 863 |
case ContextType: |
| 864 |
popStrongContext(); |
| 865 |
break; |
| 866 |
case ContextBlock: |
| 867 |
break; |
| 868 |
case ContextModule: |
| 869 |
break; |
| 870 |
case ContextClass: |
| 871 |
break; |
| 872 |
default: |
| 873 |
/* nothing more */ |
| 874 |
break; |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
/* used to prepare tag for OCaml, just in case their is a need to |
| 879 |
* add additional information to the tag. */ |
| 880 |
static void prepareTag (tagEntryInfo * tag, vString const *name, ocamlKind kind) |
| 881 |
{ |
| 882 |
int parentIndex; |
| 883 |
|
| 884 |
initTagEntry (tag, vStringValue (name)); |
| 885 |
tag->kindName = OcamlKinds[kind].name; |
| 886 |
tag->kind = OcamlKinds[kind].letter; |
| 887 |
|
| 888 |
if (kind == K_MODULE) |
| 889 |
{ |
| 890 |
tag->lineNumberEntry = TRUE; |
| 891 |
tag->lineNumber = 1; |
| 892 |
} |
| 893 |
parentIndex = getLastNamedIndex (); |
| 894 |
if (parentIndex >= 0) |
| 895 |
{ |
| 896 |
tag->extensionFields.scope[0] = |
| 897 |
contextDescription (stack[parentIndex].type); |
| 898 |
tag->extensionFields.scope[1] = |
| 899 |
vStringValue (stack[parentIndex].contextName); |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
/* Used to centralise tag creation, and be able to add |
| 904 |
* more information to it in the future */ |
| 905 |
static void addTag (vString * const ident, int kind) |
| 906 |
{ |
| 907 |
if (OcamlKinds [kind].enabled && ident != NULL && vStringLength (ident) > 0) |
| 908 |
{ |
| 909 |
tagEntryInfo toCreate; |
| 910 |
prepareTag (&toCreate, ident, kind); |
| 911 |
makeTagEntry (&toCreate); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
boolean needStrongPoping = FALSE; |
| 916 |
static void requestStrongPoping ( void ) |
| 917 |
{ |
| 918 |
needStrongPoping = TRUE; |
| 919 |
} |
| 920 |
|
| 921 |
static void cleanupPreviousParser ( void ) |
| 922 |
{ |
| 923 |
if (needStrongPoping) |
| 924 |
{ |
| 925 |
needStrongPoping = FALSE; |
| 926 |
popStrongContext (); |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
/* Due to some circular dependencies, the following functions |
| 931 |
* must be forward-declared. */ |
| 932 |
static void letParam (vString * const ident, ocaToken what); |
| 933 |
static void localScope (vString * const ident, ocaToken what); |
| 934 |
static void mayRedeclare (vString * const ident, ocaToken what); |
| 935 |
static void typeSpecification (vString * const ident, ocaToken what); |
| 936 |
|
| 937 |
/* |
| 938 |
* Parse a record type |
| 939 |
* type ident = // parsed previously |
| 940 |
* { |
| 941 |
* ident1: type1; |
| 942 |
* ident2: type2; |
| 943 |
* } |
| 944 |
*/ |
| 945 |
static void typeRecord (vString * const ident, ocaToken what) |
| 946 |
{ |
| 947 |
switch (what) |
| 948 |
{ |
| 949 |
case OcaIDENTIFIER: |
| 950 |
addTag (ident, K_RECORDFIELD); |
| 951 |
terminatingToken = Tok_CurlR; |
| 952 |
waitedToken = Tok_semi; |
| 953 |
comeAfter = &typeRecord; |
| 954 |
toDoNext = &tillTokenOrTerminatingOrFallback; |
| 955 |
break; |
| 956 |
|
| 957 |
case OcaKEYWORD_mutable: |
| 958 |
/* ignore it */ |
| 959 |
break; |
| 960 |
|
| 961 |
case Tok_CurlR: |
| 962 |
popStrongContext (); |
| 963 |
toDoNext = &globalScope; |
| 964 |
break; |
| 965 |
|
| 966 |
default: /* don't care */ |
| 967 |
break; |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
/* handle : |
| 972 |
* exception ExceptionName of ... */ |
| 973 |
static void exceptionDecl (vString * const ident, ocaToken what) |
| 974 |
{ |
| 975 |
if (what == OcaIDENTIFIER) |
| 976 |
{ |
| 977 |
addTag (ident, K_EXCEPTION); |
| 978 |
} |
| 979 |
else /* probably ill-formed, give back to global scope */ |
| 980 |
{ |
| 981 |
globalScope (ident, what); |
| 982 |
} |
| 983 |
toDoNext = &globalScope; |
| 984 |
} |
| 985 |
|
| 986 |
tagEntryInfo tempTag; |
| 987 |
vString *tempIdent; |
| 988 |
|
| 989 |
/* Ensure a constructor is not a type path beginning |
| 990 |
* with a module */ |
| 991 |
static void constructorValidation (vString * const ident, ocaToken what) |
| 992 |
{ |
| 993 |
switch (what) |
| 994 |
{ |
| 995 |
case Tok_Op: /* if we got a '.' which is an operator */ |
| 996 |
toDoNext = &globalScope; |
| 997 |
popStrongContext (); |
| 998 |
needStrongPoping = FALSE; |
| 999 |
break; |
| 1000 |
|
| 1001 |
case OcaKEYWORD_of: /* OK, it must be a constructor :) */ |
| 1002 |
makeTagEntry (&tempTag); |
| 1003 |
vStringClear (tempIdent); |
| 1004 |
toDoNext = &tillTokenOrFallback; |
| 1005 |
comeAfter = &typeSpecification; |
| 1006 |
waitedToken = Tok_Pipe; |
| 1007 |
break; |
| 1008 |
|
| 1009 |
case Tok_Pipe: /* OK, it was a constructor :) */ |
| 1010 |
makeTagEntry (&tempTag); |
| 1011 |
vStringClear (tempIdent); |
| 1012 |
toDoNext = &typeSpecification; |
| 1013 |
break; |
| 1014 |
|
| 1015 |
default: /* and mean that we're not facing a module name */ |
| 1016 |
makeTagEntry (&tempTag); |
| 1017 |
vStringClear (tempIdent); |
| 1018 |
toDoNext = &tillTokenOrFallback; |
| 1019 |
comeAfter = &typeSpecification; |
| 1020 |
waitedToken = Tok_Pipe; |
| 1021 |
|
| 1022 |
/* nothing in the context, discard it */ |
| 1023 |
popStrongContext (); |
| 1024 |
|
| 1025 |
/* to be sure we use this token */ |
| 1026 |
globalScope (ident, what); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
/* Parse beginning of type definition |
| 1032 |
* type 'avar ident = |
| 1033 |
* or |
| 1034 |
* type ('var1, 'var2) ident = |
| 1035 |
*/ |
| 1036 |
static void typeDecl (vString * const ident, ocaToken what) |
| 1037 |
{ |
| 1038 |
switch (what) |
| 1039 |
{ |
| 1040 |
/* parameterized */ |
| 1041 |
case Tok_Prime: |
| 1042 |
comeAfter = &typeDecl; |
| 1043 |
toDoNext = &ignoreToken; |
| 1044 |
break; |
| 1045 |
/* LOTS of parameters */ |
| 1046 |
case Tok_PARL: |
| 1047 |
comeAfter = &typeDecl; |
| 1048 |
waitedToken = Tok_PARR; |
| 1049 |
toDoNext = &tillToken; |
| 1050 |
break; |
| 1051 |
|
| 1052 |
case OcaIDENTIFIER: |
| 1053 |
addTag (ident, K_TYPE); |
| 1054 |
pushStrongContext (ident, ContextType); |
| 1055 |
requestStrongPoping (); |
| 1056 |
waitedToken = Tok_EQ; |
| 1057 |
comeAfter = &typeSpecification; |
| 1058 |
toDoNext = &tillTokenOrFallback; |
| 1059 |
break; |
| 1060 |
|
| 1061 |
default: |
| 1062 |
globalScope (ident, what); |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
/* Parse type of kind |
| 1067 |
* type bidule = Ctor1 of ... |
| 1068 |
* | Ctor2 |
| 1069 |
* | Ctor3 of ... |
| 1070 |
* or |
| 1071 |
* type bidule = | Ctor1 of ... | Ctor2 |
| 1072 |
* |
| 1073 |
* when type bidule = { ... } is detected, |
| 1074 |
* let typeRecord handle it. */ |
| 1075 |
static void typeSpecification (vString * const ident, ocaToken what) |
| 1076 |
{ |
| 1077 |
switch (what) |
| 1078 |
{ |
| 1079 |
case OcaIDENTIFIER: |
| 1080 |
if (isUpperAlpha (ident->buffer[0])) |
| 1081 |
{ |
| 1082 |
/* here we handle type aliases of type |
| 1083 |
* type foo = AnotherModule.bar |
| 1084 |
* AnotherModule can mistakenly be took |
| 1085 |
* for a constructor. */ |
| 1086 |
vStringCopy (tempIdent, ident); |
| 1087 |
prepareTag (&tempTag, tempIdent, K_CONSTRUCTOR); |
| 1088 |
toDoNext = &constructorValidation; |
| 1089 |
} |
| 1090 |
else |
| 1091 |
{ |
| 1092 |
toDoNext = &tillTokenOrFallback; |
| 1093 |
comeAfter = &typeSpecification; |
| 1094 |
waitedToken = Tok_Pipe; |
| 1095 |
} |
| 1096 |
break; |
| 1097 |
|
| 1098 |
case OcaKEYWORD_and: |
| 1099 |
toDoNext = &typeDecl; |
| 1100 |
break; |
| 1101 |
|
| 1102 |
case Tok_BRL: /* the '[' & ']' are ignored to accommodate */ |
| 1103 |
case Tok_BRR: /* with the revised syntax */ |
| 1104 |
case Tok_Pipe: |
| 1105 |
/* just ignore it */ |
| 1106 |
break; |
| 1107 |
|
| 1108 |
case Tok_CurlL: |
| 1109 |
toDoNext = &typeRecord; |
| 1110 |
break; |
| 1111 |
|
| 1112 |
default: /* don't care */ |
| 1113 |
break; |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
static boolean dirtySpecialParam = FALSE; |
| 1119 |
|
| 1120 |
|
| 1121 |
/* parse the ~label and ~label:type parameter */ |
| 1122 |
static void parseLabel (vString * const ident, ocaToken what) |
| 1123 |
{ |
| 1124 |
static int parCount = 0; |
| 1125 |
|
| 1126 |
switch (what) |
| 1127 |
{ |
| 1128 |
case OcaIDENTIFIER: |
| 1129 |
if (!dirtySpecialParam) |
| 1130 |
{ |
| 1131 |
|
| 1132 |
if (exportLocalInfo) |
| 1133 |
addTag (ident, K_VAR); |
| 1134 |
|
| 1135 |
dirtySpecialParam = TRUE; |
| 1136 |
} |
| 1137 |
break; |
| 1138 |
|
| 1139 |
case Tok_PARL: |
| 1140 |
parCount++; |
| 1141 |
break; |
| 1142 |
|
| 1143 |
case Tok_PARR: |
| 1144 |
parCount--; |
| 1145 |
if (parCount == 0) |
| 1146 |
toDoNext = &letParam; |
| 1147 |
break; |
| 1148 |
|
| 1149 |
case Tok_Op: |
| 1150 |
if (ident->buffer[0] == ':') |
| 1151 |
{ |
| 1152 |
toDoNext = &ignoreToken; |
| 1153 |
comeAfter = &letParam; |
| 1154 |
} |
| 1155 |
else if (parCount == 0 && dirtySpecialParam) |
| 1156 |
{ |
| 1157 |
toDoNext = &letParam; |
| 1158 |
letParam (ident, what); |
| 1159 |
} |
| 1160 |
break; |
| 1161 |
|
| 1162 |
default: |
| 1163 |
if (parCount == 0 && dirtySpecialParam) |
| 1164 |
{ |
| 1165 |
toDoNext = &letParam; |
| 1166 |
letParam (ident, what); |
| 1167 |
} |
| 1168 |
break; |
| 1169 |
} |
| 1170 |
} |
| 1171 |
|
| 1172 |
|
| 1173 |
/* Optional argument with syntax like this : |
| 1174 |
* ?(foo = value) */ |
| 1175 |
static void parseOptionnal (vString * const ident, ocaToken what) |
| 1176 |
{ |
| 1177 |
static int parCount = 0; |
| 1178 |
|
| 1179 |
|
| 1180 |
switch (what) |
| 1181 |
{ |
| 1182 |
case OcaIDENTIFIER: |
| 1183 |
if (!dirtySpecialParam) |
| 1184 |
{ |
| 1185 |
if (exportLocalInfo) |
| 1186 |
addTag (ident, K_VAR); |
| 1187 |
|
| 1188 |
dirtySpecialParam = TRUE; |
| 1189 |
|
| 1190 |
if (parCount == 0) |
| 1191 |
toDoNext = &letParam; |
| 1192 |
} |
| 1193 |
break; |
| 1194 |
|
| 1195 |
case Tok_PARL: |
| 1196 |
parCount++; |
| 1197 |
break; |
| 1198 |
|
| 1199 |
case Tok_PARR: |
| 1200 |
parCount--; |
| 1201 |
if (parCount == 0) |
| 1202 |
toDoNext = &letParam; |
| 1203 |
break; |
| 1204 |
|
| 1205 |
default: /* don't care */ |
| 1206 |
break; |
| 1207 |
} |
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
/** handle let inside functions (so like it's name |
| 1212 |
* say : local let */ |
| 1213 |
static void localLet (vString * const ident, ocaToken what) |
| 1214 |
{ |
| 1215 |
switch (what) |
| 1216 |
{ |
| 1217 |
case Tok_PARL: |
| 1218 |
/* We ignore this token to be able to parse such |
| 1219 |
* declarations : |
| 1220 |
* let (ident : type) = ... |
| 1221 |
*/ |
| 1222 |
break; |
| 1223 |
|
| 1224 |
case OcaKEYWORD_rec: |
| 1225 |
/* just ignore to be able to parse such declarations: |
| 1226 |
* let rec ident = ... */ |
| 1227 |
break; |
| 1228 |
|
| 1229 |
case Tok_Op: |
| 1230 |
/* we are defining a new operator, it's a |
| 1231 |
* function definition */ |
| 1232 |
if (exportLocalInfo) |
| 1233 |
addTag (ident, K_FUNCTION); |
| 1234 |
|
| 1235 |
pushSoftContext (mayRedeclare, ident, ContextFunction); |
| 1236 |
toDoNext = &letParam; |
| 1237 |
break; |
| 1238 |
|
| 1239 |
/* Can be a weiiird binding, or an '_' */ |
| 1240 |
case Tok_Val: |
| 1241 |
if (exportLocalInfo) |
| 1242 |
addTag (ident, K_VAR); |
| 1243 |
pushSoftContext (mayRedeclare, ident, ContextValue); |
| 1244 |
toDoNext = &letParam; |
| 1245 |
break; |
| 1246 |
|
| 1247 |
case OcaIDENTIFIER: |
| 1248 |
if (exportLocalInfo) |
| 1249 |
addTag (ident, K_VAR); |
| 1250 |
pushSoftContext (mayRedeclare, ident, ContextValue); |
| 1251 |
toDoNext = &letParam; |
| 1252 |
break; |
| 1253 |
|
| 1254 |
case OcaKEYWORD_end: |
| 1255 |
popStrongContext (); |
| 1256 |
break; |
| 1257 |
|
| 1258 |
default: |
| 1259 |
toDoNext = &localScope; |
| 1260 |
break; |
| 1261 |
} |
| 1262 |
} |
| 1263 |
|
| 1264 |
/* parse : |
| 1265 |
* | pattern pattern -> ... |
| 1266 |
* or |
| 1267 |
* pattern apttern apttern -> ... |
| 1268 |
* we ignore all identifiers declared in the pattern, |
| 1269 |
* because their scope is likely to be even more limited |
| 1270 |
* than the let definitions. |
| 1271 |
* Used after a match ... with, or a function ... or fun ... |
| 1272 |
* because their syntax is similar. */ |
| 1273 |
static void matchPattern (vString * const ident, ocaToken what) |
| 1274 |
{ |
| 1275 |
/* keep track of [], as it |
| 1276 |
* can be used in patterns and can |
| 1277 |
* mean the end of match expression in |
| 1278 |
* revised syntax */ |
| 1279 |
static int braceCount = 0; |
| 1280 |
|
| 1281 |
switch (what) |
| 1282 |
{ |
| 1283 |
case Tok_To: |
| 1284 |
pushEmptyContext (&matchPattern); |
| 1285 |
toDoNext = &mayRedeclare; |
| 1286 |
break; |
| 1287 |
|
| 1288 |
case Tok_BRL: |
| 1289 |
braceCount++; |
| 1290 |
break; |
| 1291 |
|
| 1292 |
case OcaKEYWORD_value: |
| 1293 |
popLastNamed (); |
| 1294 |
globalScope (ident, what); |
| 1295 |
break; |
| 1296 |
|
| 1297 |
case OcaKEYWORD_in: |
| 1298 |
popLastNamed (); |
| 1299 |
break; |
| 1300 |
|
| 1301 |
default: |
| 1302 |
break; |
| 1303 |
} |
| 1304 |
} |
| 1305 |
|
| 1306 |
/* Used at the beginning of a new scope (begin of a |
| 1307 |
* definition, parenthesis...) to catch inner let |
| 1308 |
* definition that may be in. */ |
| 1309 |
static void mayRedeclare (vString * const ident, ocaToken what) |
| 1310 |
{ |
| 1311 |
switch (what) |
| 1312 |
{ |
| 1313 |
case OcaKEYWORD_value: |
| 1314 |
// let globalScope handle it |
| 1315 |
globalScope (ident, what); |
| 1316 |
break; |
| 1317 |
|
| 1318 |
case OcaKEYWORD_let: |
| 1319 |
case OcaKEYWORD_val: |
| 1320 |
toDoNext = localLet; |
| 1321 |
break; |
| 1322 |
|
| 1323 |
case OcaKEYWORD_object: |
| 1324 |
vStringClear (lastClass); |
| 1325 |
pushContext (ContextStrong, ContextClass, |
| 1326 |
&localScope, NULL /*voidName */ ); |
| 1327 |
needStrongPoping = FALSE; |
| 1328 |
toDoNext = &globalScope; |
| 1329 |
break; |
| 1330 |
|
| 1331 |
case OcaKEYWORD_for: |
| 1332 |
case OcaKEYWORD_while: |
| 1333 |
toDoNext = &tillToken; |
| 1334 |
waitedToken = OcaKEYWORD_do; |
| 1335 |
comeAfter = &mayRedeclare; |
| 1336 |
break; |
| 1337 |
|
| 1338 |
case OcaKEYWORD_try: |
| 1339 |
toDoNext = &mayRedeclare; |
| 1340 |
pushSoftContext (matchPattern, ident, ContextFunction); |
| 1341 |
break; |
| 1342 |
|
| 1343 |
case OcaKEYWORD_fun: |
| 1344 |
toDoNext = &matchPattern; |
| 1345 |
break; |
| 1346 |
|
| 1347 |
/* Handle the special ;; from the OCaml |
| 1348 |
* Top level */ |
| 1349 |
case Tok_semi: |
| 1350 |
default: |
| 1351 |
toDoNext = &localScope; |
| 1352 |
localScope (ident, what); |
| 1353 |
} |
| 1354 |
} |
| 1355 |
|
| 1356 |
/* parse : |
| 1357 |
* p1 p2 ... pn = ... |
| 1358 |
* or |
| 1359 |
* ?(p1=v) p2 ~p3 ~pn:ja ... = ... */ |
| 1360 |
static void letParam (vString * const ident, ocaToken what) |
| 1361 |
{ |
| 1362 |
switch (what) |
| 1363 |
{ |
| 1364 |
case Tok_EQ: |
| 1365 |
toDoNext = &mayRedeclare; |
| 1366 |
break; |
| 1367 |
|
| 1368 |
case OcaIDENTIFIER: |
| 1369 |
if (exportLocalInfo) |
| 1370 |
addTag (ident, K_VAR); |
| 1371 |
break; |
| 1372 |
|
| 1373 |
case Tok_Op: |
| 1374 |
switch (ident->buffer[0]) |
| 1375 |
{ |
| 1376 |
case ':': |
| 1377 |
/*popSoftContext(); */ |
| 1378 |
/* we got a type signature */ |
| 1379 |
comeAfter = &mayRedeclare; |
| 1380 |
toDoNext = &tillTokenOrFallback; |
| 1381 |
waitedToken = Tok_EQ; |
| 1382 |
break; |
| 1383 |
|
| 1384 |
/* parse something like |
| 1385 |
* ~varname:type |
| 1386 |
* or |
| 1387 |
* ~varname |
| 1388 |
* or |
| 1389 |
* ~(varname: long type) */ |
| 1390 |
case '~': |
| 1391 |
toDoNext = &parseLabel; |
| 1392 |
dirtySpecialParam = FALSE; |
| 1393 |
break; |
| 1394 |
|
| 1395 |
/* Optional argument with syntax like this : |
| 1396 |
* ?(bla = value) |
| 1397 |
* or |
| 1398 |
* ?bla */ |
| 1399 |
case '?': |
| 1400 |
toDoNext = &parseOptionnal; |
| 1401 |
dirtySpecialParam = FALSE; |
| 1402 |
break; |
| 1403 |
|
| 1404 |
default: |
| 1405 |
break; |
| 1406 |
} |
| 1407 |
break; |
| 1408 |
|
| 1409 |
default: /* don't care */ |
| 1410 |
break; |
| 1411 |
} |
| 1412 |
} |
| 1413 |
|
| 1414 |
|
| 1415 |
/* parse object ... |
| 1416 |
* used to be sure the class definition is not a type |
| 1417 |
* alias */ |
| 1418 |
static void classSpecif (vString * const UNUSED (ident), ocaToken what) |
| 1419 |
{ |
| 1420 |
switch (what) |
| 1421 |
{ |
| 1422 |
case OcaKEYWORD_object: |
| 1423 |
pushStrongContext (lastClass, ContextClass); |
| 1424 |
toDoNext = &globalScope; |
| 1425 |
break; |
| 1426 |
|
| 1427 |
default: |
| 1428 |
vStringClear (lastClass); |
| 1429 |
toDoNext = &globalScope; |
| 1430 |
} |
| 1431 |
} |
| 1432 |
|
| 1433 |
/* Handle a method ... class declaration. |
| 1434 |
* nearly a copy/paste of globalLet. */ |
| 1435 |
static void methodDecl (vString * const ident, ocaToken what) |
| 1436 |
{ |
| 1437 |
|
| 1438 |
switch (what) |
| 1439 |
{ |
| 1440 |
case Tok_PARL: |
| 1441 |
/* We ignore this token to be able to parse such |
| 1442 |
* declarations : |
| 1443 |
* let (ident : type) = ... */ |
| 1444 |
break; |
| 1445 |
|
| 1446 |
case OcaKEYWORD_mutable: |
| 1447 |
case OcaKEYWORD_virtual: |
| 1448 |
case OcaKEYWORD_rec: |
| 1449 |
/* just ignore to be able to parse such declarations: |
| 1450 |
* let rec ident = ... */ |
| 1451 |
break; |
| 1452 |
|
| 1453 |
case OcaIDENTIFIER: |
| 1454 |
addTag (ident, K_METHOD); |
| 1455 |
/* Normal pushing to get good subs */ |
| 1456 |
pushStrongContext (ident, ContextMethod); |
| 1457 |
/*pushSoftContext( globalScope, ident, ContextMethod ); */ |
| 1458 |
toDoNext = &letParam; |
| 1459 |
break; |
| 1460 |
|
| 1461 |
case OcaKEYWORD_end: |
| 1462 |
popStrongContext (); |
| 1463 |
break; |
| 1464 |
|
| 1465 |
default: |
| 1466 |
toDoNext = &globalScope; |
| 1467 |
break; |
| 1468 |
} |
| 1469 |
} |
| 1470 |
|
| 1471 |
/* name of the last module, used for |
| 1472 |
* context stacking. */ |
| 1473 |
vString *lastModule; |
| 1474 |
|
| 1475 |
|
| 1476 |
/* parse |
| 1477 |
* ... struct (* new global scope *) end |
| 1478 |
* or |
| 1479 |
* ... sig (* new global scope *) end |
| 1480 |
* or |
| 1481 |
* functor ... -> moduleSpecif |
| 1482 |
*/ |
| 1483 |
static void moduleSpecif (vString * const ident, ocaToken what) |
| 1484 |
{ |
| 1485 |
|
| 1486 |
switch (what) |
| 1487 |
{ |
| 1488 |
case OcaKEYWORD_functor: |
| 1489 |
toDoNext = &contextualTillToken; |
| 1490 |
waitedToken = Tok_To; |
| 1491 |
comeAfter = &moduleSpecif; |
| 1492 |
break; |
| 1493 |
|
| 1494 |
case OcaKEYWORD_struct: |
| 1495 |
case OcaKEYWORD_sig: |
| 1496 |
pushStrongContext (lastModule, ContextModule); |
| 1497 |
toDoNext = &globalScope; |
| 1498 |
break; |
| 1499 |
|
| 1500 |
case Tok_PARL: /* ( */ |
| 1501 |
toDoNext = &contextualTillToken; |
| 1502 |
comeAfter = &globalScope; |
| 1503 |
waitedToken = Tok_PARR; |
| 1504 |
contextualTillToken (ident, what); |
| 1505 |
break; |
| 1506 |
|
| 1507 |
default: |
| 1508 |
vStringClear (lastModule); |
| 1509 |
toDoNext = &globalScope; |
| 1510 |
} |
| 1511 |
} |
| 1512 |
|
| 1513 |
/* parse : |
| 1514 |
* module name = ... |
| 1515 |
* then pass the token stream to moduleSpecif */ |
| 1516 |
static void moduleDecl (vString * const ident, ocaToken what) |
| 1517 |
{ |
| 1518 |
switch (what) |
| 1519 |
{ |
| 1520 |
case OcaKEYWORD_type: |
| 1521 |
/* just ignore it, name come after */ |
| 1522 |
break; |
| 1523 |
|
| 1524 |
case OcaIDENTIFIER: |
| 1525 |
addTag (ident, K_MODULE); |
| 1526 |
vStringCopy (lastModule, ident); |
| 1527 |
waitedToken = Tok_EQ; |
| 1528 |
comeAfter = &moduleSpecif; |
| 1529 |
toDoNext = &contextualTillToken; |
| 1530 |
break; |
| 1531 |
|
| 1532 |
default: /* don't care */ |
| 1533 |
break; |
| 1534 |
} |
| 1535 |
} |
| 1536 |
|
| 1537 |
/* parse : |
| 1538 |
* class name = ... |
| 1539 |
* or |
| 1540 |
* class virtual ['a,'b] classname = ... */ |
| 1541 |
static void classDecl (vString * const ident, ocaToken what) |
| 1542 |
{ |
| 1543 |
switch (what) |
| 1544 |
{ |
| 1545 |
case OcaIDENTIFIER: |
| 1546 |
addTag (ident, K_CLASS); |
| 1547 |
vStringCopy (lastClass, ident); |
| 1548 |
toDoNext = &contextualTillToken; |
| 1549 |
waitedToken = Tok_EQ; |
| 1550 |
comeAfter = &classSpecif; |
| 1551 |
break; |
| 1552 |
|
| 1553 |
case Tok_BRL: |
| 1554 |
toDoNext = &tillToken; |
| 1555 |
waitedToken = Tok_BRR; |
| 1556 |
comeAfter = &classDecl; |
| 1557 |
break; |
| 1558 |
|
| 1559 |
default: |
| 1560 |
break; |
| 1561 |
} |
| 1562 |
} |
| 1563 |
|
| 1564 |
/* Handle a global |
| 1565 |
* let ident ... |
| 1566 |
* or |
| 1567 |
* let rec ident ... */ |
| 1568 |
static void globalLet (vString * const ident, ocaToken what) |
| 1569 |
{ |
| 1570 |
switch (what) |
| 1571 |
{ |
| 1572 |
case Tok_PARL: |
| 1573 |
/* We ignore this token to be able to parse such |
| 1574 |
* declarations : |
| 1575 |
* let (ident : type) = ... |
| 1576 |
*/ |
| 1577 |
break; |
| 1578 |
|
| 1579 |
case OcaKEYWORD_mutable: |
| 1580 |
case OcaKEYWORD_virtual: |
| 1581 |
case OcaKEYWORD_rec: |
| 1582 |
/* just ignore to be able to parse such declarations: |
| 1583 |
* let rec ident = ... */ |
| 1584 |
break; |
| 1585 |
|
| 1586 |
case Tok_Op: |
| 1587 |
/* we are defining a new operator, it's a |
| 1588 |
* function definition */ |
| 1589 |
addTag (ident, K_FUNCTION); |
| 1590 |
pushStrongContext (ident, ContextFunction); |
| 1591 |
toDoNext = &letParam; |
| 1592 |
break; |
| 1593 |
|
| 1594 |
case OcaIDENTIFIER: |
| 1595 |
addTag (ident, K_VAR); |
| 1596 |
pushStrongContext (ident, ContextValue); |
| 1597 |
requestStrongPoping (); |
| 1598 |
toDoNext = &letParam; |
| 1599 |
break; |
| 1600 |
|
| 1601 |
case OcaKEYWORD_end: |
| 1602 |
popStrongContext (); |
| 1603 |
break; |
| 1604 |
|
| 1605 |
default: |
| 1606 |
toDoNext = &globalScope; |
| 1607 |
break; |
| 1608 |
} |
| 1609 |
} |
| 1610 |
|
| 1611 |
/* Handle the "strong" top levels, all 'big' declarations |
| 1612 |
* happen here */ |
| 1613 |
static void globalScope (vString * const UNUSED (ident), ocaToken what) |
| 1614 |
{ |
| 1615 |
/* Do not touch, this is used only by the global scope |
| 1616 |
* to handle an 'and' */ |
| 1617 |
static parseNext previousParser = &globalScope; |
| 1618 |
|
| 1619 |
switch (what) |
| 1620 |
{ |
| 1621 |
case OcaKEYWORD_and: |
| 1622 |
cleanupPreviousParser (); |
| 1623 |
toDoNext = previousParser; |
| 1624 |
break; |
| 1625 |
|
| 1626 |
case OcaKEYWORD_type: |
| 1627 |
cleanupPreviousParser (); |
| 1628 |
toDoNext = &typeDecl; |
| 1629 |
previousParser = &typeDecl; |
| 1630 |
break; |
| 1631 |
|
| 1632 |
case OcaKEYWORD_class: |
| 1633 |
cleanupPreviousParser (); |
| 1634 |
toDoNext = &classDecl; |
| 1635 |
previousParser = &classDecl; |
| 1636 |
break; |
| 1637 |
|
| 1638 |
case OcaKEYWORD_module: |
| 1639 |
cleanupPreviousParser (); |
| 1640 |
toDoNext = &moduleDecl; |
| 1641 |
previousParser = &moduleDecl; |
| 1642 |
break; |
| 1643 |
|
| 1644 |
case OcaKEYWORD_end: |
| 1645 |
needStrongPoping = FALSE; |
| 1646 |
killCurrentState (); |
| 1647 |
/*popStrongContext(); */ |
| 1648 |
break; |
| 1649 |
|
| 1650 |
case OcaKEYWORD_method: |
| 1651 |
cleanupPreviousParser (); |
| 1652 |
toDoNext = &methodDecl; |
| 1653 |
/* and is not allowed in methods */ |
| 1654 |
break; |
| 1655 |
|
| 1656 |
/* val is mixed with let as global |
| 1657 |
* to be able to handle mli & new syntax */ |
| 1658 |
case OcaKEYWORD_val: |
| 1659 |
case OcaKEYWORD_value: |
| 1660 |
case OcaKEYWORD_let: |
| 1661 |
cleanupPreviousParser (); |
| 1662 |
toDoNext = &globalLet; |
| 1663 |
previousParser = &globalLet; |
| 1664 |
break; |
| 1665 |
|
| 1666 |
case OcaKEYWORD_exception: |
| 1667 |
cleanupPreviousParser (); |
| 1668 |
toDoNext = &exceptionDecl; |
| 1669 |
previousParser = &globalScope; |
| 1670 |
break; |
| 1671 |
|
| 1672 |
/* must be a #line directive, discard the |
| 1673 |
* whole line. */ |
| 1674 |
case Tok_Sharp: |
| 1675 |
/* ignore */ |
| 1676 |
break; |
| 1677 |
|
| 1678 |
default: |
| 1679 |
/* we don't care */ |
| 1680 |
break; |
| 1681 |
} |
| 1682 |
} |
| 1683 |
|
| 1684 |
/* Parse expression. Well ignore it is more the case, |
| 1685 |
* ignore all tokens except "shocking" keywords */ |
| 1686 |
static void localScope (vString * const ident, ocaToken what) |
| 1687 |
{ |
| 1688 |
switch (what) |
| 1689 |
{ |
| 1690 |
case Tok_Pipe: |
| 1691 |
case Tok_PARR: |
| 1692 |
case Tok_BRR: |
| 1693 |
case Tok_CurlR: |
| 1694 |
popSoftContext (); |
| 1695 |
break; |
| 1696 |
|
| 1697 |
/* Everything that `begin` has an `end` |
| 1698 |
* as end is overloaded and signal many end |
| 1699 |
* of things, we add an empty strong context to |
| 1700 |
* avoid problem with the end. |
| 1701 |
*/ |
| 1702 |
case OcaKEYWORD_begin: |
| 1703 |
pushContext (ContextStrong, ContextBlock, &mayRedeclare, NULL); |
| 1704 |
toDoNext = &mayRedeclare; |
| 1705 |
break; |
| 1706 |
|
| 1707 |
case OcaKEYWORD_in: |
| 1708 |
popLastNamed (); |
| 1709 |
break; |
| 1710 |
|
| 1711 |
/* Ok, we got a '{', which is much likely to create |
| 1712 |
* a record. We cannot treat it like other [ && (, |
| 1713 |
* because it may contain the 'with' keyword and screw |
| 1714 |
* everything else. */ |
| 1715 |
case Tok_CurlL: |
| 1716 |
toDoNext = &contextualTillToken; |
| 1717 |
waitedToken = Tok_CurlR; |
| 1718 |
comeAfter = &localScope; |
| 1719 |
contextualTillToken (ident, what); |
| 1720 |
break; |
| 1721 |
|
| 1722 |
/* Yeah imperative feature of OCaml, |
| 1723 |
* a ';' like in C */ |
| 1724 |
case Tok_semi: |
| 1725 |
toDoNext = &mayRedeclare; |
| 1726 |
break; |
| 1727 |
|
| 1728 |
case Tok_PARL: |
| 1729 |
case Tok_BRL: |
| 1730 |
pushEmptyContext (&localScope); |
| 1731 |
toDoNext = &mayRedeclare; |
| 1732 |
break; |
| 1733 |
|
| 1734 |
case OcaKEYWORD_and: |
| 1735 |
popLastNamed (); |
| 1736 |
toDoNext = &localLet; |
| 1737 |
break; |
| 1738 |
|
| 1739 |
case OcaKEYWORD_else: |
| 1740 |
case OcaKEYWORD_then: |
| 1741 |
popSoftContext (); |
| 1742 |
pushEmptyContext (&localScope); |
| 1743 |
toDoNext = &mayRedeclare; |
| 1744 |
break; |
| 1745 |
|
| 1746 |
case OcaKEYWORD_if: |
| 1747 |
pushEmptyContext (&localScope); |
| 1748 |
toDoNext = &mayRedeclare; |
| 1749 |
break; |
| 1750 |
|
| 1751 |
case OcaKEYWORD_match: |
| 1752 |
pushEmptyContext (&localScope); |
| 1753 |
toDoNext = &mayRedeclare; |
| 1754 |
break; |
| 1755 |
|
| 1756 |
case OcaKEYWORD_with: |
| 1757 |
popSoftContext (); |
| 1758 |
toDoNext = &matchPattern; |
| 1759 |
pushEmptyContext (&matchPattern); |
| 1760 |
break; |
| 1761 |
|
| 1762 |
case OcaKEYWORD_end: |
| 1763 |
killCurrentState (); |
| 1764 |
break; |
| 1765 |
|
| 1766 |
|
| 1767 |
case OcaKEYWORD_fun: |
| 1768 |
comeAfter = &mayRedeclare; |
| 1769 |
toDoNext = &tillToken; |
| 1770 |
waitedToken = Tok_To; |
| 1771 |
break; |
| 1772 |
|
| 1773 |
case OcaKEYWORD_done: |
| 1774 |
case OcaKEYWORD_val: |
| 1775 |
/* doesn't care */ |
| 1776 |
break; |
| 1777 |
|
| 1778 |
default: |
| 1779 |
requestStrongPoping (); |
| 1780 |
globalScope (ident, what); |
| 1781 |
break; |
| 1782 |
} |
| 1783 |
} |
| 1784 |
|
| 1785 |
/*//////////////////////////////////////////////////////////////// |
| 1786 |
//// Deal with the system */ |
| 1787 |
/* in OCaml the file name is the module name used in the language |
| 1788 |
* with it first letter put in upper case */ |
| 1789 |
static void computeModuleName ( void ) |
| 1790 |
{ |
| 1791 |
/* in Ocaml the file name define a module. |
| 1792 |
* so we define a module =) |
| 1793 |
*/ |
| 1794 |
const char *filename = getSourceFileName (); |
| 1795 |
int beginIndex = 0; |
| 1796 |
int endIndex = strlen (filename) - 1; |
| 1797 |
vString *moduleName = vStringNew (); |
| 1798 |
|
| 1799 |
while (filename[endIndex] != '.' && endIndex > 0) |
| 1800 |
endIndex--; |
| 1801 |
|
| 1802 |
/* avoid problem with path in front of filename */ |
| 1803 |
beginIndex = endIndex; |
| 1804 |
while (beginIndex > 0) |
| 1805 |
{ |
| 1806 |
if (filename[beginIndex] == '\\' || filename[beginIndex] == '/') |
| 1807 |
{ |
| 1808 |
beginIndex++; |
| 1809 |
break; |
| 1810 |
} |
| 1811 |
|
| 1812 |
beginIndex--; |
| 1813 |
} |
| 1814 |
|
| 1815 |
vStringNCopyS (moduleName, &filename[beginIndex], endIndex - beginIndex); |
| 1816 |
vStringTerminate (moduleName); |
| 1817 |
|
| 1818 |
if (isLowerAlpha (moduleName->buffer[0])) |
| 1819 |
moduleName->buffer[0] += ('A' - 'a'); |
| 1820 |
|
| 1821 |
addTag (moduleName, K_MODULE); |
| 1822 |
vStringDelete (moduleName); |
| 1823 |
} |
| 1824 |
|
| 1825 |
/* Allocate all string of the context stack */ |
| 1826 |
static void initStack ( void ) |
| 1827 |
{ |
| 1828 |
int i; |
| 1829 |
for (i = 0; i < OCAML_MAX_STACK_SIZE; ++i) |
| 1830 |
stack[i].contextName = vStringNew (); |
| 1831 |
stackIndex = 0; |
| 1832 |
} |
| 1833 |
|
| 1834 |
static void clearStack ( void ) |
| 1835 |
{ |
| 1836 |
int i; |
| 1837 |
for (i = 0; i < OCAML_MAX_STACK_SIZE; ++i) |
| 1838 |
vStringDelete (stack[i].contextName); |
| 1839 |
} |
| 1840 |
|
| 1841 |
static void findOcamlTags (void) |
| 1842 |
{ |
| 1843 |
vString *name = vStringNew (); |
| 1844 |
lexingState st; |
| 1845 |
ocaToken tok; |
| 1846 |
|
| 1847 |
initStack (); |
| 1848 |
computeModuleName (); |
| 1849 |
tempIdent = vStringNew (); |
| 1850 |
lastModule = vStringNew (); |
| 1851 |
lastClass = vStringNew (); |
| 1852 |
voidName = vStringNew (); |
| 1853 |
vStringCopyS (voidName, "_"); |
| 1854 |
|
| 1855 |
st.name = vStringNew (); |
| 1856 |
st.cp = fileReadLine (); |
| 1857 |
toDoNext = &globalScope; |
| 1858 |
tok = lex (&st); |
| 1859 |
while (tok != Tok_EOF) |
| 1860 |
{ |
| 1861 |
(*toDoNext) (st.name, tok); |
| 1862 |
tok = lex (&st); |
| 1863 |
} |
| 1864 |
|
| 1865 |
vStringDelete (name); |
| 1866 |
vStringDelete (voidName); |
| 1867 |
vStringDelete (tempIdent); |
| 1868 |
vStringDelete (lastModule); |
| 1869 |
vStringDelete (lastClass); |
| 1870 |
clearStack (); |
| 1871 |
} |
| 1872 |
|
| 1873 |
static void ocamlInitialize (const langType language) |
| 1874 |
{ |
| 1875 |
Lang_Ocaml = language; |
| 1876 |
|
| 1877 |
initOperatorTable (); |
| 1878 |
initKeywordHash (); |
| 1879 |
} |
| 1880 |
|
| 1881 |
extern parserDefinition *OcamlParser (void) |
| 1882 |
{ |
| 1883 |
static const char *const extensions[] = { "ml", "mli", NULL }; |
| 1884 |
parserDefinition *def = parserNew ("OCaml"); |
| 1885 |
def->kinds = OcamlKinds; |
| 1886 |
def->kindCount = KIND_COUNT (OcamlKinds); |
| 1887 |
def->extensions = extensions; |
| 1888 |
def->parser = findOcamlTags; |
| 1889 |
def->initialize = ocamlInitialize; |
| 1890 |
|
| 1891 |
return def; |
| 1892 |
} |