Motr  M0
ff2c.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2011-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include <stdio.h> /* printf, stdout */
24 
25 #include "lib/misc.h" /* M0_SET0 */
26 #include "ut/ut.h"
27 
28 #include "xcode/ff2c/lex.h"
29 #include "xcode/ff2c/parser.h"
30 #include "xcode/ff2c/sem.h"
31 #include "xcode/ff2c/gen.h"
32 
33 static const char sample[] =
34 "/* comment. */ \n"
35 "require \"lib/vec.ff\"; \n"
36 " \n"
37 "record { \n"
38 " u64 f_container; \n"
39 " u64 f_offset \n"
40 "} fid; \n"
41 " \n"
42 "union { \n"
43 " u8 o_flag; \n"
44 " fid o_fid :1; \n"
45 " u32 o_short :3 \n"
46 "} optfid; \n"
47 " \n"
48 "sequence { \n"
49 " u64 ofa_nr; \n"
50 " optfid ofa_data \n"
51 "} optfidarray; \n"
52 " \n"
53 "sequence { \n"
54 " void fa_none :NR; \n"
55 " optfid fa_data \n"
56 "} fixarray; \n"
57 " \n"
58 "/*void nothing; \n"
59 "u8 byte; \n"
60 "u32 quad; \n"
61 "u64 hyper; \n"
62 "optfid t_alias;*/ \n"
63 " \n"
64 "record { \n"
65 " fid p_fid; \n"
66 " m0_vec p_vec; \n"
67 " *m0_cred p_cred [m0_package_cred_get]; \n"
68 " sequence { \n"
69 " u32 s_nr; \n"
70 " u8 s_data \n"
71 " } p_name \n"
72 "} package \n"
73 " \n";
74 
75 __attribute__((unused)) static void token_print(const struct ff2c_token *tok)
76 {
77  if (tok->ft_type != 0)
78  printf("[%s: %*.*s]", ff2c_token_type_name[tok->ft_type],
79  (int)tok->ft_len, (int)tok->ft_len, tok->ft_val);
80  else
81  printf("[no token]");
82 }
83 
84 static void ff2c_lex_test(void)
85 {
86  struct ff2c_context ctx;
87  /* struct ff2c_token tok; */
88 
89  M0_SET0(&ctx);
90  ff2c_context_init(&ctx, sample, strlen(sample));
91 
92  /* while (ff2c_token_get(&ctx, &tok) > 0)
93  token_print(&tok); */
95 }
96 
97 __attribute__((unused)) static void parser_print(struct ff2c_term *t, int depth)
98 {
99  const char ruler[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
100  printf("%*.*s %11.11s: ", depth, depth, ruler,
101  ff2c_term_type_name[t->fn_type]);
102  token_print(&t->fn_tok);
103  printf("\n");
104  for (t = t->fn_head; t != NULL; t = t->fn_next)
105  parser_print(t, depth + 1);
106 }
107 
108 static void ff2c_parser_test(void)
109 {
110  struct ff2c_context ctx;
111  struct ff2c_term *t;
112  int result;
113 
114  M0_SET0(&ctx);
115  ff2c_context_init(&ctx, sample, strlen(sample));
116  result = ff2c_parse(&ctx, &t);
117  M0_UT_ASSERT(result == 0);
118  /*
119  printf("\n");
120  parser_print(t, 0);
121  */
122  ff2c_term_fini(t);
124 }
125 
126 static void ff2c_sem_test(void)
127 {
128  int result;
129  struct ff2c_context ctx;
130  struct ff2c_term *term;
131  struct ff2c_ff ff;
132 /* struct ff2c_require *r;
133  struct ff2c_type *t;
134  struct ff2c_field *f; */
135 
136  M0_SET0(&ctx);
137  M0_SET0(&ff);
138 
139  ff2c_context_init(&ctx, sample, strlen(sample));
140  result = ff2c_parse(&ctx, &term);
141  M0_UT_ASSERT(result == 0);
142 
143  ff2c_sem_init(&ff, term);
144 /*
145  for (r = ff.ff_require.l_head; r != NULL; r = r->r_next)
146  printf("require %s\n", r->r_path);
147 
148  for (t = ff.ff_type.l_head; t != NULL; t = t->t_next) {
149  printf("type %p name: %s xc: %s c: %s\n"
150  "\t%s %s %s %s %s %s %i\n", t,
151  t->t_name, t->t_xc_name, t->t_c_name,
152  t->t_compound ? "comp" : "",
153  t->t_atomic ? "atom" : "",
154  t->t_opaque ? "opaq" : "",
155  t->t_sequence ? "seq" : "",
156  t->t_union ? "unio" : "",
157  t->t_record ? "rec" : "",
158  t->t_nr);
159  for (f = t->t_field.l_head; f != NULL; f = f->f_next) {
160  printf("\tfield: %p name: %s c: %s tag: %s escape: %s\n"
161  "\t\tdecl: %s\n",
162  f->f_type, f->f_name, f->f_c_name,
163  f->f_tag ?: "", f->f_escape ?: "",
164  f->f_decl ?: "");
165  }
166  printf("\n");
167  }
168 */
169 
170  ff2c_sem_fini(&ff);
171 
174 }
175 
176 static void ff2c_gen_test(void)
177 {
178  struct ff2c_context ctx;
179  struct ff2c_term *t;
180  struct ff2c_ff ff;
181  const struct ff2c_gen_opt opt ={
182  .go_basename = "basename",
183  .go_guardname = "__GUARD__",
184  .go_out = fopen("/dev/null", "w") /* stdout */
185  };
186 
187  int result;
188 
189  M0_SET0(&ctx);
190  M0_SET0(&ff);
191 
192  ff2c_context_init(&ctx, sample, strlen(sample));
193  result = ff2c_parse(&ctx, &t);
194  M0_UT_ASSERT(result == 0);
195 
196  ff2c_sem_init(&ff, t);
197 
198  ff2c_h_gen(&ff, &opt);
199  ff2c_c_gen(&ff, &opt);
200 
201  ff2c_sem_fini(&ff);
202  ff2c_term_fini(t);
204 }
205 
207  .ts_name = "ff2c-ut",
208  .ts_init = NULL,
209  .ts_fini = NULL,
210  .ts_tests = {
211  { "xcode-lex", ff2c_lex_test },
212  { "xcode-parser", ff2c_parser_test },
213  { "xcode-sem", ff2c_sem_test },
214  { "xcode-gen", ff2c_gen_test },
215  { NULL, NULL }
216  }
217 };
218 
219 /*
220  * Local variables:
221  * c-indentation-style: "K&R"
222  * c-basic-offset: 8
223  * tab-width: 8
224  * fill-column: 79
225  * scroll-step: 1
226  * End:
227  */
228 
229 
__attribute__((unused))
Definition: ff2c.c:75
#define NULL
Definition: misc.h:38
const char * ff2c_term_type_name[]
Definition: parser.c:236
static const char sample[]
Definition: ff2c.c:33
void ff2c_context_fini(struct ff2c_context *ctx)
Definition: lex.c:230
void ff2c_sem_fini(struct ff2c_ff *ff)
Definition: sem.c:267
Definition: sem.h:90
#define M0_SET0(obj)
Definition: misc.h:64
Definition: ut.h:77
static void ff2c_lex_test(void)
Definition: ff2c.c:84
size_t ft_len
Definition: lex.h:99
static int ff(struct ff2c_context *ctx, struct ff2c_term *term)
Definition: parser.c:212
struct m0_ut_suite xcode_ff2c_ut
Definition: ff2c.c:206
static unsigned depth
Definition: base.c:377
int ff2c_c_gen(const struct ff2c_ff *ff, const struct ff2c_gen_opt *opt)
Definition: gen.c:198
enum ff2c_token_type ft_type
Definition: lex.h:95
const char * ff2c_token_type_name[]
Definition: lex.c:241
static struct m0_thread t[8]
Definition: service_ut.c:1230
int ff2c_h_gen(const struct ff2c_ff *ff, const struct ff2c_gen_opt *opt)
Definition: gen.c:93
static void ff2c_gen_test(void)
Definition: ff2c.c:176
void ff2c_sem_init(struct ff2c_ff *ff, struct ff2c_term *top)
Definition: sem.c:247
const char * ts_name
Definition: ut.h:99
Definition: lex.h:94
const char * go_basename
Definition: gen.h:39
static void ff2c_parser_test(void)
Definition: ff2c.c:108
struct m0_rpc_fop_session_terminate term
Definition: session.c:53
Definition: nucleus.c:42
const char * ft_val
Definition: lex.h:97
void ff2c_term_fini(struct ff2c_term *term)
Definition: parser.c:231
void ff2c_context_init(struct ff2c_context *ctx, const char *buf, size_t size)
Definition: lex.c:224
#define M0_UT_ASSERT(a)
Definition: ut.h:46
int ff2c_parse(struct ff2c_context *ctx, struct ff2c_term **out)
Definition: parser.c:225
static void ff2c_sem_test(void)
Definition: ff2c.c:126