yarp 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/src/serialize.c CHANGED
@@ -5,10 +5,7 @@
5
5
  /* if you are looking to modify the */
6
6
  /* template */
7
7
  /******************************************************************************/
8
- #include "yarp/ast.h"
9
- #include "yarp/diagnostic.h"
10
- #include "yarp/parser.h"
11
- #include "yarp/util/yp_buffer.h"
8
+ #include "yarp.h"
12
9
 
13
10
  #include <stdio.h>
14
11
 
@@ -299,7 +296,7 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
299
296
  } else {
300
297
  yp_serialize_node(parser, (yp_node_t *)((yp_call_node_t *)node)->block, buffer);
301
298
  }
302
- yp_buffer_append_u32(buffer, ((yp_call_node_t *)node)->flags);
299
+ yp_buffer_append_u32(buffer, node->flags >> 1);
303
300
  uint32_t name_length = yp_sizet_to_u32(yp_string_length(&((yp_call_node_t *)node)->name));
304
301
  yp_buffer_append_u32(buffer, name_length);
305
302
  yp_buffer_append_str(buffer, yp_string_source(&((yp_call_node_t *)node)->name), name_length);
@@ -496,10 +493,10 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
496
493
  break;
497
494
  }
498
495
  case YP_NODE_DEF_NODE: {
499
- // serialize length
500
- // encoding of location u32s make us need to save this offset.
501
- size_t length_offset = buffer->length;
502
- yp_buffer_append_str(buffer, "\0\0\0\0", 4); /* consume 4 bytes, updated below */
496
+ // serialize length
497
+ // encoding of location u32s make us need to save this offset.
498
+ size_t length_offset = buffer->length;
499
+ yp_buffer_append_str(buffer, "\0\0\0\0", 4); /* consume 4 bytes, updated below */
503
500
  serialize_location(parser, &((yp_def_node_t *)node)->name_loc, buffer);
504
501
  if (((yp_def_node_t *)node)->receiver == NULL) {
505
502
  yp_buffer_append_u8(buffer, 0);
@@ -552,9 +549,9 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
552
549
  yp_buffer_append_u8(buffer, 1);
553
550
  serialize_location(parser, &((yp_def_node_t *)node)->end_keyword_loc, buffer);
554
551
  }
555
- // serialize length
556
- uint32_t length = yp_sizet_to_u32(buffer->length - offset - sizeof(uint32_t));
557
- memcpy(buffer->value + length_offset, &length, sizeof(uint32_t));
552
+ // serialize length
553
+ uint32_t length = yp_sizet_to_u32(buffer->length - offset - sizeof(uint32_t));
554
+ memcpy(buffer->value + length_offset, &length, sizeof(uint32_t));
558
555
  break;
559
556
  }
560
557
  case YP_NODE_DEFINED_NODE: {
@@ -644,6 +641,21 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
644
641
  }
645
642
  break;
646
643
  }
644
+ case YP_NODE_FLIP_FLOP_NODE: {
645
+ if (((yp_flip_flop_node_t *)node)->left == NULL) {
646
+ yp_buffer_append_u8(buffer, 0);
647
+ } else {
648
+ yp_serialize_node(parser, (yp_node_t *)((yp_flip_flop_node_t *)node)->left, buffer);
649
+ }
650
+ if (((yp_flip_flop_node_t *)node)->right == NULL) {
651
+ yp_buffer_append_u8(buffer, 0);
652
+ } else {
653
+ yp_serialize_node(parser, (yp_node_t *)((yp_flip_flop_node_t *)node)->right, buffer);
654
+ }
655
+ serialize_location(parser, &((yp_flip_flop_node_t *)node)->operator_loc, buffer);
656
+ yp_buffer_append_u32(buffer, node->flags >> 1);
657
+ break;
658
+ }
647
659
  case YP_NODE_FLOAT_NODE: {
648
660
  break;
649
661
  }
@@ -851,7 +863,7 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
851
863
  yp_serialize_node(parser, (yp_node_t *) ((yp_interpolated_regular_expression_node_t *)node)->parts.nodes[index], buffer);
852
864
  }
853
865
  serialize_location(parser, &((yp_interpolated_regular_expression_node_t *)node)->closing_loc, buffer);
854
- yp_buffer_append_u32(buffer, ((yp_interpolated_regular_expression_node_t *)node)->flags);
866
+ yp_buffer_append_u32(buffer, node->flags >> 1);
855
867
  break;
856
868
  }
857
869
  case YP_NODE_INTERPOLATED_STRING_NODE: {
@@ -1192,7 +1204,7 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
1192
1204
  yp_serialize_node(parser, (yp_node_t *)((yp_range_node_t *)node)->right, buffer);
1193
1205
  }
1194
1206
  serialize_location(parser, &((yp_range_node_t *)node)->operator_loc, buffer);
1195
- yp_buffer_append_u32(buffer, ((yp_range_node_t *)node)->flags);
1207
+ yp_buffer_append_u32(buffer, node->flags >> 1);
1196
1208
  break;
1197
1209
  }
1198
1210
  case YP_NODE_RATIONAL_NODE: {
@@ -1209,7 +1221,7 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
1209
1221
  uint32_t unescaped_length = yp_sizet_to_u32(yp_string_length(&((yp_regular_expression_node_t *)node)->unescaped));
1210
1222
  yp_buffer_append_u32(buffer, unescaped_length);
1211
1223
  yp_buffer_append_str(buffer, yp_string_source(&((yp_regular_expression_node_t *)node)->unescaped), unescaped_length);
1212
- yp_buffer_append_u32(buffer, ((yp_regular_expression_node_t *)node)->flags);
1224
+ yp_buffer_append_u32(buffer, node->flags >> 1);
1213
1225
  break;
1214
1226
  }
1215
1227
  case YP_NODE_REQUIRED_DESTRUCTURED_PARAMETER_NODE: {
@@ -1443,7 +1455,7 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
1443
1455
  } else {
1444
1456
  yp_serialize_node(parser, (yp_node_t *)((yp_until_node_t *)node)->statements, buffer);
1445
1457
  }
1446
- yp_buffer_append_u32(buffer, ((yp_until_node_t *)node)->flags);
1458
+ yp_buffer_append_u32(buffer, node->flags >> 1);
1447
1459
  break;
1448
1460
  }
1449
1461
  case YP_NODE_WHEN_NODE: {
@@ -1468,7 +1480,7 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
1468
1480
  } else {
1469
1481
  yp_serialize_node(parser, (yp_node_t *)((yp_while_node_t *)node)->statements, buffer);
1470
1482
  }
1471
- yp_buffer_append_u32(buffer, ((yp_while_node_t *)node)->flags);
1483
+ yp_buffer_append_u32(buffer, node->flags >> 1);
1472
1484
  break;
1473
1485
  }
1474
1486
  case YP_NODE_X_STRING_NODE: {
@@ -1504,6 +1516,24 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
1504
1516
  }
1505
1517
  }
1506
1518
 
1519
+ void yp_serialize_comment(yp_parser_t *parser, yp_comment_t *comment, yp_buffer_t *buffer) {
1520
+ // serialize type
1521
+ yp_buffer_append_u8(buffer, (uint8_t) comment->type);
1522
+
1523
+ // serialize location
1524
+ yp_buffer_append_u32(buffer, yp_ptrdifft_to_u32(comment->start - parser->start));
1525
+ yp_buffer_append_u32(buffer, yp_ptrdifft_to_u32(comment->end - comment->start));
1526
+ }
1527
+
1528
+ void yp_serialize_comment_list(yp_parser_t *parser, yp_list_t list, yp_buffer_t *buffer) {
1529
+ yp_buffer_append_u32(buffer, yp_list_size(&list));
1530
+
1531
+ yp_comment_t *comment;
1532
+ for (comment = (yp_comment_t *) list.head; comment != NULL; comment = (yp_comment_t *) comment->node.next) {
1533
+ yp_serialize_comment(parser, comment, buffer);
1534
+ }
1535
+ }
1536
+
1507
1537
  void yp_serialize_diagnostic(yp_parser_t *parser, yp_diagnostic_t *diagnostic, yp_buffer_t *buffer) {
1508
1538
  // serialize message
1509
1539
  size_t message_length = strlen(diagnostic->message);
@@ -1524,6 +1554,7 @@ void yp_serialize_diagnostic_list(yp_parser_t *parser, yp_list_t list, yp_buffer
1524
1554
  }
1525
1555
  }
1526
1556
 
1557
+ #line 145 "serialize.c.erb"
1527
1558
  void
1528
1559
  yp_serialize_content(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
1529
1560
  // First, serialize the encoding of the parser.
@@ -1531,6 +1562,9 @@ yp_serialize_content(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer)
1531
1562
  yp_buffer_append_u32(buffer, yp_sizet_to_u32(encoding_length));
1532
1563
  yp_buffer_append_str(buffer, parser->encoding.name, encoding_length);
1533
1564
 
1565
+ // Serialize the comments
1566
+ yp_serialize_comment_list(parser, parser->comment_list, buffer);
1567
+
1534
1568
  // Serialize the errors
1535
1569
  yp_serialize_diagnostic_list(parser, parser->error_list, buffer);
1536
1570
 
@@ -1574,3 +1608,42 @@ yp_serialize_content(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer)
1574
1608
  }
1575
1609
  }
1576
1610
  }
1611
+
1612
+ static void
1613
+ serialize_token(void *data, yp_parser_t *parser, yp_token_t *token) {
1614
+ yp_buffer_t *buffer = (yp_buffer_t *) data;
1615
+
1616
+ yp_buffer_append_u32(buffer, token->type);
1617
+ yp_buffer_append_u32(buffer, yp_ptrdifft_to_u32(token->start - parser->start));
1618
+ yp_buffer_append_u32(buffer, yp_ptrdifft_to_u32(token->end - token->start));
1619
+ yp_buffer_append_u32(buffer, parser->lex_state);
1620
+ }
1621
+
1622
+ YP_EXPORTED_FUNCTION void
1623
+ yp_lex_serialize(const char *source, size_t size, const char *filepath, yp_buffer_t *buffer) {
1624
+ yp_parser_t parser;
1625
+ yp_parser_init(&parser, source, size, filepath);
1626
+
1627
+ yp_lex_callback_t lex_callback = (yp_lex_callback_t) {
1628
+ .data = (void *) buffer,
1629
+ .callback = serialize_token,
1630
+ };
1631
+
1632
+ parser.lex_callback = &lex_callback;
1633
+ yp_node_t *node = yp_parse(&parser);
1634
+
1635
+ // Append 0 to mark end of tokens
1636
+ yp_buffer_append_u32(buffer, 0);
1637
+
1638
+ // Serialize the comments
1639
+ yp_serialize_comment_list(&parser, parser.comment_list, buffer);
1640
+
1641
+ // Serialize the errors
1642
+ yp_serialize_diagnostic_list(&parser, parser.error_list, buffer);
1643
+
1644
+ // Serialize the warnings
1645
+ yp_serialize_diagnostic_list(&parser, parser.warning_list, buffer);
1646
+
1647
+ yp_node_destroy(&parser, node);
1648
+ yp_parser_free(&parser);
1649
+ }
data/src/util/yp_string.c CHANGED
@@ -97,10 +97,8 @@ yp_string_free(yp_string_t *string) {
97
97
  void *memory = (void *) string->source;
98
98
  #if defined(_WIN32)
99
99
  UnmapViewOfFile(memory);
100
- #elif defined(HAVE_MMAP)
101
- munmap(memory, string->length);
102
100
  #else
103
- free(memory);
101
+ munmap(memory, string->length);
104
102
  #endif
105
103
  }
106
104
  }
@@ -180,28 +178,21 @@ yp_string_mapped_init(yp_string_t *string, const char *filepath) {
180
178
  return true;
181
179
  }
182
180
 
183
- #ifdef HAVE_MMAP
184
181
  source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
185
182
  if (source == MAP_FAILED) {
186
183
  perror("Map failed");
187
184
  return false;
188
185
  }
189
- #else
190
- source = malloc(size);
191
- if (source == NULL) {
192
- return false;
193
- }
194
-
195
- ssize_t read_size = read(fd, (void *) source, size);
196
- if (read_size < 0 || (size_t)read_size != size) {
197
- perror("Read size is incorrect");
198
- free((void *) source);
199
- return false;
200
- }
201
- #endif
202
186
 
203
187
  close(fd);
204
188
  yp_string_mapped_init_internal(string, source, size);
205
189
  return true;
206
190
  #endif
207
191
  }
192
+
193
+ // Returns the size of the yp_string_t struct. This is necessary to allocate the
194
+ // correct amount of memory in the FFI backend.
195
+ YP_EXPORTED_FUNCTION size_t
196
+ yp_string_sizeof(void) {
197
+ return sizeof(yp_string_t);
198
+ }