ndtypes 0.2.0dev4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +50 -0
  3. data/Gemfile +2 -0
  4. data/History.md +0 -0
  5. data/README.md +19 -0
  6. data/Rakefile +125 -0
  7. data/ext/ruby_ndtypes/extconf.rb +55 -0
  8. data/ext/ruby_ndtypes/gc_guard.c +36 -0
  9. data/ext/ruby_ndtypes/gc_guard.h +12 -0
  10. data/ext/ruby_ndtypes/ndtypes/AUTHORS.txt +5 -0
  11. data/ext/ruby_ndtypes/ndtypes/INSTALL.txt +101 -0
  12. data/ext/ruby_ndtypes/ndtypes/LICENSE.txt +29 -0
  13. data/ext/ruby_ndtypes/ndtypes/MANIFEST.in +3 -0
  14. data/ext/ruby_ndtypes/ndtypes/Makefile.in +87 -0
  15. data/ext/ruby_ndtypes/ndtypes/README.rst +47 -0
  16. data/ext/ruby_ndtypes/ndtypes/config.guess +1530 -0
  17. data/ext/ruby_ndtypes/ndtypes/config.h.in +67 -0
  18. data/ext/ruby_ndtypes/ndtypes/config.sub +1782 -0
  19. data/ext/ruby_ndtypes/ndtypes/configure +5260 -0
  20. data/ext/ruby_ndtypes/ndtypes/configure.ac +161 -0
  21. data/ext/ruby_ndtypes/ndtypes/doc/Makefile +14 -0
  22. data/ext/ruby_ndtypes/ndtypes/doc/_static/copybutton.js +66 -0
  23. data/ext/ruby_ndtypes/ndtypes/doc/conf.py +26 -0
  24. data/ext/ruby_ndtypes/ndtypes/doc/grammar/grammar.rst +27 -0
  25. data/ext/ruby_ndtypes/ndtypes/doc/index.rst +56 -0
  26. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/context.rst +131 -0
  27. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/encodings.rst +68 -0
  28. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/fields-values.rst +175 -0
  29. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/functions.rst +72 -0
  30. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/index.rst +43 -0
  31. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/init.rst +48 -0
  32. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/io.rst +100 -0
  33. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/memory.rst +124 -0
  34. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/predicates.rst +110 -0
  35. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/typedef.rst +31 -0
  36. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/types.rst +594 -0
  37. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/util.rst +166 -0
  38. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/buffer-protocol.rst +27 -0
  39. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/index.rst +21 -0
  40. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/pattern-matching.rst +330 -0
  41. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/quickstart.rst +144 -0
  42. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/types.rst +544 -0
  43. data/ext/ruby_ndtypes/ndtypes/doc/releases/index.rst +35 -0
  44. data/ext/ruby_ndtypes/ndtypes/install-sh +527 -0
  45. data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.in +271 -0
  46. data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.vc +269 -0
  47. data/ext/ruby_ndtypes/ndtypes/libndtypes/alloc.c +230 -0
  48. data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.c +268 -0
  49. data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.h +109 -0
  50. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.in +73 -0
  51. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.vc +70 -0
  52. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/README.txt +16 -0
  53. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.c +2179 -0
  54. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.h +134 -0
  55. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.y +428 -0
  56. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.c +2543 -0
  57. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.h +735 -0
  58. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.l +176 -0
  59. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/export.c +543 -0
  60. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/import.c +110 -0
  61. data/ext/ruby_ndtypes/ndtypes/libndtypes/context.c +228 -0
  62. data/ext/ruby_ndtypes/ndtypes/libndtypes/copy.c +634 -0
  63. data/ext/ruby_ndtypes/ndtypes/libndtypes/encodings.c +116 -0
  64. data/ext/ruby_ndtypes/ndtypes/libndtypes/equal.c +288 -0
  65. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.c +3067 -0
  66. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.h +180 -0
  67. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.y +417 -0
  68. data/ext/ruby_ndtypes/ndtypes/libndtypes/io.c +1658 -0
  69. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.c +2773 -0
  70. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.h +734 -0
  71. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.l +222 -0
  72. data/ext/ruby_ndtypes/ndtypes/libndtypes/match.c +1132 -0
  73. data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.c +2323 -0
  74. data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.h.in +893 -0
  75. data/ext/ruby_ndtypes/ndtypes/libndtypes/overflow.h +161 -0
  76. data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.c +473 -0
  77. data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.h +92 -0
  78. data/ext/ruby_ndtypes/ndtypes/libndtypes/parser.c +246 -0
  79. data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.c +269 -0
  80. data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.h +197 -0
  81. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.in +48 -0
  82. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.vc +46 -0
  83. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/deserialize.c +1007 -0
  84. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/serialize.c +442 -0
  85. data/ext/ruby_ndtypes/ndtypes/libndtypes/slice.h +42 -0
  86. data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.c +238 -0
  87. data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.h +50 -0
  88. data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.c +371 -0
  89. data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.h +100 -0
  90. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.in +55 -0
  91. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.vc +45 -0
  92. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.c +82 -0
  93. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.h +49 -0
  94. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/runtest.c +1657 -0
  95. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test.h +85 -0
  96. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_array.c +115 -0
  97. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_buffer.c +137 -0
  98. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_indent.c +201 -0
  99. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_match.c +2397 -0
  100. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_numba.c +57 -0
  101. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse.c +349 -0
  102. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_error.c +27839 -0
  103. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_roundtrip.c +350 -0
  104. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_record.c +231 -0
  105. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typecheck.c +375 -0
  106. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typedef.c +65 -0
  107. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/valgrind.supp +30 -0
  108. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/bench.c +79 -0
  109. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/indent.c +94 -0
  110. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/print_ast.c +96 -0
  111. data/ext/ruby_ndtypes/ndtypes/libndtypes/util.c +474 -0
  112. data/ext/ruby_ndtypes/ndtypes/libndtypes/values.c +228 -0
  113. data/ext/ruby_ndtypes/ndtypes/python/bench.py +49 -0
  114. data/ext/ruby_ndtypes/ndtypes/python/ndt_randtype.py +409 -0
  115. data/ext/ruby_ndtypes/ndtypes/python/ndt_support.py +14 -0
  116. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/__init__.py +70 -0
  117. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/_ndtypes.c +1332 -0
  118. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/docstrings.h +319 -0
  119. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/pyndtypes.h +154 -0
  120. data/ext/ruby_ndtypes/ndtypes/python/test_ndtypes.py +1977 -0
  121. data/ext/ruby_ndtypes/ndtypes/setup.py +288 -0
  122. data/ext/ruby_ndtypes/ndtypes/vcbuild/INSTALL.txt +41 -0
  123. data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest32.bat +15 -0
  124. data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest64.bat +13 -0
  125. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild32.bat +38 -0
  126. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild64.bat +38 -0
  127. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcclean.bat +13 -0
  128. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcdistclean.bat +14 -0
  129. data/ext/ruby_ndtypes/ruby_ndtypes.c +1003 -0
  130. data/ext/ruby_ndtypes/ruby_ndtypes.h +37 -0
  131. data/ext/ruby_ndtypes/ruby_ndtypes_internal.h +28 -0
  132. data/lib/ndtypes.rb +45 -0
  133. data/lib/ndtypes/errors.rb +2 -0
  134. data/lib/ndtypes/version.rb +6 -0
  135. data/ndtypes.gemspec +47 -0
  136. data/spec/gc_table_spec.rb +10 -0
  137. data/spec/ndtypes_spec.rb +289 -0
  138. data/spec/spec_helper.rb +241 -0
  139. metadata +242 -0
@@ -0,0 +1,92 @@
1
+ /*
2
+ * BSD 3-Clause License
3
+ *
4
+ * Copyright (c) 2017-2018, plures
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * 1. Redistributions of source code must retain the above copyright notice,
11
+ * this list of conditions and the following disclaimer.
12
+ *
13
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ * this list of conditions and the following disclaimer in the documentation
15
+ * and/or other materials provided with the distribution.
16
+ *
17
+ * 3. Neither the name of the copyright holder nor the names of its
18
+ * contributors may be used to endorse or promote products derived from
19
+ * this software without specific prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+
34
+ #ifndef PARSEFUNCS_H
35
+ #define PARSEFUNCS_H
36
+
37
+
38
+ #include "ndtypes.h"
39
+ #include "seq.h"
40
+ #include "attr.h"
41
+
42
+
43
+ /* LOCAL SCOPE */
44
+ NDT_PRAGMA(NDT_HIDE_SYMBOLS_START)
45
+
46
+
47
+ /*****************************************************************************/
48
+ /* Functions used in the lexer */
49
+ /*****************************************************************************/
50
+
51
+ char *mk_stringlit(const char *lexeme, ndt_context_t *ctx);
52
+
53
+
54
+ /*****************************************************************************/
55
+ /* Parser helper functions */
56
+ /*****************************************************************************/
57
+
58
+ enum ndt_encoding encoding_from_string(char *s, ndt_context_t *ctx);
59
+ ndt_attr_t *mk_attr(char *name, char *value, ndt_context_t *ctx);
60
+ ndt_attr_t *mk_attr_from_seq(char *name, ndt_string_seq_t *seq, ndt_context_t *ctx);
61
+
62
+
63
+ /*****************************************************************************/
64
+ /* Parser functions for creating types */
65
+ /*****************************************************************************/
66
+
67
+ ndt_t *mk_function(ndt_type_seq_t *in, ndt_type_seq_t *out, ndt_context_t *ctx);
68
+
69
+ ndt_t *mk_fortran(ndt_t *type, ndt_context_t *ctx);
70
+ ndt_t *mk_contig(char *name, ndt_t *type, ndt_context_t *ctx);
71
+ ndt_t *mk_fixed_dim_from_shape(char *v, ndt_t *type, ndt_context_t *ctx);
72
+ ndt_t *mk_fixed_dim_from_attrs(ndt_attr_seq_t *attrs, ndt_t *type, ndt_context_t *ctx);
73
+
74
+ ndt_t *mk_var_dim(ndt_meta_t *m, ndt_attr_seq_t *seq, ndt_t *type, ndt_context_t *ctx);
75
+ ndt_t *mk_var_ellipsis(ndt_t *type, ndt_context_t *ctx);
76
+
77
+ ndt_field_t *mk_field(char *name, ndt_t *type, ndt_attr_seq_t *seq, ndt_context_t *ctx);
78
+ ndt_t *mk_tuple(enum ndt_variadic flag, ndt_field_seq_t *fields, ndt_attr_seq_t *attrs, ndt_context_t *ctx);
79
+ ndt_t *mk_record(enum ndt_variadic flag, ndt_field_seq_t *fields, ndt_attr_seq_t *attrs, ndt_context_t *ctx);
80
+
81
+ ndt_t *mk_categorical(ndt_value_seq_t *seq, ndt_context_t *ctx);
82
+
83
+ ndt_t *mk_fixed_string(char *v, enum ndt_encoding encoding, ndt_context_t *ctx);
84
+ ndt_t *mk_bytes(ndt_attr_seq_t *seq, ndt_context_t *ctx);
85
+ ndt_t *mk_fixed_bytes(ndt_attr_seq_t *seq, ndt_context_t *ctx);
86
+
87
+
88
+ /* END LOCAL SCOPE */
89
+ NDT_PRAGMA(NDT_HIDE_SYMBOLS_END)
90
+
91
+
92
+ #endif /* PARSEFUNCS_H */
@@ -0,0 +1,246 @@
1
+ /*
2
+ * BSD 3-Clause License
3
+ *
4
+ * Copyright (c) 2017-2018, plures
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * 1. Redistributions of source code must retain the above copyright notice,
11
+ * this list of conditions and the following disclaimer.
12
+ *
13
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ * this list of conditions and the following disclaimer in the documentation
15
+ * and/or other materials provided with the distribution.
16
+ *
17
+ * 3. Neither the name of the copyright holder nor the names of its
18
+ * contributors may be used to endorse or promote products derived from
19
+ * this software without specific prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+
34
+ #include <stdio.h>
35
+ #include <assert.h>
36
+ #include <setjmp.h>
37
+ #include "ndtypes.h"
38
+ #include "seq.h"
39
+ #include "grammar.h"
40
+ #include "lexer.h"
41
+
42
+
43
+ #ifdef YYDEBUG
44
+ int ndt_yydebug = 1;
45
+ #endif
46
+
47
+
48
+ static FILE *
49
+ ndt_fopen(const char *name, const char *mode)
50
+ {
51
+ #ifdef _MSC_VER
52
+ FILE *fp;
53
+
54
+ (void)fopen_s(&fp, name, mode);
55
+ return fp;
56
+ #else
57
+ return fopen(name, mode);
58
+ #endif
59
+ }
60
+
61
+ /* The yy_fatal_error() function of flex calls exit(). We intercept the function
62
+ and do a longjmp() for proper error handling. */
63
+ jmp_buf ndt_lexerror;
64
+
65
+
66
+ static ndt_t *
67
+ _ndt_from_fp(ndt_meta_t *m, FILE *fp, ndt_context_t *ctx)
68
+ {
69
+ volatile yyscan_t scanner = NULL;
70
+ ndt_t *ast = NULL;
71
+ int ret;
72
+
73
+ if (setjmp(ndt_lexerror) == 0) {
74
+ if (ndt_yylex_init_extra(ctx, (yyscan_t *)&scanner) != 0) {
75
+ ndt_err_format(ctx, NDT_LexError, "lexer initialization failed");
76
+ return NULL;
77
+ }
78
+
79
+ if (fp != stdin) {
80
+ ndt_yyset_in(fp, scanner);
81
+ }
82
+
83
+ ret = ndt_yyparse(scanner, &ast, m, ctx);
84
+ ndt_yylex_destroy(scanner);
85
+
86
+ if (ret == 2) {
87
+ ndt_err_format(ctx, NDT_MemoryError, "out of memory");
88
+ }
89
+
90
+ return ast;
91
+ }
92
+ else {
93
+ if (scanner) {
94
+ ndt_yylex_destroy(scanner);
95
+ }
96
+ ndt_err_format(ctx, NDT_MemoryError,
97
+ "out of memory (most likely) or internal lexer error");
98
+ return NULL;
99
+ }
100
+ }
101
+
102
+ static ndt_t *
103
+ _ndt_from_file(ndt_meta_t *m, const char *name, ndt_context_t *ctx)
104
+ {
105
+ FILE *fp;
106
+ ndt_t *t;
107
+
108
+ if (strcmp(name, "-") == 0) {
109
+ fp = stdin;
110
+ }
111
+ else {
112
+ fp = ndt_fopen(name, "rb");
113
+ if (fp == NULL) {
114
+ ndt_err_format(ctx, NDT_OSError, "could not open %s", name);
115
+ return NULL;
116
+ }
117
+ }
118
+
119
+ t = _ndt_from_fp(m, fp, ctx);
120
+ fclose(fp);
121
+
122
+ return t;
123
+ }
124
+
125
+ ndt_t *
126
+ ndt_from_file(const char *name, ndt_context_t *ctx)
127
+ {
128
+ return _ndt_from_file(NULL, name, ctx);
129
+ }
130
+
131
+ ndt_t *
132
+ ndt_from_file_fill_meta(ndt_meta_t *m, const char *name, ndt_context_t *ctx)
133
+ {
134
+ return _ndt_from_file(m, name, ctx);
135
+ }
136
+
137
+ static ndt_t *
138
+ _ndt_from_string(ndt_meta_t *m, const char *input, ndt_context_t *ctx)
139
+ {
140
+ volatile yyscan_t scanner = NULL;
141
+ volatile YY_BUFFER_STATE state = NULL;
142
+ char *buffer;
143
+ size_t size;
144
+ ndt_t *ast = NULL;
145
+ int ret;
146
+
147
+ size = strlen(input);
148
+ if (size > INT_MAX / 2) {
149
+ /* The code generated by flex truncates size_t in several places. */
150
+ ndt_err_format(ctx, NDT_LexError, "maximum input length: %d", INT_MAX/2);
151
+ return NULL;
152
+ }
153
+
154
+ buffer = ndt_alloc_size(size+2);
155
+ if (buffer == NULL) {
156
+ return ndt_memory_error(ctx);
157
+ }
158
+ memcpy(buffer, input, size);
159
+ buffer[size] = '\0';
160
+ buffer[size+1] = '\0';
161
+
162
+ if (setjmp(ndt_lexerror) == 0) {
163
+ if (ndt_yylex_init_extra(ctx, (yyscan_t *)&scanner) != 0) {
164
+ ndt_err_format(ctx, NDT_LexError, "lexer initialization failed");
165
+ ndt_free(buffer);
166
+ return NULL;
167
+ }
168
+
169
+ state = ndt_yy_scan_buffer(buffer, size+2, scanner);
170
+ state->yy_bs_lineno = 1;
171
+ state->yy_bs_column = 1;
172
+
173
+ ret = ndt_yyparse(scanner, &ast, m, ctx);
174
+ ndt_yy_delete_buffer(state, scanner);
175
+ ndt_yylex_destroy(scanner);
176
+ ndt_free(buffer);
177
+
178
+ if (ret == 2) {
179
+ ndt_err_format(ctx, NDT_MemoryError, "out of memory");
180
+ }
181
+
182
+ return ast;
183
+ }
184
+ else { /* fatal lexer error */
185
+ if (state) {
186
+ ndt_free(state);
187
+ }
188
+ if (scanner) {
189
+ ndt_yylex_destroy(scanner);
190
+ }
191
+ ndt_free(buffer);
192
+ ndt_err_format(ctx, NDT_MemoryError, "flex: internal lexer error");
193
+ return NULL;
194
+ }
195
+ }
196
+
197
+ ndt_t *
198
+ ndt_from_string(const char *input, ndt_context_t *ctx)
199
+ {
200
+ return _ndt_from_string(NULL, input, ctx);
201
+ }
202
+
203
+ ndt_t *
204
+ ndt_from_string_v(const char *input, ndt_context_t *ctx)
205
+ {
206
+ ndt_t *t = _ndt_from_string(NULL, input, ctx);
207
+ if (t == NULL) {
208
+ ndt_err_append(ctx, input);
209
+ }
210
+ return t;
211
+ }
212
+
213
+ ndt_t *
214
+ ndt_from_string_fill_meta(ndt_meta_t *m, const char *input, ndt_context_t *ctx)
215
+ {
216
+ return _ndt_from_string(m, input, ctx);
217
+ }
218
+
219
+ ndt_t *
220
+ ndt_from_metadata_and_dtype(const ndt_meta_t *m, const char *dtype, ndt_context_t *ctx)
221
+ {
222
+ ndt_t *t, *type;
223
+ int i;
224
+
225
+ type = ndt_from_string(dtype, ctx);
226
+ if (type == NULL) {
227
+ return NULL;
228
+ }
229
+
230
+ if (ndt_is_abstract(type)) {
231
+ ndt_err_format(ctx, NDT_InvalidArgumentError,
232
+ "cannot create abstract type with offsets");
233
+ ndt_del(type);
234
+ return NULL;
235
+ }
236
+
237
+ for (i=0, t=type; i < m->ndims; i++, type=t) {
238
+ t = ndt_var_dim(type, ExternalOffsets, m->noffsets[i], m->offsets[i],
239
+ 0, NULL, ctx);
240
+ if (t == NULL) {
241
+ return NULL;
242
+ }
243
+ }
244
+
245
+ return t;
246
+ }
@@ -0,0 +1,269 @@
1
+ /*
2
+ * BSD 3-Clause License
3
+ *
4
+ * Copyright (c) 2017-2018, plures
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * 1. Redistributions of source code must retain the above copyright notice,
11
+ * this list of conditions and the following disclaimer.
12
+ *
13
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ * this list of conditions and the following disclaimer in the documentation
15
+ * and/or other materials provided with the distribution.
16
+ *
17
+ * 3. Neither the name of the copyright holder nor the names of its
18
+ * contributors may be used to endorse or promote products derived from
19
+ * this software without specific prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+
34
+ #include <stdio.h>
35
+ #include <stdlib.h>
36
+ #include <assert.h>
37
+ #include "ndtypes.h"
38
+ #include "seq.h"
39
+
40
+
41
+ /*****************************************************************************/
42
+ /* Field sequences */
43
+ /*****************************************************************************/
44
+
45
+ NDT_SEQ_NEW(ndt_field)
46
+ NDT_SEQ_DEL(ndt_field)
47
+ NDT_SEQ_GROW(ndt_field)
48
+ NDT_SEQ_APPEND(ndt_field)
49
+ NDT_SEQ_FINALIZE(ndt_field)
50
+
51
+
52
+ /*****************************************************************************/
53
+ /* String sequences */
54
+ /*****************************************************************************/
55
+
56
+ ndt_string_seq_t *
57
+ ndt_string_seq_new(char *elt, ndt_context_t *ctx)
58
+ {
59
+ ndt_string_seq_t *seq;
60
+ char **ptr;
61
+
62
+ seq = ndt_alloc_size(sizeof *seq);
63
+ if (seq == NULL) {
64
+ ndt_free(elt);
65
+ return ndt_memory_error(ctx);
66
+ }
67
+
68
+ ptr = ndt_alloc_size(2 * (sizeof *ptr));
69
+ if (ptr == NULL) {
70
+ ndt_free(seq);
71
+ ndt_free(elt);
72
+ return ndt_memory_error(ctx);
73
+ }
74
+
75
+ ptr[0] = elt;
76
+ seq->len = 1;
77
+ seq->reserved = 2;
78
+ seq->ptr = ptr;
79
+
80
+ return seq;
81
+ }
82
+
83
+ void
84
+ ndt_string_seq_del(ndt_string_seq_t *seq)
85
+ {
86
+ int64_t i;
87
+
88
+ if (seq != NULL) {
89
+ for (i = 0; i < seq->len; i++) {
90
+ ndt_free(seq->ptr[i]);
91
+ }
92
+ ndt_free(seq->ptr);
93
+ ndt_free(seq);
94
+ }
95
+ }
96
+
97
+ static int
98
+ ndt_string_seq_grow(ndt_string_seq_t *seq, ndt_context_t *ctx)
99
+ {
100
+ char **ptr;
101
+
102
+ ptr = ndt_realloc(seq->ptr, seq->reserved, 2 * (sizeof *ptr));
103
+ if (ptr == NULL) {
104
+ ndt_err_format(ctx, NDT_MemoryError, "out of memory");
105
+ return -1;
106
+ }
107
+
108
+ seq->ptr = ptr;
109
+ seq->reserved = 2 * seq->reserved;
110
+
111
+ return 0;
112
+ }
113
+
114
+ ndt_string_seq_t *
115
+ ndt_string_seq_append(ndt_string_seq_t *seq, char *elt, ndt_context_t *ctx)
116
+ {
117
+ assert(seq->len <= seq->reserved);
118
+
119
+ if (seq->len == seq->reserved) {
120
+ if (ndt_string_seq_grow(seq, ctx) < 0) {
121
+ ndt_string_seq_del(seq);
122
+ ndt_free(elt);
123
+ return NULL;
124
+ }
125
+ }
126
+
127
+ seq->ptr[seq->len] = elt;
128
+ seq->len++;
129
+
130
+ return seq;
131
+ }
132
+
133
+ ndt_string_seq_t *
134
+ ndt_string_seq_finalize(ndt_string_seq_t *seq)
135
+ {
136
+ char **ptr;
137
+
138
+ if (seq == NULL) {
139
+ return NULL;
140
+ }
141
+
142
+ assert(seq->len <= seq->reserved);
143
+
144
+ ptr = ndt_realloc(seq->ptr, seq->len, sizeof *ptr);
145
+ if (ptr == NULL) {
146
+ return seq; /* seq is still valid */
147
+ }
148
+
149
+ seq->ptr = ptr;
150
+ seq->reserved = seq->len;
151
+
152
+ return seq;
153
+ }
154
+
155
+
156
+ /*****************************************************************************/
157
+ /* Type sequences */
158
+ /*****************************************************************************/
159
+
160
+ ndt_type_seq_t *
161
+ ndt_type_seq_empty(ndt_context_t *ctx)
162
+ {
163
+ ndt_type_seq_t *seq;
164
+
165
+ seq = ndt_alloc_size(sizeof *seq);
166
+ if (seq == NULL) {
167
+ return ndt_memory_error(ctx);
168
+ }
169
+
170
+ seq->len = 0;
171
+ seq->reserved = 0;
172
+ seq->ptr = NULL;
173
+
174
+ return seq;
175
+ }
176
+
177
+ ndt_type_seq_t *
178
+ ndt_type_seq_new(ndt_t *elt, ndt_context_t *ctx)
179
+ {
180
+ ndt_type_seq_t *seq;
181
+ ndt_t **ptr;
182
+
183
+ seq = ndt_alloc_size(sizeof *seq);
184
+ if (seq == NULL) {
185
+ ndt_del(elt);
186
+ return ndt_memory_error(ctx);
187
+ }
188
+
189
+ ptr = ndt_alloc_size(2 * (sizeof *ptr));
190
+ if (ptr == NULL) {
191
+ ndt_free(seq);
192
+ ndt_del(elt);
193
+ return ndt_memory_error(ctx);
194
+ }
195
+
196
+ ptr[0] = elt;
197
+ seq->len = 1;
198
+ seq->reserved = 2;
199
+ seq->ptr = ptr;
200
+
201
+ return seq;
202
+ }
203
+
204
+ void
205
+ ndt_type_seq_del(ndt_type_seq_t *seq)
206
+ {
207
+ if (seq != NULL) {
208
+ ndt_type_array_del(seq->ptr, seq->len);
209
+ ndt_free(seq);
210
+ }
211
+ }
212
+
213
+ static int
214
+ ndt_type_seq_grow(ndt_type_seq_t *seq, ndt_context_t *ctx)
215
+ {
216
+ ndt_t **ptr;
217
+
218
+ ptr = ndt_realloc(seq->ptr, seq->reserved, 2 * (sizeof *ptr));
219
+ if (ptr == NULL) {
220
+ ndt_err_format(ctx, NDT_MemoryError, "out of memory");
221
+ return -1;
222
+ }
223
+
224
+ seq->ptr = ptr;
225
+ seq->reserved = 2 * seq->reserved;
226
+
227
+ return 0;
228
+ }
229
+
230
+ ndt_type_seq_t *
231
+ ndt_type_seq_append(ndt_type_seq_t *seq, ndt_t *elt, ndt_context_t *ctx)
232
+ {
233
+ assert(seq->len <= seq->reserved);
234
+
235
+ if (seq->len == seq->reserved) {
236
+ if (ndt_type_seq_grow(seq, ctx) < 0) {
237
+ ndt_type_seq_del(seq);
238
+ ndt_free(elt);
239
+ return NULL;
240
+ }
241
+ }
242
+
243
+ seq->ptr[seq->len] = elt;
244
+ seq->len++;
245
+
246
+ return seq;
247
+ }
248
+
249
+ ndt_type_seq_t *
250
+ ndt_type_seq_finalize(ndt_type_seq_t *seq)
251
+ {
252
+ ndt_t **ptr;
253
+
254
+ if (seq == NULL) {
255
+ return NULL;
256
+ }
257
+
258
+ assert(seq->len <= seq->reserved);
259
+
260
+ ptr = ndt_realloc(seq->ptr, seq->len, sizeof *ptr);
261
+ if (ptr == NULL) {
262
+ return seq; /* seq is still valid */
263
+ }
264
+
265
+ seq->ptr = ptr;
266
+ seq->reserved = seq->len;
267
+
268
+ return seq;
269
+ }