parser: NAME fallback ERROR,WARNING
[m6w6/ext-psi] / src / parser_def.h
1 /*******************************************************************************
2 Copyright (c) 2017, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #ifndef PSI_PARSER_INC
27 #define PSI_PARSER_INC
28
29 #define CONCAT2(x,y) x##y
30 #define CONCAT1(x,y) CONCAT2(x,y)
31 #define COUNTED(x) CONCAT1(parse_ ##x## _, __LINE__)
32
33 #define TOKEN_PREFIX PSI_T
34 #define TOKEN_STRUCT struct psi_token *
35
36 #ifdef GENERATE
37 # define DEF(dn, dv) dn dv
38 # define PASS(nt, rule) nt ::= rule.
39 # define PARSE(nt, rule) nt ::= rule.
40 # define PARSE_NAMED(nt, nt_name, rule) NAMED(nt, nt_name) ::= rule.
41 # define PARSE_TYPED(nt, nt_name, rule) TYPED(nt, nt_name) ::= rule.
42 # define TOKEN(t) t
43 # define NAMED(t, name) t(name)
44 # define TYPED(t, name) t(name)
45 # define TOKEN_TYPE(token, type_) %type token {type_}
46 # define TOKEN_DTOR(token, dtor) %destructor token {dtor}
47 # define T(token) token
48 # define TOKEN_CLASS(type, tokens) DEF(%token_class, type##_token tokens .)
49 #else
50 # ifndef TEST
51 # include "parser.h"
52 # include "plist.h"
53 # endif
54 # define DEF(dn, dv)
55 # define PASS(nt, rule) \
56 static void COUNTED(nt) (struct psi_parser *P) { \
57 (void) #rule; \
58 }
59 # define PARSE(nt, rule) \
60 static void COUNTED(nt) (struct psi_parser *P rule)
61 # define PARSE_NAMED(nt, nt_name, rule) \
62 static void COUNTED(nt) (struct psi_parser *P NAMED(nt, nt_name) rule)
63 # define PARSE_TYPED(nt, nt_name, rule) \
64 static void COUNTED(nt) (struct psi_parser *P TYPED(nt, nt_name) rule)
65 # define TOKEN(t)
66 # define NAMED(t, name) , struct psi_token *name
67 # define TYPED(t, name) , TOKEN_TYPE_NAME(t) name
68 # define TOKEN_TYPE_NAME(token) token##_parse_t
69 # define TOKEN_TYPE(token, type) typedef type TOKEN_TYPE_NAME(token);
70 # define TOKEN_DTOR(token, dtor)
71 # define T(token) CONCAT1(TOKEN_PREFIX, _ ##token ),
72 # define TOKEN_CLASS(type, tokens) static token_t type##_token[] = { tokens 0 };
73 #endif
74
75
76 #endif