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 enum lex_type {
28   lex_error,
29   lex_char,
30   lex_triple_minus,
31   lex_uc_ident,
32   lex_lc_ident,
33   lex_eof,
34   lex_final,
35   lex_new,
36   lex_none,
37   lex_num,
38   lex_empty
39 };
40 
41 
42 struct curlex {
43   char *ptr;
44   int len;
45   enum lex_type type;
46   int flags;
47 };
48 
49 struct parse {
50   char *text;
51   int pos;
52   int len;
53   int line;
54   int line_pos;
55   struct curlex lex;
56 };
57 
58 
59 enum tree_type {
60   type_tl_program,
61   type_fun_declarations,
62   type_constr_declarations,
63   type_declaration,
64   type_combinator_decl,
65   type_equals,
66   type_partial_app_decl,
67   type_final_decl,
68   type_full_combinator_id,
69   type_opt_args,
70   type_args,
71   type_args1,
72   type_args2,
73   type_args3,
74   type_args4,
75   type_boxed_type_ident,
76   type_subexpr,
77   type_partial_comb_app_decl,
78   type_partial_type_app_decl,
79   type_final_new,
80   type_final_final,
81   type_final_empty,
82 //  type_type,
83   type_var_ident,
84   type_var_ident_opt,
85   type_multiplicity,
86   type_type_term,
87   type_term,
88   type_percent,
89   type_result_type,
90   type_expr,
91   type_nat_term,
92   type_combinator_id,
93   type_nat_const,
94   type_type_ident,
95   type_builtin_combinator_decl,
96   type_exclam,
97   type_optional_arg_def
98 };
99 
100 struct tree {
101   char *text;
102   int len;
103   enum tree_type type;
104   int lex_line;
105   int lex_line_pos;
106   int flags;
107   int size;
108   int nc;
109   struct tree **c;
110 };
111 
112 
113 #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" : \
114     x == act_union ? "act_union" : x == act_arg ? "act_arg" : x == act_opt_field ? "act_opt_field" : "act_unknown")
115 
116 #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")
117 enum combinator_tree_action {
118   act_var,
119   act_field,
120   act_plus,
121   act_type,
122   act_nat_const,
123   act_array,
124   act_question_mark,
125   act_union,
126   act_arg,
127   act_opt_field
128 };
129 
130 enum combinator_tree_type {
131   type_num,
132   type_num_value,
133   type_type,
134   type_list_item,
135   type_list
136 };
137 
138 struct tl_combinator_tree {
139   enum combinator_tree_action act;
140   struct tl_combinator_tree *left, *right;
141   char *name;
142   void *data;
143   long long flags;
144   enum combinator_tree_type type;
145   int type_len;
146   long long type_flags;
147 };
148 
149 
150 struct tl_program {
151   int types_num;
152   int functions_num;
153   int constructors_num;
154   struct tl_type **types;
155   struct tl_function **functions;
156 //  struct tl_constuctor **constructors;
157 };
158 
159 struct tl_type {
160   char *id;
161   char *print_id;
162   char *real_id;
163   unsigned name;
164   int flags;
165 
166   int params_num;
167   long long params_types;
168 
169   int constructors_num;
170   struct tl_constructor **constructors;
171 };
172 
173 struct tl_constructor {
174   char *id;
175   char *print_id;
176   char *real_id;
177   unsigned name;
178   struct tl_type *type;
179 
180   struct tl_combinator_tree *left;
181   struct tl_combinator_tree *right;
182 };
183 
184 struct tl_var {
185   char *id;
186   struct tl_combinator_tree *ptr;
187   int type;
188   int flags;
189 };
190 
191 struct parse *tl_init_parse_file (const char *fname);
192 struct tree *tl_parse_lex (struct parse *P);
193 void tl_print_parse_error (void);
194 struct tl_program *tl_parse (struct tree *T);
195 
196 void write_types (int f);
197 
198 #define FLAG_BARE 1
199 #define FLAG_OPT_VAR (1 << 17)
200 #define FLAG_EXCL (1 << 18)
201 #define FLAG_OPT_FIELD (1 << 20)
202 #define FLAG_IS_VAR (1 << 21)
203 #define FLAG_DEFAULT_CONSTRUCTOR (1 << 25)
204 #define FLAG_EMPTY (1 << 10)
205 
206 #endif
207