-
+/* MIT (BSD) license - see LICENSE file for details */
#include "graphql.h"
#include "ccan/tal/str/str.h"
#define NAME_START(c) (((c) >= 0x61 && (c) <= 0x7A) || ((c) >= 0x41 && (c) <= 0x5A) || (c) == 0x5F)
#define NAME_CONTINUE(c) (NAME_START(c) || DIGIT(c))
+// Safe copy helper
+#define cpystr(d,s) { char *cpystr_p; char *cpystr_q; for(cpystr_p = (s), cpystr_q = (d); *cpystr_p;) *cpystr_q++ = *cpystr_p++; *cpystr_q++ = *cpystr_p++; }
// Parser shorthands
struct graphql_##type *obj = tal(tokens, struct graphql_##type); memset(obj, 0, sizeof(struct graphql_##type)); \
#define EXIT \
+ goto exit_label; \
exit_label: \
+ rollback_top = rollback_top; \
if (*err) obj = tal_free(obj); \
return obj; \
-#define CONSUME_ONE { list_add(used, list_pop(tokens, struct graphql_token, list)); }
-#define RESTORE_ONE { list_add(tokens, list_pop(used, struct graphql_token, list)); }
+#define CONSUME_ONE { list_add(used, (struct list_node *)list_pop(tokens, struct graphql_token, list)); }
+#define RESTORE_ONE { list_add(tokens, (struct list_node *)list_pop(used, struct graphql_token, list)); }
#define ROLLBACK(args) { while (list_top(tokens, struct graphql_token, list) != rollback_top) { RESTORE_ONE; } }
#define OR if (!*err) goto exit_label; *err = NULL;
#define REQ if (*err) { ROLLBACK(args); goto exit_label; }
struct list_head used = LIST_HEAD_INIT(used);
const char *err = NULL;
*doc = parse_executable_document(tokens, &used, &err);
- if (err)
- return err;
+ return err;
}
/* The following parser functions follow special rules:
RET parse_keyword(PARAMS, const char *keyword, const char *errmsg) {
struct graphql_token *tok = list_top(tokens, struct graphql_token, list);
if (!tok || tok->token_type != 'a') {
- *err = errmsg; return;
+ *err = errmsg; return NULL;
}
if (!streq(tok->token_string, keyword)) {
- *err = errmsg; return;
+ *err = errmsg; return NULL;
}
- CONSUME_ONE(ARGS);
+ CONSUME_ONE;
return tok;
}
sprintf(punctbuf, "expected: '...'");
else
sprintf(punctbuf, "expected: '%c'", punct);
- *err = punctbuf; return;
+ *err = punctbuf; return NULL;
}
- CONSUME_ONE(ARGS);
+ CONSUME_ONE;
return tok;
}
RET parse_name(PARAMS) {
struct graphql_token *tok = list_top(tokens, struct graphql_token, list);
if (!tok || tok->token_type != 'a') {
- *err = "name expected"; return 0;
+ *err = "name expected"; return NULL;
}
- CONSUME_ONE(ARGS);
+ CONSUME_ONE;
return tok;
}
RET parse_int(PARAMS) {
struct graphql_token *tok = list_top(tokens, struct graphql_token, list);
if (!tok || tok->token_type != 'i') {
- *err = "integer expected"; return;
+ *err = "integer expected"; return NULL;
}
- CONSUME_ONE(ARGS);
+ CONSUME_ONE;
return tok;
}
RET parse_float(PARAMS) {
struct graphql_token *tok = list_top(tokens, struct graphql_token, list);
if (!tok || tok->token_type != 'f') {
- *err = "float expected"; return;
+ *err = "float expected"; return NULL;
}
- CONSUME_ONE(ARGS);
+ CONSUME_ONE;
return tok;
}
RET parse_string(PARAMS) {
struct graphql_token *tok = list_top(tokens, struct graphql_token, list);
if (!tok || tok->token_type != 's') {
- *err = "string expected"; return;
+ *err = "string expected"; return NULL;
}
- CONSUME_ONE(ARGS);
+ CONSUME_ONE;
return tok;
}
}
tok = tal(tok_list, struct graphql_token);
- list_add_tail(tok_list, tok);
+ list_add_tail(tok_list, &tok->list);
tok->token_type = c;
tok->token_specific = c;
tok->token_string = NULL;
// Name/identifier tokens.
tok = tal(tok_list, struct graphql_token);
- list_add_tail(tok_list, tok);
+ list_add_tail(tok_list, &tok->list);
tok->token_type = 'a';
tok->token_specific = 'a';
// tok->token_string updated below.
int num_len = num_end - num_start;
tok = tal(tok_list, struct graphql_token);
- list_add_tail(tok_list, tok);
+ list_add_tail(tok_list, &tok->list);
tok->token_type = type;
tok->token_string = tal_strndup(tok, num_start, num_len);
tok->source_line = line_num;
int str_len = str_end - str_begin;
tok = tal(tok_list, struct graphql_token);
- list_add_tail(tok_list, tok);
+ list_add_tail(tok_list, &tok->list);
tok->token_type = 's';
tok->token_specific = 's';
tok->token_string = tal_strndup(tok, str_begin, str_len);
char *q = tok->token_string;
char *rewrite_dest;
int quotes = 0;
- while (d = *q++) {
+ while ((d = *q++)) {
if (str_block) {
if (d == '\"') quotes++; else quotes = 0;
if (quotes == 3 && *(q-4) == '\\') {
quotes = 0;
rewrite_dest = q - 4;
- strcpy(rewrite_dest, q - 3);
+ cpystr(rewrite_dest, q - 3);
}
} else {
if (d == '\\') {
switch (d) {
case '\"':
*rewrite_dest++ = '\"';
- strcpy(rewrite_dest, q--);
+ cpystr(rewrite_dest, q--);
break;
case 'b':
*rewrite_dest++ = '\b';
- strcpy(rewrite_dest, q--);
+ cpystr(rewrite_dest, q--);
break;
case 'f':
*rewrite_dest++ = '\f';
- strcpy(rewrite_dest, q--);
+ cpystr(rewrite_dest, q--);
break;
case 'n':
*rewrite_dest++ = '\n';
- strcpy(rewrite_dest, q--);
+ cpystr(rewrite_dest, q--);
break;
case 'r':
*rewrite_dest++ = '\r';
- strcpy(rewrite_dest, q--);
+ cpystr(rewrite_dest, q--);
break;
case 't':
*rewrite_dest++ = '\t';
- strcpy(rewrite_dest, q--);
+ cpystr(rewrite_dest, q--);
break;
case 'u': {
// Insert escaped character using UTF-8 multi-byte encoding.
int code_point = strtol(buf, 0, 16);
int bytes = utf8_encode(code_point, rewrite_dest);
rewrite_dest += bytes;
- strcpy(rewrite_dest, q--);
+ cpystr(rewrite_dest, q--);
}
break;
default:
- strcpy(rewrite_dest, --q);
+ cpystr(rewrite_dest, --q);
}
}
}
if (LINE_TERMINATOR(d)) {
while (LINE_TERMINATOR(d))
d = *q++;
- strcpy(tok->token_string, q - 1);
+ cpystr(tok->token_string, q - 1);
q = tok->token_string;
} else
break;
d = *q++;
--q;
- strcpy(this_indent_start, this_indent_start + common_indent_len);
+ cpystr(this_indent_start, this_indent_start + common_indent_len);
q -= common_indent_len;
d = *q++;
-#include "ccan/graphql/graphql.h"
+/* Include the C files directly. */
+#include "ccan/graphql/graphql.c"
#include "ccan/str/str.h"
/* TEST POINT MACROS
bool mute = 1;
// Helper function.
+int listlen(struct list_head *tokens);
int listlen(struct list_head *tokens) {
struct graphql_token *tok;
int n=0;
return n;
}
+/* Test case function prototypes */
+
+void check_example_3(const char *source);
+void check_example_5(char *source);
+void check_example_6(char *source);
+void check_example_7(char *source);
+void check_example_8(char *source);
+void check_example_9(char *source);
+void check_example_10(char *source);
+void check_example_11(char *source);
+void check_example_12_and_13(const char *source);
+void check_example_14(char *source);
+void check_example_16(char *source);
+void check_example_18(char *source);
+void check_example_19(char *source);
+void check_example_20(char *source);
+void check_example_21(char *source);
+void check_example_23(char *source);
+void check_example_24(char *source);
+void check_int_value(char *source, int int_value);
+void check_invalid_int_values(char *source);
+void check_float_value(char *source, float float_value, const char *format);
+void check_valid_float_values(char *source);
+void check_invalid_float_values(char *source);
+void check_boolean_values(char *source);
+void check_string_value(char *source, const char *test_value, const char *expected_result);
+void check_example_25_and_26(const char *source);
+void check_example_29(char *source);
+void check_example_30_and_31(const char *source);
+void check_example_32(char *source);
+void check_example_34(char *source);
+void check_example_35(char *source);
+
/* Test case functions begin here, called by main().
* Note: Memory should be freed correctly in the success case, but if there
* are errors, all bets are off.
*/
-int check_example_3(const char *source) {
+void check_example_3(const char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 3\n");
tokens = tal_free(tokens);
}
-int check_example_5(char *source) {
+void check_example_5(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 5\n");
tokens = tal_free(tokens);
}
-int check_example_6(char *source) {
+void check_example_6(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 6\n");
tokens = tal_free(tokens);
}
-int check_example_7(char *source) {
+void check_example_7(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 7\n");
tokens = tal_free(tokens);
}
-int check_example_8(char *source) {
+void check_example_8(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 8\n");
tokens = tal_free(tokens);
}
-int check_example_9(char *source) {
+void check_example_9(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 9\n");
tokens = tal_free(tokens);
}
-int check_example_10(char *source) {
+void check_example_10(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 10\n");
tokens = tal_free(tokens);
}
-int check_example_11(char *source) {
+void check_example_11(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 11\n");
tokens = tal_free(tokens);
}
-int check_example_12_and_13(const char *source) {
+void check_example_12_and_13(const char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example Nos. 12 and 13\n");
tokens = tal_free(tokens);
}
-int check_example_14(char *source) {
+void check_example_14(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 14\n");
tokens = tal_free(tokens);
}
-int check_example_16(char *source) {
+void check_example_16(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 16\n");
tokens = tal_free(tokens);
}
-int check_example_18(char *source) {
+void check_example_18(char *source) {
struct list_head *tokens;
- struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 18\n");
tokens = tal_free(tokens);
}
-int check_example_19(char *source) {
+void check_example_19(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 19\n");
tokens = tal_free(tokens);
}
-int check_example_20(char *source) {
+void check_example_20(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 20\n");
tokens = tal_free(tokens);
}
-int check_example_21(char *source) {
+void check_example_21(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 21\n");
tokens = tal_free(tokens);
}
-int check_example_23(char *source) {
+void check_example_23(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 23\n");
tokens = tal_free(tokens);
}
-int check_example_24(char *source) {
+void check_example_24(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 24\n");
tokens = tal_free(tokens);
}
-int check_int_value(char *source, int int_value) {
+void check_int_value(char *source, int int_value) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Int Value Range Check on %d\n", int_value);
tokens = tal_free(tokens);
}
-int check_invalid_int_values(char *source) {
+void check_invalid_int_values(char *source) {
struct list_head *tokens;
- struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Invalid Int Values\n");
- char *bad_values[] = {"00", "-00", "+1", "1.", "1a", "1e", "0x123", "123L", 0};
+ const char *bad_values[] = {"00", "-00", "+1", "1.", "1a", "1e", "0x123", "123L", 0};
for (int i=0; bad_values[i]; i++) {
sprintf(source, "\
}
}
-int check_float_value(char *source, float float_value, const char *format) {
+void check_float_value(char *source, float float_value, const char *format) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Float Value Range Check on %f\n", float_value);
tokens = tal_free(tokens);
}
-int check_valid_float_values(char *source) {
+void check_valid_float_values(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Valid Float Values\n");
- char *good_values[] = {"1.0", "1e50", "6.0221413e23", "1.23", 0};
+ const char *good_values[] = {"1.0", "1e50", "6.0221413e23", "1.23", 0};
for (int i=0; good_values[i]; i++) {
sprintf(source, "\
}
}
-int check_invalid_float_values(char *source) {
+void check_invalid_float_values(char *source) {
struct list_head *tokens;
- struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Invalid Float Values\n");
- char *bad_values[] = {"00.0", "-00.0", "00e1", "1.23.4", "0x1.2p3", 0};
+ const char *bad_values[] = {"00.0", "-00.0", "00e1", "1.23.4", "0x1.2p3", 0};
for (int i=0; bad_values[i]; i++) {
sprintf(source, "\
}
}
-int check_boolean_values(char *source) {
+void check_boolean_values(char *source) {
struct list_head *tokens;
- struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Boolean Values\n");
- char *good_values[] = {"true", "false", 0};
+ const char *good_values[] = {"true", "false", 0};
for (int i=0; good_values[i]; i++) {
sprintf(source, "\
tokens = tal_free(tokens);
}
- char *bad_values[] = {"True", "False", "TRUE", "FALSE", 0};
+ const char *bad_values[] = {"True", "False", "TRUE", "FALSE", 0};
for (int i=0; bad_values[i]; i++) {
sprintf(source, "\
}
}
-int check_string_value(char *source, const char *test_value, const char *expected_result) {
+void check_string_value(char *source, const char *test_value, const char *expected_result) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// String Value Test: %s\n", test_value);
}
}
-int check_example_25_and_26(const char *source) {
+void check_example_25_and_26(const char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 25 and 26\n");
tokens = tal_free(tokens);
}
-int check_example_29(char *source) {
+void check_example_29(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 29\n");
tokens = tal_free(tokens);
}
-int check_example_30_and_31(const char *source) {
+void check_example_30_and_31(const char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 30 and 31\n");
tokens = tal_free(tokens);
// Test the parser.
- struct graphql_argument *arg;
struct graphql_executable_document *doc;
const char *e;
TEST_CONT(graphql_lex(source, NULL, &tokens) == NULL);
tokens = tal_free(tokens);
}
-int check_example_32(char *source) {
+void check_example_32(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 32\n");
tokens = tal_free(tokens);
}
-int check_example_34(char *source) {
+void check_example_34(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 34\n");
tokens = tal_free(tokens);
}
-int check_example_35(char *source) {
+void check_example_35(char *source) {
struct list_head *tokens;
struct graphql_token *tok;
- const char *err;
if (!mute) printf("// Example No. 35\n");
int prev_fail; // Used by RUNx macros.
// Check the lexer with all valid line terminators.
- char *new_line = "\n";
- char *carriage_return = "\r";
- char *carriage_return_new_line = "\r\n";
- char *line_terminators[] = { new_line, carriage_return, carriage_return_new_line };
+ const char *new_line = "\n";
+ const char *carriage_return = "\r";
+ const char *carriage_return_new_line = "\r\n";
+ const char *line_terminators[] = { new_line, carriage_return, carriage_return_new_line };
for (int i=0; i<3; i++) {
sprintf(source, "\
{%s\