summaryrefslogtreecommitdiff
path: root/agen5/defParse.x
blob: 8317960085b3ca775430ea075510943e276653d7 (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

/**
 * @file defParse.x
 *
 * Definition parser functions.
 *
 *  Time-stamp:        "2012-03-31 13:30:28 bkorb"
 *
 *  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/>.
 */

#ifdef FSM_USER_HEADERS
static char* pz_new_name = NULL;
#endif /* FSM_USER_HEADERS */

#ifdef FSM_FIND_TRANSITION
trans_evt = yylex();
#endif /* FSM_FIND_TRANSITION */

#ifdef FSM_HANDLER_CODE
/*
 *  Print out an invalid transition message and return EXIT_FAILURE
 */
static int
dp_invalid_transition(te_dp_state st, te_dp_event evt)
{
    char const * fmt_pz = zDpStrings + DpFsmErr_off;
    fprintf(stderr, fmt_pz, st, DP_STATE_NAME(st), evt, DP_EVT_NAME(evt));

    return EXIT_FAILURE;
}

static te_dp_state
dp_do_empty_val(te_dp_state initial, te_dp_state maybe_next,
                te_dp_event trans_evt)
{
    /*
     *  Our state is either "have-name" or "indx-name" and we found a ';',
     *  end of statement.  It is a string value with an empty string.
     */
    def_ent_t * pDE = number_and_insert_ent(pz_new_name, NULL);

    (void)initial; (void)trans_evt;

    pDE->de_val.dvu_text = (char *)zNil;
    pDE->de_type    = VALTYP_TEXT;
    if (OPT_VALUE_TRACE >= TRACE_EXPRESSIONS)
        print_ent(pDE);
    return maybe_next;
}

static te_dp_state
dp_do_end_block(te_dp_state initial, te_dp_state maybe_next,
                te_dp_event trans_evt)
{
    if (ent_stack_depth <= 0)
        yyerror((void*)"Too many close braces");

    (void)initial; (void)trans_evt;

    curr_ent = ent_stack[ ent_stack_depth-- ];
    return maybe_next;
}

static te_dp_state
dp_do_have_name_lit_eq(te_dp_state initial, te_dp_state maybe_next,
                       te_dp_event trans_evt)
{
    (void)initial; (void)trans_evt;
    /*
     *  Create a new entry but defer "makeMacro" call until we have the
     *  assigned value.
     */
    number_and_insert_ent(pz_new_name, NULL);
    return maybe_next;
}

static te_dp_state
dp_do_indexed_name(te_dp_state initial, te_dp_state maybe_next,
                   te_dp_event trans_evt)
{
    (void)initial; (void)trans_evt;
    /*
     *  Create a new entry with a specified indes, but defer "makeMacro" call
     *  until we have the assigned value.
     */
    number_and_insert_ent(pz_new_name, token_str);
    return maybe_next;
}

static te_dp_state
dp_do_invalid(te_dp_state initial, te_dp_state maybe_next,
              te_dp_event trans_evt)
{
    (void)maybe_next;
    dp_invalid_transition(initial, trans_evt);
    yyerror((void*)"invalid transition");
    /* NOTREACHED */
    return DP_ST_INVALID;
}

static te_dp_state
dp_do_need_name_end(te_dp_state initial, te_dp_state maybe_next,
                    te_dp_event trans_evt)
{
    (void)initial; (void)trans_evt;
    if (ent_stack_depth != 0)
        yyerror((void*)"definition blocks were left open");

    /*
     *  We won't be using the parse stack any more.
     *  We only process definitions once.
     */
    if (ent_stack != dft_ent_stack)
        AGFREE(ent_stack);

    /*
     *  The seed has now done its job.  The real root of the
     *  definitions is the first entry off of the seed.
     */
    root_def_ctx.dcx_defent = root_def_ctx.dcx_defent->de_val.dvu_entry;
    return maybe_next;
}

static te_dp_state
dp_do_need_name_var_name(te_dp_state initial, te_dp_state maybe_next,
                         te_dp_event trans_evt)
{
    (void)initial; (void)trans_evt;
    pz_new_name = token_str;
    return maybe_next;
}

static te_dp_state
dp_do_next_val(te_dp_state initial, te_dp_state maybe_next,
               te_dp_event trans_evt)
{
    (void)initial; (void)trans_evt;
    /*
     *  Clone the entry name of the current entry.
     */
    number_and_insert_ent(curr_ent->de_name, NULL);
    return maybe_next;
}

static te_dp_state
dp_do_start_block(te_dp_state initial, te_dp_state maybe_next,
                  te_dp_event trans_evt)
{
    (void)initial; (void)trans_evt;
    if (curr_ent->de_type == VALTYP_TEXT)
        yyerror((void*)"assigning a block value to text name");
    curr_ent->de_type = VALTYP_BLOCK; /* in case not yet determined */

    if (OPT_VALUE_TRACE >= TRACE_EXPRESSIONS)
        print_ent(curr_ent);

    if (++ent_stack_depth >= ent_stack_sz) {
        def_ent_t** ppDE;
        ent_stack_sz += ent_stack_sz / 2;

        if (ent_stack == dft_ent_stack) {
            ppDE = AGALOC(ent_stack_sz * sizeof(void*), "def stack");
            memcpy(ppDE, dft_ent_stack, sizeof(dft_ent_stack));
        } else {
            ppDE = AGREALOC(ent_stack, ent_stack_sz * sizeof(void*),
                            "stretch def stack");
        }
        ent_stack = ppDE;
    }
    ent_stack[ ent_stack_depth ] = curr_ent;
    curr_ent = NULL;
    return maybe_next;
}

static te_dp_state
dp_do_str_value(te_dp_state initial, te_dp_state maybe_next,
                te_dp_event trans_evt)
{
    (void)initial; (void)trans_evt;
    if (curr_ent->de_type == VALTYP_BLOCK)
        yyerror((void*)"assigning a block value to text name");

    curr_ent->de_val.dvu_text = token_str;
    curr_ent->de_type = VALTYP_TEXT;

    if (OPT_VALUE_TRACE >= TRACE_EXPRESSIONS)
        print_ent(curr_ent);

    /*
     *  The "here string" marker is the line before where the text starts.
     */
    if (trans_evt == DP_EV_HERE_STRING)
        curr_ent->de_line++;
    return maybe_next;
}

static te_dp_state
dp_do_tpl_name(te_dp_state initial, te_dp_state maybe_next,
               te_dp_event trans_evt)
{
    (void)initial; (void)trans_evt;
    /*
     *  Allow this routine to be called multiple times.
     *  This may happen if we include another definition file.
     */
    if (root_def_ctx.dcx_defent == NULL) {
        static char const zBogus[] = "@BOGUS@";
        static def_ent_t  seed = {
            NULL, NULL, NULL, NULL, (char*)zBogus, 0, { NULL },
            (char*)zBogus, 0, VALTYP_BLOCK };

        root_def_ctx.dcx_defent = &seed;

        if (! HAVE_OPT(OVERRIDE_TPL))
             tpl_fname = token_str;

        ent_stack_depth = 0;
        ent_stack[0] = &seed;
    }
    return maybe_next;
}
#endif /* FSM_HANDLER_CODE */

/*
 * Local Variables:
 * mode: C
 * c-file-style: "stroustrup"
 * indent-tabs-mode: nil
 * End:
 * end of agen5/defParse.x */