4 #include "ccan/tal/str/str.h"
5 #include "ccan/utf8/utf8.h"
8 // GraphQL character classes
10 #define SOURCE_CHAR(c) ((c) == 9 || (c) == 10 || (c) == 13 || ((c) >= 32 && (c) <= 65535))
11 #define WHITE_SPACE(c) ((c) == 9 || (c) == 32)
12 #define LINE_TERMINATOR(c) ((c) == 10 || (c) == 13)
13 #define COMMENT(c) ((c) == 35)
14 #define COMMENT_CHAR(c) (SOURCE_CHAR(c) && !LINE_TERMINATOR(c))
15 #define STRING_CHAR(c) (SOURCE_CHAR(c) && !LINE_TERMINATOR(c) && (c)!='"' && (c)!='\\')
16 #define BLOCK_STRING_CHAR(c) (SOURCE_CHAR(c))
17 #define COMMA(c) ((c) == 44)
18 #define EOF_CHAR(c) ((c) == 0 || (c) == 4)
19 #define PUNCTUATOR(c) (strchr("!$&().:=@[]{|}", c))
20 #define HEX_DIGIT(c) (DIGIT(c) || ((c) >= 0x61 && (c) <= 0x66) || ((c) >= 0x41 && (c) <= 0x46))
21 #define DIGIT(c) ((c) >= 0x30 && (c) <= 0x39)
22 #define NAME_START(c) (((c) >= 0x61 && (c) <= 0x7A) || ((c) >= 0x41 && (c) <= 0x5A) || (c) == 0x5F)
23 #define NAME_CONTINUE(c) (NAME_START(c) || DIGIT(c))
29 #define PARAMS struct list_head *tokens, struct list_head *used, const char **err
30 #define ARGS tokens, used, err
32 struct graphql_token *rollback_top = list_top(tokens, struct graphql_token, list); \
33 struct graphql_##type *obj = tal(tokens, struct graphql_##type); memset(obj, 0, sizeof(struct graphql_##type)); \
37 if (*err) obj = tal_free(obj); \
40 #define CONSUME_ONE { list_add(used, list_pop(tokens, struct graphql_token, list)); }
41 #define RESTORE_ONE { list_add(tokens, list_pop(used, struct graphql_token, list)); }
42 #define ROLLBACK(args) { while (list_top(tokens, struct graphql_token, list) != rollback_top) { RESTORE_ONE; } }
43 #define OR if (!*err) goto exit_label; *err = NULL;
44 #define REQ if (*err) { ROLLBACK(args); goto exit_label; }
45 #define OPT *err = NULL;
46 #define WHILE_OPT while(!*err); *err = NULL;
47 #define LOOKAHEAD(args, tok) struct graphql_token *tok = list_top(tokens, struct graphql_token, list);
48 #define MSG(msg) if (*err) *err = msg;
53 RET parse_document(PARAMS);
54 RET parse_definition(PARAMS);
55 RET parse_executable_document(PARAMS);
56 RET parse_executable_definition(PARAMS);
57 RET parse_operation_definition(PARAMS);
58 RET parse_operation_type(PARAMS);
59 RET parse_selection_set(PARAMS);
60 RET parse_selection(PARAMS);
61 RET parse_field(PARAMS);
62 RET parse_arguments(PARAMS);
63 RET parse_argument(PARAMS);
64 RET parse_alias(PARAMS);
65 RET parse_fragment_spread(PARAMS);
66 RET parse_fragment_definition(PARAMS);
67 RET parse_fragment_name(PARAMS);
68 RET parse_type_condition(PARAMS);
69 RET parse_inline_fragment(PARAMS);
70 RET parse_value(PARAMS);
71 RET parse_int_value(PARAMS);
72 RET parse_negative_sign(PARAMS);
73 RET parse_non_zero_digit(PARAMS);
74 RET parse_float_value(PARAMS);
75 RET parse_boolean_value(PARAMS);
76 RET parse_string_value(PARAMS);
77 RET parse_string_character(PARAMS);
78 RET parse_escaped_unicode(PARAMS);
79 RET parse_escaped_character(PARAMS);
80 RET parse_block_string_character(PARAMS);
81 RET parse_null_value(PARAMS);
82 RET parse_enum_value(PARAMS);
83 RET parse_list_value(PARAMS);
84 RET parse_object_value(PARAMS);
85 RET parse_object_field(PARAMS);
86 RET parse_variable(PARAMS);
87 RET parse_variable_definitions(PARAMS);
88 RET parse_variable_definition(PARAMS);
89 RET parse_default_value(PARAMS);
90 RET parse_type(PARAMS);
91 RET parse_named_type(PARAMS);
92 RET parse_list_type(PARAMS);
93 RET parse_non_null_type(PARAMS);
94 RET parse_non_null_type_1(PARAMS);
95 RET parse_non_null_type_2(PARAMS);
96 RET parse_directives(PARAMS);
97 RET parse_directive(PARAMS);
98 RET parse_type_system_document(PARAMS);
99 RET parse_type_system_definition(PARAMS);
100 RET parse_type_system_extension_document(PARAMS);
101 RET parse_type_system_definition_or_extension(PARAMS);
102 RET parse_type_system_extension(PARAMS);
103 RET parse_description(PARAMS);
104 RET parse_schema_definition(PARAMS);
105 RET parse_root_operation_type_definition(PARAMS);
106 RET parse_schema_extension(PARAMS);
107 RET parse_type_definition(PARAMS);
108 RET parse_type_extension(PARAMS);
109 RET parse_scalar_type_definition(PARAMS);
110 RET parse_scalar_type_extension(PARAMS);
111 RET parse_object_type_definition(PARAMS);
112 RET parse_implements_interfaces(PARAMS);
113 RET parse_fields_definition(PARAMS);
114 RET parse_field_definition(PARAMS);
115 RET parse_arguments_definition(PARAMS);
116 RET parse_input_value_definition(PARAMS);
117 RET parse_object_type_extension(PARAMS);
118 RET parse_interface_type_definition(PARAMS);
119 RET parse_interface_type_extension(PARAMS);
120 RET parse_union_type_definition(PARAMS);
121 RET parse_union_member_types(PARAMS);
122 RET parse_union_type_extension(PARAMS);
123 RET parse_enum_type_definition(PARAMS);
124 RET parse_enum_values_definition(PARAMS);
125 RET parse_enum_value_definition(PARAMS);
126 RET parse_enum_type_extension(PARAMS);
127 RET parse_input_object_type_definition(PARAMS);
128 RET parse_input_fields_definition(PARAMS);
129 RET parse_directive_definition(PARAMS);
130 RET parse_directive_locations(PARAMS);
131 RET parse_directive_location(PARAMS);
132 RET parse_executable_directive_location(PARAMS);
133 RET parse_type_system_directive_location(PARAMS);
135 RET parse_keyword(PARAMS, const char *keyword, const char *errmsg);
136 RET parse_punct(PARAMS, int punct);
137 RET parse_name(PARAMS);
138 RET parse_int(PARAMS);
139 RET parse_float(PARAMS);
140 RET parse_string(PARAMS);
142 // Convert input string into AST.
143 const char *graphql_lexparse(const char *input, const tal_t *ctx, struct list_head **tokens, struct graphql_executable_document **doc) {
144 const char *err = graphql_lex(input, ctx, tokens);
146 err = graphql_parse(*tokens, doc);
150 // Convert lexed tokens into AST.
151 const char *graphql_parse(struct list_head *tokens, struct graphql_executable_document **doc) {
152 struct list_head used = LIST_HEAD_INIT(used);
153 const char *err = NULL;
154 *doc = parse_executable_document(tokens, &used, &err);
159 /* The following parser functions follow special rules:
160 * - The declaration is standardized with RET and PARAMS
161 * - The "err" argument is assumed to be NULL upon entrance
162 * - The "err" argument is set on failure
163 * - If the function fails to parse, then "tokens" shall be as it was upon entrance
164 * - INIT and EXIT macros are used
165 * - Macros such as REQ and OPT facilitate readability and conciseness
168 RET parse_document(PARAMS) {
170 obj->first_def = parse_definition(ARGS); REQ
171 struct graphql_definition *p = obj->first_def;
173 p->next_def = parse_definition(ARGS);
179 RET parse_definition(PARAMS) {
181 obj->executable_def = parse_executable_definition(ARGS);
183 obj->type_system_def = parse_type_system_definition_or_extension(ARGS);
184 // NOTE: Optional type system is not (yet) implemented.
189 RET parse_executable_document(PARAMS) {
190 INIT(executable_document);
191 obj->first_def = parse_executable_definition(ARGS); REQ
192 struct graphql_executable_definition *p = obj->first_def;
194 p->next_def = parse_executable_definition(ARGS);
200 RET parse_executable_definition(PARAMS) {
201 INIT(executable_definition);
202 obj->op_def = parse_operation_definition(ARGS); MSG("invalid operation or fragment definition"); OR
203 obj->frag_def = parse_fragment_definition(ARGS); MSG("invalid operation or fragment definition");
207 RET parse_operation_definition(PARAMS) {
208 INIT(operation_definition);
209 obj->op_type = parse_operation_type(ARGS);
211 obj->op_name = parse_name(ARGS); OPT
212 obj->vars = parse_variable_definitions(ARGS); OPT
213 obj->directives = parse_directives(ARGS); OPT
216 obj->sel_set = parse_selection_set(ARGS);
217 if (*err) ROLLBACK(ARGS);
221 RET parse_operation_type(PARAMS) {
222 INIT(operation_type);
223 const char *errmsg = "expected: query, mutation, or subscription";
224 obj->op_type = parse_keyword(ARGS, "query", errmsg); OR
225 obj->op_type = parse_keyword(ARGS, "mutation", errmsg); OR
226 obj->op_type = parse_keyword(ARGS, "subscription", errmsg);
230 RET parse_selection_set(PARAMS) {
232 parse_punct(ARGS, '{'); REQ;
233 obj->first = parse_selection(ARGS); REQ;
234 struct graphql_selection *p = obj->first;
235 parse_punct(ARGS, '}');
238 p->next = parse_selection(ARGS); MSG("expected: selection or '}'"); REQ;
240 parse_punct(ARGS, '}');
245 RET parse_selection(PARAMS) {
247 obj->field = parse_field(ARGS); OR
248 obj->frag_spread = parse_fragment_spread(ARGS); OR
249 obj->inline_frag = parse_inline_fragment(ARGS);
250 MSG("expected: field, fragment spread, or inline fragment");
254 RET parse_field(PARAMS) {
256 obj->alias = parse_alias(ARGS); OPT
257 obj->name = parse_name(ARGS); REQ
258 obj->args = parse_arguments(ARGS); OPT
259 obj->directives = parse_directives(ARGS); OPT
260 obj->sel_set = parse_selection_set(ARGS); OPT
264 RET parse_arguments(PARAMS) {
266 parse_punct(ARGS, '('); REQ
267 obj->first = parse_argument(ARGS); REQ
268 struct graphql_argument *p = obj->first;
269 parse_punct(ARGS, ')');
272 p->next = parse_argument(ARGS); MSG("expected: argument or ')'"); REQ;
274 parse_punct(ARGS, ')');
279 RET parse_argument(PARAMS) {
281 obj->name = parse_name(ARGS); REQ
282 parse_punct(ARGS, ':'); REQ
283 obj->val = parse_value(ARGS); REQ
287 RET parse_alias(PARAMS) {
289 obj->name = parse_name(ARGS); REQ
290 parse_punct(ARGS, ':'); REQ
294 RET parse_fragment_spread(PARAMS) {
295 INIT(fragment_spread);
296 parse_punct(ARGS, 0x2026); REQ // ...
297 obj->name = parse_fragment_name(ARGS); REQ
298 obj->directives = parse_directives(ARGS); OPT
302 RET parse_fragment_definition(PARAMS) {
303 INIT(fragment_definition);
304 parse_keyword(ARGS, "fragment", "fragment expected"); REQ
305 obj->name = parse_fragment_name(ARGS); REQ
306 obj->type_cond = parse_type_condition(ARGS); REQ
307 obj->directives = parse_directives(ARGS); OPT
308 obj->sel_set = parse_selection_set(ARGS); REQ
312 RET parse_fragment_name(PARAMS) {
314 obj->name = parse_name(ARGS); REQ
315 struct graphql_token *tok = list_top(used, struct graphql_token, list);
316 if (streq(tok->token_string, "on")) {
317 *err = "invalid fragment name";
323 RET parse_type_condition(PARAMS) {
324 INIT(type_condition);
325 parse_keyword(ARGS, "on", "expected: 'on'"); REQ
326 obj->named_type = parse_named_type(ARGS); REQ
330 RET parse_inline_fragment(PARAMS) {
331 INIT(inline_fragment);
332 parse_punct(ARGS, 0x2026); REQ // ...
333 obj->type_cond = parse_type_condition(ARGS); OPT
334 obj->directives = parse_directives(ARGS); OPT
335 obj->sel_set = parse_selection_set(ARGS); REQ
339 RET parse_value(PARAMS) {
341 obj->var = parse_variable(ARGS); // FIXME: if not const
343 obj->int_val = parse_int_value(ARGS); OR
344 obj->float_val = parse_float_value(ARGS); OR
345 obj->str_val = parse_string_value(ARGS); OR
346 obj->bool_val = parse_boolean_value(ARGS); OR
347 obj->null_val = parse_null_value(ARGS); OR
348 obj->enum_val = parse_enum_value(ARGS); OR
349 obj->list_val = parse_list_value(ARGS); OR
350 obj->obj_val = parse_object_value(ARGS);
354 RET parse_int_value(PARAMS) {
356 obj->val = parse_int(ARGS);
360 RET parse_float_value(PARAMS) {
362 obj->val = parse_float(ARGS);
366 RET parse_boolean_value(PARAMS) {
368 obj->val = parse_keyword(ARGS, "true", "invalid boolean value"); OR
369 obj->val = parse_keyword(ARGS, "false", "invalid boolean value");
373 RET parse_string_value(PARAMS) {
375 obj->val = parse_string(ARGS);
379 RET parse_null_value(PARAMS) {
381 obj->val = parse_keyword(ARGS, "null", "null expected");
385 RET parse_enum_value(PARAMS) {
387 obj->val = parse_name(ARGS); REQ
388 struct graphql_token *tok = list_top(used, struct graphql_token, list);
389 if (streq(tok->token_string, "true")
390 || streq(tok->token_string, "false")
391 || streq(tok->token_string, "null")) {
392 *err = "enum value cannot be true, false, or null";
398 RET parse_list_value(PARAMS) {
400 parse_punct(ARGS, '['); REQ
401 parse_punct(ARGS, ']');
404 parse_value(ARGS); MSG("expected: value or ']'"); REQ
405 parse_punct(ARGS, ']');
410 RET parse_object_value(PARAMS) {
412 parse_punct(ARGS, '{'); REQ
413 parse_punct(ARGS, '}');
414 struct graphql_object_field *p = NULL;
418 obj->first = p = parse_object_field(ARGS); MSG("expected: object field or '}'"); REQ
420 p->next = parse_object_field(ARGS); MSG("expected: object field or '}'"); REQ
423 parse_punct(ARGS, '}');
428 RET parse_object_field(PARAMS) {
430 obj->name = parse_name(ARGS); REQ
431 parse_punct(ARGS, ':'); REQ
432 obj->val = parse_value(ARGS); REQ
436 RET parse_variable(PARAMS) {
438 parse_punct(ARGS, '$'); REQ
439 obj->name = parse_name(ARGS); REQ
443 RET parse_variable_definitions(PARAMS) {
444 INIT(variable_definitions);
445 parse_punct(ARGS, '('); REQ
446 obj->first = parse_variable_definition(ARGS); REQ
447 struct graphql_variable_definition *p = obj->first;
448 parse_punct(ARGS, ')');
451 p->next = parse_variable_definition(ARGS); MSG("expected: variable definition or ')'"); REQ
453 parse_punct(ARGS, ')');
458 RET parse_variable_definition(PARAMS) {
459 INIT(variable_definition);
460 obj->var = parse_variable(ARGS); REQ
461 parse_punct(ARGS, ':'); REQ
462 obj->type = parse_type(ARGS); REQ
463 obj->default_val = parse_default_value(ARGS); OPT
464 obj->directives = parse_directives(ARGS); OPT
468 RET parse_default_value(PARAMS) {
470 parse_punct(ARGS, '='); REQ
471 obj->val = parse_value(ARGS); REQ
475 RET parse_type(PARAMS) {
477 obj->named = parse_named_type(ARGS);
480 obj->list = parse_list_type(ARGS); OR
481 obj->non_null = parse_non_null_type(ARGS);
486 RET parse_named_type(PARAMS) {
488 obj->name = parse_name(ARGS);
493 RET parse_list_type(PARAMS) {
495 parse_punct(ARGS, '['); REQ
496 parse_type(ARGS); REQ
497 parse_punct(ARGS, ']'); REQ
501 RET parse_non_null_type(PARAMS) {
503 parse_non_null_type_1(ARGS); OR
504 parse_non_null_type_2(ARGS);
508 RET parse_non_null_type_1(PARAMS) {
510 parse_named_type(ARGS); REQ;
511 parse_punct(ARGS, '!'); REQ;
515 RET parse_non_null_type_2(PARAMS) {
517 parse_list_type(ARGS); REQ;
518 parse_punct(ARGS, '!'); REQ;
523 RET parse_directives(PARAMS) {
525 obj->first = parse_directive(ARGS); REQ
526 struct graphql_directive *p = obj->first;
528 p->next = parse_directive(ARGS);
534 RET parse_directive(PARAMS) {
536 parse_punct(ARGS, '@'); REQ
537 obj->name = parse_name(ARGS); REQ
538 obj->args = parse_arguments(ARGS); OPT
543 /* The following functions construct the "leaves" of the abstract syntax tree. */
545 RET parse_keyword(PARAMS, const char *keyword, const char *errmsg) {
546 struct graphql_token *tok = list_top(tokens, struct graphql_token, list);
547 if (!tok || tok->token_type != 'a') {
548 *err = errmsg; return;
550 if (!streq(tok->token_string, keyword)) {
551 *err = errmsg; return;
557 // Note: a static buffer is used here.
558 RET parse_punct(PARAMS, int punct) {
559 static char punctbuf[16];
560 struct graphql_token *tok = list_top(tokens, struct graphql_token, list);
561 if (!tok || tok->token_type != punct) {
563 sprintf(punctbuf, "expected: '...'");
565 sprintf(punctbuf, "expected: '%c'", punct);
566 *err = punctbuf; return;
572 RET parse_name(PARAMS) {
573 struct graphql_token *tok = list_top(tokens, struct graphql_token, list);
574 if (!tok || tok->token_type != 'a') {
575 *err = "name expected"; return 0;
581 RET parse_int(PARAMS) {
582 struct graphql_token *tok = list_top(tokens, struct graphql_token, list);
583 if (!tok || tok->token_type != 'i') {
584 *err = "integer expected"; return;
590 RET parse_float(PARAMS) {
591 struct graphql_token *tok = list_top(tokens, struct graphql_token, list);
592 if (!tok || tok->token_type != 'f') {
593 *err = "float expected"; return;
599 RET parse_string(PARAMS) {
600 struct graphql_token *tok = list_top(tokens, struct graphql_token, list);
601 if (!tok || tok->token_type != 's') {
602 *err = "string expected"; return;
609 // Convert input string into tokens.
610 const char *graphql_lex(const char *input, const tal_t *ctx, struct list_head **tokens) {
613 const char *p, *line_beginning;
614 unsigned int line_num = 1;
615 struct list_head *tok_list;
616 struct graphql_token *tok;
618 // Initialize token output list.
619 tok_list = tal(ctx, struct list_head);
622 list_head_init(tok_list);
624 // Note: label and goto are used here like a continue statement except that
625 // it skips iteration, for when characters are fetched in the loop body.
631 // Consume line terminators and increment line counter.
632 if (LINE_TERMINATOR(c)) {
635 if (c0 == 10 || c0 == 13)
637 if (c0 == 13 && c == 10)
639 line_beginning = p - 1;
643 // Consume other ignored tokens.
644 if (COMMA(c) || WHITE_SPACE(c)) {
649 while ((c = *p++) != EOF && !EOF_CHAR(c) && COMMENT_CHAR(c))
654 // Return success when end is reached.
656 return GRAPHQL_SUCCESS;
658 // Punctuator tokens.
661 // Note beginning of token in input.
662 const char *start = p - 1;
664 // Handle the ... multi-character case.
668 return "unrecognized punctuator";
671 return "unrecognized punctuator";
675 tok = tal(tok_list, struct graphql_token);
676 list_add_tail(tok_list, tok);
678 tok->token_specific = c;
679 tok->token_string = NULL;
680 tok->source_line = line_num;
681 tok->source_column = start - line_beginning + 1;
682 tok->source_len = p - start;
684 } else if (NAME_START(c)) {
686 // Name/identifier tokens.
687 tok = tal(tok_list, struct graphql_token);
688 list_add_tail(tok_list, tok);
689 tok->token_type = 'a';
690 tok->token_specific = 'a';
691 // tok->token_string updated below.
692 tok->source_line = line_num;
693 tok->source_column = p - line_beginning;
694 // tok->source_len updated below.
696 // Note the beginning of the name.
697 const char *name_begin = p - 1;
698 const char *name_end;
701 // Consume the rest of the token.
704 } while (NAME_CONTINUE(c));
706 // Note the end of the name and calculate the length.
708 name_len = name_end - name_begin;
709 tok->source_len = name_len;
711 // Copy the token string.
712 tok->token_string = tal_strndup(tok, name_begin, name_len);
716 } else if (DIGIT(c) || c == '-') {
719 const char *num_start = p - 1;
725 return "negative sign must precede a number";
731 return "leading zeros are not allowed";
741 return "invalid float value fractional part";
747 if (c == 'e' || c == 'E') {
750 if (c == '+' || c == '-')
753 return "invalid float value exponent part";
759 if (c == '.' || NAME_START(c))
760 return "invalid numeric value";
762 const char *num_end = p - 1;
763 int num_len = num_end - num_start;
765 tok = tal(tok_list, struct graphql_token);
766 list_add_tail(tok_list, tok);
767 tok->token_type = type;
768 tok->token_string = tal_strndup(tok, num_start, num_len);
769 tok->source_line = line_num;
770 tok->source_column = num_start - line_beginning + 1;
771 tok->source_len = num_len;
775 } else if (c == '"') {
779 const char *str_begin = p - 1;
781 bool str_block = false;
791 if (c == '\"') quotes++; else quotes = 0;
792 if (quotes == 3 && *(p-4) == '\\') quotes = 0;
793 } while (BLOCK_STRING_CHAR(c) && quotes < 3);
800 return "unterminated string or invalid character";
803 return "invalid string termination";
806 return "invalid string termination";
819 if (strchr("\"\\/bfnrtu", c)) {
823 return "invalid unicode escape sequence";
826 return "invalid unicode escape sequence";
829 return "invalid unicode escape sequence";
832 return "invalid unicode escape sequence";
834 c = 'a'; // anything besides a quote to let the loop continue
837 return "invalid string escape sequence";
840 } while (STRING_CHAR(c));
842 return "unterminated string or invalid character";
845 int str_len = str_end - str_begin;
847 tok = tal(tok_list, struct graphql_token);
848 list_add_tail(tok_list, tok);
849 tok->token_type = 's';
850 tok->token_specific = 's';
851 tok->token_string = tal_strndup(tok, str_begin, str_len);
852 tok->source_line = line_num;
853 tok->source_column = str_begin - line_beginning + 1;
854 tok->source_len = str_len;
856 // Process escape sequences. These always shorten the string (so the memory allocation is always enough).
858 char *q = tok->token_string;
863 if (d == '\"') quotes++; else quotes = 0;
864 if (quotes == 3 && *(q-4) == '\\') {
866 rewrite_dest = q - 4;
867 strcpy(rewrite_dest, q - 3);
871 rewrite_dest = q - 1;
875 *rewrite_dest++ = '\"';
876 strcpy(rewrite_dest, q--);
879 *rewrite_dest++ = '\b';
880 strcpy(rewrite_dest, q--);
883 *rewrite_dest++ = '\f';
884 strcpy(rewrite_dest, q--);
887 *rewrite_dest++ = '\n';
888 strcpy(rewrite_dest, q--);
891 *rewrite_dest++ = '\r';
892 strcpy(rewrite_dest, q--);
895 *rewrite_dest++ = '\t';
896 strcpy(rewrite_dest, q--);
899 // Insert escaped character using UTF-8 multi-byte encoding.
900 char buf[] = {*q++, *q++, *q++, *q++, 0};
901 int code_point = strtol(buf, 0, 16);
902 int bytes = utf8_encode(code_point, rewrite_dest);
903 rewrite_dest += bytes;
904 strcpy(rewrite_dest, q--);
908 strcpy(rewrite_dest, --q);
914 // Strip leading lines.
915 q = tok->token_string;
918 while (WHITE_SPACE(d))
920 if (LINE_TERMINATOR(d)) {
921 while (LINE_TERMINATOR(d))
923 strcpy(tok->token_string, q - 1);
924 q = tok->token_string;
929 // Strip trailing lines.
930 q = tok->token_string + strlen(tok->token_string);
933 while (WHITE_SPACE(d))
935 if (LINE_TERMINATOR(d)) {
936 while (LINE_TERMINATOR(d))
943 // Look for common indentation.
944 char *this_indent_start;
945 const char *this_indent_end;
946 const char *common_indent_start = NULL;
947 const char *common_indent_end;
949 q = tok->token_string;
952 this_indent_start = q - 1;
953 while (WHITE_SPACE(d))
955 this_indent_end = q - 1;
956 if (LINE_TERMINATOR(d)) {
957 while (LINE_TERMINATOR(d))
964 if (common_indent_start == NULL) {
965 common_indent_start = this_indent_start;
966 common_indent_end = this_indent_end;
968 for (r = this_indent_start; r < this_indent_end && (r - this_indent_start + common_indent_start < common_indent_end); r++) {
969 if (*r != *(r - this_indent_start + common_indent_start))
972 common_indent_end = r - this_indent_start + common_indent_start;
974 while (!LINE_TERMINATOR(d) && !EOF_CHAR(d))
976 while (LINE_TERMINATOR(d))
982 // Remove common indentation.
983 int common_indent_len = common_indent_end - common_indent_start;
984 if (common_indent_len > 0) {
985 q = tok->token_string;
988 this_indent_start = q - 1;
989 while (WHITE_SPACE(d))
991 this_indent_end = q - 1;
992 if (LINE_TERMINATOR(d)) {
993 while (LINE_TERMINATOR(d))
1000 while (!LINE_TERMINATOR(d) && !EOF_CHAR(d))
1004 strcpy(this_indent_start, this_indent_start + common_indent_len);
1005 q -= common_indent_len;
1008 while (LINE_TERMINATOR(d))
1019 return "invalid source character encountered";
1022 } while (!EOF_CHAR(c));
1024 return "unexpected end-of-input encountered";