Fix typo
[rofl0r-order-pp.git] / example / lambda / brd_parser.h
blobe4de6ca19e1e4b03cb2d22190276e095b1b97106
1 #ifndef ORDER_EXAMPLE_LAMBDA_BRD_PARSER_H_VAJK20040620
2 #define ORDER_EXAMPLE_LAMBDA_BRD_PARSER_H_VAJK20040620
4 // (C) Copyright Vesa Karvonen 2004.
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE.)
9 #include "error.h"
10 #include "str.h"
11 #include "order/interpreter.h"
13 #define ORDER_PP_DEF_8bp_import_terminals ORDER_PP_MACRO(8vseq_to_seq_of_tuples)
15 #define ORDER_PP_DEF_8bp_import_nonterminals \
16 ORDER_PP_FN(8fn(8S, \
17 8seq_map(8fn(8N, \
18 8tuple(8tuple_at_0(8N), \
19 8tuple_at_1(8N), \
20 8seq_map(8fn(8P, \
21 8tuple_append(8tuple(8vseq_to_seq_of_tuples(8tuple_at_0(8P))), \
22 8tuple_drop_1(8P))), \
23 8vseq_to_seq_of_tuples(8tuple_at_2(8N))))), \
24 8vseq_to_seq_of_tuples(8S))))
26 #define BRD_PARSER(qr, name, skip_fn, match_fn, terminals, nonterminals) \
27 ORDER_PP(8seq_emit_map \
28 (8(BRD_PARSER_GEN_sym_typedef), \
29 8step(8tuple(8tuple_at_0, \
30 8tuple_at_1)), \
31 8seq_append \
32 (8bp_import_terminals(8(terminals)), \
33 8bp_import_nonterminals(8(nonterminals))))) \
35 ORDER_PP(8let((8S, 8bp_import_nonterminals(8(nonterminals))), \
36 8seq_emit_map(8(BRD_PARSER_GEN_prototype), \
37 8step(8tuple(8tuple_at_0)), \
38 8S), \
39 8emit(8(BRD_PARSER_GEN_entry_point), \
40 8tuple(8(qr), \
41 8(name), \
42 8tuple_at_0(8seq_at(0,8S)))))) \
44 inline void \
45 ORDER_PP_FRESH_ID(skip)(str_type *ORDER_PP_FRESH_ID(pstr)) { \
46 skip_fn(ORDER_PP_FRESH_ID(pstr)); \
47 } \
49 ORDER_PP(8seq_emit_map(8(BRD_PARSER_GEN_terminal), \
50 8step(8tuple(8tuple_at_0, \
51 8tuple_at_2)), \
52 8bp_import_terminals(8(terminals)))) \
54 inline _Bool \
55 ORDER_PP_FRESH_ID(match)(str_type *ORDER_PP_FRESH_ID(pstr), \
56 str_type ORDER_PP_FRESH_ID(maybe_prefix)) { \
57 ORDER_PP_FRESH_ID(skip)(ORDER_PP_FRESH_ID(pstr)); \
59 return match_fn(ORDER_PP_FRESH_ID(pstr), \
60 ORDER_PP_FRESH_ID(maybe_prefix)); \
61 } \
63 ORDER_PP(8seq_emit_map(8(BRD_PARSER_GEN_nonterminal), \
64 8step(8tuple(8tuple_at_0, \
65 8tuple_at_2)), \
66 8bp_import_nonterminals(8(nonterminals))))
68 #define BRD_PARSER_GEN_sym_typedef(sym, type) \
69 typedef type ORDER_PP_FRESH_ID(sym##_type);
71 #define BRD_PARSER_GEN_prototype(sym) \
72 static _Bool ORDER_PP_FRESH_ID(sym) \
73 (str_type *ORDER_PP_FRESH_ID(pstr), \
74 ORDER_PP_FRESH_ID(sym##_type) *sym);
76 #define BRD_PARSER_GEN_entry_point(qr, name, sym) \
77 qr _Bool name(str_type *ORDER_PP_FRESH_ID(pstr), \
78 ORDER_PP_FRESH_ID(sym##_type) *sym) { \
79 assert(ORDER_PP_FRESH_ID(pstr)); \
80 assert(*ORDER_PP_FRESH_ID(pstr)); \
81 assert(sym); \
82 return ORDER_PP_FRESH_ID(sym)(ORDER_PP_FRESH_ID(pstr), \
83 sym); \
86 #define BRD_PARSER_GEN_terminal(sym, parse) \
87 inline _Bool ORDER_PP_FRESH_ID(sym) \
88 (str_type *ORDER_PP_FRESH_ID(pstr), \
89 ORDER_PP_FRESH_ID(sym##_type) *sym) { \
90 ORDER_PP_FRESH_ID(skip)(ORDER_PP_FRESH_ID(pstr)); \
92 return parse(ORDER_PP_FRESH_ID(pstr), sym); \
95 #define BRD_PARSER_GEN_nonterminal(sym, alternatives) \
96 static _Bool ORDER_PP_FRESH_ID(sym) \
97 (str_type *ORDER_PP_FRESH_ID(pstr), \
98 ORDER_PP_FRESH_ID(sym##_type) *sym) { \
99 ORDER_PP_FRESH_ID(skip)(ORDER_PP_FRESH_ID(pstr)); \
101 ORDER_PP(8seq_emit \
102 (8(BRD_PARSER_GEN_alternative), \
103 8(alternatives))) \
105 return 0; \
108 #define BRD_PARSER_GEN_alternative(minals, ...) \
109 do { \
110 str_type ORDER_PP_FRESH_ID(alt_pstr) = \
111 *ORDER_PP_FRESH_ID(pstr); \
113 ORDER_PP(8seq_for_each \
114 (8fn(8M, \
115 8emit(8if(8equal(2, 8tuple_size(8M)), \
116 8(BRD_PARSER_GEN_try_sym), \
117 8(BRD_PARSER_GEN_try_match)), \
118 8M)), \
119 8(minals))) \
121 do { __VA_ARGS__ } while (0); \
122 *ORDER_PP_FRESH_ID(pstr) = ORDER_PP_FRESH_ID(alt_pstr); \
123 return 1; \
124 } while (0);
126 #define BRD_PARSER_GEN_try_sym(sym, var) \
127 ORDER_PP_FRESH_ID(sym##_type) var; \
128 if (!ORDER_PP_FRESH_ID(sym)(&ORDER_PP_FRESH_ID(alt_pstr), \
129 &var)) \
130 break;
132 #define BRD_PARSER_GEN_try_match(pat) \
133 if (!ORDER_PP_FRESH_ID(match)(&ORDER_PP_FRESH_ID(alt_pstr), \
134 pat)) \
135 break;
137 #endif