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.
- checksums.yaml +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +164 -0
- data/ext/liquid_c/block.c +970 -0
- data/ext/liquid_c/block.h +37 -0
- data/ext/liquid_c/c_buffer.c +48 -0
- data/ext/liquid_c/c_buffer.h +82 -0
- data/ext/liquid_c/context.c +262 -0
- data/ext/liquid_c/context.h +72 -0
- data/ext/liquid_c/document_body.c +181 -0
- data/ext/liquid_c/document_body.h +84 -0
- data/ext/liquid_c/expression.c +130 -0
- data/ext/liquid_c/expression.h +24 -0
- data/ext/liquid_c/extconf.rb +60 -0
- data/ext/liquid_c/intutil.h +36 -0
- data/ext/liquid_c/lexer.c +172 -0
- data/ext/liquid_c/lexer.h +61 -0
- data/ext/liquid_c/liquid.c +182 -0
- data/ext/liquid_c/liquid.h +112 -0
- data/ext/liquid_c/liquid_vm.c +907 -0
- data/ext/liquid_c/liquid_vm.h +35 -0
- data/ext/liquid_c/mri_compat.h +35 -0
- data/ext/liquid_c/native_parser.c +2360 -0
- data/ext/liquid_c/native_value.c +49 -0
- data/ext/liquid_c/parse_context.c +84 -0
- data/ext/liquid_c/parse_context.h +13 -0
- data/ext/liquid_c/parser.c +285 -0
- data/ext/liquid_c/parser.h +25 -0
- data/ext/liquid_c/raw.c +136 -0
- data/ext/liquid_c/raw.h +6 -0
- data/ext/liquid_c/resource_limits.c +459 -0
- data/ext/liquid_c/resource_limits.h +28 -0
- data/ext/liquid_c/stringutil.h +37 -0
- data/ext/liquid_c/tokenizer.c +326 -0
- data/ext/liquid_c/tokenizer.h +46 -0
- data/ext/liquid_c/usage.c +18 -0
- data/ext/liquid_c/usage.h +9 -0
- data/ext/liquid_c/variable.c +198 -0
- data/ext/liquid_c/variable.h +61 -0
- data/ext/liquid_c/variable_lookup.c +73 -0
- data/ext/liquid_c/variable_lookup.h +8 -0
- data/ext/liquid_c/vm_assembler.c +631 -0
- data/ext/liquid_c/vm_assembler.h +294 -0
- data/ext/liquid_c/vm_assembler_pool.c +117 -0
- data/ext/liquid_c/vm_assembler_pool.h +27 -0
- data/lib/liquid/c/compile_ext.rb +47 -0
- data/lib/liquid/c/native_compiler.rb +262 -0
- data/lib/liquid/c/native_parser.rb +261 -0
- data/lib/liquid/c/native_tags.rb +103 -0
- data/lib/liquid/c/native_values.rb +9 -0
- data/lib/liquid/c/version.rb +7 -0
- data/lib/liquid/c.rb +379 -0
- metadata +140 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
#ifndef LIQUID_DOCUMENT_BODY_H
|
|
2
|
+
#define LIQUID_DOCUMENT_BODY_H
|
|
3
|
+
|
|
4
|
+
#include "c_buffer.h"
|
|
5
|
+
#include "vm_assembler.h"
|
|
6
|
+
|
|
7
|
+
typedef struct block_body_header {
|
|
8
|
+
uint32_t instructions_offset;
|
|
9
|
+
uint32_t instructions_bytes;
|
|
10
|
+
uint32_t constants_offset;
|
|
11
|
+
uint32_t constants_len;
|
|
12
|
+
uint32_t flags;
|
|
13
|
+
uint32_t render_score;
|
|
14
|
+
uint64_t max_stack_size;
|
|
15
|
+
} block_body_header_t;
|
|
16
|
+
|
|
17
|
+
#define BLOCK_BODY_HEADER_FLAG_BLANK (1 << 0)
|
|
18
|
+
#define BLOCK_BODY_HEADER_BLANK_P(header) (header->flags & BLOCK_BODY_HEADER_FLAG_BLANK)
|
|
19
|
+
|
|
20
|
+
typedef struct {
|
|
21
|
+
size_t first, last;
|
|
22
|
+
} serial_range_t;
|
|
23
|
+
|
|
24
|
+
typedef struct document_body {
|
|
25
|
+
VALUE self;
|
|
26
|
+
VALUE constants;
|
|
27
|
+
long frontend_cache_size;
|
|
28
|
+
c_buffer_t buffer;
|
|
29
|
+
c_buffer_t invalid_serials;
|
|
30
|
+
size_t next_serial;
|
|
31
|
+
} document_body_t;
|
|
32
|
+
|
|
33
|
+
typedef struct document_body_entry {
|
|
34
|
+
document_body_t *body;
|
|
35
|
+
size_t buffer_offset;
|
|
36
|
+
size_t serial;
|
|
37
|
+
} document_body_entry_t;
|
|
38
|
+
|
|
39
|
+
void liquid_define_document_body(void);
|
|
40
|
+
VALUE document_body_new_instance(void);
|
|
41
|
+
bool document_body_cache_stable(VALUE self, long size);
|
|
42
|
+
document_body_entry_t document_body_write_block_body(VALUE self, bool blank, uint32_t render_score, vm_assembler_t *code);
|
|
43
|
+
void document_body_checkpoint(VALUE self, size_t *buffer_size, long *constants_len, size_t *serial);
|
|
44
|
+
void document_body_truncate(VALUE self, size_t buffer_size, long constants_len, size_t serial);
|
|
45
|
+
void document_body_finish(VALUE self);
|
|
46
|
+
|
|
47
|
+
static inline void document_body_entry_mark(document_body_entry_t *entry)
|
|
48
|
+
{
|
|
49
|
+
rb_gc_mark(entry->body->self);
|
|
50
|
+
rb_gc_mark(entry->body->constants);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static inline block_body_header_t *document_body_get_block_body_header_ptr(const document_body_entry_t *entry)
|
|
54
|
+
{
|
|
55
|
+
const serial_range_t *ranges = (const serial_range_t *)entry->body->invalid_serials.data;
|
|
56
|
+
size_t range_count = c_buffer_size(&entry->body->invalid_serials) / sizeof(*ranges);
|
|
57
|
+
size_t low = 0, high = range_count;
|
|
58
|
+
while (low < high) {
|
|
59
|
+
size_t middle = low + (high - low) / 2;
|
|
60
|
+
if (ranges[middle].last < entry->serial) low = middle + 1;
|
|
61
|
+
else high = middle;
|
|
62
|
+
}
|
|
63
|
+
if (low < range_count && entry->serial >= ranges[low].first)
|
|
64
|
+
rb_raise(rb_eRuntimeError, "Liquid block body was discarded during parsing");
|
|
65
|
+
size_t size = c_buffer_size(&entry->body->buffer);
|
|
66
|
+
if (entry->buffer_offset > size || size - entry->buffer_offset < sizeof(block_body_header_t))
|
|
67
|
+
rb_raise(rb_eRuntimeError, "Liquid block body was discarded during parsing");
|
|
68
|
+
return (block_body_header_t *)(entry->body->buffer.data + entry->buffer_offset);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static inline const VALUE *document_body_get_constants_ptr(const document_body_entry_t *entry)
|
|
72
|
+
{
|
|
73
|
+
block_body_header_t *header = document_body_get_block_body_header_ptr(entry);
|
|
74
|
+
return RARRAY_CONST_PTR(entry->body->constants) + header->constants_offset;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static inline void document_body_ensure_compile_finished(document_body_t *body)
|
|
78
|
+
{
|
|
79
|
+
if (RB_UNLIKELY(!RB_OBJ_FROZEN(body->self))) {
|
|
80
|
+
rb_raise(rb_eRuntimeError, "Liquid document hasn't finished compilation");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#endif
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
#include "liquid.h"
|
|
2
|
+
#include "vm_assembler.h"
|
|
3
|
+
#include "parser.h"
|
|
4
|
+
#include "liquid_vm.h"
|
|
5
|
+
#include "expression.h"
|
|
6
|
+
|
|
7
|
+
VALUE cLiquidCExpression;
|
|
8
|
+
|
|
9
|
+
static void expression_mark(void *ptr)
|
|
10
|
+
{
|
|
11
|
+
expression_t *expression = ptr;
|
|
12
|
+
vm_assembler_gc_mark(&expression->code);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static void expression_free(void *ptr)
|
|
16
|
+
{
|
|
17
|
+
expression_t *expression = ptr;
|
|
18
|
+
vm_assembler_free(&expression->code);
|
|
19
|
+
xfree(expression);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static size_t expression_memsize(const void *ptr)
|
|
23
|
+
{
|
|
24
|
+
const expression_t *expression = ptr;
|
|
25
|
+
return sizeof(expression_t) + vm_assembler_alloc_memsize(&expression->code);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const rb_data_type_t expression_data_type = {
|
|
29
|
+
"liquid_expression",
|
|
30
|
+
{ expression_mark, expression_free, expression_memsize, },
|
|
31
|
+
NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
VALUE expression_new(VALUE klass, expression_t **expression_ptr)
|
|
35
|
+
{
|
|
36
|
+
expression_t *expression;
|
|
37
|
+
VALUE obj = TypedData_Make_Struct(klass, expression_t, &expression_data_type, expression);
|
|
38
|
+
*expression_ptr = expression;
|
|
39
|
+
vm_assembler_init(&expression->code);
|
|
40
|
+
return obj;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static VALUE internal_expression_parse(parser_t *p)
|
|
44
|
+
{
|
|
45
|
+
if (p->cur.type == TOKEN_EOS)
|
|
46
|
+
return Qnil;
|
|
47
|
+
|
|
48
|
+
// Avoid allocating an expression object just to wrap a constant
|
|
49
|
+
VALUE const_obj = try_parse_constant_expression(p);
|
|
50
|
+
if (const_obj != Qundef)
|
|
51
|
+
return const_obj;
|
|
52
|
+
|
|
53
|
+
expression_t *expression;
|
|
54
|
+
VALUE expr_obj = expression_new(cLiquidCExpression, &expression);
|
|
55
|
+
|
|
56
|
+
parse_and_compile_expression(p, &expression->code);
|
|
57
|
+
assert(expression->code.stack_size == 1);
|
|
58
|
+
vm_assembler_add_leave(&expression->code);
|
|
59
|
+
|
|
60
|
+
return expr_obj;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static VALUE expression_parse_pinned(VALUE opaque)
|
|
64
|
+
{
|
|
65
|
+
VALUE markup = *(VALUE *)opaque;
|
|
66
|
+
char *start = RSTRING_PTR(markup);
|
|
67
|
+
|
|
68
|
+
parser_t p;
|
|
69
|
+
init_parser(&p, start, start + RSTRING_LEN(markup));
|
|
70
|
+
VALUE expr_obj = internal_expression_parse(&p);
|
|
71
|
+
|
|
72
|
+
if (p.cur.type != TOKEN_EOS)
|
|
73
|
+
rb_enc_raise(utf8_encoding, cLiquidSyntaxError, "[:%s] is not a valid expression", symbol_names[p.cur.type]);
|
|
74
|
+
|
|
75
|
+
RB_GC_GUARD(markup);
|
|
76
|
+
return expr_obj;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static VALUE expression_unpin(VALUE opaque)
|
|
80
|
+
{
|
|
81
|
+
rb_gc_unregister_address((VALUE *)opaque);
|
|
82
|
+
return Qnil;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static VALUE expression_strict_parse(VALUE klass, VALUE markup)
|
|
86
|
+
{
|
|
87
|
+
if (NIL_P(markup)) return Qnil;
|
|
88
|
+
StringValue(markup);
|
|
89
|
+
markup = rb_str_dup_frozen(markup);
|
|
90
|
+
rb_gc_register_address(&markup);
|
|
91
|
+
return rb_ensure(expression_parse_pinned, (VALUE)&markup, expression_unpin, (VALUE)&markup);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
VALUE expression_evaluate(VALUE self, VALUE context)
|
|
95
|
+
{
|
|
96
|
+
expression_t *expression;
|
|
97
|
+
Expression_Get_Struct(self, expression);
|
|
98
|
+
return liquid_vm_evaluate(context, &expression->code);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
VALUE internal_expression_evaluate(expression_t *expression, VALUE context)
|
|
102
|
+
{
|
|
103
|
+
return liquid_vm_evaluate(context, &expression->code);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static VALUE expression_disassemble(VALUE self)
|
|
107
|
+
{
|
|
108
|
+
expression_t *expression;
|
|
109
|
+
Expression_Get_Struct(self, expression);
|
|
110
|
+
|
|
111
|
+
VALUE constants = rb_ary_new();
|
|
112
|
+
uint32_t constants_len = (uint32_t)(c_buffer_size(&expression->code.constants) / sizeof(VALUE));
|
|
113
|
+
rb_ary_cat(constants, (VALUE *)expression->code.constants.data, constants_len);
|
|
114
|
+
|
|
115
|
+
return vm_assembler_disassemble(
|
|
116
|
+
expression->code.instructions.data,
|
|
117
|
+
expression->code.instructions.data_end,
|
|
118
|
+
&constants,
|
|
119
|
+
0
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
void liquid_define_expression(void)
|
|
124
|
+
{
|
|
125
|
+
cLiquidCExpression = rb_define_class_under(mLiquidC, "Expression", rb_cObject);
|
|
126
|
+
rb_undef_alloc_func(cLiquidCExpression);
|
|
127
|
+
rb_define_singleton_method(cLiquidCExpression, "strict_parse", expression_strict_parse, 1);
|
|
128
|
+
rb_define_method(cLiquidCExpression, "evaluate", expression_evaluate, 1);
|
|
129
|
+
rb_define_method(cLiquidCExpression, "disassemble", expression_disassemble, 0);
|
|
130
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#if !defined(LIQUID_EXPRESSION_H)
|
|
2
|
+
#define LIQUID_EXPRESSION_H
|
|
3
|
+
|
|
4
|
+
#include "vm_assembler.h"
|
|
5
|
+
#include "parser.h"
|
|
6
|
+
|
|
7
|
+
extern VALUE cLiquidCExpression;
|
|
8
|
+
extern const rb_data_type_t expression_data_type;
|
|
9
|
+
|
|
10
|
+
typedef struct expression {
|
|
11
|
+
vm_assembler_t code;
|
|
12
|
+
} expression_t;
|
|
13
|
+
|
|
14
|
+
extern const rb_data_type_t expression_data_type;
|
|
15
|
+
#define Expression_Get_Struct(obj, sval) TypedData_Get_Struct(obj, expression_t, &expression_data_type, sval)
|
|
16
|
+
|
|
17
|
+
void liquid_define_expression(void);
|
|
18
|
+
|
|
19
|
+
VALUE expression_new(VALUE klass, expression_t **expression_ptr);
|
|
20
|
+
VALUE expression_evaluate(VALUE self, VALUE context);
|
|
21
|
+
VALUE internal_expression_evaluate(expression_t *expression, VALUE context);
|
|
22
|
+
|
|
23
|
+
#endif
|
|
24
|
+
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "mkmf"
|
|
4
|
+
$defs << "-DLIQUID_C_TEST_ALLOC_FAILURES" if ENV["LIQUID_C_TEST_ALLOC_FAILURES"] == "true"
|
|
5
|
+
$CFLAGS << " -std=c11 -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers"
|
|
6
|
+
append_cflags("-fvisibility=hidden")
|
|
7
|
+
# In Ruby 2.6 and earlier, the Ruby headers did not have struct timespec defined
|
|
8
|
+
valid_headers = RbConfig::CONFIG["host_os"] !~ /linux/ || Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7")
|
|
9
|
+
pedantic = !ENV["LIQUID_C_PEDANTIC"].to_s.empty?
|
|
10
|
+
if pedantic && valid_headers
|
|
11
|
+
$CFLAGS << " -Werror"
|
|
12
|
+
end
|
|
13
|
+
if ENV["DEBUG"] == "true"
|
|
14
|
+
append_cflags("-fbounds-check")
|
|
15
|
+
CONFIG["optflags"] = " -O0"
|
|
16
|
+
# Hack to enable assertions since ruby/assert.h disables assertions unless
|
|
17
|
+
# Ruby was compiled with -DRUBY_DEBUG.
|
|
18
|
+
# https://github.com/ruby/ruby/blob/9e678cdbd054f78576a8f21b3f97cccc395ade22/include/ruby/assert.h#L36-L41
|
|
19
|
+
$CFLAGS << (RUBY_ENGINE == "ruby" ? " -DRUBY_DEBUG" : " -DNDEBUG")
|
|
20
|
+
else
|
|
21
|
+
$CFLAGS << " -DNDEBUG"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
have_func "rb_autoload_p"
|
|
25
|
+
have_func "st_memsize", "ruby/st.h"
|
|
26
|
+
have_func("rb_method_basic_definition_p") if RUBY_ENGINE == "ruby"
|
|
27
|
+
["rb_gc_count", "rb_method_call", "rb_obj_init_copy", "rb_str_to_dbl", "rb_big_lshift", "rb_big_or"].each do |function|
|
|
28
|
+
have_func(function)
|
|
29
|
+
end
|
|
30
|
+
unless have_func("ruby_stack_check")
|
|
31
|
+
$defs << "-D_GNU_SOURCE" if RbConfig::CONFIG["host_os"].include?("linux")
|
|
32
|
+
unless have_func("pthread_getattr_np(pthread_self(), &(pthread_attr_t){0})", "pthread.h") ||
|
|
33
|
+
have_func("pthread_get_stackaddr_np(pthread_self())", "pthread.h")
|
|
34
|
+
abort("Native stack bounds are unavailable on this platform")
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
# Link tests against a static libruby can find hidden symbols which extensions
|
|
38
|
+
# cannot resolve at load time (MRI 3.3+). Probe the running process as well.
|
|
39
|
+
def mri_exports?(symbol)
|
|
40
|
+
return false unless RUBY_ENGINE == "ruby"
|
|
41
|
+
|
|
42
|
+
require "fiddle"
|
|
43
|
+
Fiddle::Handle::DEFAULT[symbol]
|
|
44
|
+
rescue LoadError, Fiddle::DLError
|
|
45
|
+
false
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
["rb_callable_method_entry", "rb_hash_compare_by_id_p"].each do |function|
|
|
49
|
+
have_func(function) if mri_exports?(function)
|
|
50
|
+
end
|
|
51
|
+
if mri_exports?("ruby_vm_event_flags") && try_link(<<~C)
|
|
52
|
+
#define HAVE_RUBY_VM_EVENT_FLAGS 1
|
|
53
|
+
#include "mri_compat.h"
|
|
54
|
+
int main(void) { return liquid_vm_tracing(); }
|
|
55
|
+
C
|
|
56
|
+
$defs << "-DHAVE_RUBY_VM_EVENT_FLAGS"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
$warnflags&.gsub!("-Wdeclaration-after-statement", "")
|
|
60
|
+
create_makefile("liquid_c")
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#ifndef LIQUID_INTUTIL_H
|
|
2
|
+
#define LIQUID_INTUTIL_H
|
|
3
|
+
|
|
4
|
+
#include <stdint.h>
|
|
5
|
+
|
|
6
|
+
static inline uint32_t bytes_to_uint32(const uint8_t *bytes)
|
|
7
|
+
{
|
|
8
|
+
return ((uint32_t)bytes[0] << 24) | ((uint32_t)bytes[1] << 16) |
|
|
9
|
+
((uint32_t)bytes[2] << 8) | bytes[3];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static inline void uint32_to_bytes(uint32_t num, uint8_t *bytes)
|
|
13
|
+
{
|
|
14
|
+
bytes[0] = num >> 24;
|
|
15
|
+
bytes[1] = num >> 16;
|
|
16
|
+
bytes[2] = num >> 8;
|
|
17
|
+
bytes[3] = num;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static inline unsigned int bytes_to_uint24(const uint8_t *bytes)
|
|
21
|
+
{
|
|
22
|
+
return (bytes[0] << 16) | (bytes[1] << 8) | bytes[2];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static inline void uint24_to_bytes(unsigned int num, uint8_t *bytes)
|
|
26
|
+
{
|
|
27
|
+
if (num >= (1U << 24)) rb_raise(rb_eRangeError, "Value exceeds 24-bit bytecode operand");
|
|
28
|
+
|
|
29
|
+
bytes[0] = num >> 16;
|
|
30
|
+
bytes[1] = num >> 8;
|
|
31
|
+
bytes[2] = num;
|
|
32
|
+
|
|
33
|
+
assert(bytes_to_uint24(bytes) == num);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
#endif
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
#include "liquid.h"
|
|
2
|
+
#include "lexer.h"
|
|
3
|
+
#include "usage.h"
|
|
4
|
+
#include <stdio.h>
|
|
5
|
+
|
|
6
|
+
const char *symbol_names[TOKEN_END] = {
|
|
7
|
+
[TOKEN_NONE] = "none",
|
|
8
|
+
[TOKEN_COMPARISON] = "comparison",
|
|
9
|
+
[TOKEN_STRING] = "string",
|
|
10
|
+
[TOKEN_NUMBER] = "number",
|
|
11
|
+
[TOKEN_IDENTIFIER] = "id",
|
|
12
|
+
[TOKEN_DOTDOT] = "dotdot",
|
|
13
|
+
[TOKEN_EOS] = "end_of_string",
|
|
14
|
+
[TOKEN_PIPE] = "pipe",
|
|
15
|
+
[TOKEN_DOT] = "dot",
|
|
16
|
+
[TOKEN_COLON] = "colon",
|
|
17
|
+
[TOKEN_COMMA] = "comma",
|
|
18
|
+
[TOKEN_OPEN_SQUARE] = "open_square",
|
|
19
|
+
[TOKEN_CLOSE_SQUARE] = "close_square",
|
|
20
|
+
[TOKEN_OPEN_ROUND] = "open_round",
|
|
21
|
+
[TOKEN_CLOSE_ROUND] = "close_round",
|
|
22
|
+
[TOKEN_QUESTION] = "question",
|
|
23
|
+
[TOKEN_DASH] = "dash"
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
inline static int is_identifier(char c)
|
|
27
|
+
{
|
|
28
|
+
return ISALNUM(c) || c == '_' || c == '-';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
inline static int is_special(char c)
|
|
32
|
+
{
|
|
33
|
+
switch (c) {
|
|
34
|
+
case '|': case '.': case ':': case ',':
|
|
35
|
+
case '[': case ']': case '(': case ')':
|
|
36
|
+
case '?': case '-':
|
|
37
|
+
return 1;
|
|
38
|
+
}
|
|
39
|
+
return 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
inline static const char *scan_past(const char *cur, const char *end, char target)
|
|
43
|
+
{
|
|
44
|
+
const char *match = memchr(cur + 1, target, end - cur - 1);
|
|
45
|
+
return match ? match + 1 : NULL;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
#define RETURN_TOKEN(t, n) { \
|
|
49
|
+
const char *tok_end = str + (n); \
|
|
50
|
+
token->type = (t); \
|
|
51
|
+
token->val = str; \
|
|
52
|
+
return (token->val_end = tok_end); \
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Reads one token from start, and fills it into the token argument.
|
|
56
|
+
// Returns the start of the next token if any, otherwise the end of the string.
|
|
57
|
+
const char *lex_one(const char *start, const char *end, lexer_token_t *token, unsigned char previous_type)
|
|
58
|
+
{
|
|
59
|
+
// str references the start of the token, after whitespace is skipped.
|
|
60
|
+
// cur references the currently processing character during iterative lexing.
|
|
61
|
+
const char *str = start, *cur;
|
|
62
|
+
|
|
63
|
+
while (str < end && ISSPACE(*str)) ++str;
|
|
64
|
+
|
|
65
|
+
token->val = token->val_end = NULL;
|
|
66
|
+
token->flags = 0;
|
|
67
|
+
|
|
68
|
+
if (str >= end) return str;
|
|
69
|
+
|
|
70
|
+
char c = *str; // First character of the token.
|
|
71
|
+
char cn = '\0'; // Second character if available, for lookahead.
|
|
72
|
+
if (str + 1 < end) cn = str[1];
|
|
73
|
+
|
|
74
|
+
switch (c) {
|
|
75
|
+
case '<':
|
|
76
|
+
RETURN_TOKEN(TOKEN_COMPARISON, cn == '>' || cn == '=' ? 2 : 1);
|
|
77
|
+
case '>':
|
|
78
|
+
RETURN_TOKEN(TOKEN_COMPARISON, cn == '=' ? 2 : 1);
|
|
79
|
+
case '=':
|
|
80
|
+
case '!':
|
|
81
|
+
if (cn == '=') RETURN_TOKEN(TOKEN_COMPARISON, 2);
|
|
82
|
+
break;
|
|
83
|
+
case '.':
|
|
84
|
+
if (cn == '.') RETURN_TOKEN(TOKEN_DOTDOT, 2);
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (c == '\'' || c == '"') {
|
|
89
|
+
cur = scan_past(str, end, c);
|
|
90
|
+
|
|
91
|
+
if (cur) {
|
|
92
|
+
// Quote was properly terminated.
|
|
93
|
+
RETURN_TOKEN(TOKEN_STRING, cur - str);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Instrument for bug: https://github.com/Shopify/liquid-c/pull/120
|
|
98
|
+
if (c == '-' && str + 1 < end && str[1] == '.') {
|
|
99
|
+
usage_increment("liquid_c_negative_float_without_integer");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (ISDIGIT(c) || (c == '-' && ISDIGIT(cn))) {
|
|
103
|
+
int has_dot = 0;
|
|
104
|
+
cur = str;
|
|
105
|
+
while (++cur < end) {
|
|
106
|
+
if (!has_dot && *cur == '.') {
|
|
107
|
+
has_dot = 1;
|
|
108
|
+
} else if (!ISDIGIT(*cur)) {
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
cur--; // Point to last digit (or dot).
|
|
113
|
+
|
|
114
|
+
if (*cur == '.') {
|
|
115
|
+
cur--; // Ignore any trailing dot.
|
|
116
|
+
has_dot = 0;
|
|
117
|
+
}
|
|
118
|
+
if (*cur != '-') {
|
|
119
|
+
if (has_dot) token->flags |= TOKEN_FLOAT_NUMBER;
|
|
120
|
+
RETURN_TOKEN(TOKEN_NUMBER, cur + 1 - str);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (ISALPHA(c) || c == '_') {
|
|
125
|
+
cur = str;
|
|
126
|
+
while (++cur < end && is_identifier(*cur)) {}
|
|
127
|
+
if (cur < end && *cur == '?') cur++;
|
|
128
|
+
if (previous_type != TOKEN_DOT && cur - str == 8 && memcmp(str, "contains", 8) == 0)
|
|
129
|
+
RETURN_TOKEN(TOKEN_COMPARISON, 8);
|
|
130
|
+
RETURN_TOKEN(TOKEN_IDENTIFIER, cur - str);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (is_special(c)) RETURN_TOKEN(c, 1);
|
|
134
|
+
|
|
135
|
+
long remaining_str_len = end - str;
|
|
136
|
+
int char_len = 0;
|
|
137
|
+
|
|
138
|
+
// read multibyte UTF-8 character
|
|
139
|
+
if ((c & 0x80) == 0) {
|
|
140
|
+
// 1-byte character
|
|
141
|
+
char_len = 1;
|
|
142
|
+
} else if ((c & 0xE0) == 0xC0) {
|
|
143
|
+
// 2-byte character
|
|
144
|
+
if (remaining_str_len >= 2) {
|
|
145
|
+
char_len = 2;
|
|
146
|
+
}
|
|
147
|
+
} else if ((c & 0xF0) == 0xE0) {
|
|
148
|
+
// 3-byte character
|
|
149
|
+
if (remaining_str_len >= 3) {
|
|
150
|
+
char_len = 3;
|
|
151
|
+
}
|
|
152
|
+
} else if ((c & 0xF8) == 0xF0) {
|
|
153
|
+
// 4-byte character
|
|
154
|
+
if (remaining_str_len >= 4) {
|
|
155
|
+
char_len = 4;
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
// this should never happen
|
|
159
|
+
rb_enc_raise(utf8_encoding, cLiquidSyntaxError, "Unexpected character %c", c);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (char_len > 0) {
|
|
163
|
+
rb_enc_raise(utf8_encoding, cLiquidSyntaxError, "Unexpected character %.*s", char_len, str);
|
|
164
|
+
} else {
|
|
165
|
+
rb_enc_raise(utf8_encoding, cLiquidSyntaxError, "Invalid byte sequence in UTF-8");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return NULL;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
#undef RETURN_TOKEN
|
|
172
|
+
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
#if !defined(LIQUID_LEXER_H)
|
|
2
|
+
#define LIQUID_LEXER_H
|
|
3
|
+
|
|
4
|
+
enum lexer_token_type {
|
|
5
|
+
TOKEN_NONE,
|
|
6
|
+
TOKEN_COMPARISON,
|
|
7
|
+
TOKEN_STRING,
|
|
8
|
+
TOKEN_NUMBER,
|
|
9
|
+
TOKEN_IDENTIFIER,
|
|
10
|
+
TOKEN_DOTDOT,
|
|
11
|
+
TOKEN_EOS,
|
|
12
|
+
|
|
13
|
+
TOKEN_PIPE = '|',
|
|
14
|
+
TOKEN_DOT = '.',
|
|
15
|
+
TOKEN_COLON = ':',
|
|
16
|
+
TOKEN_COMMA = ',',
|
|
17
|
+
TOKEN_OPEN_SQUARE = '[',
|
|
18
|
+
TOKEN_CLOSE_SQUARE = ']',
|
|
19
|
+
TOKEN_OPEN_ROUND = '(',
|
|
20
|
+
TOKEN_CLOSE_ROUND = ')',
|
|
21
|
+
TOKEN_QUESTION = '?',
|
|
22
|
+
TOKEN_DASH = '-',
|
|
23
|
+
|
|
24
|
+
TOKEN_END = 256
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
#define TOKEN_FLOAT_NUMBER 0x4
|
|
28
|
+
|
|
29
|
+
typedef struct lexer_token {
|
|
30
|
+
unsigned char type, flags;
|
|
31
|
+
const char *val, *val_end;
|
|
32
|
+
} lexer_token_t;
|
|
33
|
+
|
|
34
|
+
extern const char *symbol_names[TOKEN_END];
|
|
35
|
+
|
|
36
|
+
const char *lex_one(const char *str, const char *end, lexer_token_t *token, unsigned char previous_type);
|
|
37
|
+
|
|
38
|
+
inline static VALUE token_to_rstr(lexer_token_t token) {
|
|
39
|
+
return rb_enc_str_new(token.val, token.val_end - token.val, utf8_encoding);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
inline static VALUE token_check_for_symbol(lexer_token_t token) {
|
|
43
|
+
return rb_check_symbol_cstr(token.val, token.val_end - token.val, utf8_encoding);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
inline static VALUE token_to_rstr_leveraging_existing_symbol(lexer_token_t token) {
|
|
47
|
+
VALUE sym = token_check_for_symbol(token);
|
|
48
|
+
if (RB_LIKELY(sym != Qnil))
|
|
49
|
+
return rb_sym2str(sym);
|
|
50
|
+
return token_to_rstr(token);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
inline static VALUE token_to_rsym(lexer_token_t token) {
|
|
54
|
+
VALUE sym = token_check_for_symbol(token);
|
|
55
|
+
if (RB_LIKELY(sym != Qnil))
|
|
56
|
+
return sym;
|
|
57
|
+
return rb_str_intern(token_to_rstr(token));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
#endif
|
|
61
|
+
|