3#include "prism/internal/assert.h"
4#include "prism/internal/char.h"
5#include "prism/internal/encoding.h"
6#include "prism/internal/memequal.h"
7#include "prism/internal/options.h"
8#include "prism/internal/parser.h"
15 PM_SLICE_TYPE_ERROR_VERSION = -2,
18 PM_SLICE_TYPE_ERROR = -1,
27 PM_SLICE_TYPE_CONSTANT,
30 PM_SLICE_TYPE_METHOD_NAME
39pm_slice_type(
const uint8_t *source,
size_t length,
const char *encoding_name,
size_t encoding_length,
const char *version,
size_t version_length) {
41 const uint8_t *name = (
const uint8_t *) encoding_name;
42 const pm_encoding_t *encoding = pm_encoding_find(name, name + encoding_length);
43 if (encoding == NULL)
return PM_SLICE_TYPE_ERROR;
46 pm_options_version_t target;
47 if (!pm_options_version_parse(version, version_length, &target))
return PM_SLICE_TYPE_ERROR_VERSION;
50 if (length == 0)
return PM_SLICE_TYPE_NONE;
55 if (*source >= 0x80) {
56 if ((width = pm_encoding_char_width(encoding, source, (ptrdiff_t) length)) == 0)
return PM_SLICE_TYPE_NONE;
57 }
else if (pm_encoding_ascii_alpha_p(*source) || (*source ==
'_')) {
60 return PM_SLICE_TYPE_NONE;
64 const uint8_t *end = source + length;
65 pm_slice_type_t result = pm_encoding_isupper_char(encoding, source, end - source, target) ? PM_SLICE_TYPE_CONSTANT : PM_SLICE_TYPE_LOCAL;
71 while (source < end) {
72 if (*source >= 0x80) {
73 if ((width = pm_encoding_char_width(encoding, source, end - source)) == 0)
break;
75 }
else if (pm_encoding_ascii_alnum_p(*source) || (*source ==
'_')) {
83 if (source < end && (*source ==
'!' || *source ==
'?' || *source ==
'=')) {
85 result = PM_SLICE_TYPE_METHOD_NAME;
89 return source == end ? result : PM_SLICE_TYPE_NONE;
96pm_string_query_local(
const uint8_t *source,
size_t length,
const char *encoding_name,
size_t encoding_length,
const char *version,
size_t version_length) {
97 switch (pm_slice_type(source, length, encoding_name, encoding_length, version, version_length)) {
98 case PM_SLICE_TYPE_ERROR_VERSION:
100 case PM_SLICE_TYPE_ERROR:
102 case PM_SLICE_TYPE_NONE:
103 case PM_SLICE_TYPE_CONSTANT:
104 case PM_SLICE_TYPE_METHOD_NAME:
106 case PM_SLICE_TYPE_LOCAL:
110 PRISM_ASSERT(
false &&
"unreachable");
118pm_string_query_constant(
const uint8_t *source,
size_t length,
const char *encoding_name,
size_t encoding_length,
const char *version,
size_t version_length) {
119 switch (pm_slice_type(source, length, encoding_name, encoding_length, version, version_length)) {
120 case PM_SLICE_TYPE_ERROR_VERSION:
122 case PM_SLICE_TYPE_ERROR:
124 case PM_SLICE_TYPE_NONE:
125 case PM_SLICE_TYPE_LOCAL:
126 case PM_SLICE_TYPE_METHOD_NAME:
128 case PM_SLICE_TYPE_CONSTANT:
132 PRISM_ASSERT(
false &&
"unreachable");
140pm_string_query_method_name(
const uint8_t *source,
size_t length,
const char *encoding_name,
size_t encoding_length,
const char *version,
size_t version_length) {
141#define B(p) ((p) ? PM_STRING_QUERY_TRUE : PM_STRING_QUERY_FALSE)
142#define C1(c) (*source == c)
143#define C2(s) pm_memequal(source, s, 2)
144#define C3(s) pm_memequal(source, s, 3)
146 switch (pm_slice_type(source, length, encoding_name, encoding_length, version, version_length)) {
147 case PM_SLICE_TYPE_ERROR_VERSION:
149 case PM_SLICE_TYPE_ERROR:
151 case PM_SLICE_TYPE_NONE:
153 case PM_SLICE_TYPE_LOCAL:
155 return B((length != 2) || (source[0] !=
'_') || (source[1] ==
'0') || !pm_char_is_decimal_digit(source[1]));
156 case PM_SLICE_TYPE_CONSTANT:
158 case PM_SLICE_TYPE_METHOD_NAME:
165 return B(C1(
'&') || C1(
'`') || C1(
'!') || C1(
'^') || C1(
'>') || C1(
'<') || C1(
'-') || C1(
'%') || C1(
'|') || C1(
'+') || C1(
'/') || C1(
'*') || C1(
'~'));
167 return B(C2(
"!=") || C2(
"!~") || C2(
"[]") || C2(
"==") || C2(
"=~") || C2(
">=") || C2(
">>") || C2(
"<=") || C2(
"<<") || C2(
"**"));
169 return B(C3(
"===") || C3(
"<=>") || C3(
"[]="));
187pm_string_query_keyword(
const uint8_t *source,
size_t length,
const char *encoding_name,
size_t encoding_length,
const char *version,
size_t version_length) {
188 const uint8_t *name = (
const uint8_t *) encoding_name;
189 if (pm_encoding_find(name, name + encoding_length) == NULL) {
193 if (version != NULL && !pm_options_version_supported_p(version, version_length)) {
Functions for querying properties of strings, such as whether they are valid local variable names,...
pm_string_query_t
Represents the results of a slice query.
@ PM_STRING_QUERY_TRUE
Returned if the result of the slice query is true.
@ PM_STRING_QUERY_ERROR
Returned if the encoding given to a slice query was invalid.
@ PM_STRING_QUERY_FALSE
Returned if the result of the slice query is false.
@ PM_STRING_QUERY_ERROR_VERSION
Returned if the version given to a slice query was invalid.