packcr 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -0
  3. data/README.md +73 -0
  4. data/lib/packcr/context.rb +1 -1
  5. data/lib/packcr/generated/context.rb +121 -78
  6. data/lib/packcr/generated/node/action_node.rb +2 -2
  7. data/lib/packcr/generated/node/alternate_node.rb +2 -2
  8. data/lib/packcr/generated/node/capture_node.rb +1 -1
  9. data/lib/packcr/generated/node/charclass_node.rb +9 -9
  10. data/lib/packcr/generated/node/eof_node.rb +1 -1
  11. data/lib/packcr/generated/node/error_node.rb +2 -2
  12. data/lib/packcr/generated/node/expand_node.rb +2 -2
  13. data/lib/packcr/generated/node/predicate_node.rb +2 -2
  14. data/lib/packcr/generated/node/quantity_node.rb +30 -31
  15. data/lib/packcr/generated/node/reference_node.rb +2 -2
  16. data/lib/packcr/generated/node/rule_node.rb +7 -7
  17. data/lib/packcr/generated/node/string_node.rb +4 -4
  18. data/lib/packcr/parser.rb +619 -613
  19. data/lib/packcr/templates/context/header.c.erb +3 -3
  20. data/lib/packcr/templates/context/source.c.erb +417 -403
  21. data/lib/packcr/templates/context/source.rb.erb +42 -25
  22. data/lib/packcr/templates/node/action.c.erb +2 -2
  23. data/lib/packcr/templates/node/alternate.c.erb +2 -2
  24. data/lib/packcr/templates/node/capture.c.erb +2 -2
  25. data/lib/packcr/templates/node/charclass.c.erb +4 -4
  26. data/lib/packcr/templates/node/charclass_any.c.erb +2 -2
  27. data/lib/packcr/templates/node/charclass_one.c.erb +4 -4
  28. data/lib/packcr/templates/node/charclass_utf8.c.erb +2 -2
  29. data/lib/packcr/templates/node/eof.c.erb +1 -1
  30. data/lib/packcr/templates/node/error.c.erb +4 -4
  31. data/lib/packcr/templates/node/expand.c.erb +2 -2
  32. data/lib/packcr/templates/node/predicate.c.erb +1 -1
  33. data/lib/packcr/templates/node/predicate_neg.c.erb +1 -1
  34. data/lib/packcr/templates/node/quantity_many.c.erb +9 -7
  35. data/lib/packcr/templates/node/quantity_one.c.erb +9 -7
  36. data/lib/packcr/templates/node/quantity_one.rb.erb +4 -4
  37. data/lib/packcr/templates/node/reference.c.erb +6 -6
  38. data/lib/packcr/templates/node/rule.c.erb +9 -9
  39. data/lib/packcr/templates/node/string_many.c.erb +2 -2
  40. data/lib/packcr/templates/node/string_one.c.erb +2 -2
  41. data/lib/packcr/util.rb +1 -1
  42. data/lib/packcr/version.rb +1 -1
  43. metadata +5 -3
@@ -12,12 +12,12 @@ class Packcr
12
12
  if !code(:eheader).empty?
13
13
  erbout << "\n".freeze
14
14
  end
15
- erbout << "#ifndef PCC_INCLUDED_#{@hid}\n#define PCC_INCLUDED_#{@hid}\n\n".freeze
15
+ erbout << "#ifndef PACKCR_INCLUDED_#{@hid}\n#define PACKCR_INCLUDED_#{@hid}\n\n".freeze
16
16
 
17
17
  code(:header).each do |code|
18
18
  erbout << "#{stream.get_code_block(code, 0, @iname)}".freeze
19
19
  end
20
- erbout << "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\ntypedef struct #{prefix}_context_tag #{prefix}_context_t;\n\n#{prefix}_context_t *#{prefix}_create(#{auxil_def}auxil);\nint #{prefix}_parse(#{prefix}_context_t *ctx, #{value_def}*ret);\nvoid #{prefix}_destroy(#{prefix}_context_t *ctx);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* !PCC_INCLUDED_#{@hid} */\n".freeze
20
+ erbout << "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\ntypedef struct #{prefix}_context_tag #{prefix}_context_t;\n\n#{prefix}_context_t *#{prefix}_create(#{auxil_def}auxil);\nint #{prefix}_parse(#{prefix}_context_t *ctx, #{value_def}*ret);\nvoid #{prefix}_destroy(#{prefix}_context_t *ctx);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* !PACKCR_INCLUDED_#{@hid} */\n".freeze
21
21
 
22
22
  erbout
23
23
  end
@@ -35,7 +35,7 @@ class Packcr
35
35
  if !code(:esource).empty?
36
36
  erbout << "\n".freeze
37
37
  end
38
- erbout << "#ifdef _MSC_VER\n#undef _CRT_SECURE_NO_WARNINGS\n#define _CRT_SECURE_NO_WARNINGS\n#endif /* _MSC_VER */\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifndef _MSC_VER\n#if defined __GNUC__ && defined _WIN32 /* MinGW */\n#ifndef PCC_USE_SYSTEM_STRNLEN\n#define strnlen(str, maxlen) pcc_strnlen(str, maxlen)\nstatic size_t pcc_strnlen(const char *str, size_t maxlen) {\n size_t i;\n for (i = 0; i < maxlen && str[i]; i++);\n return i;\n}\n#endif /* !PCC_USE_SYSTEM_STRNLEN */\n#endif /* defined __GNUC__ && defined _WIN32 */\n#endif /* !_MSC_VER */\n\n#include \"#{@hname}\"\n".freeze
38
+ erbout << "#ifdef _MSC_VER\n#undef _CRT_SECURE_NO_WARNINGS\n#define _CRT_SECURE_NO_WARNINGS\n#endif /* _MSC_VER */\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#ifndef _MSC_VER\n#if defined __GNUC__ && defined _WIN32 /* MinGW */\n#ifndef PACKCR_USE_SYSTEM_STRNLEN\n#define strnlen(str, maxlen) packcr_strnlen(str, maxlen)\nstatic size_t packcr_strnlen(const char *str, size_t maxlen) {\n size_t i;\n for (i = 0; i < maxlen && str[i]; i++);\n return i;\n}\n#endif /* !PACKCR_USE_SYSTEM_STRNLEN */\n#endif /* defined __GNUC__ && defined _WIN32 */\n#endif /* !_MSC_VER */\n\n#include \"#{@hname}\"\n".freeze
39
39
 
40
40
  if !code(:location).empty?
41
41
  erbout << "\n".freeze
@@ -49,154 +49,174 @@ class Packcr
49
49
  code(:source).each do |code|
50
50
  erbout << "#{stream.get_code_block(code, 0, @iname)}".freeze
51
51
  end
52
- erbout << "#if !defined __has_attribute || defined _MSC_VER\n#define __attribute__(x)\n#endif\n\n#ifdef _MSC_VER\n#define MARK_FUNC_AS_USED __pragma(warning(suppress:4505))\n#else\n#define MARK_FUNC_AS_USED __attribute__((__unused__))\n#endif\n\n#ifndef PCC_BUFFER_MIN_SIZE\n#define PCC_BUFFER_MIN_SIZE 256\n#endif /* !PCC_BUFFER_MIN_SIZE */\n\n#ifndef PCC_ARRAY_MIN_SIZE\n#define PCC_ARRAY_MIN_SIZE 2\n#endif /* !PCC_ARRAY_MIN_SIZE */\n\n#ifndef PCC_POOL_MIN_SIZE\n#define PCC_POOL_MIN_SIZE 65536\n#endif /* !PCC_POOL_MIN_SIZE */\n\n#define PCC_DBG_EVALUATE 0\n#define PCC_DBG_MATCH 1\n#define PCC_DBG_NOMATCH 2\n\n#define PCC_VOID_VALUE (~(size_t)0)\n\ntypedef enum pcc_bool_tag {\n PCC_FALSE = 0,\n PCC_TRUE\n} pcc_bool_t;\n\ntypedef struct pcc_char_array_tag {\n char *buf;\n size_t max;\n size_t len;\n} pcc_char_array_t;\n\ntypedef struct pcc_range_tag {\n size_t start;\n size_t end;\n".freeze
52
+ erbout << "#if !defined __has_attribute || defined _MSC_VER\n#define __attribute__(x)\n#endif\n\n#ifdef _MSC_VER\n#define MARK_FUNC_AS_USED __pragma(warning(suppress:4505))\n#else\n#define MARK_FUNC_AS_USED __attribute__((__unused__))\n#endif\n\n#ifndef PACKCR_BUFFER_MIN_SIZE\n#define PACKCR_BUFFER_MIN_SIZE 256\n#endif /* !PACKCR_BUFFER_MIN_SIZE */\n\n#ifndef PACKCR_ARRAY_MIN_SIZE\n#define PACKCR_ARRAY_MIN_SIZE 2\n#endif /* !PACKCR_ARRAY_MIN_SIZE */\n\n#ifndef PACKCR_POOL_MIN_SIZE\n#define PACKCR_POOL_MIN_SIZE 65536\n#endif /* !PACKCR_POOL_MIN_SIZE */\n\n#define PACKCR_DBG_EVALUATE 0\n#define PACKCR_DBG_MATCH 1\n#define PACKCR_DBG_NOMATCH 2\n\n#define PACKCR_VOID_VALUE (~(size_t)0)\n\ntypedef enum packcr_bool_tag {\n PACKCR_FALSE = 0,\n PACKCR_TRUE\n} packcr_bool_t;\n\ntypedef struct packcr_char_array_tag {\n char *buf;\n size_t max;\n size_t len;\n} packcr_char_array_t;\n\ntypedef struct packcr_range_tag {\n size_t start;\n size_t end;\n".freeze
53
53
 
54
54
  if @location
55
- erbout << " pcc_location_t start_loc;\n pcc_location_t end_loc;\n".freeze
55
+ erbout << " packcr_location_t start_loc;\n packcr_location_t end_loc;\n".freeze
56
56
  end
57
- erbout << "} pcc_range_t;\n\ntypedef #{value_def}pcc_value_t;\n\ntypedef #{auxil_def}pcc_auxil_t;\n\n".freeze
57
+ erbout << "} packcr_range_t;\n\ntypedef #{value_def}packcr_value_t;\n\ntypedef #{auxil_def}packcr_auxil_t;\n\n".freeze
58
58
 
59
- if prefix != "pcc"
60
- erbout << "typedef #{prefix}_context_t pcc_context_t;\n\n".freeze
59
+ if prefix != "packcr"
60
+ erbout << "typedef #{prefix}_context_t packcr_context_t;\n\n".freeze
61
61
  end
62
- erbout << "typedef struct pcc_value_table_tag {\n pcc_value_t *buf;\n size_t max;\n size_t len;\n} pcc_value_table_t;\n\ntypedef struct pcc_value_refer_table_tag {\n pcc_value_t **buf;\n size_t max;\n size_t len;\n} pcc_value_refer_table_t;\n\ntypedef struct pcc_capture_tag {\n pcc_range_t range;\n char *string; /* mutable */\n} pcc_capture_t;\n\ntypedef struct pcc_capture_table_tag {\n pcc_capture_t *buf;\n size_t max;\n size_t len;\n} pcc_capture_table_t;\n\ntypedef struct pcc_capture_const_table_tag {\n const pcc_capture_t **buf;\n size_t max;\n size_t len;\n} pcc_capture_const_table_t;\n\ntypedef struct pcc_thunk_tag pcc_thunk_t;\ntypedef struct pcc_thunk_array_tag pcc_thunk_array_t;\n\ntypedef void (*pcc_action_t)(pcc_context_t *, pcc_thunk_t *, pcc_value_t *);\n\ntypedef enum pcc_thunk_type_tag {\n PCC_THUNK_LEAF,\n PCC_THUNK_NODE\n} pcc_thunk_type_t;\n\ntypedef struct pcc_thunk_leaf_tag {\n pcc_value_refer_table_t values;\n pcc_capture_const_table_t capts;\n pcc_capture_t capt0;\n pcc_action_t action;\n} pcc_thunk_leaf_t;\n\ntypedef struct pcc_thunk_node_tag {\n const pcc_thunk_array_t *thunks; /* just a reference */\n pcc_value_t *value; /* just a reference */\n} pcc_thunk_node_t;\n\ntypedef union pcc_thunk_data_tag {\n pcc_thunk_leaf_t leaf;\n pcc_thunk_node_t node;\n} pcc_thunk_data_t;\n\nstruct pcc_thunk_tag {\n pcc_thunk_type_t type;\n pcc_thunk_data_t data;\n};\n\nstruct pcc_thunk_array_tag {\n pcc_thunk_t **buf;\n size_t max;\n size_t len;\n};\n\ntypedef struct pcc_thunk_chunk_tag {\n pcc_value_table_t values;\n pcc_capture_table_t capts;\n pcc_thunk_array_t thunks;\n size_t pos; /* the starting position in the character buffer */\n".freeze
62
+ erbout << "typedef struct packcr_value_table_tag {\n packcr_value_t *buf;\n size_t max;\n size_t len;\n} packcr_value_table_t;\n\ntypedef struct packcr_value_refer_table_tag {\n packcr_value_t **buf;\n size_t max;\n size_t len;\n} packcr_value_refer_table_t;\n\ntypedef struct packcr_capture_tag {\n packcr_range_t range;\n char *string; /* mutable */\n} packcr_capture_t;\n\ntypedef struct packcr_capture_table_tag {\n packcr_capture_t *buf;\n size_t max;\n size_t len;\n} packcr_capture_table_t;\n\ntypedef struct packcr_capture_const_table_tag {\n const packcr_capture_t **buf;\n size_t max;\n size_t len;\n} packcr_capture_const_table_t;\n\ntypedef struct packcr_thunk_tag packcr_thunk_t;\ntypedef struct packcr_thunk_array_tag packcr_thunk_array_t;\n\ntypedef void (*packcr_action_t)(packcr_context_t *, packcr_thunk_t *, packcr_value_t *);\n\ntypedef enum packcr_thunk_type_tag {\n PACKCR_THUNK_LEAF,\n PACKCR_THUNK_NODE\n} packcr_thunk_type_t;\n\ntypedef struct packcr_thunk_leaf_tag {\n packcr_value_refer_table_t values;\n packcr_capture_const_table_t capts;\n packcr_capture_t capt0;\n packcr_action_t action;\n} packcr_thunk_leaf_t;\n\ntypedef struct packcr_thunk_node_tag {\n const packcr_thunk_array_t *thunks; /* just a reference */\n packcr_value_t *value; /* just a reference */\n} packcr_thunk_node_t;\n\ntypedef union packcr_thunk_data_tag {\n packcr_thunk_leaf_t leaf;\n packcr_thunk_node_t node;\n} packcr_thunk_data_t;\n\nstruct packcr_thunk_tag {\n packcr_thunk_type_t type;\n packcr_thunk_data_t data;\n};\n\nstruct packcr_thunk_array_tag {\n packcr_thunk_t **buf;\n size_t max;\n size_t len;\n};\n\ntypedef struct packcr_thunk_chunk_tag {\n packcr_value_table_t values;\n packcr_capture_table_t capts;\n packcr_thunk_array_t thunks;\n size_t pos; /* the starting position in the character buffer */\n".freeze
63
63
 
64
64
  if @location
65
- erbout << " pcc_location_t pos_loc;\n".freeze
65
+ erbout << " packcr_location_t pos_loc;\n".freeze
66
66
  end
67
- erbout << "} pcc_thunk_chunk_t;\n\ntypedef struct pcc_lr_memo_tag pcc_lr_memo_t;\n\nstruct pcc_lr_memo_tag {\n size_t offset;\n".freeze
67
+ erbout << "} packcr_thunk_chunk_t;\n\ntypedef struct packcr_lr_memo_tag packcr_lr_memo_t;\n\nstruct packcr_lr_memo_tag {\n size_t offset;\n".freeze
68
68
 
69
69
  if @location
70
- erbout << " pcc_location_t offset_loc;\n".freeze
70
+ erbout << " packcr_location_t offset_loc;\n".freeze
71
71
  end
72
- erbout << " pcc_lr_memo_t *hold;\n pcc_thunk_chunk_t *chunk;\n pcc_bool_t fail;\n pcc_bool_t grow;\n};\n\ntypedef struct pcc_rule_set_tag pcc_rule_set_t;\n\ntypedef pcc_thunk_chunk_t *(*pcc_rule_t)(pcc_context_t *, size_t".freeze
72
+ erbout << " packcr_lr_memo_t *hold;\n packcr_thunk_chunk_t *chunk;\n packcr_bool_t fail;\n packcr_bool_t grow;\n};\n\ntypedef struct packcr_rule_set_tag packcr_rule_set_t;\n\ntypedef packcr_thunk_chunk_t *(*packcr_rule_t)(packcr_context_t *, size_t".freeze
73
73
  if @location
74
- erbout << ", pcc_location_t".freeze
74
+ erbout << ", packcr_location_t".freeze
75
75
  end
76
- erbout << ", pcc_rule_set_t*);\n\ntypedef struct pcc_rule_set_tag {\n pcc_rule_t *buf;\n size_t max;\n size_t len;\n} pcc_rule_set_t;\n\ntypedef struct pcc_lr_memo_map_entry_tag {\n pcc_rule_t rule;\n pcc_lr_memo_t *memo;\n} pcc_lr_memo_map_entry_t;\n\ntypedef struct pcc_lr_memo_map_tag {\n pcc_lr_memo_map_entry_t *buf;\n size_t max;\n size_t len;\n} pcc_lr_memo_map_t;\n\ntypedef struct pcc_lr_table_tag {\n pcc_lr_memo_map_t **buf;\n size_t max;\n size_t len;\n size_t ofs;\n} pcc_lr_table_t;\n\ntypedef struct pcc_memory_entry_tag pcc_memory_entry_t;\ntypedef struct pcc_memory_pool_tag pcc_memory_pool_t;\n\nstruct pcc_memory_entry_tag {\n pcc_memory_entry_t *next;\n};\n\nstruct pcc_memory_pool_tag {\n pcc_memory_pool_t *next;\n size_t allocated;\n size_t unused;\n};\n\ntypedef struct pcc_memory_recycler_tag {\n pcc_memory_pool_t *pool_list;\n pcc_memory_entry_t *entry_list;\n size_t element_size;\n} pcc_memory_recycler_t;\n\nstruct #{prefix}_context_tag {\n size_t buffer_start_position; /* the position in the input of the first character currently buffered */\n size_t position_offset; /* the current parsing position in the character buffer */\n".freeze
76
+ erbout << ", packcr_rule_set_t*);\n\ntypedef struct packcr_rule_set_tag {\n packcr_rule_t *buf;\n size_t max;\n size_t len;\n} packcr_rule_set_t;\n\ntypedef struct packcr_lr_memo_map_entry_tag {\n packcr_rule_t rule;\n packcr_lr_memo_t *memo;\n} packcr_lr_memo_map_entry_t;\n\ntypedef struct packcr_lr_memo_map_tag {\n packcr_lr_memo_map_entry_t *buf;\n size_t max;\n size_t len;\n} packcr_lr_memo_map_t;\n\ntypedef struct packcr_lr_table_tag {\n packcr_lr_memo_map_t **buf;\n size_t max;\n size_t len;\n size_t ofs;\n} packcr_lr_table_t;\n\ntypedef struct packcr_memory_entry_tag packcr_memory_entry_t;\ntypedef struct packcr_memory_pool_tag packcr_memory_pool_t;\n\nstruct packcr_memory_entry_tag {\n packcr_memory_entry_t *next;\n};\n\nstruct packcr_memory_pool_tag {\n packcr_memory_pool_t *next;\n size_t allocated;\n size_t unused;\n};\n\ntypedef struct packcr_memory_recycler_tag {\n packcr_memory_pool_t *pool_list;\n packcr_memory_entry_t *entry_list;\n size_t element_size;\n} packcr_memory_recycler_t;\n\nstruct #{prefix}_context_tag {\n size_t buffer_start_position; /* the position in the input of the first character currently buffered */\n size_t position_offset; /* the current parsing position in the character buffer */\n".freeze
77
77
 
78
78
  if @location
79
- erbout << " pcc_location_t buffer_start_position_loc;\n pcc_location_t position_offset_loc;\n".freeze
79
+ erbout << " packcr_location_t buffer_start_position_loc;\n packcr_location_t position_offset_loc;\n".freeze
80
80
  end
81
- erbout << " size_t level;\n pcc_char_array_t buffer;\n pcc_lr_table_t lrtable;\n pcc_thunk_array_t thunks;\n pcc_auxil_t auxil;\n pcc_memory_recycler_t thunk_chunk_recycler;\n pcc_memory_recycler_t lr_memo_recycler;\n};\n\n#ifndef PCC_ERROR\n#define PCC_ERROR(auxil) pcc_error()\nMARK_FUNC_AS_USED\nstatic void pcc_error(void) {\n fprintf(stderr, \"Syntax error\\n\");\n exit(1);\n}\n#endif /* !PCC_ERROR */\n\n#ifndef PCC_GETCHAR\n#define PCC_GETCHAR(auxil) getchar()\n#endif /* !PCC_GETCHAR */\n\n#ifndef PCC_MALLOC\n#define PCC_MALLOC(auxil, size) pcc_malloc_e(size)\nstatic void *pcc_malloc_e(size_t size) {\n void *const p = malloc(size);\n if (p == NULL) {\n fprintf(stderr, \"Out of memory\\n\");\n exit(1);\n }\n return p;\n}\n#endif /* !PCC_MALLOC */\n\n#ifndef PCC_REALLOC\n#define PCC_REALLOC(auxil, ptr, size) pcc_realloc_e(ptr, size)\nstatic void *pcc_realloc_e(void *ptr, size_t size) {\n void *const p = realloc(ptr, size);\n if (p == NULL) {\n fprintf(stderr, \"Out of memory\\n\");\n exit(1);\n }\n return p;\n}\n#endif /* !PCC_REALLOC */\n\n#ifndef PCC_FREE\n#define PCC_FREE(auxil, ptr) free(ptr)\n#endif /* !PCC_FREE */\n\n#ifndef PCC_DEBUG\n#define PCC_DEBUG(auxil, event, rule, level, pos, buffer, length) ((void)0)\n#endif /* !PCC_DEBUG */\n\nstatic char *pcc_strndup_e(pcc_auxil_t auxil, const char *str, size_t len) {\n const size_t m = strnlen(str, len);\n char *const s = (char *)PCC_MALLOC(auxil, m + 1);\n memcpy(s, str, m);\n s[m] = '\\0';\n return s;\n}\n\nstatic void pcc_char_array__init(pcc_auxil_t auxil, pcc_char_array_t *array) {\n array->len = 0;\n array->max = 0;\n array->buf = NULL;\n}\n\nstatic void pcc_char_array__add(pcc_auxil_t auxil, pcc_char_array_t *array, char ch) {\n if (array->max <= array->len) {\n const size_t n = array->len + 1;\n size_t m = array->max;\n if (m == 0) m = PCC_BUFFER_MIN_SIZE;\n while (m < n && m != 0) m <<= 1;\n if (m == 0) m = n;\n array->buf = (char *)PCC_REALLOC(auxil, array->buf, m);\n array->max = m;\n }\n array->buf[array->len++] = ch;\n}\n\nstatic void pcc_char_array__term(pcc_auxil_t auxil, pcc_char_array_t *array) {\n PCC_FREE(auxil, array->buf);\n}\n\nstatic void pcc_value_table__init(pcc_auxil_t auxil, pcc_value_table_t *table) {\n table->len = 0;\n table->max = 0;\n table->buf = NULL;\n}\n\nMARK_FUNC_AS_USED\nstatic void pcc_value_table__resize(pcc_auxil_t auxil, pcc_value_table_t *table, size_t len) {\n if (table->max < len) {\n size_t m = table->max;\n if (m == 0) m = PCC_ARRAY_MIN_SIZE;\n while (m < len && m != 0) m <<= 1;\n if (m == 0) m = len;\n table->buf = (pcc_value_t *)PCC_REALLOC(auxil, table->buf, sizeof(pcc_value_t) * m);\n table->max = m;\n }\n table->len = len;\n}\n\nMARK_FUNC_AS_USED\nstatic void pcc_value_table__clear(pcc_auxil_t auxil, pcc_value_table_t *table) {\n memset(table->buf, 0, sizeof(pcc_value_t) * table->len);\n}\n\nstatic void pcc_value_table__term(pcc_auxil_t auxil, pcc_value_table_t *table) {\n PCC_FREE(auxil, table->buf);\n}\n\nstatic void pcc_value_refer_table__init(pcc_auxil_t auxil, pcc_value_refer_table_t *table) {\n table->len = 0;\n table->max = 0;\n table->buf = NULL;\n}\n\nstatic void pcc_value_refer_table__resize(pcc_auxil_t auxil, pcc_value_refer_table_t *table, size_t len) {\n size_t i;\n if (table->max < len) {\n size_t m = table->max;\n if (m == 0) m = PCC_ARRAY_MIN_SIZE;\n while (m < len && m != 0) m <<= 1;\n if (m == 0) m = len;\n table->buf = (pcc_value_t **)PCC_REALLOC(auxil, table->buf, sizeof(pcc_value_t *) * m);\n table->max = m;\n }\n for (i = table->len; i < len; i++) table->buf[i] = NULL;\n table->len = len;\n}\n\nstatic void pcc_value_refer_table__term(pcc_auxil_t auxil, pcc_value_refer_table_t *table) {\n PCC_FREE(auxil, table->buf);\n}\n\nstatic void pcc_capture_table__init(pcc_auxil_t auxil, pcc_capture_table_t *table) {\n table->len = 0;\n table->max = 0;\n table->buf = NULL;\n}\n\nMARK_FUNC_AS_USED\nstatic void pcc_capture_table__resize(pcc_auxil_t auxil, pcc_capture_table_t *table, size_t len) {\n size_t i;\n for (i = len; i < table->len; i++) PCC_FREE(auxil, table->buf[i].string);\n if (table->max < len) {\n size_t m = table->max;\n if (m == 0) m = PCC_ARRAY_MIN_SIZE;\n while (m < len && m != 0) m <<= 1;\n if (m == 0) m = len;\n table->buf = (pcc_capture_t *)PCC_REALLOC(auxil, table->buf, sizeof(pcc_capture_t) * m);\n table->max = m;\n }\n for (i = table->len; i < len; i++) {\n table->buf[i].range.start = 0;\n table->buf[i].range.end = 0;\n".freeze
81
+ erbout << " size_t level;\n packcr_char_array_t buffer;\n packcr_lr_table_t lrtable;\n packcr_thunk_array_t thunks;\n packcr_auxil_t auxil;\n packcr_memory_recycler_t thunk_chunk_recycler;\n packcr_memory_recycler_t lr_memo_recycler;\n};\n\n#ifndef PACKCR_ERROR\n#define PACKCR_ERROR(auxil) packcr_error()\nMARK_FUNC_AS_USED\nstatic void packcr_error(void) {\n fprintf(stderr, \"Syntax error\\n\");\n exit(1);\n}\n#endif /* !PACKCR_ERROR */\n\n#ifndef PACKCR_GETCHAR\n#define PACKCR_GETCHAR(auxil) getchar()\n#endif /* !PACKCR_GETCHAR */\n\n#ifndef PACKCR_MALLOC\n#define PACKCR_MALLOC(auxil, size) packcr_malloc_e(size)\nstatic void *packcr_malloc_e(size_t size) {\n void *const p = malloc(size);\n if (p == NULL) {\n fprintf(stderr, \"Out of memory\\n\");\n exit(1);\n }\n return p;\n}\n#endif /* !PACKCR_MALLOC */\n\n#ifndef PACKCR_REALLOC\n#define PACKCR_REALLOC(auxil, ptr, size) packcr_realloc_e(ptr, size)\nstatic void *packcr_realloc_e(void *ptr, size_t size) {\n void *const p = realloc(ptr, size);\n if (p == NULL) {\n fprintf(stderr, \"Out of memory\\n\");\n exit(1);\n }\n return p;\n}\n#endif /* !PACKCR_REALLOC */\n\n#ifndef PACKCR_FREE\n#define PACKCR_FREE(auxil, ptr) free(ptr)\n#endif /* !PACKCR_FREE */\n\n#ifndef PACKCR_DEBUG\n#define PACKCR_DEBUG(auxil, event, rule, level, pos, buffer, length) ((void)0)\n#endif /* !PACKCR_DEBUG */\n\nstatic char *packcr_strndup_e(packcr_auxil_t auxil, const char *str, size_t len) {\n const size_t m = strnlen(str, len);\n char *const s = (char *)PACKCR_MALLOC(auxil, m + 1);\n memcpy(s, str, m);\n s[m] = '\\0';\n return s;\n}\n\nstatic void packcr_char_array__init(packcr_auxil_t auxil, packcr_char_array_t *array) {\n array->len = 0;\n array->max = 0;\n array->buf = NULL;\n}\n\nstatic void packcr_char_array__add(packcr_auxil_t auxil, packcr_char_array_t *array, char ch) {\n if (array->max <= array->len) {\n const size_t n = array->len + 1;\n size_t m = array->max;\n if (m == 0) m = PACKCR_BUFFER_MIN_SIZE;\n while (m < n && m != 0) m <<= 1;\n if (m == 0) m = n;\n array->buf = (char *)PACKCR_REALLOC(auxil, array->buf, m);\n array->max = m;\n }\n array->buf[array->len++] = ch;\n}\n\nstatic void packcr_char_array__term(packcr_auxil_t auxil, packcr_char_array_t *array) {\n PACKCR_FREE(auxil, array->buf);\n}\n\nstatic void packcr_value_table__init(packcr_auxil_t auxil, packcr_value_table_t *table) {\n table->len = 0;\n table->max = 0;\n table->buf = NULL;\n}\n\nMARK_FUNC_AS_USED\nstatic void packcr_value_table__resize(packcr_auxil_t auxil, packcr_value_table_t *table, size_t len) {\n if (table->max < len) {\n size_t m = table->max;\n if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;\n while (m < len && m != 0) m <<= 1;\n if (m == 0) m = len;\n table->buf = (packcr_value_t *)PACKCR_REALLOC(auxil, table->buf, sizeof(packcr_value_t) * m);\n table->max = m;\n }\n table->len = len;\n}\n\nMARK_FUNC_AS_USED\nstatic void packcr_value_table__clear(packcr_auxil_t auxil, packcr_value_table_t *table) {\n memset(table->buf, 0, sizeof(packcr_value_t) * table->len);\n}\n\nstatic void packcr_value_table__term(packcr_auxil_t auxil, packcr_value_table_t *table) {\n PACKCR_FREE(auxil, table->buf);\n}\n\nstatic void packcr_value_refer_table__init(packcr_auxil_t auxil, packcr_value_refer_table_t *table) {\n table->len = 0;\n table->max = 0;\n table->buf = NULL;\n}\n\nstatic void packcr_value_refer_table__resize(packcr_auxil_t auxil, packcr_value_refer_table_t *table, size_t len) {\n size_t i;\n if (table->max < len) {\n size_t m = table->max;\n if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;\n while (m < len && m != 0) m <<= 1;\n if (m == 0) m = len;\n table->buf = (packcr_value_t **)PACKCR_REALLOC(auxil, table->buf, sizeof(packcr_value_t *) * m);\n table->max = m;\n }\n for (i = table->len; i < len; i++) table->buf[i] = NULL;\n table->len = len;\n}\n\nstatic void packcr_value_refer_table__term(packcr_auxil_t auxil, packcr_value_refer_table_t *table) {\n PACKCR_FREE(auxil, table->buf);\n}\n\nstatic void packcr_capture_table__init(packcr_auxil_t auxil, packcr_capture_table_t *table) {\n table->len = 0;\n table->max = 0;\n table->buf = NULL;\n}\n\nMARK_FUNC_AS_USED\nstatic void packcr_capture_table__resize(packcr_auxil_t auxil, packcr_capture_table_t *table, size_t len) {\n size_t i;\n for (i = len; i < table->len; i++) PACKCR_FREE(auxil, table->buf[i].string);\n if (table->max < len) {\n size_t m = table->max;\n if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;\n while (m < len && m != 0) m <<= 1;\n if (m == 0) m = len;\n table->buf = (packcr_capture_t *)PACKCR_REALLOC(auxil, table->buf, sizeof(packcr_capture_t) * m);\n table->max = m;\n }\n for (i = table->len; i < len; i++) {\n table->buf[i].range.start = 0;\n table->buf[i].range.end = 0;\n".freeze
82
82
 
83
83
  if @location
84
- erbout << " pcc_location_init(&table->buf[i].range.start_loc);\n pcc_location_init(&table->buf[i].range.end_loc);\n".freeze
84
+ erbout << " packcr_location_init(&table->buf[i].range.start_loc);\n packcr_location_init(&table->buf[i].range.end_loc);\n".freeze
85
85
  end
86
- erbout << " table->buf[i].string = NULL;\n }\n table->len = len;\n}\n\nstatic void pcc_capture_table__term(pcc_auxil_t auxil, pcc_capture_table_t *table) {\n while (table->len > 0) {\n table->len--;\n PCC_FREE(auxil, table->buf[table->len].string);\n }\n PCC_FREE(auxil, table->buf);\n}\n\nstatic void pcc_capture_const_table__init(pcc_auxil_t auxil, pcc_capture_const_table_t *table) {\n table->len = 0;\n table->max = 0;\n table->buf = NULL;\n}\n\nstatic void pcc_capture_const_table__resize(pcc_auxil_t auxil, pcc_capture_const_table_t *table, size_t len) {\n size_t i;\n if (table->max < len) {\n size_t m = table->max;\n if (m == 0) m = PCC_ARRAY_MIN_SIZE;\n while (m < len && m != 0) m <<= 1;\n if (m == 0) m = len;\n table->buf = (const pcc_capture_t **)PCC_REALLOC(auxil, (pcc_capture_t **)table->buf, sizeof(const pcc_capture_t *) * m);\n table->max = m;\n }\n for (i = table->len; i < len; i++) table->buf[i] = NULL;\n table->len = len;\n}\n\nstatic void pcc_capture_const_table__term(pcc_auxil_t auxil, pcc_capture_const_table_t *table) {\n PCC_FREE(auxil, (void *)table->buf);\n}\n\nMARK_FUNC_AS_USED\nstatic pcc_thunk_t *pcc_thunk__create_leaf(pcc_auxil_t auxil, pcc_action_t action, size_t valuec, size_t captc) {\n pcc_thunk_t *const thunk = (pcc_thunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_t));\n thunk->type = PCC_THUNK_LEAF;\n pcc_value_refer_table__init(auxil, &thunk->data.leaf.values);\n pcc_value_refer_table__resize(auxil, &thunk->data.leaf.values, valuec);\n pcc_capture_const_table__init(auxil, &thunk->data.leaf.capts);\n pcc_capture_const_table__resize(auxil, &thunk->data.leaf.capts, captc);\n thunk->data.leaf.capt0.range.start = 0;\n thunk->data.leaf.capt0.range.end = 0;\n".freeze
86
+ erbout << " table->buf[i].string = NULL;\n }\n table->len = len;\n}\n\nstatic void packcr_capture_table__term(packcr_auxil_t auxil, packcr_capture_table_t *table) {\n while (table->len > 0) {\n table->len--;\n PACKCR_FREE(auxil, table->buf[table->len].string);\n }\n PACKCR_FREE(auxil, table->buf);\n}\n\nstatic void packcr_capture_const_table__init(packcr_auxil_t auxil, packcr_capture_const_table_t *table) {\n table->len = 0;\n table->max = 0;\n table->buf = NULL;\n}\n\nstatic void packcr_capture_const_table__resize(packcr_auxil_t auxil, packcr_capture_const_table_t *table, size_t len) {\n size_t i;\n if (table->max < len) {\n size_t m = table->max;\n if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;\n while (m < len && m != 0) m <<= 1;\n if (m == 0) m = len;\n table->buf = (const packcr_capture_t **)PACKCR_REALLOC(auxil, (packcr_capture_t **)table->buf, sizeof(const packcr_capture_t *) * m);\n table->max = m;\n }\n for (i = table->len; i < len; i++) table->buf[i] = NULL;\n table->len = len;\n}\n\nstatic void packcr_capture_const_table__term(packcr_auxil_t auxil, packcr_capture_const_table_t *table) {\n PACKCR_FREE(auxil, (void *)table->buf);\n}\n\nMARK_FUNC_AS_USED\nstatic packcr_thunk_t *packcr_thunk__create_leaf(packcr_auxil_t auxil, packcr_action_t action, size_t valuec, size_t captc) {\n packcr_thunk_t *const thunk = (packcr_thunk_t *)PACKCR_MALLOC(auxil, sizeof(packcr_thunk_t));\n thunk->type = PACKCR_THUNK_LEAF;\n packcr_value_refer_table__init(auxil, &thunk->data.leaf.values);\n packcr_value_refer_table__resize(auxil, &thunk->data.leaf.values, valuec);\n packcr_capture_const_table__init(auxil, &thunk->data.leaf.capts);\n packcr_capture_const_table__resize(auxil, &thunk->data.leaf.capts, captc);\n thunk->data.leaf.capt0.range.start = 0;\n thunk->data.leaf.capt0.range.end = 0;\n".freeze
87
87
 
88
88
  if @location
89
- erbout << " pcc_location_init(&thunk->data.leaf.capt0.range.start_loc);\n pcc_location_init(&thunk->data.leaf.capt0.range.end_loc);\n".freeze
89
+ erbout << " packcr_location_init(&thunk->data.leaf.capt0.range.start_loc);\n packcr_location_init(&thunk->data.leaf.capt0.range.end_loc);\n".freeze
90
90
  end
91
- erbout << " thunk->data.leaf.capt0.string = NULL;\n thunk->data.leaf.action = action;\n return thunk;\n}\n\nstatic pcc_thunk_t *pcc_thunk__create_node(pcc_auxil_t auxil, const pcc_thunk_array_t *thunks, pcc_value_t *value) {\n pcc_thunk_t *const thunk = (pcc_thunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_t));\n thunk->type = PCC_THUNK_NODE;\n thunk->data.node.thunks = thunks;\n thunk->data.node.value = value;\n return thunk;\n}\n\nstatic void pcc_thunk__destroy(pcc_auxil_t auxil, pcc_thunk_t *thunk) {\n if (thunk == NULL) return;\n switch (thunk->type) {\n case PCC_THUNK_LEAF:\n PCC_FREE(auxil, thunk->data.leaf.capt0.string);\n pcc_capture_const_table__term(auxil, &thunk->data.leaf.capts);\n pcc_value_refer_table__term(auxil, &thunk->data.leaf.values);\n break;\n case PCC_THUNK_NODE:\n break;\n default: /* unknown */\n break;\n }\n PCC_FREE(auxil, thunk);\n}\n\nstatic void pcc_thunk_array__init(pcc_auxil_t auxil, pcc_thunk_array_t *array) {\n array->len = 0;\n array->max = 0;\n array->buf = NULL;\n}\n\nstatic void pcc_thunk_array__add(pcc_auxil_t auxil, pcc_thunk_array_t *array, pcc_thunk_t *thunk) {\n if (array->max <= array->len) {\n const size_t n = array->len + 1;\n size_t m = array->max;\n if (m == 0) m = PCC_ARRAY_MIN_SIZE;\n while (m < n && m != 0) m <<= 1;\n if (m == 0) m = n;\n array->buf = (pcc_thunk_t **)PCC_REALLOC(auxil, array->buf, sizeof(pcc_thunk_t *) * m);\n array->max = m;\n }\n array->buf[array->len++] = thunk;\n}\n\nstatic void pcc_thunk_array__revert(pcc_auxil_t auxil, pcc_thunk_array_t *array, size_t len) {\n while (array->len > len) {\n array->len--;\n pcc_thunk__destroy(auxil, array->buf[array->len]);\n }\n}\n\nstatic void pcc_thunk_array__term(pcc_auxil_t auxil, pcc_thunk_array_t *array) {\n while (array->len > 0) {\n array->len--;\n pcc_thunk__destroy(auxil, array->buf[array->len]);\n }\n PCC_FREE(auxil, array->buf);\n}\n\nstatic void pcc_memory_recycler__init(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, size_t element_size) {\n recycler->pool_list = NULL;\n recycler->entry_list = NULL;\n recycler->element_size = element_size;\n}\n\nstatic void *pcc_memory_recycler__supply(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler) {\n if (recycler->entry_list) {\n pcc_memory_entry_t *const tmp = recycler->entry_list;\n recycler->entry_list = tmp->next;\n return tmp;\n }\n if (!recycler->pool_list || recycler->pool_list->unused == 0) {\n size_t size = PCC_POOL_MIN_SIZE;\n if (recycler->pool_list) {\n size = recycler->pool_list->allocated << 1;\n if (size == 0) size = recycler->pool_list->allocated;\n }\n {\n pcc_memory_pool_t *const pool = (pcc_memory_pool_t *)PCC_MALLOC(\n auxil, sizeof(pcc_memory_pool_t) + recycler->element_size * size\n );\n pool->allocated = size;\n pool->unused = size;\n pool->next = recycler->pool_list;\n recycler->pool_list = pool;\n }\n }\n recycler->pool_list->unused--;\n return (char *)recycler->pool_list + sizeof(pcc_memory_pool_t) + recycler->element_size * recycler->pool_list->unused;\n}\n\nstatic void pcc_memory_recycler__recycle(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, void *ptr) {\n pcc_memory_entry_t *const tmp = (pcc_memory_entry_t *)ptr;\n tmp->next = recycler->entry_list;\n recycler->entry_list = tmp;\n}\n\nstatic void pcc_memory_recycler__term(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler) {\n while (recycler->pool_list) {\n pcc_memory_pool_t *const tmp = recycler->pool_list;\n recycler->pool_list = tmp->next;\n PCC_FREE(auxil, tmp);\n }\n}\n\nMARK_FUNC_AS_USED\nstatic pcc_thunk_chunk_t *pcc_thunk_chunk__create(pcc_context_t *ctx) {\n pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)pcc_memory_recycler__supply(ctx->auxil, &ctx->thunk_chunk_recycler);\n pcc_value_table__init(ctx->auxil, &chunk->values);\n pcc_capture_table__init(ctx->auxil, &chunk->capts);\n pcc_thunk_array__init(ctx->auxil, &chunk->thunks);\n chunk->pos = 0;\n return chunk;\n}\n\nstatic void pcc_thunk_chunk__destroy(pcc_context_t *ctx, pcc_thunk_chunk_t *chunk) {\n if (chunk == NULL) return;\n pcc_thunk_array__term(ctx->auxil, &chunk->thunks);\n pcc_capture_table__term(ctx->auxil, &chunk->capts);\n pcc_value_table__term(ctx->auxil, &chunk->values);\n pcc_memory_recycler__recycle(ctx->auxil, &ctx->thunk_chunk_recycler, chunk);\n}\n\nstatic void pcc_rule_set__init(pcc_auxil_t auxil, pcc_rule_set_t *set) {\n set->len = 0;\n set->max = 0;\n set->buf = NULL;\n}\n\nstatic size_t pcc_rule_set__index(pcc_auxil_t auxil, const pcc_rule_set_t *set, pcc_rule_t rule) {\n size_t i;\n for (i = 0; i < set->len; i++) {\n if (set->buf[i] == rule) return i;\n }\n return PCC_VOID_VALUE;\n}\n\nstatic pcc_bool_t pcc_rule_set__add(pcc_auxil_t auxil, pcc_rule_set_t *set, pcc_rule_t rule) {\n const size_t i = pcc_rule_set__index(auxil, set, rule);\n if (i != PCC_VOID_VALUE) return PCC_FALSE;\n if (set->max <= set->len) {\n const size_t n = set->len + 1;\n size_t m = set->max;\n if (m == 0) m = PCC_ARRAY_MIN_SIZE;\n while (m < n && m != 0) m <<= 1;\n if (m == 0) m = n;\n set->buf = (pcc_rule_t *)PCC_REALLOC(auxil, set->buf, sizeof(pcc_rule_t) * m);\n set->max = m;\n }\n set->buf[set->len++] = rule;\n return PCC_TRUE;\n}\n\nstatic void pcc_rule_set__term(pcc_auxil_t auxil, pcc_rule_set_t *set) {\n PCC_FREE(auxil, set->buf);\n}\n\nstatic pcc_lr_memo_t *pcc_lr_memo__create(pcc_context_t *ctx, size_t offset".freeze
91
+ erbout << " thunk->data.leaf.capt0.string = NULL;\n thunk->data.leaf.action = action;\n return thunk;\n}\n\nstatic packcr_thunk_t *packcr_thunk__create_node(packcr_auxil_t auxil, const packcr_thunk_array_t *thunks, packcr_value_t *value) {\n packcr_thunk_t *const thunk = (packcr_thunk_t *)PACKCR_MALLOC(auxil, sizeof(packcr_thunk_t));\n thunk->type = PACKCR_THUNK_NODE;\n thunk->data.node.thunks = thunks;\n thunk->data.node.value = value;\n return thunk;\n}\n\nstatic void packcr_thunk__destroy(packcr_auxil_t auxil, packcr_thunk_t *thunk) {\n if (thunk == NULL) return;\n switch (thunk->type) {\n case PACKCR_THUNK_LEAF:\n PACKCR_FREE(auxil, thunk->data.leaf.capt0.string);\n packcr_capture_const_table__term(auxil, &thunk->data.leaf.capts);\n packcr_value_refer_table__term(auxil, &thunk->data.leaf.values);\n break;\n case PACKCR_THUNK_NODE:\n break;\n default: /* unknown */\n break;\n }\n PACKCR_FREE(auxil, thunk);\n}\n\nstatic void packcr_thunk_array__init(packcr_auxil_t auxil, packcr_thunk_array_t *array) {\n array->len = 0;\n array->max = 0;\n array->buf = NULL;\n}\n\nstatic void packcr_thunk_array__add(packcr_auxil_t auxil, packcr_thunk_array_t *array, packcr_thunk_t *thunk) {\n if (array->max <= array->len) {\n const size_t n = array->len + 1;\n size_t m = array->max;\n if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;\n while (m < n && m != 0) m <<= 1;\n if (m == 0) m = n;\n array->buf = (packcr_thunk_t **)PACKCR_REALLOC(auxil, array->buf, sizeof(packcr_thunk_t *) * m);\n array->max = m;\n }\n array->buf[array->len++] = thunk;\n}\n\nstatic void packcr_thunk_array__revert(packcr_auxil_t auxil, packcr_thunk_array_t *array, size_t len) {\n while (array->len > len) {\n array->len--;\n packcr_thunk__destroy(auxil, array->buf[array->len]);\n }\n}\n\nstatic void packcr_thunk_array__term(packcr_auxil_t auxil, packcr_thunk_array_t *array) {\n while (array->len > 0) {\n array->len--;\n packcr_thunk__destroy(auxil, array->buf[array->len]);\n }\n PACKCR_FREE(auxil, array->buf);\n}\n\nstatic void packcr_memory_recycler__init(packcr_auxil_t auxil, packcr_memory_recycler_t *recycler, size_t element_size) {\n recycler->pool_list = NULL;\n recycler->entry_list = NULL;\n recycler->element_size = element_size;\n}\n\nstatic void *packcr_memory_recycler__supply(packcr_auxil_t auxil, packcr_memory_recycler_t *recycler) {\n if (recycler->entry_list) {\n packcr_memory_entry_t *const tmp = recycler->entry_list;\n recycler->entry_list = tmp->next;\n return tmp;\n }\n if (!recycler->pool_list || recycler->pool_list->unused == 0) {\n size_t size = PACKCR_POOL_MIN_SIZE;\n if (recycler->pool_list) {\n size = recycler->pool_list->allocated << 1;\n if (size == 0) size = recycler->pool_list->allocated;\n }\n {\n packcr_memory_pool_t *const pool = (packcr_memory_pool_t *)PACKCR_MALLOC(\n auxil, sizeof(packcr_memory_pool_t) + recycler->element_size * size\n );\n pool->allocated = size;\n pool->unused = size;\n pool->next = recycler->pool_list;\n recycler->pool_list = pool;\n }\n }\n recycler->pool_list->unused--;\n return (char *)recycler->pool_list + sizeof(packcr_memory_pool_t) + recycler->element_size * recycler->pool_list->unused;\n}\n\nstatic void packcr_memory_recycler__recycle(packcr_auxil_t auxil, packcr_memory_recycler_t *recycler, void *ptr) {\n packcr_memory_entry_t *const tmp = (packcr_memory_entry_t *)ptr;\n tmp->next = recycler->entry_list;\n recycler->entry_list = tmp;\n}\n\nstatic void packcr_memory_recycler__term(packcr_auxil_t auxil, packcr_memory_recycler_t *recycler) {\n while (recycler->pool_list) {\n packcr_memory_pool_t *const tmp = recycler->pool_list;\n recycler->pool_list = tmp->next;\n PACKCR_FREE(auxil, tmp);\n }\n}\n\nMARK_FUNC_AS_USED\nstatic packcr_thunk_chunk_t *packcr_thunk_chunk__create(packcr_context_t *ctx) {\n packcr_thunk_chunk_t *const chunk = (packcr_thunk_chunk_t *)packcr_memory_recycler__supply(ctx->auxil, &ctx->thunk_chunk_recycler);\n packcr_value_table__init(ctx->auxil, &chunk->values);\n packcr_capture_table__init(ctx->auxil, &chunk->capts);\n packcr_thunk_array__init(ctx->auxil, &chunk->thunks);\n chunk->pos = 0;\n return chunk;\n}\n\nstatic void packcr_thunk_chunk__destroy(packcr_context_t *ctx, packcr_thunk_chunk_t *chunk) {\n if (chunk == NULL) return;\n packcr_thunk_array__term(ctx->auxil, &chunk->thunks);\n packcr_capture_table__term(ctx->auxil, &chunk->capts);\n packcr_value_table__term(ctx->auxil, &chunk->values);\n packcr_memory_recycler__recycle(ctx->auxil, &ctx->thunk_chunk_recycler, chunk);\n}\n\nstatic void packcr_rule_set__init(packcr_auxil_t auxil, packcr_rule_set_t *set) {\n set->len = 0;\n set->max = 0;\n set->buf = NULL;\n}\n\nstatic size_t packcr_rule_set__index(packcr_auxil_t auxil, const packcr_rule_set_t *set, packcr_rule_t rule) {\n size_t i;\n for (i = 0; i < set->len; i++) {\n if (set->buf[i] == rule) return i;\n }\n return PACKCR_VOID_VALUE;\n}\n\nstatic packcr_bool_t packcr_rule_set__add(packcr_auxil_t auxil, packcr_rule_set_t *set, packcr_rule_t rule) {\n const size_t i = packcr_rule_set__index(auxil, set, rule);\n if (i != PACKCR_VOID_VALUE) return PACKCR_FALSE;\n if (set->max <= set->len) {\n const size_t n = set->len + 1;\n size_t m = set->max;\n if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;\n while (m < n && m != 0) m <<= 1;\n if (m == 0) m = n;\n set->buf = (packcr_rule_t *)PACKCR_REALLOC(auxil, set->buf, sizeof(packcr_rule_t) * m);\n set->max = m;\n }\n set->buf[set->len++] = rule;\n return PACKCR_TRUE;\n}\n\nstatic void packcr_rule_set__term(packcr_auxil_t auxil, packcr_rule_set_t *set) {\n PACKCR_FREE(auxil, set->buf);\n}\n\nstatic packcr_lr_memo_t *packcr_lr_memo__create(packcr_context_t *ctx, size_t offset".freeze
92
92
  if @location
93
- erbout << ", pcc_location_t offset_loc".freeze
93
+ erbout << ", packcr_location_t offset_loc".freeze
94
94
  end
95
- erbout << ") {\n pcc_lr_memo_t *memo = (pcc_lr_memo_t *)pcc_memory_recycler__supply(ctx->auxil, &ctx->lr_memo_recycler);\n memo->offset = offset;\n".freeze
95
+ erbout << ") {\n packcr_lr_memo_t *memo = (packcr_lr_memo_t *)packcr_memory_recycler__supply(ctx->auxil, &ctx->lr_memo_recycler);\n memo->offset = offset;\n".freeze
96
96
 
97
97
  if @location
98
98
  erbout << " memo->offset_loc = offset_loc;\n".freeze
99
99
  end
100
- erbout << " memo->chunk = NULL;\n memo->fail = PCC_TRUE;\n memo->grow = PCC_FALSE;\n memo->hold = NULL;\n return memo;\n}\n\nstatic void pcc_lr_memo__set_chunk(pcc_context_t *ctx, pcc_lr_memo_t *memo, pcc_thunk_chunk_t *chunk) {\n if (memo->chunk) {\n pcc_lr_memo_t *const a = pcc_lr_memo__create(ctx, memo->offset".freeze
100
+ erbout << " memo->chunk = NULL;\n memo->fail = PACKCR_TRUE;\n memo->grow = PACKCR_FALSE;\n memo->hold = NULL;\n return memo;\n}\n\nstatic void packcr_lr_memo__set_chunk(packcr_context_t *ctx, packcr_lr_memo_t *memo, packcr_thunk_chunk_t *chunk) {\n if (memo->chunk) {\n packcr_lr_memo_t *const new_hold_memo = packcr_lr_memo__create(ctx, memo->offset".freeze
101
101
 
102
102
  if @location
103
103
  erbout << ", memo->offset_loc".freeze
104
104
  end
105
- erbout << ");\n a->chunk = memo->chunk;\n a->hold = memo->hold;\n memo->hold = a;\n }\n memo->chunk = chunk;\n memo->fail = PCC_FALSE;\n}\n\nstatic void pcc_lr_memo__destroy(pcc_context_t *ctx, pcc_lr_memo_t *memo) {\n while (memo != NULL) {\n pcc_lr_memo_t *const a = memo->hold;\n pcc_thunk_chunk__destroy(ctx, memo->chunk);\n pcc_memory_recycler__recycle(ctx->auxil, &ctx->lr_memo_recycler, memo);\n memo = a;\n }\n}\n\nstatic void pcc_lr_memo_map__init(pcc_auxil_t auxil, pcc_lr_memo_map_t *map) {\n map->len = 0;\n map->max = 0;\n map->buf = NULL;\n}\n\nstatic size_t pcc_lr_memo_map__index(pcc_context_t *ctx, pcc_lr_memo_map_t *map, pcc_rule_t rule) {\n size_t i;\n for (i = 0; i < map->len; i++) {\n if (map->buf[i].rule == rule) return i;\n }\n return PCC_VOID_VALUE;\n}\n\nstatic void pcc_lr_memo_map__put(pcc_context_t *ctx, pcc_lr_memo_map_t *map, pcc_rule_t rule, pcc_lr_memo_t *memo) {\n const size_t i = pcc_lr_memo_map__index(ctx, map, rule);\n if (i != PCC_VOID_VALUE) {\n pcc_lr_memo__destroy(ctx, map->buf[i].memo);\n map->buf[i].memo = memo;\n }\n else {\n if (map->max <= map->len) {\n const size_t n = map->len + 1;\n size_t m = map->max;\n if (m == 0) m = PCC_ARRAY_MIN_SIZE;\n while (m < n && m != 0) m <<= 1;\n if (m == 0) m = n;\n map->buf = (pcc_lr_memo_map_entry_t *)PCC_REALLOC(ctx->auxil, map->buf, sizeof(pcc_lr_memo_map_entry_t) * m);\n map->max = m;\n }\n map->buf[map->len].rule = rule;\n map->buf[map->len].memo = memo;\n map->len++;\n }\n}\n\nstatic pcc_lr_memo_t *pcc_lr_memo_map__get(pcc_context_t *ctx, pcc_lr_memo_map_t *map, pcc_rule_t rule) {\n const size_t i = pcc_lr_memo_map__index(ctx, map, rule);\n return (i != PCC_VOID_VALUE) ? map->buf[i].memo : NULL;\n}\n\nstatic void pcc_lr_memo_map__term(pcc_context_t *ctx, pcc_lr_memo_map_t *map) {\n while (map->len > 0) {\n map->len--;\n pcc_lr_memo__destroy(ctx, map->buf[map->len].memo);\n }\n PCC_FREE(ctx->auxil, map->buf);\n}\n\nstatic pcc_lr_memo_map_t *pcc_lr_memo_map__create(pcc_context_t *ctx) {\n pcc_lr_memo_map_t *const memo = (pcc_lr_memo_map_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_lr_memo_map_t));\n pcc_lr_memo_map__init(ctx->auxil, memo);\n return memo;\n}\n\nstatic void pcc_lr_memo_map__destroy(pcc_context_t *ctx, pcc_lr_memo_map_t *memo) {\n if (memo == NULL) return;\n pcc_lr_memo_map__term(ctx, memo);\n PCC_FREE(ctx->auxil, memo);\n}\n\nstatic void pcc_lr_table__init(pcc_auxil_t auxil, pcc_lr_table_t *table) {\n table->ofs = 0;\n table->len = 0;\n table->max = 0;\n table->buf = NULL;\n}\n\nstatic void pcc_lr_table__resize(pcc_context_t *ctx, pcc_lr_table_t *table, size_t len) {\n size_t i;\n for (i = len; i < table->len; i++) pcc_lr_memo_map__destroy(ctx, table->buf[i]);\n if (table->max < len) {\n size_t m = table->max;\n if (m == 0) m = PCC_ARRAY_MIN_SIZE;\n while (m < len && m != 0) m <<= 1;\n if (m == 0) m = len;\n table->buf = (pcc_lr_memo_map_t **)PCC_REALLOC(ctx->auxil, table->buf, sizeof(pcc_lr_memo_map_t *) * m);\n table->max = m;\n }\n for (i = table->len; i < len; i++) table->buf[i] = NULL;\n table->len = len;\n}\n\nstatic void pcc_lr_table__set_memo(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_rule_t rule, pcc_lr_memo_t *memo) {\n index += table->ofs;\n if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);\n if (table->buf[index] == NULL) table->buf[index] = pcc_lr_memo_map__create(ctx);\n pcc_lr_memo_map__put(ctx, table->buf[index], rule, memo);\n}\n\nstatic pcc_lr_memo_t *pcc_lr_table__get_memo(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_rule_t rule) {\n index += table->ofs;\n if (index >= table->len || table->buf[index] == NULL) return NULL;\n return pcc_lr_memo_map__get(ctx, table->buf[index], rule);\n}\n\nstatic void pcc_lr_table__shift(pcc_context_t *ctx, pcc_lr_table_t *table, size_t count) {\n size_t i;\n if (count > table->len - table->ofs) count = table->len - table->ofs;\n for (i = 0; i < count; i++) pcc_lr_memo_map__destroy(ctx, table->buf[table->ofs++]);\n if (table->ofs > (table->max >> 1)) {\n memmove(table->buf, table->buf + table->ofs, sizeof(pcc_lr_memo_map_t *) * (table->len - table->ofs));\n table->len -= table->ofs;\n table->ofs = 0;\n }\n}\n\nstatic void pcc_lr_table__term(pcc_context_t *ctx, pcc_lr_table_t *table) {\n while (table->len > table->ofs) {\n table->len--;\n pcc_lr_memo_map__destroy(ctx, table->buf[table->len]);\n }\n PCC_FREE(ctx->auxil, table->buf);\n}\n\nstatic pcc_context_t *pcc_context__create(pcc_auxil_t auxil) {\n pcc_context_t *const ctx = (pcc_context_t *)PCC_MALLOC(auxil, sizeof(pcc_context_t));\n ctx->buffer_start_position = 0;\n ctx->position_offset = 0;\n".freeze
105
+ erbout << ");\n new_hold_memo->chunk = memo->chunk;\n new_hold_memo->hold = memo->hold;\n memo->hold = new_hold_memo;\n }\n memo->chunk = chunk;\n memo->fail = PACKCR_FALSE;\n}\n\nstatic void packcr_lr_memo__destroy(packcr_context_t *ctx, packcr_lr_memo_t *memo) {\n while (memo != NULL) {\n packcr_lr_memo_t *const hold_memo = memo->hold;\n packcr_thunk_chunk__destroy(ctx, memo->chunk);\n packcr_memory_recycler__recycle(ctx->auxil, &ctx->lr_memo_recycler, memo);\n memo = hold_memo;\n }\n}\n\nstatic void packcr_lr_memo_map__init(packcr_auxil_t auxil, packcr_lr_memo_map_t *map) {\n map->len = 0;\n map->max = 0;\n map->buf = NULL;\n}\n\nstatic size_t packcr_lr_memo_map__index(packcr_context_t *ctx, packcr_lr_memo_map_t *map, packcr_rule_t rule) {\n size_t i;\n for (i = 0; i < map->len; i++) {\n if (map->buf[i].rule == rule) return i;\n }\n return PACKCR_VOID_VALUE;\n}\n\nstatic void packcr_lr_memo_map__put(packcr_context_t *ctx, packcr_lr_memo_map_t *map, packcr_rule_t rule, packcr_lr_memo_t *memo) {\n const size_t i = packcr_lr_memo_map__index(ctx, map, rule);\n if (i != PACKCR_VOID_VALUE) {\n packcr_lr_memo__destroy(ctx, map->buf[i].memo);\n map->buf[i].memo = memo;\n }\n else {\n if (map->max <= map->len) {\n const size_t n = map->len + 1;\n size_t m = map->max;\n if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;\n while (m < n && m != 0) m <<= 1;\n if (m == 0) m = n;\n map->buf = (packcr_lr_memo_map_entry_t *)PACKCR_REALLOC(ctx->auxil, map->buf, sizeof(packcr_lr_memo_map_entry_t) * m);\n map->max = m;\n }\n map->buf[map->len].rule = rule;\n map->buf[map->len].memo = memo;\n map->len++;\n }\n}\n\nstatic packcr_lr_memo_t *packcr_lr_memo_map__get(packcr_context_t *ctx, packcr_lr_memo_map_t *map, packcr_rule_t rule) {\n const size_t i = packcr_lr_memo_map__index(ctx, map, rule);\n return (i != PACKCR_VOID_VALUE) ? map->buf[i].memo : NULL;\n}\n\nstatic void packcr_lr_memo_map__term(packcr_context_t *ctx, packcr_lr_memo_map_t *map) {\n while (map->len > 0) {\n map->len--;\n packcr_lr_memo__destroy(ctx, map->buf[map->len].memo);\n }\n PACKCR_FREE(ctx->auxil, map->buf);\n}\n\nstatic packcr_lr_memo_map_t *packcr_lr_memo_map__create(packcr_context_t *ctx) {\n packcr_lr_memo_map_t *const memo = (packcr_lr_memo_map_t *)PACKCR_MALLOC(ctx->auxil, sizeof(packcr_lr_memo_map_t));\n packcr_lr_memo_map__init(ctx->auxil, memo);\n return memo;\n}\n\nstatic void packcr_lr_memo_map__destroy(packcr_context_t *ctx, packcr_lr_memo_map_t *memo) {\n if (memo == NULL) return;\n packcr_lr_memo_map__term(ctx, memo);\n PACKCR_FREE(ctx->auxil, memo);\n}\n\nstatic void packcr_lr_table__init(packcr_auxil_t auxil, packcr_lr_table_t *table) {\n table->ofs = 0;\n table->len = 0;\n table->max = 0;\n table->buf = NULL;\n}\n\nstatic void packcr_lr_table__resize(packcr_context_t *ctx, packcr_lr_table_t *table, size_t len) {\n size_t i;\n for (i = len; i < table->len; i++) packcr_lr_memo_map__destroy(ctx, table->buf[i]);\n if (table->max < len) {\n size_t m = table->max;\n if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;\n while (m < len && m != 0) m <<= 1;\n if (m == 0) m = len;\n table->buf = (packcr_lr_memo_map_t **)PACKCR_REALLOC(ctx->auxil, table->buf, sizeof(packcr_lr_memo_map_t *) * m);\n table->max = m;\n }\n for (i = table->len; i < len; i++) table->buf[i] = NULL;\n table->len = len;\n}\n\nstatic void packcr_lr_table__set_memo(packcr_context_t *ctx, packcr_lr_table_t *table, size_t index, packcr_rule_t rule, packcr_lr_memo_t *memo) {\n index += table->ofs;\n if (index >= table->len) packcr_lr_table__resize(ctx, table, index + 1);\n if (table->buf[index] == NULL) table->buf[index] = packcr_lr_memo_map__create(ctx);\n packcr_lr_memo_map__put(ctx, table->buf[index], rule, memo);\n}\n\nstatic packcr_lr_memo_t *packcr_lr_table__get_memo(packcr_context_t *ctx, packcr_lr_table_t *table, size_t index, packcr_rule_t rule) {\n index += table->ofs;\n if (index >= table->len || table->buf[index] == NULL) return NULL;\n return packcr_lr_memo_map__get(ctx, table->buf[index], rule);\n}\n\nstatic void packcr_lr_table__shift(packcr_context_t *ctx, packcr_lr_table_t *table, size_t count) {\n size_t i;\n if (count > table->len - table->ofs) count = table->len - table->ofs;\n for (i = 0; i < count; i++) packcr_lr_memo_map__destroy(ctx, table->buf[table->ofs++]);\n if (table->ofs > (table->max >> 1)) {\n memmove(table->buf, table->buf + table->ofs, sizeof(packcr_lr_memo_map_t *) * (table->len - table->ofs));\n table->len -= table->ofs;\n table->ofs = 0;\n }\n}\n\nstatic void packcr_lr_table__term(packcr_context_t *ctx, packcr_lr_table_t *table) {\n while (table->len > table->ofs) {\n table->len--;\n packcr_lr_memo_map__destroy(ctx, table->buf[table->len]);\n }\n PACKCR_FREE(ctx->auxil, table->buf);\n}\n\nstatic packcr_context_t *packcr_context__create(packcr_auxil_t auxil) {\n packcr_context_t *const ctx = (packcr_context_t *)PACKCR_MALLOC(auxil, sizeof(packcr_context_t));\n ctx->buffer_start_position = 0;\n ctx->position_offset = 0;\n".freeze
106
106
 
107
107
  if @location
108
- erbout << " pcc_location_init(&ctx->buffer_start_position_loc);\n pcc_location_init(&ctx->position_offset_loc);\n".freeze
108
+ erbout << " packcr_location_init(&ctx->buffer_start_position_loc);\n packcr_location_init(&ctx->position_offset_loc);\n".freeze
109
109
  end
110
- erbout << " ctx->level = 0;\n pcc_char_array__init(auxil, &ctx->buffer);\n pcc_lr_table__init(auxil, &ctx->lrtable);\n pcc_thunk_array__init(auxil, &ctx->thunks);\n pcc_memory_recycler__init(auxil, &ctx->thunk_chunk_recycler, sizeof(pcc_thunk_chunk_t));\n pcc_memory_recycler__init(auxil, &ctx->lr_memo_recycler, sizeof(pcc_lr_memo_t));\n ctx->auxil = auxil;\n return ctx;\n}\n\nstatic void pcc_context__destroy(pcc_context_t *ctx) {\n if (ctx == NULL) return;\n pcc_thunk_array__term(ctx->auxil, &ctx->thunks);\n pcc_lr_table__term(ctx, &ctx->lrtable);\n pcc_char_array__term(ctx->auxil, &ctx->buffer);\n pcc_memory_recycler__term(ctx->auxil, &ctx->thunk_chunk_recycler);\n pcc_memory_recycler__term(ctx->auxil, &ctx->lr_memo_recycler);\n PCC_FREE(ctx->auxil, ctx);\n}\n\nstatic size_t pcc_refill_buffer(pcc_context_t *ctx, size_t num) {\n if (ctx->buffer.len >= ctx->position_offset + num) return ctx->buffer.len - ctx->position_offset;\n while (ctx->buffer.len < ctx->position_offset + num) {\n const int c = PCC_GETCHAR(ctx->auxil);\n if (c < 0) break;\n pcc_char_array__add(ctx->auxil, &ctx->buffer, (char)c);\n }\n return ctx->buffer.len - ctx->position_offset;\n}\n\nMARK_FUNC_AS_USED\nstatic void pcc_commit_buffer(pcc_context_t *ctx) {\n memmove(ctx->buffer.buf, ctx->buffer.buf + ctx->position_offset, ctx->buffer.len - ctx->position_offset);\n ctx->buffer.len -= ctx->position_offset;\n ctx->buffer_start_position += ctx->position_offset;\n pcc_lr_table__shift(ctx, &ctx->lrtable, ctx->position_offset);\n ctx->position_offset = 0;\n".freeze
110
+ erbout << " ctx->level = 0;\n packcr_char_array__init(auxil, &ctx->buffer);\n packcr_lr_table__init(auxil, &ctx->lrtable);\n packcr_thunk_array__init(auxil, &ctx->thunks);\n packcr_memory_recycler__init(auxil, &ctx->thunk_chunk_recycler, sizeof(packcr_thunk_chunk_t));\n packcr_memory_recycler__init(auxil, &ctx->lr_memo_recycler, sizeof(packcr_lr_memo_t));\n ctx->auxil = auxil;\n return ctx;\n}\n\nstatic void packcr_context__destroy(packcr_context_t *ctx) {\n if (ctx == NULL) return;\n packcr_thunk_array__term(ctx->auxil, &ctx->thunks);\n packcr_lr_table__term(ctx, &ctx->lrtable);\n packcr_char_array__term(ctx->auxil, &ctx->buffer);\n packcr_memory_recycler__term(ctx->auxil, &ctx->thunk_chunk_recycler);\n packcr_memory_recycler__term(ctx->auxil, &ctx->lr_memo_recycler);\n PACKCR_FREE(ctx->auxil, ctx);\n}\n\nstatic size_t packcr_refill_buffer(packcr_context_t *ctx, size_t num) {\n if (ctx->buffer.len >= ctx->position_offset + num) return ctx->buffer.len - ctx->position_offset;\n while (ctx->buffer.len < ctx->position_offset + num) {\n const int c = PACKCR_GETCHAR(ctx->auxil);\n if (c < 0) break;\n packcr_char_array__add(ctx->auxil, &ctx->buffer, (char)c);\n }\n return ctx->buffer.len - ctx->position_offset;\n}\n\nMARK_FUNC_AS_USED\nstatic void packcr_commit_buffer(packcr_context_t *ctx) {\n memmove(ctx->buffer.buf, ctx->buffer.buf + ctx->position_offset, ctx->buffer.len - ctx->position_offset);\n ctx->buffer.len -= ctx->position_offset;\n ctx->buffer_start_position += ctx->position_offset;\n packcr_lr_table__shift(ctx, &ctx->lrtable, ctx->position_offset);\n ctx->position_offset = 0;\n".freeze
111
111
 
112
112
  if @location
113
- erbout << " ctx->buffer_start_position_loc = pcc_location_add(ctx->buffer_start_position_loc, ctx->position_offset_loc);\n pcc_location_init(&ctx->position_offset_loc);\n".freeze
113
+ erbout << " ctx->buffer_start_position_loc = packcr_location_add(ctx->buffer_start_position_loc, ctx->position_offset_loc);\n packcr_location_init(&ctx->position_offset_loc);\n".freeze
114
114
  end
115
- erbout << "}\n\nMARK_FUNC_AS_USED\nstatic const char *pcc_get_capture_string(pcc_context_t *ctx, const pcc_capture_t *capt) {\n if (capt->string == NULL)\n ((pcc_capture_t *)capt)->string =\n pcc_strndup_e(ctx->auxil, ctx->buffer.buf + capt->range.start, capt->range.end - capt->range.start);\n return capt->string;\n}\n\n".freeze
115
+ erbout << "}\n\nMARK_FUNC_AS_USED\nstatic const char *packcr_get_capture_string(packcr_context_t *ctx, const packcr_capture_t *capt) {\n if (capt->string == NULL)\n ((packcr_capture_t *)capt)->string =\n packcr_strndup_e(ctx->auxil, ctx->buffer.buf + capt->range.start, capt->range.end - capt->range.start);\n return capt->string;\n}\n\n".freeze
116
116
 
117
117
  if @utf8
118
- erbout << "static size_t pcc_get_char_as_utf32(pcc_context_t *ctx, int *out) { /* with checking UTF-8 validity */\n int c, u;\n size_t n;\n if (pcc_refill_buffer(ctx, 1) < 1) return 0;\n c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset];\n n = (c < 0x80) ? 1 :\n ((c & 0xe0) == 0xc0) ? 2 :\n ((c & 0xf0) == 0xe0) ? 3 :\n ((c & 0xf8) == 0xf0) ? 4 : 0;\n if (n < 1) return 0;\n if (pcc_refill_buffer(ctx, n) < n) return 0;\n switch (n) {\n case 1:\n u = c;\n break;\n case 2:\n u = c & 0x1f;\n c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 1];\n if ((c & 0xc0) != 0x80) return 0;\n u <<= 6; u |= c & 0x3f;\n if (u < 0x80) return 0;\n break;\n case 3:\n u = c & 0x0f;\n c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 1];\n if ((c & 0xc0) != 0x80) return 0;\n u <<= 6; u |= c & 0x3f;\n c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 2];\n if ((c & 0xc0) != 0x80) return 0;\n u <<= 6; u |= c & 0x3f;\n if (u < 0x800) return 0;\n break;\n case 4:\n u = c & 0x07;\n c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 1];\n if ((c & 0xc0) != 0x80) return 0;\n u <<= 6; u |= c & 0x3f;\n c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 2];\n if ((c & 0xc0) != 0x80) return 0;\n u <<= 6; u |= c & 0x3f;\n c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 3];\n if ((c & 0xc0) != 0x80) return 0;\n u <<= 6; u |= c & 0x3f;\n if (u < 0x10000 || u > 0x10ffff) return 0;\n break;\n default:\n return 0;\n }\n if (out) *out = u;\n return n;\n}\n\n".freeze
118
+ erbout << "static size_t packcr_get_char_as_utf32(packcr_context_t *ctx, int *out) { /* with checking UTF-8 validity */\n int c, u;\n size_t n;\n if (packcr_refill_buffer(ctx, 1) < 1) return 0;\n c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset];\n n = (c < 0x80) ? 1 :\n ((c & 0xe0) == 0xc0) ? 2 :\n ((c & 0xf0) == 0xe0) ? 3 :\n ((c & 0xf8) == 0xf0) ? 4 : 0;\n if (n < 1) return 0;\n if (packcr_refill_buffer(ctx, n) < n) return 0;\n switch (n) {\n case 1:\n u = c;\n break;\n case 2:\n u = c & 0x1f;\n c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 1];\n if ((c & 0xc0) != 0x80) return 0;\n u <<= 6; u |= c & 0x3f;\n if (u < 0x80) return 0;\n break;\n case 3:\n u = c & 0x0f;\n c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 1];\n if ((c & 0xc0) != 0x80) return 0;\n u <<= 6; u |= c & 0x3f;\n c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 2];\n if ((c & 0xc0) != 0x80) return 0;\n u <<= 6; u |= c & 0x3f;\n if (u < 0x800) return 0;\n break;\n case 4:\n u = c & 0x07;\n c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 1];\n if ((c & 0xc0) != 0x80) return 0;\n u <<= 6; u |= c & 0x3f;\n c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 2];\n if ((c & 0xc0) != 0x80) return 0;\n u <<= 6; u |= c & 0x3f;\n c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 3];\n if ((c & 0xc0) != 0x80) return 0;\n u <<= 6; u |= c & 0x3f;\n if (u < 0x10000 || u > 0x10ffff) return 0;\n break;\n default:\n return 0;\n }\n if (out) *out = u;\n return n;\n}\n\n".freeze
119
119
  end
120
- erbout << "static void pcc_grow_lr(pcc_context_t *ctx, pcc_rule_t rule, size_t offset".freeze
120
+ erbout << "static void packcr_grow_lr(packcr_context_t *ctx, packcr_rule_t rule, size_t offset".freeze
121
121
  if @location
122
- erbout << ", pcc_location_t offset_loc".freeze
122
+ erbout << ", packcr_location_t offset_loc".freeze
123
123
  end
124
- erbout << ") {\n while(1) {\n const size_t old_offset = ctx->position_offset;\n pcc_thunk_chunk_t *chunk;\n pcc_lr_memo_t *memo;\n pcc_rule_set_t limits;\n ctx->position_offset = offset;\n".freeze
124
+ erbout << ") {\n while(1) {\n const size_t old_offset = ctx->position_offset;\n packcr_thunk_chunk_t *chunk;\n packcr_lr_memo_t *memo;\n packcr_rule_set_t limits;\n ctx->position_offset = offset;\n".freeze
125
125
 
126
126
  if @location
127
127
  erbout << " ctx->position_offset_loc = offset_loc;\n".freeze
128
128
  end
129
- erbout << " pcc_rule_set__init(ctx->auxil, &limits);\n pcc_rule_set__add(ctx->auxil, &limits, rule);\n chunk = rule(ctx, offset".freeze
129
+ erbout << " packcr_rule_set__init(ctx->auxil, &limits);\n packcr_rule_set__add(ctx->auxil, &limits, rule);\n chunk = rule(ctx, offset".freeze
130
130
 
131
131
  if @location
132
132
  erbout << ", offset_loc".freeze
133
133
  end
134
- erbout << ", &limits);\n pcc_rule_set__term(ctx->auxil, &limits);\n if (!chunk)\n break;\n if (ctx->position_offset <= old_offset) {\n pcc_thunk_chunk__destroy(ctx, chunk);\n break;\n }\n memo = pcc_lr_table__get_memo(ctx, &ctx->lrtable, offset, rule);\n pcc_lr_memo__set_chunk(ctx, memo, chunk);\n memo->offset = ctx->position_offset;\n".freeze
134
+ erbout << ", &limits);\n packcr_rule_set__term(ctx->auxil, &limits);\n if (!chunk)\n break;\n if (ctx->position_offset <= old_offset) {\n packcr_thunk_chunk__destroy(ctx, chunk);\n break;\n }\n memo = packcr_lr_table__get_memo(ctx, &ctx->lrtable, offset, rule);\n packcr_lr_memo__set_chunk(ctx, memo, chunk);\n memo->offset = ctx->position_offset;\n".freeze
135
135
 
136
136
  if @location
137
137
  erbout << " memo->offset_loc = ctx->position_offset_loc;\n".freeze
138
138
  end
139
- erbout << " }\n}\n\nMARK_FUNC_AS_USED\nstatic pcc_thunk_chunk_t *pcc_get_rule_thunk_chunk(pcc_context_t *ctx, pcc_rule_t rule) {\n pcc_thunk_chunk_t *c = NULL;\n size_t offset = ctx->position_offset;\n".freeze
139
+ erbout << " }\n}\n\nMARK_FUNC_AS_USED\nstatic packcr_thunk_chunk_t *packcr_get_rule_thunk_chunk(packcr_context_t *ctx, packcr_rule_t rule) {\n packcr_thunk_chunk_t *c = NULL;\n size_t offset = ctx->position_offset;\n".freeze
140
140
 
141
141
  if @location
142
- erbout << " pcc_location_t offset_loc = ctx->position_offset_loc;\n".freeze
142
+ erbout << " packcr_location_t offset_loc = ctx->position_offset_loc;\n".freeze
143
143
  end
144
- erbout << " pcc_lr_memo_t *a = pcc_lr_table__get_memo(ctx, &ctx->lrtable, offset, rule);\n\n if (a == NULL) {\n a = pcc_lr_memo__create(ctx, offset".freeze
144
+ erbout << " packcr_lr_memo_t *memo = packcr_lr_table__get_memo(ctx, &ctx->lrtable, offset, rule);\n\n if (memo == NULL) {\n memo = packcr_lr_memo__create(ctx, offset".freeze
145
145
 
146
146
  if @location
147
147
  erbout << ", offset_loc".freeze
148
148
  end
149
- erbout << ");\n pcc_lr_table__set_memo(ctx, &ctx->lrtable, offset, rule, a);\n c = rule(ctx, offset".freeze
149
+ erbout << ");\n packcr_lr_table__set_memo(ctx, &ctx->lrtable, offset, rule, memo);\n c = rule(ctx, offset".freeze
150
150
 
151
151
  if @location
152
152
  erbout << ", offset_loc".freeze
153
153
  end
154
- erbout << ", NULL);\n pcc_lr_memo__set_chunk(ctx, a, c);\n a->offset = ctx->position_offset;\n".freeze
154
+ erbout << ", NULL);\n packcr_lr_memo__set_chunk(ctx, memo, c);\n memo->offset = ctx->position_offset;\n".freeze
155
155
 
156
156
  if @location
157
- erbout << " a->offset_loc = ctx->position_offset_loc;\n".freeze
157
+ erbout << " memo->offset_loc = ctx->position_offset_loc;\n".freeze
158
158
  end
159
- erbout << " if (a->grow) {\n pcc_grow_lr(ctx, rule, offset".freeze
159
+ erbout << " if (memo->grow) {\n packcr_grow_lr(ctx, rule, offset".freeze
160
160
 
161
161
  if @location
162
162
  erbout << ", offset_loc".freeze
163
163
  end
164
- erbout << ");\n a->grow = PCC_FALSE;\n ctx->position_offset = a->offset;\n return a->chunk;\n }\n return c;\n } else if (a->fail) {\n pcc_lr_memo__set_chunk(ctx, a, NULL);\n a->grow = PCC_TRUE;\n return NULL;\n }\n ctx->position_offset = a->offset;\n return a->chunk;\n}\n\nMARK_FUNC_AS_USED\nstatic pcc_bool_t pcc_apply_rule(pcc_context_t *ctx, pcc_rule_t rule, pcc_thunk_array_t *thunks, pcc_value_t *value, size_t offset".freeze
164
+ erbout << ");\n memo->grow = PACKCR_FALSE;\n ctx->position_offset = memo->offset;\n".freeze
165
+
166
+ if @location
167
+ erbout << " ctx->position_offset_loc = memo->offset_loc;\n".freeze
168
+ end
169
+ erbout << " return memo->chunk;\n }\n return c;\n } else if (memo->fail) {\n packcr_lr_memo__set_chunk(ctx, memo, NULL);\n memo->grow = PACKCR_TRUE;\n return NULL;\n }\n ctx->position_offset = memo->offset;\n".freeze
165
170
 
166
171
  if @location
167
- erbout << ", pcc_location_t offset_loc".freeze
172
+ erbout << " ctx->position_offset_loc = memo->offset_loc;\n".freeze
168
173
  end
169
- erbout << ", pcc_rule_set_t *limits) {\n static pcc_value_t null;\n pcc_thunk_chunk_t *c;\n if (limits != NULL) {\n pcc_lr_memo_t *a;\n pcc_rule_set__add(ctx->auxil, limits, rule);\n c = rule(ctx, offset".freeze
174
+ erbout << " return memo->chunk;\n}\n\nMARK_FUNC_AS_USED\nstatic packcr_bool_t packcr_apply_rule(packcr_context_t *ctx, packcr_rule_t rule, packcr_thunk_array_t *thunks, packcr_value_t *value, size_t offset".freeze
175
+
176
+ if @location
177
+ erbout << ", packcr_location_t offset_loc".freeze
178
+ end
179
+ erbout << ", packcr_rule_set_t *limits) {\n static packcr_value_t null;\n packcr_thunk_chunk_t *c;\n if (limits != NULL) {\n packcr_lr_memo_t *memo;\n packcr_rule_set__add(ctx->auxil, limits, rule);\n c = rule(ctx, offset".freeze
170
180
 
171
181
  if @location
172
182
  erbout << ", offset_loc".freeze
173
183
  end
174
- erbout << ", limits);\n a = pcc_lr_table__get_memo(ctx, &ctx->lrtable, offset, rule);\n if (a == NULL || ctx->position_offset <= a->offset) {\n c = a->chunk;\n ctx->position_offset = a->offset;\n } else {\n pcc_lr_memo__set_chunk(ctx, a, c);\n a->offset = ctx->position_offset;\n }\n } else {\n c = pcc_get_rule_thunk_chunk(ctx, rule);\n }\n if (c == NULL) return PCC_FALSE;\n if (value == NULL) value = &null;\n memset(value, 0, sizeof(pcc_value_t)); /* in case */\n pcc_thunk_array__add(ctx->auxil, thunks, pcc_thunk__create_node(ctx->auxil, &c->thunks, value));\n return PCC_TRUE;\n}\n\nMARK_FUNC_AS_USED\nstatic void pcc_do_action(pcc_context_t *ctx, const pcc_thunk_array_t *thunks, pcc_value_t *value) {\n size_t i;\n for (i = 0; i < thunks->len; i++) {\n pcc_thunk_t *const thunk = thunks->buf[i];\n switch (thunk->type) {\n case PCC_THUNK_LEAF:\n thunk->data.leaf.action(ctx, thunk, value);\n break;\n case PCC_THUNK_NODE:\n pcc_do_action(ctx, thunk->data.node.thunks, thunk->data.node.value);\n break;\n default: /* unknown */\n break;\n }\n }\n}\n\n".freeze
184
+ erbout << ", limits);\n memo = packcr_lr_table__get_memo(ctx, &ctx->lrtable, offset, rule);\n if (memo == NULL || ctx->position_offset <= memo->offset) {\n if (memo) {\n c = memo->chunk;\n ctx->position_offset = memo->offset;\n".freeze
185
+
186
+ if @location
187
+ erbout << " ctx->position_offset_loc = memo->offset_loc;\n".freeze
188
+ end
189
+ erbout << " }\n } else {\n packcr_lr_memo__set_chunk(ctx, memo, c);\n memo->offset = ctx->position_offset;\n".freeze
190
+
191
+ if @location
192
+ erbout << " memo->offset_loc = ctx->position_offset_loc;\n".freeze
193
+ end
194
+ erbout << " }\n } else {\n c = packcr_get_rule_thunk_chunk(ctx, rule);\n }\n if (c == NULL) return PACKCR_FALSE;\n if (value == NULL) value = &null;\n memset(value, 0, sizeof(packcr_value_t)); /* in case */\n packcr_thunk_array__add(ctx->auxil, thunks, packcr_thunk__create_node(ctx->auxil, &c->thunks, value));\n return PACKCR_TRUE;\n}\n\nMARK_FUNC_AS_USED\nstatic void packcr_do_action(packcr_context_t *ctx, const packcr_thunk_array_t *thunks, packcr_value_t *value) {\n size_t i;\n for (i = 0; i < thunks->len; i++) {\n packcr_thunk_t *const thunk = thunks->buf[i];\n switch (thunk->type) {\n case PACKCR_THUNK_LEAF:\n thunk->data.leaf.action(ctx, thunk, value);\n break;\n case PACKCR_THUNK_NODE:\n packcr_do_action(ctx, thunk->data.node.thunks, thunk->data.node.value);\n break;\n default: /* unknown */\n break;\n }\n }\n}\n\n".freeze
175
195
 
176
196
  @root.rules.each do |rule|
177
197
  rule.codes.each do |code|
178
- erbout << "static void pcc_action_#{rule.name}_#{code.index}(#{prefix}_context_t *__pcc_ctx, pcc_thunk_t *__pcc_in, pcc_value_t *__pcc_out) {\n#define auxil (__pcc_ctx->auxil)\n#define __ (*__pcc_out)\n".freeze
198
+ erbout << "static void packcr_action_#{rule.name}_#{code.index}(#{prefix}_context_t *__packcr_ctx, packcr_thunk_t *__packcr_in, packcr_value_t *__packcr_out) {\n#define auxil (__packcr_ctx->auxil)\n#define __ (*__packcr_out)\n".freeze
179
199
 
180
200
  code.vars.each do |ref|
181
- erbout << "#define #{ref.var} (*__pcc_in->data.leaf.values.buf[#{ref.index}])\n".freeze
201
+ erbout << "#define #{ref.var} (*__packcr_in->data.leaf.values.buf[#{ref.index}])\n".freeze
182
202
  end
183
- erbout << "#define _0 pcc_get_capture_string(__pcc_ctx, &__pcc_in->data.leaf.capt0)\n#define _0s ((const size_t)(__pcc_ctx->buffer_start_position + __pcc_in->data.leaf.capt0.range.start))\n#define _0e ((const size_t)(__pcc_ctx->buffer_start_position + __pcc_in->data.leaf.capt0.range.end))\n".freeze
203
+ erbout << "#define _0 packcr_get_capture_string(__packcr_ctx, &__packcr_in->data.leaf.capt0)\n#define _0s ((const size_t)(__packcr_ctx->buffer_start_position + __packcr_in->data.leaf.capt0.range.start))\n#define _0e ((const size_t)(__packcr_ctx->buffer_start_position + __packcr_in->data.leaf.capt0.range.end))\n".freeze
184
204
 
185
205
  if @location
186
- erbout << "#define _0sl ((const pcc_location_t)(pcc_location_add(__pcc_ctx->buffer_start_position_loc, __pcc_in->data.leaf.capt0.range.start_loc)))\n#define _0el ((const pcc_location_t)(pcc_location_add(__pcc_ctx->buffer_start_position_loc, __pcc_in->data.leaf.capt0.range.end_loc)))\n".freeze
206
+ erbout << "#define _0sl ((const packcr_location_t)(packcr_location_add(__packcr_ctx->buffer_start_position_loc, __packcr_in->data.leaf.capt0.range.start_loc)))\n#define _0el ((const packcr_location_t)(packcr_location_add(__packcr_ctx->buffer_start_position_loc, __packcr_in->data.leaf.capt0.range.end_loc)))\n".freeze
187
207
  end
188
208
  if @capture_in_code
189
- erbout << "#define _0c __pcc_in->data.leaf.capt0\n".freeze
209
+ erbout << "#define _0c __packcr_in->data.leaf.capt0\n".freeze
190
210
  end
191
211
  code.capts.each do |capture|
192
- erbout << "#define _#{capture.index + 1} pcc_get_capture_string(__pcc_ctx, __pcc_in->data.leaf.capts.buf[#{capture.index}])\n#define _#{capture.index + 1}s ((const size_t)(__pcc_ctx->buffer_start_position + __pcc_in->data.leaf.capts.buf[#{capture.index}]->range.start))\n#define _#{capture.index + 1}e ((const size_t)(__pcc_ctx->buffer_start_position + __pcc_in->data.leaf.capts.buf[#{capture.index}]->range.end))\n".freeze
212
+ erbout << "#define _#{capture.index + 1} packcr_get_capture_string(__packcr_ctx, __packcr_in->data.leaf.capts.buf[#{capture.index}])\n#define _#{capture.index + 1}s ((const size_t)(__packcr_ctx->buffer_start_position + __packcr_in->data.leaf.capts.buf[#{capture.index}]->range.start))\n#define _#{capture.index + 1}e ((const size_t)(__packcr_ctx->buffer_start_position + __packcr_in->data.leaf.capts.buf[#{capture.index}]->range.end))\n".freeze
193
213
 
194
214
  if @location
195
- erbout << "#define _#{capture.index + 1}sl ((const pcc_location_t)(pcc_location_add(__pcc_ctx->buffer_start_position_loc, __pcc_in->data.leaf.capts.buf[#{capture.index}]->range.start_loc)))\n#define _#{capture.index + 1}el ((const pcc_location_t)(pcc_location_add(__pcc_ctx->buffer_start_position_loc, __pcc_in->data.leaf.capts.buf[#{capture.index}]->range.end_loc)))\n".freeze
215
+ erbout << "#define _#{capture.index + 1}sl ((const packcr_location_t)(packcr_location_add(__packcr_ctx->buffer_start_position_loc, __packcr_in->data.leaf.capts.buf[#{capture.index}]->range.start_loc)))\n#define _#{capture.index + 1}el ((const packcr_location_t)(packcr_location_add(__packcr_ctx->buffer_start_position_loc, __packcr_in->data.leaf.capts.buf[#{capture.index}]->range.end_loc)))\n".freeze
196
216
  end
197
217
  next unless @capture_in_code
198
218
 
199
- erbout << "#define _#{capture.index + 1}c (*__pcc_in->data.leaf.capts.buf[#{capture.index}])\n".freeze
219
+ erbout << "#define _#{capture.index + 1}c (*__packcr_in->data.leaf.capts.buf[#{capture.index}])\n".freeze
200
220
  end
201
221
  erbout << "#{stream.get_code_block(code.code, 4, @iname)}".freeze
202
222
  code.capts.reverse_each do |capture|
@@ -214,11 +234,11 @@ class Packcr
214
234
  end
215
235
  end
216
236
  @root.rules.each do |rule|
217
- erbout << "static pcc_thunk_chunk_t *pcc_evaluate_rule_#{rule.name}(pcc_context_t *ctx, size_t offset".freeze
237
+ erbout << "static packcr_thunk_chunk_t *packcr_evaluate_rule_#{rule.name}(packcr_context_t *ctx, size_t offset".freeze
218
238
  if @location
219
- erbout << ", pcc_location_t offset_loc".freeze
239
+ erbout << ", packcr_location_t offset_loc".freeze
220
240
  end
221
- erbout << ", pcc_rule_set_t *limits);\n".freeze
241
+ erbout << ", packcr_rule_set_t *limits);\n".freeze
222
242
  end
223
243
  erbout << "\n".freeze
224
244
 
@@ -226,16 +246,16 @@ class Packcr
226
246
  gen = ::Packcr::Generator.new(rule, @ascii, @location)
227
247
  erbout << "#{gen.generate_code(rule, 0, 0, false)}\n".freeze
228
248
  end
229
- erbout << "#{prefix}_context_t *#{prefix}_create(#{auxil_def}auxil) {\n return pcc_context__create(auxil);\n}\n\nint #{prefix}_parse(#{prefix}_context_t *ctx, #{value_def}*ret) {\n size_t pos = ctx->buffer_start_position;\n".freeze
249
+ erbout << "#{prefix}_context_t *#{prefix}_create(#{auxil_def}auxil) {\n return packcr_context__create(auxil);\n}\n\nint #{prefix}_parse(#{prefix}_context_t *ctx, #{value_def}*ret) {\n size_t pos = ctx->buffer_start_position;\n".freeze
230
250
 
231
251
  if !@root.rules.empty?
232
- erbout << " if (pcc_apply_rule(ctx, pcc_evaluate_rule_#{@root.rules[0].name}, &ctx->thunks, ret, ctx->position_offset".freeze
252
+ erbout << " if (packcr_apply_rule(ctx, packcr_evaluate_rule_#{@root.rules[0].name}, &ctx->thunks, ret, ctx->position_offset".freeze
233
253
  if @location
234
254
  erbout << ", ctx->position_offset_loc".freeze
235
255
  end
236
- erbout << ", NULL))\n pcc_do_action(ctx, &ctx->thunks, ret);\n else\n PCC_ERROR(ctx->auxil);\n pcc_commit_buffer(ctx);\n".freeze
256
+ erbout << ", NULL))\n packcr_do_action(ctx, &ctx->thunks, ret);\n else\n PACKCR_ERROR(ctx->auxil);\n packcr_commit_buffer(ctx);\n".freeze
237
257
  end
238
- erbout << " pcc_thunk_array__revert(ctx->auxil, &ctx->thunks, 0);\n return pos != ctx->buffer_start_position && pcc_refill_buffer(ctx, 1) >= 1;\n}\n\nvoid #{prefix}_destroy(#{prefix}_context_t *ctx) {\n pcc_context__destroy(ctx);\n}\n".freeze
258
+ erbout << " packcr_thunk_array__revert(ctx->auxil, &ctx->thunks, 0);\n return pos != ctx->buffer_start_position && packcr_refill_buffer(ctx, 1) >= 1;\n}\n\nvoid #{prefix}_destroy(#{prefix}_context_t *ctx) {\n packcr_context__destroy(ctx);\n}\n".freeze
239
259
 
240
260
  if !code(:lsource).empty?
241
261
  erbout << "\n".freeze
@@ -249,14 +269,22 @@ class Packcr
249
269
  erbout = +""
250
270
  erbout << "# A packrat parser generated by PackCR #{Packcr::VERSION}\n".freeze
251
271
 
252
- if !code(:source).empty?
272
+ if !code(:esource).empty?
253
273
  erbout << "\n".freeze
254
274
 
255
- code(:source).each do |code|
275
+ code(:esource).each do |code|
256
276
  erbout << "#{stream.get_code_block(code, 0, @iname)}".freeze
257
277
  end
258
278
  end
259
- erbout << "\nclass #{class_name}\n def initialize(".freeze
279
+ erbout << "class #{class_name}\n".freeze
280
+
281
+ code(:location).each do |code|
282
+ erbout << "#{stream.get_code_block(code, 2, @iname)}\n".freeze
283
+ end
284
+ code(:source).each do |code|
285
+ erbout << " #{stream.get_code_block(code, 2, @iname)}\n".freeze
286
+ end
287
+ erbout << " def initialize(".freeze
260
288
  if @auxil_type
261
289
  erbout << "#{auxil_type}, ".freeze
262
290
  end
@@ -302,31 +330,31 @@ class Packcr
302
330
 
303
331
  @root.rules.each do |rule|
304
332
  rule.codes.each do |code|
305
- erbout << " def action_#{rule.name}_#{code.index}(__pcc_in, __pcc_vars, __pcc_index)\n ____ = (__pcc_vars[__pcc_index] ||= Value.new).value if __pcc_vars\n".freeze
333
+ erbout << " def action_#{rule.name}_#{code.index}(__packcr_in, __packcr_vars, __packcr_index)\n ____ = (__packcr_vars[__packcr_index] ||= Value.new).value if __packcr_vars\n".freeze
306
334
 
307
335
  code.vars.each do |ref|
308
- erbout << " #{ref.var} = (__pcc_in.value_refs[#{ref.index}] ||= Value.new).value\n".freeze
336
+ erbout << " #{ref.var} = (__packcr_in.value_refs[#{ref.index}] ||= Value.new).value\n".freeze
309
337
  end
310
- erbout << " __0 = __pcc_in.capt0.capture_string(@buffer)\n __0s = @buffer_start_position + __pcc_in.capt0.range_start\n __0e = @buffer_start_position + __pcc_in.capt0.range_end\n".freeze
338
+ erbout << " __0 = __packcr_in.capt0.capture_string(@buffer)\n __0s = @buffer_start_position + __packcr_in.capt0.range_start\n __0e = @buffer_start_position + __packcr_in.capt0.range_end\n".freeze
311
339
 
312
340
  if @location
313
- erbout << " __0sl = @buffer_start_position_loc + __pcc_in.capt0.start_loc\n __0el = @buffer_start_position_loc + __pcc_in.capt0.end_loc\n".freeze
341
+ erbout << " __0sl = @buffer_start_position_loc + __packcr_in.capt0.start_loc\n __0el = @buffer_start_position_loc + __packcr_in.capt0.end_loc\n".freeze
314
342
  end
315
343
  if @capture_in_code
316
- erbout << " __0c = __pcc_in.capt0\n".freeze
344
+ erbout << " __0c = __packcr_in.capt0\n".freeze
317
345
  end
318
346
  code.capts.each do |capture|
319
- erbout << " __#{capture.index + 1} = __pcc_in.capts[#{capture.index}].capture_string(@buffer)\n __#{capture.index + 1}s = @buffer_start_position + __pcc_in.capts[#{capture.index}].range_start\n __#{capture.index + 1}e = @buffer_start_position + __pcc_in.capts[#{capture.index}].range_end\n".freeze
347
+ erbout << " __#{capture.index + 1} = __packcr_in.capts[#{capture.index}].capture_string(@buffer)\n __#{capture.index + 1}s = @buffer_start_position + __packcr_in.capts[#{capture.index}].range_start\n __#{capture.index + 1}e = @buffer_start_position + __packcr_in.capts[#{capture.index}].range_end\n".freeze
320
348
 
321
349
  if @location
322
- erbout << " __#{capture.index + 1}sl = @buffer_start_position_loc + __pcc_in.capts[#{capture.index}].start_loc\n __#{capture.index + 1}el = @buffer_start_position_loc + __pcc_in.capts[#{capture.index}].end_loc\n".freeze
350
+ erbout << " __#{capture.index + 1}sl = @buffer_start_position_loc + __packcr_in.capts[#{capture.index}].start_loc\n __#{capture.index + 1}el = @buffer_start_position_loc + __packcr_in.capts[#{capture.index}].end_loc\n".freeze
323
351
  end
324
352
  next unless @capture_in_code
325
353
 
326
- erbout << " __#{capture.index + 1}c = __pcc_in.capts[#{capture.index}]\n".freeze
354
+ erbout << " __#{capture.index + 1}c = __packcr_in.capts[#{capture.index}]\n".freeze
327
355
  end
328
356
 
329
- erbout << "#{stream.get_code_block(code.code, 4, @iname)}\n __pcc_vars[__pcc_index].value = ____ if __pcc_vars\n end\n\n".freeze
357
+ erbout << "#{stream.get_code_block(code.code, 4, @iname)}\n __packcr_vars[__packcr_index].value = ____ if __packcr_vars\n end\n\n".freeze
330
358
  end
331
359
  end
332
360
  @root.rules.each do |rule|
@@ -377,7 +405,17 @@ class Packcr
377
405
  if @location
378
406
  erbout << ", offset_loc".freeze
379
407
  end
380
- erbout << ")\n memo.grow = false\n answer = memo.answer\n @position_offset = memo.offset\n end\n return answer\n elsif memo.fail\n memo.answer = nil\n memo.grow = true\n return nil\n else\n @position_offset = memo.offset\n return memo.answer\n end\n end\n\n def apply_rule(rule, thunks, values, index, offset".freeze
408
+ erbout << ")\n memo.grow = false\n answer = memo.answer\n @position_offset = memo.offset\n".freeze
409
+
410
+ if @location
411
+ erbout << " @position_offset_loc = memo.offset_loc\n".freeze
412
+ end
413
+ erbout << " end\n return answer\n elsif memo.fail\n memo.answer = nil\n memo.grow = true\n return nil\n else\n @position_offset = memo.offset\n".freeze
414
+
415
+ if @location
416
+ erbout << " @position_offset_loc = memo.offset_loc\n".freeze
417
+ end
418
+ erbout << " return memo.answer\n end\n end\n\n def apply_rule(rule, thunks, values, index, offset".freeze
381
419
  if @location
382
420
  erbout << ", offset_loc".freeze
383
421
  end
@@ -386,12 +424,17 @@ class Packcr
386
424
  if @location
387
425
  erbout << ", offset_loc".freeze
388
426
  end
389
- erbout << ", limits: limits)\n memo = @memos[offset, rule]\n if !answer || @position_offset <= memo.offset\n answer = memo.answer\n @position_offset = memo.offset\n else\n memo.answer = answer\n memo.offset = @position_offset\n end\n else\n answer = rule_answer(rule)\n end\n\n if !answer\n return false\n end\n values ||= @global_values\n thunks << ThunkNode.new(answer.thunks, values, index)\n return true\n end\n\n def do_action(thunks, values, index)\n thunks.each do |thunk|\n thunk.do_action(self, values, index)\n end\n end\n\n".freeze
427
+ erbout << ", limits: limits)\n memo = @memos[offset, rule]\n if !answer || @position_offset <= memo.offset\n if memo\n answer = memo.answer\n @position_offset = memo.offset\n".freeze
390
428
 
391
- code(:location).each do |code|
392
- erbout << "#{stream.get_code_block(code, 2, @iname)}\n".freeze
429
+ if @location
430
+ erbout << " @position_offset_loc = memo.offset_loc\n".freeze
431
+ end
432
+ erbout << " end\n else\n memo.answer = answer\n memo.offset = @position_offset\n".freeze
433
+
434
+ if @location
435
+ erbout << " memo.offset_loc = @position_offset_loc\n".freeze
393
436
  end
394
- erbout << " class LrMemoTable\n def initialize\n @memos = {}\n end\n\n def clear\n @memos.clear\n end\n\n def []=(index, rule_name, memo)\n entry = @memos[index] ||= {}\n entry[rule_name] = memo\n end\n\n def [](index, rule_name)\n @memos.dig(index, rule_name)\n end\n end\n\n class LrMemo\n attr_accessor :grow, :answer, :offset, :fail\n".freeze
437
+ erbout << " end\n else\n answer = rule_answer(rule)\n end\n\n if !answer\n return false\n end\n values ||= @global_values\n thunks << ThunkNode.new(answer.thunks, values, index)\n return true\n end\n\n def do_action(thunks, values, index)\n thunks.each do |thunk|\n thunk.do_action(self, values, index)\n end\n end\n\n class LrMemoTable\n def initialize\n @memos = {}\n end\n\n def clear\n @memos.clear\n end\n\n def []=(index, rule_name, memo)\n entry = @memos[index] ||= {}\n entry[rule_name] = memo\n end\n\n def [](index, rule_name)\n @memos.dig(index, rule_name)\n end\n end\n\n class LrMemo\n attr_accessor :grow, :answer, :offset, :fail\n".freeze
395
438
 
396
439
  if @location
397
440
  erbout << " attr_accessor :offset_loc\n".freeze
@@ -5,7 +5,7 @@ class Packcr
5
5
  case gen.lang
6
6
  when :c
7
7
  erbout = +""
8
- erbout << "{\n pcc_thunk_t *const thunk = pcc_thunk__create_leaf(ctx->auxil, pcc_action_#{gen.rule.name}_#{index}, #{gen.rule.vars.length}, #{gen.rule.capts.length});\n".freeze
8
+ erbout << "{\n packcr_thunk_t *const thunk = packcr_thunk__create_leaf(ctx->auxil, packcr_action_#{gen.rule.name}_#{index}, #{gen.rule.vars.length}, #{gen.rule.capts.length});\n".freeze
9
9
 
10
10
  vars.each do |var|
11
11
  erbout << " thunk->data.leaf.values.buf[#{var.index}] = &(chunk->values.buf[#{var.index}]);\n".freeze
@@ -18,7 +18,7 @@ class Packcr
18
18
  if gen.location
19
19
  erbout << " thunk->data.leaf.capt0.range.start_loc = chunk->pos_loc;\n thunk->data.leaf.capt0.range.end_loc = ctx->position_offset_loc;\n".freeze
20
20
  end
21
- erbout << " pcc_thunk_array__add(ctx->auxil, &chunk->thunks, thunk);\n}\n".freeze
21
+ erbout << " packcr_thunk_array__add(ctx->auxil, &chunk->thunks, thunk);\n}\n".freeze
22
22
 
23
23
  erbout
24
24
  when :rb
@@ -9,7 +9,7 @@ class Packcr
9
9
  erbout << "{\n const size_t p = ctx->position_offset;\n".freeze
10
10
 
11
11
  if gen.location
12
- erbout << " const pcc_location_t p_loc = ctx->position_offset_loc;\n".freeze
12
+ erbout << " const packcr_location_t p_loc = ctx->position_offset_loc;\n".freeze
13
13
  end
14
14
  erbout << " const size_t n = chunk->thunks.len;\n".freeze
15
15
 
@@ -33,7 +33,7 @@ class Packcr
33
33
  if gen.location
34
34
  erbout << " ctx->position_offset_loc = p_loc;\n".freeze
35
35
  end
36
- erbout << " pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);\n".freeze
36
+ erbout << " packcr_thunk_array__revert(ctx->auxil, &chunk->thunks, n);\n".freeze
37
37
 
38
38
  next if c
39
39
 
@@ -8,7 +8,7 @@ class Packcr
8
8
  erbout << "{\n const size_t p = ctx->position_offset;\n size_t q;\n".freeze
9
9
 
10
10
  if gen.location
11
- erbout << " pcc_location_t p_loc = ctx->position_offset_loc;\n pcc_location_t q_loc;\n".freeze
11
+ erbout << " packcr_location_t p_loc = ctx->position_offset_loc;\n packcr_location_t q_loc;\n".freeze
12
12
  end
13
13
  erbout << "#{gen.generate_code(expr, onfail, 4, false)} q = ctx->position_offset;\n chunk->capts.buf[#{index}].range.start = p;\n chunk->capts.buf[#{index}].range.end = q;\n".freeze
14
14