1 #ifndef ORDER_EXAMPLE_LAMBDA_BRD_PARSER_H_VAJK20040620
2 #define ORDER_EXAMPLE_LAMBDA_BRD_PARSER_H_VAJK20040620
4 // (C) Copyright Vesa Karvonen 2004.
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE.)
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 \
18 8tuple(8tuple_at_0(8N), \
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, \
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)), \
39 8emit(8(BRD_PARSER_GEN_entry_point), \
42 8tuple_at_0(8seq_at(0,8S)))))) \
45 ORDER_PP_FRESH_ID(skip)(str_type *ORDER_PP_FRESH_ID(pstr)) { \
46 skip_fn(ORDER_PP_FRESH_ID(pstr)); \
49 ORDER_PP(8seq_emit_map(8(BRD_PARSER_GEN_terminal), \
50 8step(8tuple(8tuple_at_0, \
52 8bp_import_terminals(8(terminals)))) \
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)); \
63 ORDER_PP(8seq_emit_map(8(BRD_PARSER_GEN_nonterminal), \
64 8step(8tuple(8tuple_at_0, \
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)); \
82 return ORDER_PP_FRESH_ID(sym)(ORDER_PP_FRESH_ID(pstr), \
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)); \
102 (8(BRD_PARSER_GEN_alternative), \
108 #define BRD_PARSER_GEN_alternative(minals, ...) \
110 str_type ORDER_PP_FRESH_ID(alt_pstr) = \
111 *ORDER_PP_FRESH_ID(pstr); \
113 ORDER_PP(8seq_for_each \
115 8emit(8if(8equal(2, 8tuple_size(8M)), \
116 8(BRD_PARSER_GEN_try_sym), \
117 8(BRD_PARSER_GEN_try_match)), \
121 do { __VA_ARGS__ } while (0); \
122 *ORDER_PP_FRESH_ID(pstr) = ORDER_PP_FRESH_ID(alt_pstr); \
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), \
132 #define BRD_PARSER_GEN_try_match(pat) \
133 if (!ORDER_PP_FRESH_ID(match)(&ORDER_PP_FRESH_ID(alt_pstr), \