liquid5-c 5.0.0

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 (53) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.md +164 -0
  4. data/ext/liquid_c/block.c +970 -0
  5. data/ext/liquid_c/block.h +37 -0
  6. data/ext/liquid_c/c_buffer.c +48 -0
  7. data/ext/liquid_c/c_buffer.h +82 -0
  8. data/ext/liquid_c/context.c +262 -0
  9. data/ext/liquid_c/context.h +72 -0
  10. data/ext/liquid_c/document_body.c +181 -0
  11. data/ext/liquid_c/document_body.h +84 -0
  12. data/ext/liquid_c/expression.c +130 -0
  13. data/ext/liquid_c/expression.h +24 -0
  14. data/ext/liquid_c/extconf.rb +60 -0
  15. data/ext/liquid_c/intutil.h +36 -0
  16. data/ext/liquid_c/lexer.c +172 -0
  17. data/ext/liquid_c/lexer.h +61 -0
  18. data/ext/liquid_c/liquid.c +182 -0
  19. data/ext/liquid_c/liquid.h +112 -0
  20. data/ext/liquid_c/liquid_vm.c +907 -0
  21. data/ext/liquid_c/liquid_vm.h +35 -0
  22. data/ext/liquid_c/mri_compat.h +35 -0
  23. data/ext/liquid_c/native_parser.c +2360 -0
  24. data/ext/liquid_c/native_value.c +49 -0
  25. data/ext/liquid_c/parse_context.c +84 -0
  26. data/ext/liquid_c/parse_context.h +13 -0
  27. data/ext/liquid_c/parser.c +285 -0
  28. data/ext/liquid_c/parser.h +25 -0
  29. data/ext/liquid_c/raw.c +136 -0
  30. data/ext/liquid_c/raw.h +6 -0
  31. data/ext/liquid_c/resource_limits.c +459 -0
  32. data/ext/liquid_c/resource_limits.h +28 -0
  33. data/ext/liquid_c/stringutil.h +37 -0
  34. data/ext/liquid_c/tokenizer.c +326 -0
  35. data/ext/liquid_c/tokenizer.h +46 -0
  36. data/ext/liquid_c/usage.c +18 -0
  37. data/ext/liquid_c/usage.h +9 -0
  38. data/ext/liquid_c/variable.c +198 -0
  39. data/ext/liquid_c/variable.h +61 -0
  40. data/ext/liquid_c/variable_lookup.c +73 -0
  41. data/ext/liquid_c/variable_lookup.h +8 -0
  42. data/ext/liquid_c/vm_assembler.c +631 -0
  43. data/ext/liquid_c/vm_assembler.h +294 -0
  44. data/ext/liquid_c/vm_assembler_pool.c +117 -0
  45. data/ext/liquid_c/vm_assembler_pool.h +27 -0
  46. data/lib/liquid/c/compile_ext.rb +47 -0
  47. data/lib/liquid/c/native_compiler.rb +262 -0
  48. data/lib/liquid/c/native_parser.rb +261 -0
  49. data/lib/liquid/c/native_tags.rb +103 -0
  50. data/lib/liquid/c/native_values.rb +9 -0
  51. data/lib/liquid/c/version.rb +7 -0
  52. data/lib/liquid/c.rb +379 -0
  53. metadata +140 -0
@@ -0,0 +1,49 @@
1
+ #include "liquid.h"
2
+ #include "mri_compat.h"
3
+ #include "context.h"
4
+
5
+ #ifdef HAVE_RB_CALLABLE_METHOD_ENTRY
6
+ static VALUE identity_entries[8];
7
+ static bool identity_registered;
8
+ static VALUE register_identity_methods(VALUE self, VALUE enabled)
9
+ {
10
+ Check_Type(enabled, T_ARRAY);
11
+ if (RARRAY_LEN(enabled) != 8) rb_raise(rb_eArgError, "invalid identity method flags");
12
+ if (identity_registered) rb_raise(rb_eRuntimeError, "identity methods already registered");
13
+ VALUE owners[] = { rb_cString, rb_cArray, rb_cHash, rb_cNumeric, rb_cRange,
14
+ rb_cTrueClass, rb_cFalseClass, rb_cNilClass };
15
+ for (long i = 0; i < 8; i++) {
16
+ identity_entries[i] = RTEST(RARRAY_AREF(enabled, i)) ?
17
+ liquid_method_entry(owners[i], id_to_liquid) : 0;
18
+ rb_gc_register_address(&identity_entries[i]);
19
+ }
20
+ identity_registered = true;
21
+ return Qnil;
22
+ }
23
+
24
+ VALUE liquid_value_to_liquid(VALUE value)
25
+ {
26
+ int index;
27
+ switch (TYPE(value)) {
28
+ case T_STRING: index = 0; break;
29
+ case T_ARRAY: index = 1; break;
30
+ case T_HASH: index = 2; break;
31
+ case T_FIXNUM: case T_BIGNUM: case T_FLOAT: index = 3; break;
32
+ case T_STRUCT: index = rb_obj_is_kind_of(value, rb_cRange) ? 4 : -1; break;
33
+ case T_TRUE: index = 5; break;
34
+ case T_FALSE: index = 6; break;
35
+ case T_NIL: index = 7; break;
36
+ default: index = -1; break;
37
+ }
38
+ if (index >= 0 && identity_entries[index] && identity_entries[index] ==
39
+ liquid_method_entry(CLASS_OF(value), id_to_liquid)) return value;
40
+ return rb_funcall(value, id_to_liquid, 0);
41
+ }
42
+ #endif
43
+
44
+ void liquid_define_native_values(void)
45
+ {
46
+ #ifdef HAVE_RB_CALLABLE_METHOD_ENTRY
47
+ rb_define_singleton_method(mLiquidC, "native_register_identity_methods", register_identity_methods, 1);
48
+ #endif
49
+ }
@@ -0,0 +1,84 @@
1
+ #include "parse_context.h"
2
+ #include "document_body.h"
3
+
4
+ static ID id_document_body, id_vm_assembler_pool;
5
+
6
+ static bool parse_context_document_body_initialized_p(VALUE self)
7
+ {
8
+ return RTEST(rb_attr_get(self, id_document_body));
9
+ }
10
+
11
+ static void parse_context_init_document_body(VALUE self)
12
+ {
13
+ VALUE document_body = document_body_new_instance();
14
+ rb_ivar_set(self, id_document_body, document_body);
15
+ }
16
+
17
+ VALUE parse_context_get_document_body(VALUE self)
18
+ {
19
+ if (!parse_context_document_body_initialized_p(self))
20
+ rb_raise(rb_eRuntimeError, "Liquid::ParseContext#start_liquid_c_parsing has not yet been called");
21
+
22
+ return rb_ivar_get(self, id_document_body);
23
+ }
24
+
25
+ vm_assembler_pool_t *parse_context_init_vm_assembler_pool(VALUE self)
26
+ {
27
+ assert(!RTEST(rb_attr_get(self, id_vm_assembler_pool)));
28
+
29
+ VALUE vm_assembler_pool_obj = vm_assembler_pool_new();
30
+ rb_ivar_set(self, id_vm_assembler_pool, vm_assembler_pool_obj);
31
+
32
+ vm_assembler_pool_t *vm_assembler_pool;
33
+ VMAssemblerPool_Get_Struct(vm_assembler_pool_obj, vm_assembler_pool);
34
+
35
+ return vm_assembler_pool;
36
+ }
37
+
38
+ vm_assembler_pool_t *parse_context_get_vm_assembler_pool(VALUE self)
39
+ {
40
+ VALUE obj = rb_ivar_get(self, id_vm_assembler_pool);
41
+
42
+ if (obj == Qnil) {
43
+ rb_raise(rb_eRuntimeError, "Liquid::ParseContext#start_liquid_c_parsing has not yet been called");
44
+ }
45
+
46
+ vm_assembler_pool_t *vm_assembler_pool;
47
+ VMAssemblerPool_Get_Struct(obj, vm_assembler_pool);
48
+ return vm_assembler_pool;
49
+ }
50
+
51
+ static VALUE parse_context_start_liquid_c_parsing(VALUE self)
52
+ {
53
+ if (RB_UNLIKELY(parse_context_document_body_initialized_p(self))) {
54
+ rb_raise(rb_eRuntimeError, "liquid-c parsing already started for this parse context");
55
+ }
56
+ parse_context_init_document_body(self);
57
+ parse_context_init_vm_assembler_pool(self);
58
+ return Qnil;
59
+ }
60
+
61
+ static VALUE parse_context_cleanup_liquid_c_parsing(VALUE self)
62
+ {
63
+ VALUE pool_obj = rb_ivar_get(self, id_vm_assembler_pool);
64
+ if (!NIL_P(pool_obj)) {
65
+ vm_assembler_pool_t *pool;
66
+ VMAssemblerPool_Get_Struct(pool_obj, pool);
67
+ vm_assembler_pool_clear(pool);
68
+ }
69
+ VALUE document = rb_ivar_get(self, id_document_body);
70
+ if (!NIL_P(document)) document_body_finish(document);
71
+ rb_ivar_set(self, id_document_body, Qnil);
72
+ rb_ivar_set(self, id_vm_assembler_pool, Qnil);
73
+ return Qnil;
74
+ }
75
+
76
+ void liquid_define_parse_context(void)
77
+ {
78
+ id_document_body = rb_intern("document_body");
79
+ id_vm_assembler_pool = rb_intern("vm_assembler_pool");
80
+
81
+ VALUE cLiquidParseContext = rb_const_get(mLiquid, rb_intern("ParseContext"));
82
+ rb_define_method(cLiquidParseContext, "start_liquid_c_parsing", parse_context_start_liquid_c_parsing, 0);
83
+ rb_define_method(cLiquidParseContext, "cleanup_liquid_c_parsing", parse_context_cleanup_liquid_c_parsing, 0);
84
+ }
@@ -0,0 +1,13 @@
1
+ #ifndef LIQUID_PARSE_CONTEXT_H
2
+ #define LIQUID_PARSE_CONTEXT_H
3
+
4
+ #include <ruby.h>
5
+ #include <stdbool.h>
6
+ #include "vm_assembler_pool.h"
7
+
8
+ void liquid_define_parse_context(void);
9
+ VALUE parse_context_get_document_body(VALUE self);
10
+
11
+ vm_assembler_pool_t *parse_context_get_vm_assembler_pool(VALUE self);
12
+
13
+ #endif
@@ -0,0 +1,285 @@
1
+ #include "liquid.h"
2
+ #include "parser.h"
3
+ #include "lexer.h"
4
+
5
+ static VALUE empty_string;
6
+ static ID id_to_i, idEvaluate;
7
+
8
+ void init_parser(parser_t *p, const char *str, const char *end)
9
+ {
10
+ p->str_end = end;
11
+ p->cur.type = p->next.type = TOKEN_EOS;
12
+ p->str = lex_one(str, end, &p->cur, TOKEN_EOS);
13
+ p->str = lex_one(p->str, end, &p->next, p->cur.type);
14
+ }
15
+
16
+ lexer_token_t parser_consume_any(parser_t *p)
17
+ {
18
+ lexer_token_t cur = p->cur;
19
+ p->cur = p->next;
20
+ p->next.type = TOKEN_EOS;
21
+ p->str = lex_one(p->str, p->str_end, &p->next, p->cur.type);
22
+ return cur;
23
+ }
24
+
25
+ lexer_token_t parser_must_consume(parser_t *p, unsigned char type)
26
+ {
27
+ if (p->cur.type != type) {
28
+ rb_enc_raise(utf8_encoding, cLiquidSyntaxError, "Expected %s but found %s",
29
+ symbol_names[type], symbol_names[p->cur.type]);
30
+ }
31
+ return parser_consume_any(p);
32
+ }
33
+
34
+ lexer_token_t parser_consume(parser_t *p, unsigned char type)
35
+ {
36
+ if (p->cur.type != type) {
37
+ lexer_token_t zero = {0};
38
+ return zero;
39
+ }
40
+ return parser_consume_any(p);
41
+ }
42
+
43
+ inline static int rstring_eq(VALUE rstr, const char *str) {
44
+ size_t str_len = strlen(str);
45
+
46
+ return TYPE(rstr) == T_STRING &&
47
+ str_len == (size_t)RSTRING_LEN(rstr) &&
48
+ memcmp(RSTRING_PTR(rstr), str, str_len) == 0;
49
+ }
50
+
51
+ static VALUE parse_number(parser_t *p)
52
+ {
53
+ VALUE out;
54
+ lexer_token_t token = parser_must_consume(p, TOKEN_NUMBER);
55
+
56
+ VALUE string = token_to_rstr(token);
57
+ if (token.flags & TOKEN_FLOAT_NUMBER) {
58
+ out = DBL2NUM(liquid_str_to_dbl(string, 1));
59
+ } else {
60
+ out = rb_str_to_inum(string, 10, 1);
61
+ }
62
+ return out;
63
+ }
64
+
65
+ __attribute__((noreturn)) static void raise_invalid_expression_type(const char *expr, int expr_len)
66
+ {
67
+ rb_enc_raise(utf8_encoding, cLiquidSyntaxError, "Invalid expression type '%.*s' in range expression", expr_len, expr);
68
+ }
69
+
70
+ static VALUE try_parse_constant_range(parser_t *p)
71
+ {
72
+ if (liquid_stack_check()) rb_raise(rb_eSysStackError, "stack level too deep");
73
+ parser_t saved_state = *p;
74
+
75
+ parser_must_consume(p, TOKEN_OPEN_ROUND);
76
+
77
+ const char *begin_str = p->cur.val;
78
+ VALUE begin = try_parse_constant_expression(p);
79
+ const char *begin_str_end = p->cur.val;
80
+ if (begin == Qundef) {
81
+ *p = saved_state;
82
+ return Qundef;
83
+ }
84
+ parser_must_consume(p, TOKEN_DOTDOT);
85
+
86
+ const char *end_str = p->cur.val;
87
+ VALUE end = try_parse_constant_expression(p);
88
+ const char *end_str_end = p->cur.val;
89
+ if (end == Qundef) {
90
+ *p = saved_state;
91
+ return Qundef;
92
+ }
93
+ parser_must_consume(p, TOKEN_CLOSE_ROUND);
94
+
95
+ begin = rb_check_funcall(begin, id_to_i, 0, NULL);
96
+ if (begin == Qundef) raise_invalid_expression_type(begin_str, (int)(begin_str_end - begin_str));
97
+
98
+ end = rb_check_funcall(end, id_to_i, 0, NULL);
99
+ if (end == Qundef) raise_invalid_expression_type(end_str, (int)(end_str_end - end_str));
100
+
101
+ bool exclude_end = false;
102
+ return rb_range_new(begin, end, exclude_end);
103
+ }
104
+
105
+ static void parse_and_compile_range(parser_t *p, vm_assembler_t *code)
106
+ {
107
+ VALUE const_range = try_parse_constant_range(p);
108
+ if (const_range != Qundef) {
109
+ vm_assembler_add_push_const(code, const_range);
110
+ return;
111
+ }
112
+
113
+ parser_must_consume(p, TOKEN_OPEN_ROUND);
114
+ parse_and_compile_expression(p, code);
115
+ parser_must_consume(p, TOKEN_DOTDOT);
116
+ parse_and_compile_expression(p, code);
117
+ parser_must_consume(p, TOKEN_CLOSE_ROUND);
118
+ vm_assembler_add_new_int_range(code);
119
+ }
120
+
121
+ static void parse_and_compile_variable_lookup(parser_t *p, vm_assembler_t *code)
122
+ {
123
+ if (parser_consume(p, TOKEN_OPEN_SQUARE).type) {
124
+ parse_and_compile_expression(p, code);
125
+ parser_must_consume(p, TOKEN_CLOSE_SQUARE);
126
+ vm_assembler_add_find_variable(code);
127
+ } else {
128
+ VALUE name = token_to_rstr_leveraging_existing_symbol(parser_must_consume(p, TOKEN_IDENTIFIER));
129
+ vm_assembler_add_find_static_variable(code, name);
130
+ }
131
+
132
+ VALUE guards = rb_ary_new();
133
+ while (true) {
134
+ if (p->cur.type == TOKEN_OPEN_SQUARE) {
135
+ parser_consume_any(p);
136
+ parse_and_compile_expression(p, code);
137
+ parser_must_consume(p, TOKEN_CLOSE_SQUARE);
138
+ vm_assembler_add_lookup_key(code);
139
+ } else if (p->cur.type == TOKEN_DOT) {
140
+ parser_consume_any(p);
141
+ VALUE key = token_to_rstr_leveraging_existing_symbol(parser_must_consume(p, TOKEN_IDENTIFIER));
142
+
143
+ if (rstring_eq(key, "size") || rstring_eq(key, "first") || rstring_eq(key, "last"))
144
+ vm_assembler_add_lookup_command(code, key);
145
+ else
146
+ vm_assembler_add_lookup_const_key(code, key);
147
+ } else {
148
+ break;
149
+ }
150
+ if (p->cur.type == TOKEN_OPEN_SQUARE || p->cur.type == TOKEN_DOT)
151
+ rb_ary_push(guards, SIZET2NUM(vm_assembler_add_lookup_guard(code)));
152
+ }
153
+ for (long i = 0; i < RARRAY_LEN(guards); i++)
154
+ vm_assembler_patch_lookup_guard(code, NUM2SIZET(RARRAY_AREF(guards, i)));
155
+ }
156
+
157
+ static VALUE try_parse_literal(parser_t *p)
158
+ {
159
+ if (p->next.type == TOKEN_DOT || p->next.type == TOKEN_OPEN_SQUARE)
160
+ return Qundef;
161
+
162
+ const char *str = p->cur.val;
163
+ long size = p->cur.val_end - str;
164
+ VALUE result = Qundef;
165
+ switch (size) {
166
+ case 3:
167
+ if (memcmp(str, "nil", size) == 0)
168
+ result = Qnil;
169
+ break;
170
+ case 4:
171
+ if (memcmp(str, "null", size) == 0) {
172
+ result = Qnil;
173
+ } else if (memcmp(str, "true", size) == 0) {
174
+ result = Qtrue;
175
+ }
176
+ break;
177
+ case 5:
178
+ switch (*str) {
179
+ case 'f':
180
+ if (memcmp(str, "false", size) == 0)
181
+ result = Qfalse;
182
+ break;
183
+ case 'b':
184
+ if (memcmp(str, "blank", size) == 0)
185
+ result = empty_string;
186
+ break;
187
+ case 'e':
188
+ if (memcmp(str, "empty", size) == 0)
189
+ result = empty_string;
190
+ break;
191
+ }
192
+ break;
193
+ }
194
+ if (result != Qundef)
195
+ parser_consume_any(p);
196
+ return result;
197
+ }
198
+
199
+ VALUE try_parse_constant_expression(parser_t *p)
200
+ {
201
+ switch (p->cur.type) {
202
+ case TOKEN_IDENTIFIER:
203
+ return try_parse_literal(p);
204
+
205
+ case TOKEN_NUMBER:
206
+ return parse_number(p);
207
+
208
+ case TOKEN_OPEN_ROUND:
209
+ return try_parse_constant_range(p);
210
+
211
+ case TOKEN_STRING:
212
+ {
213
+ lexer_token_t token = parser_consume_any(p);
214
+ token.val++;
215
+ token.val_end--;
216
+ return token_to_rstr(token);
217
+ }
218
+ }
219
+ return Qundef;
220
+ }
221
+
222
+ static void parse_and_compile_number(parser_t *p, vm_assembler_t *code)
223
+ {
224
+ VALUE num = parse_number(p);
225
+ if (RB_FIXNUM_P(num))
226
+ vm_assembler_add_push_fixnum(code, num);
227
+ else
228
+ vm_assembler_add_push_const(code, num);
229
+ return;
230
+ }
231
+
232
+ void parse_and_compile_expression(parser_t *p, vm_assembler_t *code)
233
+ {
234
+ if (liquid_stack_check()) rb_raise(rb_eSysStackError, "stack level too deep");
235
+ switch (p->cur.type) {
236
+ case TOKEN_IDENTIFIER:
237
+ {
238
+ VALUE literal = try_parse_literal(p);
239
+ if (literal != Qundef) {
240
+ vm_assembler_add_push_literal(code, literal);
241
+ return;
242
+ }
243
+
244
+ __attribute__ ((fallthrough));
245
+ }
246
+ case TOKEN_OPEN_SQUARE:
247
+ parse_and_compile_variable_lookup(p, code);
248
+ return;
249
+
250
+ case TOKEN_NUMBER:
251
+ parse_and_compile_number(p, code);
252
+ return;
253
+
254
+ case TOKEN_OPEN_ROUND:
255
+ parse_and_compile_range(p, code);
256
+ return;
257
+
258
+ case TOKEN_STRING:
259
+ {
260
+ lexer_token_t token = parser_consume_any(p);
261
+ token.val++;
262
+ token.val_end--;
263
+ VALUE str = token_to_rstr(token);
264
+ vm_assembler_add_push_const(code, str);
265
+ return;
266
+ }
267
+ }
268
+
269
+ if (p->cur.type == TOKEN_EOS) {
270
+ rb_enc_raise(utf8_encoding, cLiquidSyntaxError, "[:%s] is not a valid expression", symbol_names[p->cur.type]);
271
+ } else {
272
+ rb_enc_raise(utf8_encoding, cLiquidSyntaxError, "[:%s, \"%.*s\"] is not a valid expression",
273
+ symbol_names[p->cur.type], (int)(p->cur.val_end - p->cur.val), p->cur.val);
274
+ }
275
+ }
276
+
277
+ void liquid_define_parser(void)
278
+ {
279
+ id_to_i = rb_intern("to_i");
280
+ idEvaluate = rb_intern("evaluate");
281
+
282
+ empty_string = rb_utf8_str_new_literal("");
283
+ rb_global_variable(&empty_string);
284
+ }
285
+
@@ -0,0 +1,25 @@
1
+ #if !defined(LIQUID_PARSER_H)
2
+ #define LIQUID_PARSER_H
3
+
4
+ #include "lexer.h"
5
+ #include "vm_assembler.h"
6
+
7
+ typedef struct parser {
8
+ lexer_token_t cur, next;
9
+ const char *str, *str_end;
10
+ } parser_t;
11
+
12
+ void init_parser(parser_t *parser, const char *str, const char *end);
13
+
14
+ lexer_token_t parser_must_consume(parser_t *parser, unsigned char type);
15
+ lexer_token_t parser_consume(parser_t *parser, unsigned char type);
16
+ lexer_token_t parser_consume_any(parser_t *parser);
17
+
18
+ void parse_and_compile_expression(parser_t *p, vm_assembler_t *code);
19
+ VALUE try_parse_constant_expression(parser_t *p);
20
+
21
+ void liquid_define_parser(void);
22
+ void liquid_define_native_parser(void);
23
+
24
+ #endif
25
+
@@ -0,0 +1,136 @@
1
+ #include "liquid.h"
2
+ #include "raw.h"
3
+ #include "stringutil.h"
4
+ #include "tokenizer.h"
5
+
6
+ static VALUE id_block_name, id_raise_tag_never_closed, id_block_delimiter, id_ivar_body;
7
+ static VALUE cLiquidRaw;
8
+
9
+ struct full_token_possibly_invalid_t {
10
+ long body_len;
11
+ const char *delimiter_start;
12
+ long delimiter_len;
13
+ };
14
+
15
+ static bool match_full_token_possibly_invalid(token_t *token, struct full_token_possibly_invalid_t *match)
16
+ {
17
+ const char *str = token->str_full;
18
+ long len = token->len_full;
19
+
20
+ match->body_len = 0;
21
+ match->delimiter_start = NULL;
22
+ match->delimiter_len = 0;
23
+
24
+ if (len < 5) return false; // Must be at least 5 characters: \{%\w%\}
25
+ if (str[len - 1] != '}' || str[len - 2] != '%') return false;
26
+
27
+ const char *curr_delimiter_start;
28
+ long curr_delimiter_len = 0;
29
+
30
+ bool is_last_char_whitespace = true;
31
+
32
+ // Search from the end of the string.
33
+ // The token could have a part of the body like this:
34
+ // {% endraw {% endraw %}
35
+ // In this case, we need to return body_len to 10 to preserve the body content.
36
+ for (long i = len - 3; i > 1; i--) {
37
+ char c = str[i];
38
+
39
+ // match \s
40
+ bool is_whitespace = rb_isspace(c);
41
+
42
+ if (is_word_char(c)) {
43
+ curr_delimiter_start = str + i;
44
+
45
+ if (is_last_char_whitespace) {
46
+ // start a new delimiter match
47
+ curr_delimiter_len = 1;
48
+ } else {
49
+ curr_delimiter_len++;
50
+ }
51
+ } else if (!is_word_char(c) && !is_whitespace) {
52
+ curr_delimiter_start = NULL;
53
+ curr_delimiter_len = 0;
54
+ }
55
+
56
+ is_last_char_whitespace = is_whitespace;
57
+
58
+ if (curr_delimiter_len > 0) {
59
+ // match start of a tag which is {% or {%-
60
+ if (
61
+ (str[i - 1] == '%' && str[i - 2] == '{') ||
62
+ (i - 3 >= 0 && str[i - 1] == '-' && str[i - 2] == '%' && str[i - 3] == '{')
63
+ ) {
64
+ match->delimiter_start = curr_delimiter_start;
65
+ match->delimiter_len = curr_delimiter_len;
66
+
67
+ if (str[i - 1] == '-') {
68
+ match->body_len = i - 3;
69
+ } else {
70
+ match->body_len = i - 2;
71
+ }
72
+
73
+ return true;
74
+ }
75
+ }
76
+ }
77
+
78
+ return false;
79
+ }
80
+
81
+ static VALUE raw_parse_method(VALUE self, VALUE tokens)
82
+ {
83
+ tokenizer_t *tokenizer;
84
+ Tokenizer_Get_Struct(tokens, tokenizer);
85
+
86
+ token_t token;
87
+ struct full_token_possibly_invalid_t match;
88
+
89
+ VALUE block_delimiter = rb_funcall(self, id_block_delimiter, 0);
90
+ Check_Type(block_delimiter, T_STRING);
91
+ char *block_delimiter_str = RSTRING_PTR(block_delimiter);
92
+ long block_delimiter_len = RSTRING_LEN(block_delimiter);
93
+
94
+ const char *body = NULL;
95
+ long body_len = 0;
96
+
97
+ while (true) {
98
+ tokenizer_next(tokenizer, &token);
99
+
100
+ if (!token.type) break;
101
+
102
+ if (body == NULL) {
103
+ body = token.str_full;
104
+ }
105
+
106
+ if (match_full_token_possibly_invalid(&token, &match)
107
+ && match.delimiter_len == block_delimiter_len
108
+ && memcmp(match.delimiter_start, block_delimiter_str, block_delimiter_len) == 0) {
109
+ tokenizer->lstrip_flag = token.len_full >= 3 && token.str_full[token.len_full - 3] == '-';
110
+ body_len += match.body_len;
111
+ VALUE body_str = rb_enc_str_new(body, body_len, utf8_encoding);
112
+ rb_ivar_set(self, id_ivar_body, body_str);
113
+ if (RBASIC_CLASS(self) == cLiquidRaw) {
114
+ tokenizer->raw_tag_body = body_str;
115
+ }
116
+ return Qnil;
117
+ }
118
+
119
+ body_len += token.len_full;
120
+ }
121
+
122
+ rb_funcall(self, id_raise_tag_never_closed, 1, rb_funcall(self, id_block_name, 0));
123
+ return Qnil;
124
+ }
125
+
126
+ void liquid_define_raw(void)
127
+ {
128
+ id_block_name = rb_intern("block_name");
129
+ id_raise_tag_never_closed = rb_intern("raise_tag_never_closed");
130
+ id_block_delimiter = rb_intern("block_delimiter");
131
+ id_ivar_body = rb_intern("@body");
132
+
133
+ cLiquidRaw = rb_const_get(mLiquid, rb_intern("Raw"));
134
+
135
+ rb_define_method(cLiquidRaw, "c_parse", raw_parse_method, 1);
136
+ }
@@ -0,0 +1,6 @@
1
+ #ifndef LIQUID_RAW_H
2
+ #define LIQUID_RAW_H
3
+
4
+ void liquid_define_raw(void);
5
+
6
+ #endif