File: mklang.c

package info (click to toggle)
gretl 2019a-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 53,708 kB
  • sloc: ansic: 367,137; sh: 4,416; makefile: 2,636; cpp: 2,499; xml: 580; perl: 364
file content (463 lines) | stat: -rw-r--r-- 12,645 bytes parent folder | download
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
#include <stdio.h>

#include "libgretl.h"
#include "genparse.h"

const char *special_keyword[] = {
    "for",
    "foreach",
    "funcerr",
    "return", 
    "while",
    "elif",
    "eval",
    "const",
    "3sls",
    "liml",
    "fiml",
    "sur",
    "params",    
    "deriv",
    "orthog",
    "weights",
    "hessian",
    "option",
    "options",
    "literal",
    "printf",
    "identity",
    "endog",
    "instr",    
    NULL
};

const char *gretl_data_types[] = {
    "bool",
    "int",
    "scalar",
    "series",
    "matrix",
    "list",
    "string",
    "bundle",
    "strings",
    "matrices",
    "bundles",
    "lists",
    "null",
    "void",
    "const",
    NULL
};

const char *builtin_strings[] = {
    "gretldir",
    "dotdir",
    "workdir",
    "gnuplot",
    "x12a",
    "x12adir",
    "tramo",
    "tramodir",
    "seats",
    "pkgdir",
    "lang"
};

/* Similar to strcmp, except that in case two strings match
   to the length of the shorter one, we order the longer
   one first. This is needed to avoid the shorter string
   "masking" the longer one when doing syntax highlighting.
*/

static int mklang_strcmp (const void *a, const void *b)
{
    const char **sa = (const char **) a;
    const char **sb = (const char **) b;
    int na = strlen(*sa);
    int nb = strlen(*sb);
    int n = (na > nb) ? nb : na;
    int cmp = strncmp(*sa, *sb, n);

    if (cmp == 0) {
	cmp = nb - na;
    }

    return cmp;
}

char **make_var_name_list (int *pn)
{
    char **S;
    const char *s;
    int n1, n2, n3, n4;
    int n, m;
    int i;

    n1 = model_var_count();
    n2 = data_var_count();
    n3 = bundle_var_count();
    n4 = gretl_const_count();
    n = n1 + n2 + n3 + n4;

    S = strings_array_new(n);
    if (S == NULL) {
	return NULL;
    }

    m = 0;

    for (i=0; i<n1; i++) {
	s = model_var_name(i);
	if (s == NULL) {
	    continue;
	}
	if (*s == '$') s++;
	S[m++] = gretl_strdup(s);
    }

    for (i=0; i<n2; i++) {
	s = data_var_name(i);
	if (s == NULL) {
	    continue;
	}
	if (*s == '$') s++;
	S[m++] = gretl_strdup(s);
    }

    for (i=0; i<n3; i++) {
	s = bundle_var_name(i);
	if (s == NULL) {
	    continue;
	}
	if (*s == '$') s++;
	S[m++] = gretl_strdup(s);
    }    

    for (i=0; i<n4; i++) {
	s = gretl_const_name(i);
	if (s == NULL) {
	    continue;
	}
	if (*s == '$') s++;
	S[m++] = gretl_strdup(s);
    }    

    qsort(S, m, sizeof *S, mklang_strcmp);

    *pn = n;

    return S;
}

void output_emacs_block (void)
{
    char **strs;
    int nopts;
    int i, n;

    n = 1;
    fputs("(defvar gretl-command-words\n '(", stdout);
    for (i=1; i<NC; i++) {
	printf("\"%s\"", gretl_command_word(i));
	if (n % 8 == 0) {
	    fputs("\n   ", stdout);
	} else {
	    putchar(' ');
	}
	n++;
    } 
    for (i=0; special_keyword[i] != NULL; i++) {
	printf("\"%s\"", special_keyword[i]);
	if (special_keyword[i+1] != NULL) {
	    if (n % 8 == 0) {
		fputs("\n   ", stdout);
	    } else {
		putchar(' ');
	    }
	}
	n++;
    }	
    puts(")\n  \"Commands in gretl.\")\n");

    fputs("(defvar gretl-genr-functions\n '(", stdout);
    n = gen_func_count();
    for (i=0; i<n; i++) {
	printf("\"%s\"", gen_func_name(i));
	if (i < n-1) {
	    if ((i+1) % 8 == 0) {
		fputs("\n   ", stdout);
	    } else {
		putchar(' ');
	    }
	}
    }

    puts(")\n  \"Built-in functions in gretl.\")\n");

    strs = get_all_option_strings(&nopts);
    if (strs != NULL) {
	n = 1;
	fputs("(defvar gretl-option-flags\n '(", stdout);
	for (i=1; i<nopts; i++) {
	    printf("\"%s\"", strs[i]);
	    if (i < nopts-1) {
		if (n % 4 == 0) {
		    fputs("\n   ", stdout);
		} else {
		    putchar(' ');
		}
	    }
	    n++;
	}
	puts(")\n  \"Gretl option flags.\")\n");
	strings_array_free(strs, nopts);
    }

    /* "dollar" variables */
    fputs("(defvar gretl-internal-vars\n '(", stdout);

    strs = make_var_name_list(&n);
    if (strs != NULL) {
	for (i=0; i<n; i++) {
	    printf("\"%s\"", strs[i]);
	    if ((i+1) % 7 == 0) {
		fputs("\n   ", stdout);
	    } else {
		putchar(' ');
	    }	    
	}
	strings_array_free(strs, n);
    }

    puts(")\n  \"Model- and dataset-related variables.\")\n");    
}

static int compare_options (const void *a, const void *b)
{
    const char *sa = *(const char **) a;
    const char *sb = *(const char **) b;
    int ret = strcmp(sa, sb);

    return ret == 0 ? ret : -ret;
}

#define TRY_FOREIGN 1

#if TRY_FOREIGN

static void output_octave_specials (void)
{
    puts("    <context id=\"octave-keyword\" style-ref=\"command\">");
    puts("      <keyword>assert</keyword>");
    puts("      <keyword>break</keyword>");
    puts("      <keyword>case</keyword>");
    puts("      <keyword>catch</keyword>");
    puts("      <keyword>continue</keyword>");
    puts("      <keyword>do</keyword>");
    puts("      <keyword>elseif</keyword>");
    puts("      <keyword>else</keyword>");
    puts("      <keyword>endfor</keyword>");
    puts("      <keyword>endfunction</keyword>");
    puts("      <keyword>endif</keyword>");
    /* here's the required modification */
    puts("      <keyword>end(?! foreign)</keyword>");
    puts("      <keyword>endswitch</keyword>");
    puts("      <keyword>end_try_catch</keyword>");
    puts("      <keyword>end_unwind_protect</keyword>");
    puts("      <keyword>endwhile</keyword>");
    puts("      <keyword>for</keyword>");
    puts("      <keyword>function</keyword>");
    puts("      <keyword>global</keyword>");
    puts("      <keyword>if</keyword>");
    puts("      <keyword>nargin</keyword>");
    puts("      <keyword>nargout</keyword>");
    puts("      <keyword>otherwise</keyword>");
    puts("      <keyword>return</keyword>");
    puts("      <keyword>switch</keyword>");
    puts("      <keyword>try</keyword>");
    puts("      <keyword>until</keyword>");
    puts("      <keyword>unwind_protect_cleanup</keyword>");
    puts("      <keyword>unwind_protect</keyword>");
    puts("      <keyword>while</keyword>");
    puts("    </context>\n");

    puts("    <context id=\"gretl-octave\">");
    puts("      <include>");
    puts("        <context ref=\"octave:line-comment\"/>");
    puts("        <context ref=\"c:string\"/>");
    puts("        <context ref=\"octave:boolean\"/>");
    puts("        <context ref=\"octave:reserved-constant\"/>");
    /* note that here we use our modified keywords list */
    puts("        <context ref=\"octave-keyword\"/>");
    puts("        <context ref=\"def:decimal\"/>");
    puts("        <context ref=\"def:float\"/>");
    puts("        <context ref=\"def:hexadecimal\"/>");
    puts("      </include>");
    puts("    </context>\n");   
}

static void output_foreign_context (const char *id, const char *ctxt)
{
    printf("    <context id=\"foreign-%s\" style-inside=\"true\">\n", id);
    printf("      <start>^\\s*(foreign)\\s+language=\\%%{%s}\\%%{foreign-opt}</start>\n", id);
    puts("      <end>^\\s*end\\s+foreign</end>");
    puts("      <include>");
    puts("        <context sub-pattern=\"1\" where=\"start\" style-ref=\"command\"/>");
    puts("        <context sub-pattern=\"2\" where=\"start\" style-ref=\"option\"/>");
    puts("        <context sub-pattern=\"3\" where=\"start\" style-ref=\"option\"/>");
    puts("        <context sub-pattern=\"0\" where=\"end\" style-ref=\"command\"/>");
    printf("        <context ref=\"%s\"/>\n", ctxt);
    puts("      </include>");
    puts("    </context>\n");
}

#endif

void output_lang2_file (void)
{
    char **strs;
    int nopts;
    int i, n;

    puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    puts("<language id=\"gretl\" _name=\"gretl\" version=\"2.0\" _section=\"Scientific\">");
    puts("  <metadata>");
    puts("    <property name=\"mimetypes\">application/x-gretlscript</property>");
    puts("    <property name=\"globs\">*.inp</property>");
    puts("    <property name=\"line-comment-start\">#</property>");
    puts("  </metadata>\n");

    puts("  <styles>");
    puts("    <style id=\"comment\" _name=\"Comment\" map-to=\"def:comment\"/>");
    puts("    <style id=\"function\" _name=\"Function\" map-to=\"def:function\"/>");
    puts("    <style id=\"data-type\" _name=\"Data Type\" map-to=\"def:type\"/>");
    puts("    <style id=\"command\" _name=\"Command\" map-to=\"def:keyword\"/>");
    puts("    <style id=\"option\" _name=\"Option\" map-to=\"def:type\"/>");
    puts("  </styles>\n");

    puts("  <definitions>\n");

#if TRY_FOREIGN
    puts("    <define-regex id=\"R\">R|r</define-regex>");
    puts("    <define-regex id=\"stata\">Stata|stata|STATA</define-regex>");
    puts("    <define-regex id=\"python\">Python|python</define-regex>");
    puts("    <define-regex id=\"ox\">Ox|ox</define-regex>");
    puts("    <define-regex id=\"octave\">Octave|octave</define-regex>");
    puts("    <define-regex id=\"foreign-opt\">(\\s+--(?:quiet|send-data))?"
	 "(\\s+--(?:send-data|quiet))?</define-regex>\n");

    output_foreign_context("R", "r:r");
    output_foreign_context("stata", "stata:stata");
    output_foreign_context("python", "python:python");
    output_foreign_context("ox", "cpp:cpp");
    output_foreign_context("octave", "gretl-octave");
#endif    
    
    puts("    <context id=\"block-comment\" style-ref=\"comment\">");
    puts("      <start>/\\*</start>");
    puts("      <end>\\*/</end>");
    puts("      <include>");
    puts("        <context ref=\"def:escape\"/>");
    puts("        <context ref=\"def:line-continue\"/>");
    puts("      </include>");
    puts("    </context>\n");
    
    /* gretl data types */
    puts("    <context id=\"gretl-types\" style-ref=\"data-type\">");
    for (i=0; gretl_data_types[i] != NULL; i++) {
	printf("      <keyword>%s</keyword>\n", gretl_data_types[i]);  
    }
    puts("    </context>\n");

    /* gretl functions */
    puts("    <context id=\"genr-functions\" style-ref=\"function\">");
    n = gen_func_count();
    for (i=0; i<n; i++) {
	printf("      <keyword>%s</keyword>\n", gen_func_name(i));
    }
    printf("      <keyword>catch</keyword>\n");
    puts("    </context>\n");

    /* gretl commands */
    puts("    <context id=\"commands\" style-ref=\"command\">");
    puts("      <prefix>(^|\\040|\\011)</prefix>");
    puts("      <suffix>(?![\\w\\-\\.\\(])</suffix>");
    for (i=1; i<NC; i++) {
	printf("      <keyword>%s</keyword>\n", gretl_command_word(i));
    }
    /* plus a few specials */
    for (i=0; special_keyword[i] != NULL; i++) {
	printf("      <keyword>%s</keyword>\n", special_keyword[i]);
    }
    puts("    </context>\n");

    /* command option strings */
    strs = get_all_option_strings(&nopts);
    qsort(strs, nopts, sizeof *strs, compare_options);
    if (strs != NULL) {
	puts("    <context id=\"options\" style-ref=\"option\">");
	puts("      <prefix>--</prefix>");
	for (i=1; i<nopts; i++) {
	    printf("      <keyword>%s</keyword>\n", strs[i]);
	}    
	puts("    </context>\n");
	strings_array_free(strs, nopts);
    }

    /* dollar variables */
    puts("    <context id=\"internalvars\" style-ref=\"data-type\">");
    puts("      <prefix>\\$</prefix>");
    puts("      <suffix></suffix>");
    strs = make_var_name_list(&n);
    if (strs != NULL) {
	for (i=0; i<n; i++) {
	   printf("      <keyword>%s</keyword>\n", strs[i]);
	}
	strings_array_free(strs, n);
    }
    strs = (char **) builtin_strings;
    for (i=0; strs[i] != NULL; i++) {
	printf("      <keyword>%s</keyword>\n", strs[i]);
    }
    puts("    </context>\n");

#if TRY_FOREIGN
    /* octave special: prevent octave from eating "end foreign" */
    output_octave_specials();
#endif    
    
    puts("    <context id=\"gretl\">");
    puts("      <include>");
    puts("        <context ref=\"def:shell-like-comment\"/>");
    puts("        <context ref=\"def:string\"/>");
    puts("        <context ref=\"block-comment\"/>");
#if TRY_FOREIGN
    puts("        <context ref=\"foreign-R\"/>");
    puts("        <context ref=\"foreign-stata\"/>");
    puts("        <context ref=\"foreign-python\"/>");
    puts("        <context ref=\"foreign-ox\"/>");
    puts("        <context ref=\"foreign-octave\"/>");
#endif    
    puts("        <context ref=\"gretl-types\"/>");
    puts("        <context ref=\"commands\"/>");
    puts("        <context ref=\"genr-functions\"/>");
    puts("        <context ref=\"options\"/>");
    puts("        <context ref=\"internalvars\"/>");
    puts("      </include>");
    puts("    </context>\n");
    puts("  </definitions>");

    puts("</language>");
}

int main (int argc, char **argv)
{
    if (argc == 2 && !strcmp(argv[1], "--emacs")) {
	output_emacs_block();
    } else {
	output_lang2_file();
    }

    return 0;
}