summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/util/levcomp.ypp
blob: 757fc025047d1d503c4285ff6069b3a84d39d778 (plain) (blame)
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
%{

#include "AppHdr.h"
#include "clua.h"
#include "libutil.h"
#include "levcomp.h"
#include "luadgn.h"
#include "mapdef.h"
#include "stuff.h"
#include <map>

#define YYERROR_VERBOSE 1

int yylex();

extern int yylineno;

struct map_file_place
{
    std::string filename;
    int lineno;

    map_file_place(const std::string &s = "", int line = 0)
        : filename(s), lineno(line)
    {
    }
};

typedef std::map<std::string, map_file_place> map_load_info_t;

static map_load_info_t loaded_maps;

void yyerror(const char *e)
{
    fprintf(stderr, "%s:%d: %s\n", lc_desfile.c_str(), yylineno, e);
    // Bail bail bail.
    end(1);
}

level_range set_range(const char *s, int start, int end)
{
    try
    {
        lc_range.set(s, start, end);
    }
    catch (const std::string &err)
    {
        yyerror(err.c_str());
    }
    return (lc_range);
}

%}

%union
{
    int i;
    const char *text;
    raw_range range;
}

%token <i>      BRANCHDEF BRANCH DESC DEFAULT
%token <i>      DEFAULT_DEPTH SHUFFLE SUBST TAGS KFEAT KITEM KMONS
%token <i>      NAME DEPTH ORIENT PLACE CHANCE MONS ITEM
%token <i>      PRELUDE MAIN

%token <i>      CHARACTER

%token <i>      DASH COMMA QUOTE OPAREN CPAREN
%token <i>      INTEGER

%token <text>   STRING MAP_LINE MONSTER_NAME ITEM_INFO
%token <text>   IDENTIFIER LUA_LINE

%%

file            : definitions { }
                ;

definitions     : /* empty */               {}
                | definitions definition    {}
                ;

definition      : def       {}
                | level     {}
                ;

def             : defdepth  {}
                ;

defdepth        : DEFAULT_DEPTH STRING
                {
                    dgn_reset_default_depth();
                    std::string err = dgn_set_default_depth($2);
                    if (!err.empty())
                        yyerror(make_stringf("Bad default-depth: %s (%s)",
                                $2, err.c_str()).c_str());
                }
                ;

level           : name metalines map_def metalines
                {
                    std::string err = lc_map.validate();
                    if (!err.empty())
                        yyerror(err.c_str());
                    if (!lc_map.has_depth() && !lc_default_depths.empty())
                        lc_map.add_depths(lc_default_depths.begin(),
                                          lc_default_depths.end());
                    add_parsed_map( lc_map );
                }
                ;

name            : NAME STRING
                {
                    lc_map.init();
                    lc_map.name = $2;

                    map_load_info_t::const_iterator i = 
                        loaded_maps.find($2);

                    if (i != loaded_maps.end())
                    {
                        yyerror(
                            make_stringf(
                                "Map named '%s' already loaded at %s:%d",
                                $2,
                                i->second.filename.c_str(),
                                i->second.lineno).c_str() );
                    }

                    loaded_maps[$2] = map_file_place(lc_desfile, yylineno);
                }
                ;

metalines       : /* no metadata */
                | metaline metalines
                ;

metaline        : place
                | depth
                | chance
                | orientation
                | mons
                | items
                | subst
                | tags
                | shuffle
                | kfeat
                | kitem
                | kmons
                | main_lua
                | prelude_lua
                ;

main_lua        : MAIN main_lua_lines { }

main_lua_lines  : /* empty */ { }
                | main_lua_lines main_lua_line { }
                ;

main_lua_line   : LUA_LINE
                {
                    lc_map.main.add(yylineno, $1);
                }

prelude_lua     : PRELUDE prelude_lua_lines { }

prelude_lua_lines : /* empty */ { }
                | prelude_lua_lines prelude_lua_line { }
                ;

prelude_lua_line : LUA_LINE
                {
                    lc_map.prelude.add(yylineno, $1);
                }

kfeat           : KFEAT { }
                | KFEAT STRING
                {
                    lc_map.main.add(
                        yylineno, 
                        make_stringf("kfeat(\"%s\")", 
                            quote_lua_string($2).c_str()));
                }

kmons           : KMONS { }
                | KMONS STRING
                {
                    lc_map.main.add(
                        yylineno, 
                        make_stringf("kmons(\"%s\")", 
                            quote_lua_string($2).c_str()));
                }

kitem           : KITEM { }
                | KITEM STRING
                {
                    lc_map.main.add(
                        yylineno, 
                        make_stringf("kitem(\"%s\")", 
                            quote_lua_string($2).c_str()));
                }

shuffle         : SHUFFLE shuffle_specifiers {} 
                ;

shuffle_specifiers : shuffle_spec
                   | shuffle_specifiers COMMA shuffle_spec
                   ;

shuffle_spec    : ITEM_INFO
                {
                    lc_map.main.add(
                        yylineno, 
                        make_stringf("shuffle(\"%s\")", 
                            quote_lua_string($1).c_str()));
                }

tags            : TAGS tagstrings {}
                ;

tagstrings      : /* empty */
                | tagstrings tagstring
                ;

tagstring       : STRING
                {
                    lc_map.main.add(
                        yylineno, 
                        make_stringf("tags(\"%s\")", 
                            quote_lua_string($1).c_str()));
                }
                ;

subst           : SUBST subst_specifiers { }
                ;

subst_specifiers : subst_spec
                 | subst_spec COMMA subst_specifiers
                 ;

subst_spec      : ITEM_INFO
                {
                    lc_map.main.add(
                        yylineno, 
                        make_stringf("subst(\"%s\")", 
                            quote_lua_string($1).c_str()));
                }
                ;

items           : ITEM {}
                | ITEM item_specifiers {}
                ;

item_specifiers : item_specifier COMMA item_specifiers
                | item_specifier
                ;

item_specifier  : ITEM_INFO
                {
                    lc_map.main.add(
                        yylineno, 
                        make_stringf("item(\"%s\")", 
                            quote_lua_string($1).c_str()));
                }
                
mons            : MONS {}
                | MONS mnames {}
                ;

mnames          : mname COMMA mnames
                | mname
                ;

mname           : MONSTER_NAME
                {
                    lc_map.main.add(
                        yylineno, 
                        make_stringf("mons(\"%s\")", 
                            quote_lua_string($1).c_str()));
                }
                ;

place           : PLACE STRING
                {
                    lc_map.main.add(
                        yylineno, 
                        make_stringf("place(\"%s\")", 
                            quote_lua_string($2).c_str()));
                }
                ;

depth           : DEPTH {}
                | DEPTH STRING 
                {
                    lc_map.main.add(
                        yylineno, 
                        make_stringf("depth(\"%s\")", 
                            quote_lua_string($2).c_str()));
                }
                ;

chance          : CHANCE INTEGER
                {
                    lc_map.main.add(
                        yylineno, 
                        make_stringf("chance(\"%d\")", $2));
                }
                ;

orientation     : ORIENT {}
                | ORIENT STRING
                {
                    lc_map.main.add(
                        yylineno, 
                        make_stringf("orient(\"%s\")", 
                                     quote_lua_string($2).c_str()));
                }
                ;

map_def         : map_lines
                ;

map_lines       : map_line
                | map_line map_lines
                ;

map_line        : MAP_LINE
                {
                    lc_map.main.add(
                        yylineno, 
                        make_stringf("map(\"%s\")", 
                                     quote_lua_string($1).c_str()));
                }
                ;

%%