herb 0.11.2 → 0.11.3

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 (41) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +2 -0
  3. data/config.yml +7 -8
  4. data/ext/herb/error_helpers.c +3 -3
  5. data/lib/herb/ast/erb_render_node.rb +6 -2
  6. data/lib/herb/cli.rb +7 -3
  7. data/lib/herb/dev/runner.rb +5 -2
  8. data/lib/herb/engine/compiler.rb +39 -4
  9. data/lib/herb/engine/slots/dynamics_compiler.rb +1 -1
  10. data/lib/herb/engine/slots/state_compiler.rb +9 -1
  11. data/lib/herb/engine/slots/state_overrides.rb +17 -3
  12. data/lib/herb/engine/slots/visitor.rb +37 -7
  13. data/lib/herb/engine.rb +11 -1
  14. data/lib/herb/errors.rb +7 -7
  15. data/lib/herb/version.rb +1 -1
  16. data/sig/herb/ast/erb_render_node.rbs +2 -0
  17. data/sig/herb/dev/runner.rbs +2 -2
  18. data/sig/herb/engine/compiler.rbs +9 -0
  19. data/sig/herb/engine/slots/state_compiler.rbs +3 -0
  20. data/sig/herb/engine/slots/state_overrides.rbs +11 -2
  21. data/sig/herb/engine/slots/visitor.rbs +7 -0
  22. data/sig/herb/engine.rbs +2 -0
  23. data/sig/herb/errors.rbs +2 -2
  24. data/sig/serialized_ast_errors.rbs +1 -1
  25. data/src/analyze/action_view/tag_helpers.c +23 -9
  26. data/src/analyze/analyze.c +17 -10
  27. data/src/analyze/render_nodes.c +189 -38
  28. data/src/diff/herb_diff_children.c +1 -1
  29. data/src/diff/herb_diff_nodes.c +1 -9
  30. data/src/diff/herb_hash_tree.c +1 -1
  31. data/src/errors.c +17 -22
  32. data/src/extract.c +47 -7
  33. data/src/include/errors.h +3 -3
  34. data/src/include/version.h +1 -1
  35. data/src/lexer/token.c +2 -2
  36. data/src/parser/dot_notation.c +15 -5
  37. data/src/parser.c +3 -1
  38. data/templates/src/diff/herb_diff_nodes.c.erb +1 -9
  39. data/templates/src/diff/herb_hash_tree.c.erb +1 -1
  40. data/templates/src/errors.c.erb +2 -2
  41. metadata +1 -1
data/src/extract.c CHANGED
@@ -9,6 +9,7 @@
9
9
  #include "include/util/util.h"
10
10
 
11
11
  #include <assert.h>
12
+ #include <ctype.h>
12
13
  #include <stdlib.h>
13
14
  #include <string.h>
14
15
 
@@ -19,6 +20,39 @@ const herb_extract_ruby_options_T HERB_EXTRACT_RUBY_DEFAULT_OPTIONS = { .semicol
19
20
  .erb_openers = NULL,
20
21
  .erb_opener_count = 0 };
21
22
 
23
+ static bool ruby_comment_starts_line(const lexer_T* lexer, uint32_t tag_start) {
24
+ for (uint32_t position = tag_start; position > 0; position--) {
25
+ unsigned char character = (unsigned char) lexer->source.data[position - 1];
26
+
27
+ if (is_newline(character)) { return true; }
28
+ if (!isspace(character)) { return false; }
29
+ }
30
+
31
+ return true;
32
+ }
33
+
34
+ static bool ruby_comment_ends_line(const lexer_T* lexer) {
35
+ lexer_T lookahead = *lexer;
36
+ token_T* closing = lexer_next_token(&lookahead);
37
+ bool has_closing = closing->type == TOKEN_ERB_END;
38
+ token_free(closing, lookahead.allocator);
39
+
40
+ if (!has_closing) { return false; }
41
+
42
+ for (size_t position = lookahead.current_position; position < lookahead.source.length; position++) {
43
+ unsigned char character = (unsigned char) lookahead.source.data[position];
44
+
45
+ if (is_newline(character)) { return true; }
46
+ if (!isspace(character)) { return false; }
47
+ }
48
+
49
+ return true;
50
+ }
51
+
52
+ static bool ruby_comment_on_own_line(const lexer_T* lexer, uint32_t tag_start) {
53
+ return ruby_comment_starts_line(lexer, tag_start) && ruby_comment_ends_line(lexer);
54
+ }
55
+
22
56
  typedef struct {
23
57
  herb_extract_ruby_options_T options;
24
58
  hb_buffer_T* output;
@@ -26,6 +60,7 @@ typedef struct {
26
60
  bool is_comment_tag;
27
61
  bool is_erb_comment_tag;
28
62
  bool need_newline;
63
+ uint32_t tag_start;
29
64
  } extract_ruby_state_T;
30
65
 
31
66
  static void extract_ruby_data(extract_ruby_state_T* state, const char* data, uint32_t from, uint32_t to) {
@@ -44,7 +79,12 @@ static void extract_ruby_data(extract_ruby_state_T* state, const char* data, uin
44
79
  if (state->options.preserve_positions) { hb_buffer_append_whitespace(state->output, to - run_start); }
45
80
  }
46
81
 
47
- static void extract_ruby_token(extract_ruby_state_T* state, const token_T* token, const token_T* next) {
82
+ static void extract_ruby_token(
83
+ extract_ruby_state_T* state,
84
+ const token_T* token,
85
+ const token_T* next,
86
+ const lexer_T* lexer
87
+ ) {
48
88
  switch (token->type) {
49
89
  case TOKEN_NEWLINE: {
50
90
  hb_buffer_append_string(state->output, token->value);
@@ -53,6 +93,7 @@ static void extract_ruby_token(extract_ruby_state_T* state, const token_T* token
53
93
  }
54
94
 
55
95
  case TOKEN_ERB_START: {
96
+ state->tag_start = token->range.from;
56
97
  state->is_erb_comment_tag = hb_string_equals(token->value, hb_string("<%#"));
57
98
 
58
99
  if (state->is_erb_comment_tag) {
@@ -113,20 +154,19 @@ static void extract_ruby_token(extract_ruby_state_T* state, const token_T* token
113
154
  if (state->skip_erb_content == false) {
114
155
  bool is_inline_comment = false;
115
156
 
116
- if (!state->options.comments && !state->is_comment_tag && !hb_string_is_empty(token->value)) {
157
+ if (!state->options.comments && state->options.preserve_positions && !state->is_comment_tag
158
+ && !hb_string_is_empty(token->value)) {
117
159
  hb_string_T trimmed = hb_string_trim_start(token->value);
118
160
 
119
161
  if (!hb_string_is_empty(trimmed) && trimmed.data[0] == '#'
120
162
  && token->location.start.line == token->location.end.line) {
121
163
  state->is_comment_tag = true;
122
- is_inline_comment = true;
164
+ is_inline_comment = !ruby_comment_on_own_line(lexer, state->tag_start);
123
165
  }
124
166
  }
125
167
 
126
168
  if (is_inline_comment) {
127
- if (state->options.preserve_positions) {
128
- hb_buffer_append_whitespace(state->output, range_length(token->range));
129
- }
169
+ hb_buffer_append_whitespace(state->output, range_length(token->range));
130
170
  } else if (state->is_erb_comment_tag && !hb_string_is_null(token->value)) {
131
171
  const char* content = token->value.data;
132
172
  size_t content_remaining = token->value.length;
@@ -260,7 +300,7 @@ void herb_extract_ruby_to_buffer_with_options(
260
300
 
261
301
  if (token->type == TOKEN_ERB_START) { pending = lexer_next_token(&lexer); }
262
302
 
263
- extract_ruby_token(&state, token, pending);
303
+ extract_ruby_token(&state, token, pending, &lexer);
264
304
  token_free(token, allocator);
265
305
  }
266
306
  }
data/src/include/errors.h CHANGED
@@ -269,8 +269,8 @@ typedef struct {
269
269
 
270
270
  typedef struct {
271
271
  ERROR_T base;
272
- hb_string_T partial;
273
272
  hb_string_T keywords;
273
+ hb_string_T suggestion;
274
274
  } RENDER_MISSING_LOCALS_ERROR_T;
275
275
 
276
276
  typedef struct {
@@ -430,8 +430,8 @@ NESTED_ERB_TAG_ERROR_T* nested_erb_tag_error_init(token_T* opening_tag, size_t n
430
430
  void append_nested_erb_tag_error(token_T* opening_tag, size_t nested_tag_line, size_t nested_tag_column, position_T start, position_T end, hb_allocator_T* allocator, hb_array_T** errors, const struct PARSER_OPTIONS_STRUCT* options);
431
431
  RENDER_AMBIGUOUS_LOCALS_ERROR_T* render_ambiguous_locals_error_init(hb_string_T partial, position_T start, position_T end, hb_allocator_T* allocator);
432
432
  void append_render_ambiguous_locals_error(hb_string_T partial, position_T start, position_T end, hb_allocator_T* allocator, hb_array_T** errors, const struct PARSER_OPTIONS_STRUCT* options);
433
- RENDER_MISSING_LOCALS_ERROR_T* render_missing_locals_error_init(hb_string_T partial, hb_string_T keywords, position_T start, position_T end, hb_allocator_T* allocator);
434
- void append_render_missing_locals_error(hb_string_T partial, hb_string_T keywords, position_T start, position_T end, hb_allocator_T* allocator, hb_array_T** errors, const struct PARSER_OPTIONS_STRUCT* options);
433
+ RENDER_MISSING_LOCALS_ERROR_T* render_missing_locals_error_init(hb_string_T keywords, hb_string_T suggestion, position_T start, position_T end, hb_allocator_T* allocator);
434
+ void append_render_missing_locals_error(hb_string_T keywords, hb_string_T suggestion, position_T start, position_T end, hb_allocator_T* allocator, hb_array_T** errors, const struct PARSER_OPTIONS_STRUCT* options);
435
435
  RENDER_NO_ARGUMENTS_ERROR_T* render_no_arguments_error_init(position_T start, position_T end, hb_allocator_T* allocator);
436
436
  void append_render_no_arguments_error(position_T start, position_T end, hb_allocator_T* allocator, hb_array_T** errors, const struct PARSER_OPTIONS_STRUCT* options);
437
437
  RENDER_CONFLICTING_PARTIAL_ERROR_T* render_conflicting_partial_error_init(hb_string_T positional_partial, hb_string_T keyword_partial, position_T start, position_T end, hb_allocator_T* allocator);
@@ -1,6 +1,6 @@
1
1
  #ifndef HERB_VERSION_H
2
2
  #define HERB_VERSION_H
3
3
 
4
- #define HERB_VERSION "0.11.2"
4
+ #define HERB_VERSION "0.11.3"
5
5
 
6
6
  #endif
data/src/lexer/token.c CHANGED
@@ -220,7 +220,7 @@ token_T* token_copy(token_T* token, hb_allocator_T* allocator) {
220
220
  if (!new_token) { return NULL; }
221
221
 
222
222
  new_token->value = token->owns_value ? hb_string_copy(token->value, allocator) : token->value;
223
- new_token->owns_value = token->owns_value;
223
+ new_token->owns_value = token->owns_value && !hb_string_is_empty(new_token->value);
224
224
 
225
225
  new_token->type = token->type;
226
226
  new_token->range = token->range;
@@ -261,7 +261,7 @@ static token_T* token_from_slice(
261
261
  if (!slice) { return NULL; }
262
262
 
263
263
  slice->value = token->owns_value ? hb_string_copy(value, allocator) : value;
264
- slice->owns_value = token->owns_value;
264
+ slice->owns_value = token->owns_value && !hb_string_is_empty(slice->value);
265
265
 
266
266
  slice->type = token->type;
267
267
  slice->range = (range_T) { .from = token->range.from + offset, .to = token->range.from + offset + value.length };
@@ -2,6 +2,7 @@
2
2
  #include "../include/errors.h"
3
3
  #include "../include/lexer/lexer.h"
4
4
  #include "../include/lexer/token.h"
5
+ #include "../include/lexer/token_matchers.h"
5
6
  #include "../include/parser/parser_helpers.h"
6
7
 
7
8
  #include <ctype.h>
@@ -17,16 +18,25 @@ static bool current_token_is_dot(parser_T* parser) {
17
18
  static void consume_dot_notation_loop(parser_T* parser, token_T* tag_name, hb_array_T** errors) {
18
19
  while (current_token_is_dot(parser)) {
19
20
  token_T* dot = parser_advance(parser);
20
- token_T* segment = parser_consume_expected(parser, TOKEN_IDENTIFIER, errors);
21
21
 
22
- if (segment == NULL || segment->value.length == 0) {
23
- if (segment) { token_free(segment, parser->allocator); }
22
+ if (!token_is(parser, TOKEN_IDENTIFIER) || parser->current_token->value.length == 0) {
23
+ append_unexpected_token_error(
24
+ TOKEN_IDENTIFIER,
25
+ parser->current_token,
26
+ parser->current_token->location.start,
27
+ parser->current_token->location.end,
28
+ parser->allocator,
29
+ errors,
30
+ &parser->options
31
+ );
32
+
24
33
  token_free(dot, parser->allocator);
25
34
  break;
26
35
  }
27
36
 
28
- uint32_t new_length = (uint32_t) (segment->value.data + segment->value.length - tag_name->value.data);
29
- tag_name->value.length = new_length;
37
+ token_T* segment = parser_advance(parser);
38
+
39
+ tag_name->value.length = segment->range.to - tag_name->range.from;
30
40
  tag_name->location.end = segment->location.end;
31
41
  tag_name->range.to = segment->range.to;
32
42
 
data/src/parser.c CHANGED
@@ -773,7 +773,9 @@ static AST_HTML_ATTRIBUTE_NODE_T* parser_parse_html_attribute_missing_value(
773
773
  if (hb_array_size(attribute_name->children) > 0) {
774
774
  AST_LITERAL_NODE_T* first_child = (AST_LITERAL_NODE_T*) hb_array_get(attribute_name->children, 0);
775
775
 
776
- if (first_child && !hb_string_is_empty(first_child->content)) { attribute_name_string = first_child->content; }
776
+ if (first_child && first_child->base.type == AST_LITERAL_NODE && !hb_string_is_empty(first_child->content)) {
777
+ attribute_name_string = first_child->content;
778
+ }
777
779
  }
778
780
 
779
781
  append_missing_attribute_value_error(
@@ -34,15 +34,7 @@ static void diff_element_node(
34
34
  }
35
35
 
36
36
  if (old_element->open_tag != NULL && new_element->open_tag != NULL) {
37
- herb_hash_T old_open_hash = herb_hash_map_get(old_hashes, old_element->open_tag);
38
- herb_hash_T new_open_hash = herb_hash_map_get(new_hashes, new_element->open_tag);
39
-
40
- if (old_open_hash != new_open_hash) {
41
- const AST_HTML_OPEN_TAG_NODE_T* old_open_tag = (const AST_HTML_OPEN_TAG_NODE_T*) old_element->open_tag;
42
- const AST_HTML_OPEN_TAG_NODE_T* new_open_tag = (const AST_HTML_OPEN_TAG_NODE_T*) new_element->open_tag;
43
-
44
- herb_diff_attributes(old_open_tag->children, new_open_tag->children, path, old_hashes, new_hashes, result);
45
- }
37
+ herb_diff_node(old_element->open_tag, new_element->open_tag, path, old_hashes, new_hashes, preserve_whitespace, result);
46
38
  }
47
39
 
48
40
  if (old_element->body != NULL && new_element->body != NULL) {
@@ -98,7 +98,7 @@ herb_hash_T herb_hash_node_move_identity(const AST_NODE_T* node, const herb_hash
98
98
  const AST_HTML_ELEMENT_NODE_T* element = (const AST_HTML_ELEMENT_NODE_T*) node;
99
99
  hash = hash_token(hash, element->tag_name);
100
100
 
101
- if (element->open_tag != NULL) {
101
+ if (element->open_tag != NULL && element->open_tag->type == AST_HTML_OPEN_TAG_NODE) {
102
102
  const AST_HTML_OPEN_TAG_NODE_T* open_tag = (const AST_HTML_OPEN_TAG_NODE_T*) element->open_tag;
103
103
  hash = herb_hash_combine(hash, identity_hash_attributes(open_tag->children, hash_map));
104
104
  }
@@ -39,7 +39,7 @@ void error_init(ERROR_T* error, const error_type_T type, position_T start, posit
39
39
  error_init(&<%= error.human %>->base, <%= error.type %>, start, end);
40
40
 
41
41
  <%- if error.message_arguments.any? -%>
42
- const char* message_template = "<%= error.message_template %>";
42
+ const char* message_template = "<%= error.message_template.gsub(/[\\"]/) { |character| "\\#{character}" } %>";
43
43
 
44
44
  <%- error.message_arguments.each_with_index do |argument, i| -%>
45
45
  <%- if error.message_template.scan(/%(?:zu|llu|lf|ld|[sdulf])/)[i] == "%s" -%>
@@ -69,7 +69,7 @@ void error_init(ERROR_T* error, const error_type_T type, position_T start, posit
69
69
  <%- end -%>
70
70
  <%= error.human %>->base.message = hb_string_copy(hb_string(message), allocator);
71
71
  <%- else -%>
72
- <%= error.human %>->base.message = hb_string_copy(hb_string("<%= error.message_template %>"), allocator);
72
+ <%= error.human %>->base.message = hb_string_copy(hb_string("<%= error.message_template.gsub(/[\\"]/) { |character| "\\#{character}" } %>"), allocator);
73
73
  <%- end -%>
74
74
 
75
75
  <%- error.fields.each do |field| -%>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: herb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.2
4
+ version: 0.11.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Roth