1 /*
2     This file is part of tgl-libary/tlc
3 
4     Tgl-library/tlc is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 2 of the License, or
7     (at your option) any later version.
8 
9     Tgl-library/tlc is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this tgl-library/tlc.  If not, see <http://www.gnu.org/licenses/>.
16 
17     Copyright Vitaly Valtman 2014
18 
19     It is derivative work of VK/KittenPHP-DB-Engine (https://github.com/vk-com/kphp-kdb/)
20     Copyright 2012-2013 Vkontakte Ltd
21               2012-2013 Vitaliy Valtman
22 
23 */
24 
25 #ifndef __TL_PARSER_NEW_H__
26 #define __TL_PARSER_NEW_H__
27 
28 #include <stdio.h>
29 
30 enum lex_type {
31   lex_error,
32   lex_char,
33   lex_triple_minus,
34   lex_uc_ident,
35   lex_lc_ident,
36   lex_eof,
37   lex_final,
38   lex_new,
39   lex_none,
40   lex_num,
41   lex_empty
42 };
43 
44 
45 struct curlex {
46   char *ptr;
47   int len;
48   enum lex_type type;
49   int flags;
50 };
51 
52 struct parse {
53   char *text;
54   int pos;
55   int len;
56   int line;
57   int line_pos;
58   struct curlex lex;
59 };
60 
61 
62 enum tree_type {
63   type_tl_program,
64   type_fun_declarations,
65   type_constr_declarations,
66   type_declaration,
67   type_combinator_decl,
68   type_equals,
69   type_partial_app_decl,
70   type_final_decl,
71   type_full_combinator_id,
72   type_opt_args,
73   type_args,
74   type_args1,
75   type_args2,
76   type_args3,
77   type_args4,
78   type_boxed_type_ident,
79   type_subexpr,
80   type_partial_comb_app_decl,
81   type_partial_type_app_decl,
82   type_final_new,
83   type_final_final,
84   type_final_empty,
85 //  type_type,
86   type_var_ident,
87   type_var_ident_opt,
88   type_multiplicity,
89   type_type_term,
90   type_term,
91   type_percent,
92   type_result_type,
93   type_expr,
94   type_nat_term,
95   type_combinator_id,
96   type_nat_const,
97   type_type_ident,
98   type_builtin_combinator_decl,
99   type_exclam,
100   type_optional_arg_def
101 };
102 
103 struct tree {
104   char *text;
105   int len;
106   enum tree_type type;
107   int lex_line;
108   int lex_line_pos;
109   int flags;
110   int size;
111   int nc;
112   struct tree **c;
113 };
114 
115 
116 #define TL_ACT(x) (x == act_var ? "act_var" : x == act_field ? "act_field" : x == act_plus ? "act_plus" : x == act_type ? "act_type" : x == act_nat_const ? "act_nat_const" : x == act_array ? "act_array" : x == act_question_mark ? "act_question_mark" : \
117     x == act_union ? "act_union" : x == act_arg ? "act_arg" : x == act_opt_field ? "act_opt_field" : "act_unknown")
118 
119 #define TL_TYPE(x) (x == type_num ? "type_num" : x == type_type ? "type_type" : x == type_list_item ? "type_list_item" : x == type_list ? "type_list" : x == type_num_value ? "type_num_value" : "type_unknown")
120 enum combinator_tree_action {
121   act_var,
122   act_field,
123   act_plus,
124   act_type,
125   act_nat_const,
126   act_array,
127   act_question_mark,
128   act_union,
129   act_arg,
130   act_opt_field
131 };
132 
133 enum combinator_tree_type {
134   type_num,
135   type_num_value,
136   type_type,
137   type_list_item,
138   type_list
139 };
140 
141 struct tl_combinator_tree {
142   enum combinator_tree_action act;
143   struct tl_combinator_tree *left, *right;
144   char *name;
145   void *data;
146   long long flags;
147   enum combinator_tree_type type;
148   int type_len;
149   long long type_flags;
150 };
151 
152 
153 struct tl_program {
154   int types_num;
155   int functions_num;
156   int constructors_num;
157   struct tl_type **types;
158   struct tl_function **functions;
159 //  struct tl_constuctor **constructors;
160 };
161 
162 struct tl_type {
163   char *id;
164   char *print_id;
165   char *real_id;
166   unsigned name;
167   int flags;
168 
169   int params_num;
170   long long params_types;
171 
172   int constructors_num;
173   struct tl_constructor **constructors;
174 };
175 
176 struct tl_constructor {
177   char *id;
178   char *print_id;
179   char *real_id;
180   unsigned name;
181   struct tl_type *type;
182 
183   struct tl_combinator_tree *left;
184   struct tl_combinator_tree *right;
185 };
186 
187 struct tl_var {
188   char *id;
189   struct tl_combinator_tree *ptr;
190   int type;
191   int flags;
192 };
193 
194 struct parse *tl_init_parse_file (const char *fname);
195 struct tree *tl_parse_lex (struct parse *P);
196 void tl_print_parse_error (void);
197 struct tl_program *tl_parse (struct tree *T);
198 
199 void write_types (FILE *f);
200 
201 #define FLAG_BARE 1
202 #define FLAG_OPT_VAR (1 << 17)
203 #define FLAG_EXCL (1 << 18)
204 #define FLAG_OPT_FIELD (1 << 20)
205 #define FLAG_IS_VAR (1 << 21)
206 #define FLAG_DEFAULT_CONSTRUCTOR (1 << 25)
207 #define FLAG_EMPTY (1 << 10)
208 
209 #ifdef NDEBUG
210 #undef assert
211 #define assert(x) if (!(x)) { fprintf(stderr, "Assertion error!\n"); abort(); }
212 #endif
213 
214 #ifdef _WIN32
215 #include "wgetopt.h"
216 
217 #define __attribute__(x)
218 
219 #define lrand48() rand()
220 #define strdup _strdup
221 #endif
222 
223 #endif
224