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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
|
#include <ctype.h> /* for toupper etc */
#include <stdio.h> /* for fprintf etc */
#include <stdlib.h> /* for free etc */
#include <string.h> /* for strcmp */
#include "header.h"
#define DEFAULT_JAVA_PACKAGE "org.tartarus.snowball.ext"
#define DEFAULT_JAVA_BASE_CLASS "org.tartarus.snowball.SnowballProgram"
#define DEFAULT_JAVA_AMONG_CLASS "org.tartarus.snowball.Among"
#define DEFAULT_JAVA_STRING_CLASS "java.lang.StringBuilder"
#define DEFAULT_GO_PACKAGE "snowball"
#define DEFAULT_GO_SNOWBALL_RUNTIME "github.com/snowballstem/snowball/go"
#define DEFAULT_ADA_PACKAGE "Snowball"
#define DEFAULT_ADA_SNOWBALL_RUNTIME "github.com/snowballstem/snowball/ada"
#define DEFAULT_CS_NAMESPACE "Snowball"
#define DEFAULT_CS_BASE_CLASS "Stemmer"
#define DEFAULT_CS_AMONG_CLASS "Among"
#define DEFAULT_CS_STRING_CLASS "StringBuilder"
#define DEFAULT_JS_BASE_CLASS "BaseStemmer"
#define DEFAULT_PYTHON_BASE_CLASS "BaseStemmer"
static int eq(const char * s1, const char * s2) {
return strcmp(s1, s2) == 0;
}
static void print_arglist(int exit_code) {
FILE * f = exit_code ? stderr : stdout;
fprintf(f, "Usage: snowball SOURCE_FILE... [OPTIONS]\n\n"
"Supported options:\n"
" -o, -output OUTPUT_BASE\n"
" -s, -syntax\n"
" -comments\n"
#ifndef DISABLE_JAVA
" -j, -java\n"
#endif
#ifndef DISABLE_CSHARP
" -cs, -csharp\n"
#endif
" -c++\n"
#ifndef DISABLE_PASCAL
" -pascal\n"
#endif
#ifndef DISABLE_PYTHON
" -py, -python\n"
#endif
#ifndef DISABLE_JS
" -js generate Javascript\n"
#endif
#ifndef DISABLE_RUST
" -rust\n"
#endif
#ifndef DISABLE_GO
" -go\n"
#endif
#ifndef DISABLE_ADA
" -ada\n"
#endif
" -w, -widechars\n"
" -u, -utf8\n"
" -n, -name CLASS_NAME\n"
" -ep, -eprefix EXTERNAL_PREFIX\n"
" -vp, -vprefix VARIABLE_PREFIX\n"
" -i, -include DIRECTORY\n"
" -r, -runtime DIRECTORY\n"
" -p, -parentclassname CLASS_NAME fully qualified parent class name\n"
#if !defined(DISABLE_JAVA) || !defined(DISABLE_CSHARP)
" -P, -Package PACKAGE_NAME package name for stemmers\n"
" -S, -Stringclass STRING_CLASS StringBuffer-compatible class\n"
" -a, -amongclass AMONG_CLASS fully qualified name of the Among class\n"
#endif
#ifndef DISABLE_GO
" -gop, -gopackage PACKAGE_NAME Go package name for stemmers\n"
" -gor, -goruntime PACKAGE_NAME Go snowball runtime package\n"
#endif
" --help display this help and exit\n"
" --version output version information and exit\n"
);
exit(exit_code);
}
static void check_lim(int i, int argc) {
if (i >= argc) {
fprintf(stderr, "argument list is one short\n");
print_arglist(1);
}
}
static FILE * get_output(byte * s) {
s[SIZE(s)] = 0;
const char * filename = (const char *)s;
FILE * output = fopen(filename, "w");
if (output == NULL) {
fprintf(stderr, "Can't open output %s\n", filename);
exit(1);
}
return output;
}
static int read_options(struct options * o, int argc, char * argv[]) {
int i = 1;
int new_argc = 1;
/* Note down the last option used to specify an explicit encoding so
* we can warn we ignored it for languages with a fixed encoding.
*/
const char * encoding_opt = NULL;
/* set defaults: */
o->output_file = NULL;
o->syntax_tree = false;
o->comments = false;
o->js_esm = false;
o->externals_prefix = NULL;
o->variables_prefix = NULL;
o->runtime_path = NULL;
o->parent_class_name = NULL;
o->string_class = NULL;
o->among_class = NULL;
o->package = NULL;
o->go_snowball_runtime = DEFAULT_GO_SNOWBALL_RUNTIME;
o->name = NULL;
o->make_lang = LANG_C;
o->includes = NULL;
o->includes_end = NULL;
o->encoding = ENC_SINGLEBYTE;
/* read options: */
while (i < argc) {
char * s = argv[i++];
if (s[0] != '-') {
/* Non-option argument - shuffle down. */
argv[new_argc++] = s;
continue;
}
{
if (eq(s, "-o") || eq(s, "-output")) {
check_lim(i, argc);
o->output_file = argv[i++];
continue;
}
if (eq(s, "-n") || eq(s, "-name")) {
char * new_name;
size_t len;
check_lim(i, argc);
/* Take a copy of the argument here, because
* later we will free o->name */
len = strlen(argv[i]);
new_name = MALLOC(len + 1);
memcpy(new_name, argv[i++], len);
new_name[len] = '\0';
o->name = new_name;
continue;
}
#ifndef DISABLE_JS
if (eq(s, "-js")) {
o->make_lang = LANG_JAVASCRIPT;
o->js_esm = false;
continue;
}
#endif
#ifndef DISABLE_RUST
if (eq(s, "-rust")) {
o->make_lang = LANG_RUST;
continue;
}
#endif
#ifndef DISABLE_GO
if (eq(s, "-go")) {
o->make_lang = LANG_GO;
continue;
}
#endif
#ifndef DISABLE_JAVA
if (eq(s, "-j") || eq(s, "-java")) {
o->make_lang = LANG_JAVA;
continue;
}
#endif
#ifndef DISABLE_CSHARP
if (eq(s, "-cs") || eq(s, "-csharp")) {
o->make_lang = LANG_CSHARP;
continue;
}
#endif
if (eq(s, "-c++")) {
o->make_lang = LANG_CPLUSPLUS;
continue;
}
#ifndef DISABLE_PASCAL
if (eq(s, "-pascal")) {
o->make_lang = LANG_PASCAL;
continue;
}
#endif
#ifndef DISABLE_PYTHON
if (eq(s, "-py") || eq(s, "-python")) {
o->make_lang = LANG_PYTHON;
continue;
}
#endif
#ifndef DISABLE_ADA
if (eq(s, "-ada")) {
o->make_lang = LANG_ADA;
continue;
}
#endif
if (eq(s, "-w") || eq(s, "-widechars")) {
encoding_opt = s;
o->encoding = ENC_WIDECHARS;
continue;
}
if (eq(s, "-s") || eq(s, "-syntax")) {
o->syntax_tree = true;
continue;
}
if (eq(s, "-comments")) {
o->comments = true;
continue;
}
if (eq(s, "-ep") || eq(s, "-eprefix")) {
check_lim(i, argc);
o->externals_prefix = argv[i++];
continue;
}
if (eq(s, "-vp") || eq(s, "-vprefix")) {
check_lim(i, argc);
o->variables_prefix = argv[i++];
continue;
}
if (eq(s, "-i") || eq(s, "-include")) {
check_lim(i, argc);
{
NEW(include, p);
byte * include_dir = add_sz_to_s(NULL, argv[i++]);
include_dir = add_char_to_s(include_dir, '/');
p->next = NULL; p->s = include_dir;
if (o->includes == NULL) {
o->includes = p;
} else {
o->includes_end->next = p;
}
o->includes_end = p;
}
continue;
}
if (eq(s, "-r") || eq(s, "-runtime")) {
check_lim(i, argc);
o->runtime_path = argv[i++];
continue;
}
if (eq(s, "-u") || eq(s, "-utf8")) {
encoding_opt = s;
o->encoding = ENC_UTF8;
continue;
}
if (eq(s, "-p") || eq(s, "-parentclassname")) {
check_lim(i, argc);
o->parent_class_name = argv[i++];
continue;
}
#if !defined(DISABLE_JAVA) || !defined(DISABLE_CSHARP)
if (eq(s, "-P") || eq(s, "-Package")) {
check_lim(i, argc);
o->package = argv[i++];
continue;
}
if (eq(s, "-S") || eq(s, "-stringclass")) {
check_lim(i, argc);
o->string_class = argv[i++];
continue;
}
if (eq(s, "-a") || eq(s, "-amongclass")) {
check_lim(i, argc);
o->among_class = argv[i++];
continue;
}
#endif
#ifndef DISABLE_GO
if (eq(s, "-gop") || eq(s, "-gopackage")) {
check_lim(i, argc);
o->package = argv[i++];
continue;
}
if (eq(s, "-gor") || eq(s, "-goruntime")) {
check_lim(i, argc);
o->go_snowball_runtime = argv[i++];
continue;
}
#endif
if (eq(s, "--help")) {
print_arglist(0);
}
if (eq(s, "--version")) {
printf("Snowball compiler version " SNOWBALL_VERSION "\n");
exit(0);
}
fprintf(stderr, "'%s' misplaced\n", s);
print_arglist(1);
}
}
if (new_argc == 1) {
fprintf(stderr, "no source files specified\n");
print_arglist(1);
}
argv[new_argc] = NULL;
/* Set language-dependent defaults. */
switch (o->make_lang) {
case LANG_C:
case LANG_CPLUSPLUS:
encoding_opt = NULL;
break;
case LANG_CSHARP:
o->encoding = ENC_WIDECHARS;
if (!o->parent_class_name)
o->parent_class_name = DEFAULT_CS_BASE_CLASS;
if (!o->string_class)
o->string_class = DEFAULT_CS_STRING_CLASS;
if (!o->among_class)
o->among_class = DEFAULT_CS_AMONG_CLASS;
if (!o->package)
o->package = DEFAULT_CS_NAMESPACE;
break;
case LANG_GO:
o->encoding = ENC_UTF8;
if (!o->package)
o->package = DEFAULT_GO_PACKAGE;
break;
case LANG_ADA:
o->encoding = ENC_UTF8;
if (!o->package)
o->package = DEFAULT_ADA_PACKAGE;
break;
case LANG_JAVA:
o->encoding = ENC_WIDECHARS;
if (!o->parent_class_name)
o->parent_class_name = DEFAULT_JAVA_BASE_CLASS;
if (!o->string_class)
o->string_class = DEFAULT_JAVA_STRING_CLASS;
if (!o->among_class)
o->among_class = DEFAULT_JAVA_AMONG_CLASS;
if (!o->package)
o->package = DEFAULT_JAVA_PACKAGE;
break;
case LANG_JAVASCRIPT:
o->encoding = ENC_WIDECHARS;
if (!o->parent_class_name)
o->parent_class_name = DEFAULT_JS_BASE_CLASS;
break;
case LANG_PYTHON:
o->encoding = ENC_WIDECHARS;
if (!o->parent_class_name)
o->parent_class_name = DEFAULT_PYTHON_BASE_CLASS;
break;
case LANG_RUST:
o->encoding = ENC_UTF8;
break;
default:
break;
}
if (encoding_opt) {
fprintf(stderr, "warning: %s only meaningful for C and C++\n",
encoding_opt);
}
if (o->make_lang != LANG_C && o->make_lang != LANG_CPLUSPLUS) {
if (o->runtime_path) {
fprintf(stderr, "warning: -r/-runtime only meaningful for C and C++\n");
}
if (o->externals_prefix) {
fprintf(stderr, "warning: -ep/-eprefix only meaningful for C and C++\n");
}
}
if (!o->externals_prefix) o->externals_prefix = "";
if (!o->name && o->output_file) {
/* Default class name to basename of output_file - this is the standard
* convention for at least Java and C#.
*/
const char * slash = strrchr(o->output_file, '/');
size_t len;
const char * leaf = (slash == NULL) ? o->output_file : slash + 1;
slash = strrchr(leaf, '\\');
if (slash != NULL) leaf = slash + 1;
{
const char * dot = strchr(leaf, '.');
len = (dot == NULL) ? strlen(leaf) : (size_t)(dot - leaf);
}
{
char * new_name = MALLOC(len + 1);
switch (o->make_lang) {
case LANG_CSHARP:
case LANG_PASCAL:
/* Upper case initial letter. */
memcpy(new_name, leaf, len);
new_name[0] = toupper(new_name[0]);
break;
case LANG_JAVASCRIPT:
case LANG_PYTHON: {
/* Upper case initial letter and change each
* underscore+letter or hyphen+letter to an upper case
* letter.
*/
size_t new_len = 0;
int uc_next = true;
for (size_t j = 0; j != len; ++j) {
unsigned char ch = leaf[j];
if (ch == '_' || ch == '-') {
uc_next = true;
} else {
if (uc_next) {
new_name[new_len] = toupper(ch);
uc_next = false;
} else {
new_name[new_len] = ch;
}
++new_len;
}
}
len = new_len;
break;
}
default:
/* Just copy. */
memcpy(new_name, leaf, len);
break;
}
new_name[len] = '\0';
o->name = new_name;
}
}
return new_argc;
}
extern int main(int argc, char * argv[]) {
int i;
NEW(options, o);
argc = read_options(o, argc, argv);
{
char * file = argv[1];
byte * u = get_input(file);
if (u == NULL) {
fprintf(stderr, "Can't open input %s\n", file);
exit(1);
}
{
struct tokeniser * t = create_tokeniser(u, file);
struct analyser * a = create_analyser(t);
struct input ** next_input_ptr = &(t->next);
a->encoding = t->encoding = o->encoding;
t->includes = o->includes;
/* If multiple source files are specified, set up the others to be
* read after the first in order, using the same mechanism as
* 'get' uses. */
for (i = 2; i != argc; ++i) {
NEW(input, q);
file = argv[i];
u = get_input(file);
if (u == NULL) {
fprintf(stderr, "Can't open input %s\n", file);
exit(1);
}
q->p = u;
q->c = 0;
q->file = file;
q->file_owned = 0;
q->line_number = 1;
*next_input_ptr = q;
next_input_ptr = &(q->next);
}
*next_input_ptr = NULL;
read_program(a);
if (t->error_count > 0) exit(1);
if (o->syntax_tree) print_program(a);
if (!o->syntax_tree) {
struct generator * g;
const char * output_base = o->output_file;
if (!output_base) {
fprintf(stderr, "Please include the -o option\n");
print_arglist(1);
}
g = create_generator(a, o);
if (o->make_lang == LANG_C || o->make_lang == LANG_CPLUSPLUS) {
byte * s = add_sz_to_s(NULL, output_base);
s = add_literal_to_s(s, ".h");
o->output_h = get_output(s);
s[SIZE(s) - 1] = 'c';
if (o->make_lang == LANG_CPLUSPLUS) {
s = add_char_to_s(s, 'c');
}
o->output_src = get_output(s);
lose_s(s);
generate_program_c(g);
fclose(o->output_src);
fclose(o->output_h);
}
#ifndef DISABLE_JAVA
if (o->make_lang == LANG_JAVA) {
byte * s = add_sz_to_s(NULL, output_base);
s = add_literal_to_s(s, ".java");
o->output_src = get_output(s);
lose_s(s);
generate_program_java(g);
fclose(o->output_src);
}
#endif
#ifndef DISABLE_PASCAL
if (o->make_lang == LANG_PASCAL) {
byte * s = add_sz_to_s(NULL, output_base);
s = add_literal_to_s(s, ".pas");
o->output_src = get_output(s);
lose_s(s);
generate_program_pascal(g);
fclose(o->output_src);
}
#endif
#ifndef DISABLE_PYTHON
if (o->make_lang == LANG_PYTHON) {
byte * s = add_sz_to_s(NULL, output_base);
s = add_literal_to_s(s, ".py");
o->output_src = get_output(s);
lose_s(s);
generate_program_python(g);
fclose(o->output_src);
}
#endif
#ifndef DISABLE_JS
if (o->make_lang == LANG_JAVASCRIPT) {
byte * s = add_sz_to_s(NULL, output_base);
if (o->js_esm) {
s = add_literal_to_s(s, ".mjs");
} else {
s = add_literal_to_s(s, ".js");
}
o->output_src = get_output(s);
lose_s(s);
generate_program_js(g);
fclose(o->output_src);
}
#endif
#ifndef DISABLE_CSHARP
if (o->make_lang == LANG_CSHARP) {
byte * s = add_sz_to_s(NULL, output_base);
s = add_literal_to_s(s, ".cs");
o->output_src = get_output(s);
lose_s(s);
generate_program_csharp(g);
fclose(o->output_src);
}
#endif
#ifndef DISABLE_RUST
if (o->make_lang == LANG_RUST) {
byte * s = add_sz_to_s(NULL, output_base);
s = add_literal_to_s(s, ".rs");
o->output_src = get_output(s);
lose_s(s);
generate_program_rust(g);
fclose(o->output_src);
}
#endif
#ifndef DISABLE_GO
if (o->make_lang == LANG_GO) {
byte * s = add_sz_to_s(NULL, output_base);
s = add_literal_to_s(s, ".go");
o->output_src = get_output(s);
lose_s(s);
generate_program_go(g);
fclose(o->output_src);
}
#endif
#ifndef DISABLE_ADA
if (o->make_lang == LANG_ADA) {
byte * s = add_sz_to_s(NULL, output_base);
s = add_literal_to_s(s, ".ads");
o->output_h = get_output(s);
s[SIZE(s) - 1] = 'b';
o->output_src = get_output(s);
lose_s(s);
generate_program_ada(g);
fclose(o->output_src);
fclose(o->output_h);
}
#endif
close_generator(g);
}
close_tokeniser(t);
close_analyser(a);
}
lose_s(u);
}
{ struct include * p = o->includes;
while (p) {
struct include * q = p->next;
lose_s(p->s);
FREE(p);
p = q;
}
}
FREE(o->name);
FREE(o);
if (space_count) fprintf(stderr, "%d blocks unfreed\n", space_count);
return 0;
}
|