herb 0.0.1 → 0.1.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 (108) hide show
  1. checksums.yaml +5 -5
  2. data/{LICENSE → LICENSE.txt} +4 -3
  3. data/Makefile +121 -0
  4. data/README.md +102 -107
  5. data/Rakefile +184 -0
  6. data/exe/herb +5 -0
  7. data/ext/herb/error_helpers.c +302 -0
  8. data/ext/herb/error_helpers.h +15 -0
  9. data/ext/herb/extconf.rb +75 -0
  10. data/ext/herb/extension.c +110 -0
  11. data/ext/herb/extension.h +6 -0
  12. data/ext/herb/extension_helpers.c +117 -0
  13. data/ext/herb/extension_helpers.h +24 -0
  14. data/ext/herb/nodes.c +936 -0
  15. data/ext/herb/nodes.h +12 -0
  16. data/herb.gemspec +49 -0
  17. data/lib/herb/ast/node.rb +61 -0
  18. data/lib/herb/ast/nodes.rb +1542 -0
  19. data/lib/herb/ast.rb +6 -0
  20. data/lib/herb/cli.rb +164 -0
  21. data/lib/herb/errors.rb +352 -0
  22. data/lib/herb/lex_result.rb +20 -0
  23. data/lib/herb/libherb/array.rb +48 -0
  24. data/lib/herb/libherb/ast_node.rb +47 -0
  25. data/lib/herb/libherb/buffer.rb +53 -0
  26. data/lib/herb/libherb/extract_result.rb +17 -0
  27. data/lib/herb/libherb/lex_result.rb +29 -0
  28. data/lib/herb/libherb/libherb.rb +49 -0
  29. data/lib/herb/libherb/parse_result.rb +17 -0
  30. data/lib/herb/libherb/token.rb +43 -0
  31. data/lib/herb/libherb.rb +32 -0
  32. data/lib/herb/location.rb +42 -0
  33. data/lib/herb/parse_result.rb +26 -0
  34. data/lib/herb/position.rb +36 -0
  35. data/lib/herb/project.rb +361 -0
  36. data/lib/herb/range.rb +40 -0
  37. data/lib/herb/result.rb +21 -0
  38. data/lib/herb/token.rb +43 -0
  39. data/lib/herb/token_list.rb +11 -0
  40. data/lib/herb/version.rb +5 -0
  41. data/lib/herb.rb +21 -68
  42. data/src/analyze.c +989 -0
  43. data/src/analyze_helpers.c +241 -0
  44. data/src/analyzed_ruby.c +35 -0
  45. data/src/array.c +137 -0
  46. data/src/ast_node.c +81 -0
  47. data/src/ast_nodes.c +866 -0
  48. data/src/ast_pretty_print.c +588 -0
  49. data/src/buffer.c +199 -0
  50. data/src/errors.c +740 -0
  51. data/src/extract.c +110 -0
  52. data/src/herb.c +103 -0
  53. data/src/html_util.c +143 -0
  54. data/src/include/analyze.h +36 -0
  55. data/src/include/analyze_helpers.h +43 -0
  56. data/src/include/analyzed_ruby.h +33 -0
  57. data/src/include/array.h +33 -0
  58. data/src/include/ast_node.h +35 -0
  59. data/src/include/ast_nodes.h +303 -0
  60. data/src/include/ast_pretty_print.h +17 -0
  61. data/src/include/buffer.h +36 -0
  62. data/src/include/errors.h +125 -0
  63. data/src/include/extract.h +20 -0
  64. data/src/include/herb.h +32 -0
  65. data/src/include/html_util.h +13 -0
  66. data/src/include/io.h +9 -0
  67. data/src/include/json.h +28 -0
  68. data/src/include/lexer.h +13 -0
  69. data/src/include/lexer_peek_helpers.h +23 -0
  70. data/src/include/lexer_struct.h +32 -0
  71. data/src/include/location.h +25 -0
  72. data/src/include/macros.h +10 -0
  73. data/src/include/memory.h +12 -0
  74. data/src/include/parser.h +22 -0
  75. data/src/include/parser_helpers.h +33 -0
  76. data/src/include/position.h +22 -0
  77. data/src/include/pretty_print.h +53 -0
  78. data/src/include/prism_helpers.h +18 -0
  79. data/src/include/range.h +23 -0
  80. data/src/include/ruby_parser.h +6 -0
  81. data/src/include/token.h +25 -0
  82. data/src/include/token_matchers.h +21 -0
  83. data/src/include/token_struct.h +51 -0
  84. data/src/include/util.h +25 -0
  85. data/src/include/version.h +6 -0
  86. data/src/include/visitor.h +11 -0
  87. data/src/io.c +30 -0
  88. data/src/json.c +205 -0
  89. data/src/lexer.c +284 -0
  90. data/src/lexer_peek_helpers.c +59 -0
  91. data/src/location.c +41 -0
  92. data/src/main.c +162 -0
  93. data/src/memory.c +53 -0
  94. data/src/parser.c +704 -0
  95. data/src/parser_helpers.c +161 -0
  96. data/src/position.c +33 -0
  97. data/src/pretty_print.c +242 -0
  98. data/src/prism_helpers.c +50 -0
  99. data/src/range.c +38 -0
  100. data/src/ruby_parser.c +47 -0
  101. data/src/token.c +194 -0
  102. data/src/token_matchers.c +32 -0
  103. data/src/util.c +128 -0
  104. data/src/visitor.c +321 -0
  105. metadata +126 -82
  106. data/test/helper.rb +0 -7
  107. data/test/helpers_test.rb +0 -25
  108. data/test/parsing_test.rb +0 -110
@@ -0,0 +1,10 @@
1
+ #ifndef HERB_MACROS_H
2
+ #define HERB_MACROS_H
3
+
4
+ #define MAX(a, b) (a) > (b) ? (a) : (b)
5
+
6
+ #define MIN(a, b) (a) < (b) ? (a) : (b)
7
+
8
+ #define unlikely(x) __builtin_expect(!!(x), 0)
9
+
10
+ #endif
@@ -0,0 +1,12 @@
1
+ #ifndef HERB_MEMORY_H
2
+ #define HERB_MEMORY_H
3
+
4
+ #include <stddef.h>
5
+
6
+ void* safe_malloc(size_t size);
7
+ void* safe_realloc(void* pointer, size_t new_size);
8
+
9
+ void* nullable_safe_malloc(size_t size);
10
+ void* nullable_safe_realloc(void* pointer, size_t new_size);
11
+
12
+ #endif
@@ -0,0 +1,22 @@
1
+ #ifndef HERB_PARSER_H
2
+ #define HERB_PARSER_H
3
+
4
+ #include "array.h"
5
+ #include "ast_node.h"
6
+ #include "lexer.h"
7
+
8
+ typedef struct PARSER_STRUCT {
9
+ lexer_T* lexer;
10
+ token_T* current_token;
11
+ array_T* open_tags_stack;
12
+ } parser_T;
13
+
14
+ parser_T* parser_init(lexer_T* lexer);
15
+
16
+ AST_DOCUMENT_NODE_T* parser_parse(parser_T* parser);
17
+
18
+ size_t parser_sizeof(void);
19
+
20
+ void parser_free(parser_T* parser);
21
+
22
+ #endif
@@ -0,0 +1,33 @@
1
+ #ifndef HERB_PARSER_HELPERS_H
2
+ #define HERB_PARSER_HELPERS_H
3
+
4
+ #include "array.h"
5
+ #include "ast_nodes.h"
6
+ #include "buffer.h"
7
+ #include "errors.h"
8
+ #include "parser.h"
9
+ #include "token.h"
10
+
11
+ void parser_push_open_tag(const parser_T* parser, token_T* tag_name);
12
+ bool parser_check_matching_tag(const parser_T* parser, const char* tag_name);
13
+ token_T* parser_pop_open_tag(const parser_T* parser);
14
+
15
+ void parser_append_unexpected_error(parser_T* parser, const char* description, const char* expected, array_T* errors);
16
+ void parser_append_unexpected_token_error(parser_T* parser, token_type_T expected_type, array_T* errors);
17
+
18
+ void parser_append_literal_node_from_buffer(
19
+ const parser_T* parser, buffer_T* buffer, array_T* children, position_T* start
20
+ );
21
+
22
+ bool parser_in_svg_context(const parser_T* parser);
23
+
24
+ token_T* parser_advance(parser_T* parser);
25
+ token_T* parser_consume_if_present(parser_T* parser, token_type_T type);
26
+ token_T* parser_consume_expected(parser_T* parser, token_type_T type, array_T* array);
27
+
28
+ AST_HTML_ELEMENT_NODE_T* parser_handle_missing_close_tag(
29
+ AST_HTML_OPEN_TAG_NODE_T* open_tag, array_T* body, array_T* errors
30
+ );
31
+ void parser_handle_mismatched_tags(const parser_T* parser, const AST_HTML_CLOSE_TAG_NODE_T* close_tag, array_T* errors);
32
+
33
+ #endif
@@ -0,0 +1,22 @@
1
+ #ifndef HERB_POSITION_H
2
+ #define HERB_POSITION_H
3
+
4
+ #include <stdlib.h>
5
+
6
+ typedef struct POSITION_STRUCT {
7
+ size_t line;
8
+ size_t column;
9
+ } position_T;
10
+
11
+ position_T* position_init(size_t line, size_t column);
12
+
13
+ size_t position_line(const position_T* position);
14
+ size_t position_column(const position_T* position);
15
+
16
+ size_t position_sizeof(void);
17
+
18
+ position_T* position_copy(position_T* position);
19
+
20
+ void position_free(position_T* position);
21
+
22
+ #endif
@@ -0,0 +1,53 @@
1
+ #ifndef HERB_PRETTY_PRINT_H
2
+ #define HERB_PRETTY_PRINT_H
3
+
4
+ #include "analyzed_ruby.h"
5
+ #include "ast_nodes.h"
6
+ #include "buffer.h"
7
+ #include "location.h"
8
+
9
+ #include <stdbool.h>
10
+
11
+ void pretty_print_indent(buffer_T* buffer, size_t indent);
12
+ void pretty_print_newline(size_t indent, size_t relative_indent, buffer_T* buffer);
13
+ void pretty_print_label(const char* name, size_t indent, size_t relative_indent, bool last_property, buffer_T* buffer);
14
+
15
+ void pretty_print_position_property(
16
+ position_T* position, const char* name, size_t indent, size_t relative_indent, bool last_property, buffer_T* buffer
17
+ );
18
+
19
+ void pretty_print_location(location_T* location, buffer_T* buffer);
20
+
21
+ void pretty_print_property(
22
+ const char* name, const char* value, size_t indent, size_t relative_indent, bool last_property, buffer_T* buffer
23
+ );
24
+
25
+ void pretty_print_size_t_property(
26
+ size_t value, const char* name, size_t indent, size_t relative_indent, bool last_property, buffer_T* buffer
27
+ );
28
+
29
+ void pretty_print_string_property(
30
+ const char* string, const char* name, size_t indent, size_t relative_indent, bool last_property, buffer_T* buffer
31
+ );
32
+
33
+ void pretty_print_quoted_property(
34
+ const char* name, const char* value, size_t indent, size_t relative_indent, bool last_property, buffer_T* buffer
35
+ );
36
+
37
+ void pretty_print_boolean_property(
38
+ const char* name, bool value, size_t indent, size_t relative_indent, bool last_property, buffer_T* buffer
39
+ );
40
+
41
+ void pretty_print_token_property(
42
+ token_T* token, const char* name, size_t indent, size_t relative_indent, bool last_property, buffer_T* buffer
43
+ );
44
+
45
+ void pretty_print_array(
46
+ const char* name, array_T* array, size_t indent, size_t relative_indent, bool last_property, buffer_T* buffer
47
+ );
48
+
49
+ void pretty_print_errors(AST_NODE_T* node, size_t indent, size_t relative_indent, bool last_property, buffer_T* buffer);
50
+
51
+ void pretty_print_analyed_ruby(analyzed_ruby_T* analyzed, const char* source);
52
+
53
+ #endif
@@ -0,0 +1,18 @@
1
+ #ifndef HERB_PRISM_HELPERS_H
2
+ #define HERB_PRISM_HELPERS_H
3
+
4
+ #include "ast_nodes.h"
5
+ #include "errors.h"
6
+ #include "position.h"
7
+
8
+ #include <prism.h>
9
+
10
+ const char* pm_error_level_to_string(pm_error_level_t level);
11
+
12
+ RUBY_PARSE_ERROR_T* ruby_parse_error_from_prism_error(
13
+ const pm_diagnostic_t* error, const AST_NODE_T* node, const char* source, pm_parser_t* parser
14
+ );
15
+
16
+ position_T* position_from_source_with_offset(const char* source, size_t offset);
17
+
18
+ #endif
@@ -0,0 +1,23 @@
1
+ #ifndef HERB_RANGE_H
2
+ #define HERB_RANGE_H
3
+
4
+ #include <stdlib.h>
5
+
6
+ typedef struct RANGE_STRUCT {
7
+ size_t from;
8
+ size_t to;
9
+ } range_T;
10
+
11
+ range_T* range_init(size_t from, size_t to);
12
+
13
+ size_t range_from(const range_T* range);
14
+ size_t range_to(const range_T* range);
15
+ size_t range_length(range_T* range);
16
+
17
+ range_T* range_copy(range_T* range);
18
+
19
+ size_t range_sizeof(void);
20
+
21
+ void range_free(range_T* range);
22
+
23
+ #endif
@@ -0,0 +1,6 @@
1
+ #ifndef HERB_RUBY_PARSER_H
2
+ #define HERB_RUBY_PARSER_H
3
+
4
+ void herb_parse_ruby_to_stdout(char* source);
5
+
6
+ #endif
@@ -0,0 +1,25 @@
1
+ #ifndef HERB_TOKEN_H
2
+ #define HERB_TOKEN_H
3
+
4
+ #include "lexer_struct.h"
5
+ #include "position.h"
6
+ #include "token_struct.h"
7
+
8
+ token_T* token_init(const char* value, token_type_T type, lexer_T* lexer);
9
+ char* token_to_string(const token_T* token);
10
+ char* token_to_json(const token_T* token);
11
+ const char* token_type_to_string(token_type_T type);
12
+
13
+ char* token_value(const token_T* token);
14
+ int token_type(const token_T* token);
15
+
16
+ position_T* token_start_position(token_T* token);
17
+ position_T* token_end_position(token_T* token);
18
+
19
+ size_t token_sizeof(void);
20
+
21
+ token_T* token_copy(token_T* token);
22
+
23
+ void token_free(token_T* token);
24
+
25
+ #endif
@@ -0,0 +1,21 @@
1
+ #ifndef HERB_TOKEN_MATCHERS_H
2
+ #define HERB_TOKEN_MATCHERS_H
3
+
4
+ #include "parser.h"
5
+ #include "token.h"
6
+
7
+ #include <stdarg.h>
8
+ #include <stdbool.h>
9
+
10
+ // This "TOKEN" is used to terminate the va_list arguments in the token_matches_any function
11
+ #define TOKEN_SENTINEL 99999999
12
+
13
+ bool token_is(parser_T* parser, token_type_T expected_type);
14
+ bool token_is_not(parser_T* parser, token_type_T type);
15
+
16
+ bool token_matches_any(token_type_T current_token, token_type_T first_token, ...);
17
+
18
+ #define token_is_any_of(parser, ...) (token_matches_any((parser)->current_token->type, __VA_ARGS__, TOKEN_SENTINEL))
19
+ #define token_is_none_of(parser, ...) (!token_matches_any((parser)->current_token->type, __VA_ARGS__, TOKEN_SENTINEL))
20
+
21
+ #endif
@@ -0,0 +1,51 @@
1
+ #ifndef HERB_TOKEN_STRUCT_H
2
+ #define HERB_TOKEN_STRUCT_H
3
+
4
+ #include "location.h"
5
+ #include "range.h"
6
+
7
+ typedef enum {
8
+ TOKEN_WHITESPACE, // ' '
9
+ TOKEN_NBSP, // \xC2\xA0
10
+ TOKEN_NEWLINE, // \n
11
+ TOKEN_IDENTIFIER,
12
+
13
+ TOKEN_HTML_DOCTYPE, // <!DOCTYPE, <!doctype, <!DoCtYpE, <!dOcTyPe
14
+
15
+ TOKEN_HTML_TAG_START, // <
16
+ TOKEN_HTML_TAG_START_CLOSE, // </
17
+ TOKEN_HTML_TAG_END, // >
18
+ TOKEN_HTML_TAG_SELF_CLOSE, // />
19
+
20
+ TOKEN_HTML_COMMENT_START, // <!--
21
+ TOKEN_HTML_COMMENT_END, // -->
22
+
23
+ TOKEN_ERB_START, // <%, <%=, <%#, <%-, <%==, <%%
24
+ TOKEN_ERB_CONTENT, // Ruby Code
25
+ TOKEN_ERB_END, // %>, -%>, %%>
26
+
27
+ TOKEN_LT, // <
28
+ TOKEN_SLASH, // /
29
+ TOKEN_EQUALS, // =
30
+ TOKEN_QUOTE, // ", '
31
+ TOKEN_DASH, // -
32
+ TOKEN_UNDERSCORE, // _
33
+ TOKEN_EXCLAMATION, // !
34
+ TOKEN_SEMICOLON, // ;
35
+ TOKEN_COLON, // :
36
+ TOKEN_PERCENT, // %
37
+ TOKEN_AMPERSAND, // &
38
+
39
+ TOKEN_CHARACTER,
40
+ TOKEN_ERROR,
41
+ TOKEN_EOF,
42
+ } token_type_T;
43
+
44
+ typedef struct TOKEN_STRUCT {
45
+ char* value;
46
+ range_T* range;
47
+ location_T* location;
48
+ token_type_T type;
49
+ } token_T;
50
+
51
+ #endif
@@ -0,0 +1,25 @@
1
+ #ifndef HERB_UTIL_H
2
+ #define HERB_UTIL_H
3
+
4
+ #include <stdbool.h>
5
+ #include <stdlib.h>
6
+
7
+ int is_whitespace(int character);
8
+ int is_newline(int character);
9
+
10
+ int count_in_string(const char* string, char character);
11
+ int count_newlines(const char* string);
12
+
13
+ char* replace_char(char* string, char find, char replace);
14
+ char* escape_newlines(const char* input);
15
+ char* quoted_string(const char* input);
16
+ char* wrap_string(const char* input, char character);
17
+
18
+ bool string_blank(const char* input);
19
+ bool string_present(const char* input);
20
+
21
+ char* herb_strdup(const char* s);
22
+
23
+ char* size_t_to_string(size_t value);
24
+
25
+ #endif
@@ -0,0 +1,6 @@
1
+ #ifndef HERB_VERSION_H
2
+ #define HERB_VERSION_H
3
+
4
+ #define HERB_VERSION "0.1.0"
5
+
6
+ #endif
@@ -0,0 +1,11 @@
1
+ #ifndef HERB_VISITOR_H
2
+ #define HERB_VISITOR_H
3
+
4
+ #include "array.h"
5
+ #include "ast_node.h"
6
+ #include "ast_nodes.h"
7
+
8
+ void herb_visit_node(const AST_NODE_T* node, bool (*visitor)(const AST_NODE_T*, void*), void* data);
9
+ void herb_visit_child_nodes(const AST_NODE_T* node, bool (*visitor)(const AST_NODE_T* node, void* data), void* data);
10
+
11
+ #endif
data/src/io.c ADDED
@@ -0,0 +1,30 @@
1
+ #include "include/io.h"
2
+ #include "include/buffer.h"
3
+
4
+ #include <errno.h>
5
+ #include <stdio.h>
6
+ #include <stdlib.h>
7
+
8
+ #define FILE_READ_CHUNK 4096
9
+
10
+ char* herb_read_file(const char* filename) {
11
+ FILE* fp = fopen(filename, "rb");
12
+
13
+ if (fp == NULL) {
14
+ fprintf(stderr, "Could not read file '%s'\n", filename);
15
+ exit(1);
16
+ }
17
+
18
+ buffer_T buffer = buffer_new();
19
+
20
+ char chunk[FILE_READ_CHUNK];
21
+ size_t bytes_read;
22
+
23
+ while ((bytes_read = fread(chunk, 1, FILE_READ_CHUNK, fp)) > 0) {
24
+ buffer_append_with_length(&buffer, chunk, bytes_read);
25
+ }
26
+
27
+ fclose(fp);
28
+
29
+ return buffer_value(&buffer);
30
+ }
data/src/json.c ADDED
@@ -0,0 +1,205 @@
1
+ #include "include/json.h"
2
+ #include "include/buffer.h"
3
+
4
+ void json_escape_string(buffer_T* json, const char* string) {
5
+ if (!string) {
6
+ buffer_append(json, "null");
7
+ return;
8
+ }
9
+
10
+ buffer_append(json, "\"");
11
+
12
+ while (*string) {
13
+ switch (*string) {
14
+ case '\"': buffer_append(json, "\\\""); break;
15
+ case '\\': buffer_append(json, "\\\\"); break;
16
+ case '\n': buffer_append(json, "\\n"); break;
17
+ case '\t': buffer_append(json, "\\t"); break;
18
+ default: buffer_append_char(json, *string); break;
19
+ }
20
+ string++;
21
+ }
22
+
23
+ buffer_append(json, "\"");
24
+ }
25
+
26
+ void json_int_to_string(const int value, char* buffer) {
27
+ char string[20]; // Enough to hold all possible int values
28
+ int i = 0;
29
+
30
+ // Handle negative numbers
31
+ unsigned int abs_value = (unsigned int) abs(value);
32
+
33
+ do {
34
+ string[i++] = (char) ((abs_value % 10) + '0');
35
+ abs_value /= 10;
36
+ } while (abs_value > 0);
37
+
38
+ if (value < 0) { string[i++] = '-'; }
39
+
40
+ int j = 0;
41
+
42
+ while (i > 0) {
43
+ buffer[j++] = string[--i];
44
+ }
45
+
46
+ buffer[j] = '\0';
47
+ }
48
+
49
+ void json_double_to_string(const double value, char* buffer) {
50
+ const int int_part = (int) value;
51
+ const double frac_part = value - (double) int_part;
52
+ const int frac_as_int = (int) (frac_part * 100); // Keep 2 decimal places
53
+
54
+ char int_buffer[20];
55
+ char frac_buffer[5];
56
+
57
+ json_int_to_string(int_part, int_buffer);
58
+ json_int_to_string(frac_as_int < 0 ? -frac_as_int : frac_as_int, frac_buffer);
59
+
60
+ char* pointer = buffer;
61
+ for (const char* source = int_buffer; *source != '\0'; ++source) {
62
+ *pointer++ = *source;
63
+ }
64
+
65
+ *pointer++ = '.';
66
+
67
+ for (const char* source = frac_buffer; *source != '\0'; ++source) {
68
+ *pointer++ = *source;
69
+ }
70
+
71
+ *pointer = '\0';
72
+ }
73
+
74
+ void json_add_string(buffer_T* json, const char* key, const char* value) {
75
+ if (!json) { return; }
76
+
77
+ if (json->length > 1) { buffer_append(json, ", "); }
78
+
79
+ if (key) {
80
+ json_escape_string(json, key);
81
+ buffer_append(json, ": ");
82
+ }
83
+
84
+ json_escape_string(json, value);
85
+ }
86
+
87
+ void json_add_double(buffer_T* json, const char* key, const double value) {
88
+ if (!json) { return; }
89
+
90
+ char number[32];
91
+ json_double_to_string(value, number);
92
+
93
+ if (json->length > 1) { buffer_append(json, ", "); }
94
+
95
+ if (key) {
96
+ json_escape_string(json, key);
97
+ buffer_append(json, ": ");
98
+ }
99
+
100
+ buffer_append(json, number);
101
+ }
102
+
103
+ void json_add_int(buffer_T* json, const char* key, const int value) {
104
+ if (!json) { return; }
105
+
106
+ char number[20];
107
+ json_int_to_string(value, number);
108
+
109
+ if (json->length > 1) { buffer_append(json, ", "); }
110
+
111
+ if (key) {
112
+ json_escape_string(json, key);
113
+ buffer_append(json, ": ");
114
+ }
115
+
116
+ buffer_append(json, number);
117
+ if (json->length == 1) { buffer_append(json, " "); }
118
+ }
119
+
120
+ void json_add_size_t(buffer_T* json, const char* key, size_t value) {
121
+ if (!json) { return; }
122
+
123
+ char number[32];
124
+ char temp[32];
125
+ int i = 0;
126
+
127
+ do {
128
+ temp[i++] = (char) ((value % 10) + '0');
129
+ value /= 10;
130
+ } while (value > 0);
131
+
132
+ int j = 0;
133
+ while (i > 0) {
134
+ number[j++] = temp[--i];
135
+ }
136
+ number[j] = '\0';
137
+
138
+ if (json->length > 1) { buffer_append(json, ", "); }
139
+
140
+ if (key) {
141
+ json_escape_string(json, key);
142
+ buffer_append(json, ": ");
143
+ }
144
+
145
+ buffer_append(json, number);
146
+ if (json->length == 1) { buffer_append(json, " "); }
147
+ }
148
+
149
+ void json_add_bool(buffer_T* json, const char* key, const int value) {
150
+ if (!json) { return; }
151
+
152
+ if (json->length > 1) { buffer_append(json, ", "); }
153
+
154
+ if (key) {
155
+ json_escape_string(json, key);
156
+ buffer_append(json, ": ");
157
+ }
158
+
159
+ buffer_append(json, value ? "true" : "false");
160
+ }
161
+
162
+ void json_add_raw_string(buffer_T* json, const char* string) {
163
+ if (!json) { return; }
164
+
165
+ if (json->length > 1) { buffer_append(json, ", "); }
166
+
167
+ buffer_append(json, string);
168
+ }
169
+
170
+ void json_start_root_object(buffer_T* json) {
171
+ if (json) { buffer_append(json, "{"); }
172
+ }
173
+
174
+ void json_start_object(buffer_T* json, const char* key) {
175
+ if (!json) { return; }
176
+
177
+ if (json->length > 1) { buffer_append(json, ", "); }
178
+
179
+ if (key) {
180
+ json_escape_string(json, key);
181
+ buffer_append(json, ": ");
182
+ }
183
+
184
+ buffer_append(json, "{");
185
+ }
186
+
187
+ void json_end_object(buffer_T* json) {
188
+ if (json) { buffer_append(json, "}"); }
189
+ }
190
+
191
+ void json_start_root_array(buffer_T* json) {
192
+ if (json) { buffer_append(json, "["); }
193
+ }
194
+
195
+ void json_start_array(buffer_T* json, const char* key) {
196
+ if (!json) { return; }
197
+
198
+ if (json->length > 1) { buffer_append(json, ", "); }
199
+ json_escape_string(json, key);
200
+ buffer_append(json, ": [");
201
+ }
202
+
203
+ void json_end_array(buffer_T* json) {
204
+ if (json) { buffer_append(json, "]"); }
205
+ }