summaryrefslogtreecommitdiff
path: root/agen5/defLex.c
blob: 39c5a25280ce3db17e608bbe92c242b7d8a09080 (plain)
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678

/**
 * @file defLex.c
 *
 *  Time-stamp:        "2012-03-31 13:00:08 bkorb"
 *
 *  This module scans the template variable declarations and passes
 *  tokens back to the parser.
 *
 *  This file is part of AutoGen.
 *  AutoGen Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
 *
 * AutoGen is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * AutoGen is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 *  This keyword table must match those found in agParse.y.
 *  You will find them in a %token statement that follows
 *  a comment  "Keywords"
 */
#define KEYWORD_TABLE                           \
  _KW_( AUTOGEN )                               \
  _KW_( DEFINITIONS )

#define _KW_(w) static char const z ## w [] = #w;
KEYWORD_TABLE
#undef _KW_

#define _KW_(w) z ## w,
static char const*  apzKeywords[] = { KEYWORD_TABLE };
#undef _KW_

#define _KW_(w) DP_EV_ ## w,
te_dp_event aKeywordTkn[] = { KEYWORD_TABLE };
#undef _KW_

#define KEYWORD_CT  (sizeof(apzKeywords) / sizeof(apzKeywords[0]))

#define ERROR  (-1)
#define FINISH (-1)

/* = = = START-STATIC-FORWARD = = = */
static void
pop_context(void);

static void
trim_whitespace(void);

static void
lex_escaped_char(void);

static tSuccess
lex_backquote(void);

static tSuccess
lex_comment(void);

static tSuccess
lex_here_string(void);

static void
loadScheme(void);

static void
alist_to_autogen_def(void);

static char*
gather_name(char * pzScan, te_dp_event * pRetVal);

static char*
build_here_str(char* scan);
/* = = = END-STATIC-FORWARD = = = */

/**
 *  Pop off an include context and resume from the including file.
 */
static void
pop_context(void)
{
    scan_ctx_t* pCX = cctx;
    cctx          = cctx->scx_next;
    pCX->scx_next = end_ctx;
    end_ctx       = pCX;
}

static void
trim_whitespace(void)
{
    char * pz = cctx->scx_scan;

    /*
     *  This ensures that any names found previously
     *  are NUL terminated.
     */
    if (*pz == NL)
        cctx->scx_line++;
    *pz = NUL;

    for (;;) {
        pz = SPN_HORIZ_WHITE_CHARS(pz+1);
        if (*pz != NL)
            break;
        cctx->scx_line++;
    }

    cctx->scx_scan = pz;
}

static void
lex_escaped_char(void)
{
    static int const semi_colon = ';';

    char* pz = strchr(cctx->scx_scan, semi_colon);

    for (;;) {
        if (pz == NULL) {
            pz = cctx->scx_scan + strlen(cctx->scx_scan);
            break;
        }
        if (IS_WHITESPACE_CHAR(pz[1])) {
            *pz = NUL;
            pz[1] = semi_colon;
            break;
        }
        pz = strchr(pz+1, semi_colon);
    }

    token_code = DP_EV_STRING;
    token_str = pz;
}

static tSuccess
lex_backquote(void)
{
    int line_no = cctx->scx_line;
    char* pz = ao_string_cook(cctx->scx_scan, &line_no);

    if (pz == NULL)
        return FAILURE;

    token_str = cctx->scx_scan;

    cctx->scx_scan = pz;

    token_code = DP_EV_STRING;
    pz = shell_cmd((char const*)token_str);
    cctx->scx_line = line_no;

    if (pz == NULL)
        return PROBLEM;

    token_str = pz;
    return SUCCESS;
}

static tSuccess
lex_comment(void)
{
    /*
     *  Allow for a comment, C or C++ style
     */
    switch (cctx->scx_scan[1]) {
    case '*':
    {
        char* pz = strstr(cctx->scx_scan+2, END_C_COMMENT);
        if (pz != NULL) {
            char* p = cctx->scx_scan+1;
            for (;;) {
                p = strchr(p+1, NL);
                if ((p == NULL) || (p > pz))
                    break;
                cctx->scx_line++;
            }
            cctx->scx_scan = pz+2;
            return SUCCESS;
        }
        break;
    }
    case '/':
    {
        char* pz = strchr(cctx->scx_scan+2, NL);
        if (pz != NULL) {
            cctx->scx_scan = pz+1;
            cctx->scx_line++;
            return SUCCESS;
        }
        break;
    }
    }

    return FAILURE;
}

static tSuccess
lex_here_string(void)
{
    char* pz;
    if (cctx->scx_scan[1] != '<')
        return FAILURE;

    pz = build_here_str(cctx->scx_scan + 2);
    if (pz == NULL) {
        token_code = DP_EV_INVALID;
        return PROBLEM;
    }

    token_code = DP_EV_HERE_STRING;
    cctx->scx_scan = pz;
    return SUCCESS;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *   LEXICAL SCANNER
 */
LOCAL te_dp_event
yylex(void)
{
#define SET_LIT_TKN(t) \
    token_code = DP_EV_LIT_ ## t; *(cctx->scx_scan++) = NUL;

    token_code = DP_EV_INVALID;

scanAgain:
    /*
     *  Start the process of locating a token.
     *  We branch here after skipping over a comment
     *  or processing a directive (which may change our context).
     */
    if (IS_WHITESPACE_CHAR(*cctx->scx_scan))
        trim_whitespace();

    switch (*cctx->scx_scan) {
    case NUL:
        /*
         *  IF we are not inside an include context,
         *  THEN go finish.
         */
        if (cctx->scx_next == NULL)
            goto lex_done;

        pop_context();
        goto scanAgain;

    case '#':
    {
        extern char * processDirective(char*);
        char * pz = processDirective(cctx->scx_scan+1);
        /*
         *  Ensure that the compiler doesn't try to save a copy of
         *  "cctx" in a register.  It must be reloaded from memory.
         */
        cctx->scx_scan = pz;
        goto scanAgain;
    }

    case '{': SET_LIT_TKN(O_BRACE);   break;
    case '=': SET_LIT_TKN(EQ);        break;
    case '}': SET_LIT_TKN(C_BRACE);   break;
    case '[': SET_LIT_TKN(OPEN_BKT);  break;
    case ']': SET_LIT_TKN(CLOSE_BKT); break;
    case ';': SET_LIT_TKN(SEMI);      break;
    case ',': SET_LIT_TKN(COMMA);     break;

    case '\'':
    case '"':
    {
        char* pz = ao_string_cook(cctx->scx_scan, &(cctx->scx_line));
        if (pz == NULL)
            goto NUL_error;

        token_str = cctx->scx_scan;

        token_code = DP_EV_STRING;
        cctx->scx_scan = pz;
        break;
    }

    case '<':
        switch (lex_here_string()) {
        case SUCCESS: break;
        case FAILURE: goto BrokenToken;
        case PROBLEM: return DP_EV_INVALID;
        }
        break;

    case '(':
        loadScheme();
        break;

    case '\\':
        if (strncmp(cctx->scx_scan+1, START_SCHEME_LIST, (size_t)2) == 0) {
            alist_to_autogen_def();
            goto scanAgain;
        }
        lex_escaped_char();
        break;

    case '`':
        switch (lex_backquote()) {
        case FAILURE: goto NUL_error;
        case PROBLEM: goto scanAgain;
        case SUCCESS: break;
        }
        break;

    case '/':
        switch (lex_comment()) {
        case SUCCESS: goto scanAgain;
        default: break;
        }
        /* FALLTHROUGH */ /* to Invalid input char */

    default:
    BrokenToken:
        cctx->scx_scan = gather_name(cctx->scx_scan, &token_code);
        break;
    }   /* switch (*cctx->scx_scan) */

    return token_code;

NUL_error:

    AG_ABEND(aprf(DEF_ERR_FMT, ag_pname, YYLEX_UNENDING_QUOTE,
                  cctx->scx_fname, cctx->scx_line));
    return DP_EV_INVALID;

lex_done:
    /*
     *  First time through, return the DP_EV_END token.
     *  Second time through, we really finish.
     */
    if (cctx->scx_scan == zNil) {
        cctx->scx_next = end_ctx;
        end_ctx        = cctx;

        return DP_EV_INVALID;
    }

    cctx->scx_scan = (char*)zNil;
    return DP_EV_END;
#undef SET_LIT_TKN
}


LOCAL void
yyerror(char* s)
{
    char * pz;

    if (strlen(cctx->scx_scan) > 64 )
        cctx->scx_scan[64] = NUL;

    switch (token_code) {
    case DP_EV_VAR_NAME:
    case DP_EV_OTHER_NAME:
    case DP_EV_STRING:
    case DP_EV_NUMBER:
        if (strlen(token_str) > 64 )
            token_str[64] = NUL;

        pz = aprf(YYLEX_TOKEN_STR, DP_EVT_NAME(token_code), token_str);
        break;

    default:
        pz = aprf(YYLEX_DF_STR, DP_EVT_NAME(token_code));
    }
    AG_ABEND(aprf(YYLEX_ERR_FMT, s, cctx->scx_fname, cctx->scx_line,
                  pz, cctx->scx_scan));
}


static void
loadScheme(void)
{
    char*    pzText    = cctx->scx_scan;
    char*    pzEnd     = (char*)skip_scheme(pzText, pzText + strlen(pzText));
    char     endCh     = *pzEnd;
    int      schemeLen = (pzEnd - pzText);
    int      next_ln;
    SCM      res;

    /*
     *  NUL terminate the Scheme expression, run it, then restore
     *  the NUL-ed character.
     */
    if (*pzEnd == NUL)
        AG_ABEND(aprf(DEF_ERR_FMT, ag_pname, LOAD_SCM_ENDLESS,
                      cctx->scx_fname, cctx->scx_line));

    *pzEnd  = NUL;
    next_ln = cctx->scx_line + count_nl(pzText);

    processing_state = PROC_STATE_GUILE_PRELOAD;
    res = ag_scm_c_eval_string_from_file_line(
        pzText, cctx->scx_fname, cctx->scx_line );
    processing_state = PROC_STATE_LOAD_DEFS;
    *pzEnd = endCh;

    cctx->scx_scan = pzEnd;
    pzEnd = (char*)scm2display(res); /* ignore const-ness */
    cctx->scx_line = next_ln;

    if ((int)strlen(pzEnd) >= schemeLen) {
        AGDUPSTR(pzEnd, pzEnd, "SCM Result");

        token_str = pzEnd;
    }

    else {
        /*
         *  We know the result is smaller than the source.  Copy in place.
         */
        strcpy(pzText, pzEnd);
        token_str = pzText;
    }

    token_code = DP_EV_STRING;
}

/*
 *  process a single scheme expression, yielding text that gets processed
 *  into AutoGen definitions.
 */
static void
alist_to_autogen_def(void)
{
    char*  pzText  = ++(cctx->scx_scan);
    char*  pzEnd   = (char*)skip_scheme(pzText, pzText + strlen(pzText));

    SCM    res;
    size_t res_len;
    scan_ctx_t*  pCtx;

    /*
     *  Wrap the scheme expression with the `alist->autogen-def' function
     */
    {
        char endCh = *pzEnd;
        *pzEnd = NUL;
        pzText = aprf(ALIST_TO_AG_WRAP, pzText);
        *pzEnd = endCh;
    }

    /*
     *  Run the scheme expression.  The result is autogen definition text.
     */
    processing_state = PROC_STATE_GUILE_PRELOAD;
    res = ag_scm_c_eval_string_from_file_line(
        pzText, cctx->scx_fname, cctx->scx_line );

    /*
     *  The result *must* be a string, or we choke.
     */
    if (! AG_SCM_STRING_P(res))
        AG_ABEND(ALIST_TO_AG_ERR);

    res_len   = AG_SCM_STRLEN(res);
    processing_state = PROC_STATE_LOAD_DEFS;
    cctx->scx_scan = pzEnd;
    AGFREE(pzText);

    /*
     *  Now, push the resulting string onto the input stack
     *  and link the new scan data into the context stack
     */
    pCtx = (scan_ctx_t*)AGALOC(sizeof(scan_ctx_t) + 4 + res_len, "lex ctx");
    pCtx->scx_next = cctx;
    cctx           = pCtx;

    /*
     *  Set up the rest of the context structure
     */
    AGDUPSTR(pCtx->scx_fname, ALIST_TO_AG_TEXT, "scheme text");
    pCtx->scx_scan = \
    pCtx->scx_data = (char*)(pCtx+1);
    pCtx->scx_line = 0;
    memcpy((void*)(pCtx->scx_scan), (void*)AG_SCM_CHARS(res), res_len);
    pCtx->scx_scan[ res_len ] = NUL;

    /*
     *  At this point, the next token will be obtained from the newly
     *  allocated context structure.  When empty, input will resume
     *  from the '}' that we left as the next input token in the old
     *  context.
     */
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *  It may be a number, a name, a keyword or garbage.
 *  Figure out which.
 */
static char*
gather_name(char * pzScan, te_dp_event * pRetVal)
{
    /*
     *  Check for a number.
     *  Scan it in and advance "pzScan".
     */
    if (  IS_DEC_DIGIT_CHAR(*pzScan)
       || (  (*pzScan == '-')
          && IS_DEC_DIGIT_CHAR(pzScan[1])
       )  )  {
        token_str = pzScan;
        (void)strtol(pzScan, &pzScan, 0);
        *pRetVal = DP_EV_NUMBER;
        return pzScan;
    }

    if (! IS_UNQUOTABLE_CHAR(*pzScan))
        AG_ABEND(aprf(ASSEMBLE_NAME_ERR, ag_pname, *pzScan, cctx->scx_fname,
                      cctx->scx_line));

    {
        char* pz = SPN_VALUE_NAME_CHARS(pzScan);

        if (IS_UNQUOTABLE_CHAR(*pz)) {
            *pRetVal = DP_EV_OTHER_NAME;
            pz = SPN_UNQUOTABLE_CHARS(pz+1);

        } else
            *pRetVal = DP_EV_VAR_NAME;

        /*
         *  Return a NAME token, maybe.
         *  If the name is actually a keyword,
         *  we will return that token code instead.
         */
        token_str = pzScan;
        pzScan   = (char*)pz;
    }

    /*
     *  Now scan the keyword table.
     */
    if (*pRetVal == DP_EV_VAR_NAME) {
        char sv_ch = *pzScan;  /* preserve the following character */
        int  kw_ix = 0;
        *pzScan = NUL;         /* NUL terminate the name           */

        do  {
            if (streqvcmp(apzKeywords[ kw_ix ], (char*)token_str) == 0) {
                /*
                 *  Return the keyword token code instead of DP_EV_NAME
                 */
                *pRetVal = aKeywordTkn[ kw_ix ];
                break;
            }
        } while (++kw_ix < (int)KEYWORD_CT);

        *pzScan = sv_ch;         /* restore the following character  */
    }

    return pzScan;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *  A quoted string has been found.
 *  Find the end of it and compress any escape sequences.
 */
static char*
build_here_str(char* scan)
{
    bool     no_tabs = false;
    char     zMark[ MAX_HEREMARK_LEN ];
    size_t   markLen = 0;
    char*    dest;
    int      here_str_line;

    /*
     *  See if we are to strip leading tab chars
     */
    if (*scan == '-') {
        no_tabs = true;
        scan++;
    }

    /*
     *  Skip white space up to the marker or EOL
     */
    scan = SPN_NON_NL_WHITE_CHARS(scan);
    if (*scan == NL)
        AG_ABEND(aprf(DEF_ERR_FMT, ag_pname, HERE_MISS_MARK_STR,
                      cctx->scx_fname, cctx->scx_line));

    /*
     *  Copy the marker, noting its length
     */
    {
        char * pz = SPN_VARIABLE_NAME_CHARS(scan);
        markLen = pz - scan;

        if (markLen == 0)
            AG_ABEND(aprf(DEF_ERR_FMT, ag_pname, HERE_MISS_MARK_STR,
                          cctx->scx_fname, cctx->scx_line));

        if (markLen >= sizeof(zMark))
            AG_ABEND(aprf(DEF_ERR_FMT, ag_pname, HERE_MARK_TOO_LONG,
                          cctx->scx_fname, cctx->scx_line));

        memcpy(zMark, scan, markLen);
        zMark[markLen] = NUL;
    }

    token_str = dest = scan;

    /*
     *  Skip forward to the EOL after the marker.
     */
    scan = strchr(scan, NL);
    if (scan == NULL)
        AG_ABEND(aprf(DEF_ERR_FMT, ag_pname, HERE_ENDLESS_STR,
                      cctx->scx_fname, cctx->scx_line));

    /*
     *  And skip the first new line + conditionally skip tabs
     */
    here_str_line = cctx->scx_line++;
    scan++;

    for (;;) {
    next_line:
        if (no_tabs) {
            while (*scan == TAB)  ++scan;
            if ((scan[0] == '\\') && (scan[1] == TAB))
                ++scan;
        }

        /*
         *  If we recognize the end mark, break out.
         */
        if (! IS_VARIABLE_NAME_CHAR(scan[markLen]))
            if (strncmp(scan, zMark, markLen) == 0)
                break;

        for (;;) {
            switch (*(dest++) = *(scan++)) {
            case NL:
                cctx->scx_line++;
                goto next_line;

            case NUL:
                AG_ABEND(aprf(DEF_ERR_FMT, ag_pname, HERE_ENDLESS_STR,
                              cctx->scx_fname, here_str_line));
            }
        }
    } /* while strncmp ... */

    /*
     *  dest may still equal token_str, if no data were copied
     */
    if (dest > (char*)token_str)
         dest[-1] = NUL;
    else dest[0]  = NUL;

    return scan + markLen;
}
/*
 * Local Variables:
 * mode: C
 * c-file-style: "stroustrup"
 * indent-tabs-mode: nil
 * End:
 * end of agen5/defLex.c */