Skip to content

Commit 854175c

Browse files
committed
migrate packcc to v3
1 parent a323379 commit 854175c

File tree

10 files changed

+94
-136
lines changed

10 files changed

+94
-136
lines changed

import/unicode_general_category_fix.peg

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if(MDD_GENERATE_PEG)
1919
OUTPUT markdowndown.h markdowndown.c
2020
COMMAND ${CMAKE_COMMAND} -E copy_if_different
2121
"${CMAKE_CURRENT_SOURCE_DIR}/markdowndown.peg" "${CMAKE_CURRENT_BINARY_DIR}"
22-
COMMAND "${PACKCC_EXECUTABLE}" -I "${CMAKE_SOURCE_DIR}/import" -I "${PACKCC_IMPORT_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/markdowndown.peg"
22+
COMMAND "${PACKCC_EXECUTABLE}" -I "${PACKCC_IMPORT_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/markdowndown.peg"
2323
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/markdowndown.peg
2424
VERBATIM)
2525
endif()

src/data.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ typedef union mdd_node_type_data_tag {
102102
void mdd_node_type_data_finalize(mdd_node_type_t type, mdd_node_type_data_t *obj);
103103

104104
typedef struct mdd_node_data_tag {
105+
// TODO: use the built-in label of AST node.
105106
mdd_node_type_t type;
107+
// TODO: use the built-in range of AST node.
106108
range_t range;
107109
// Data for specific type.
108110
mdd_node_type_data_t *type_data;

src/interface.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
#include "data.h"
88
#include "markdowndown.h"
99

10-
static void validate_reference_links(pcc_ast_manager_t *mgr) {
10+
static void validate_reference_links(mddi_ast_manager_t *mgr) {
1111
const pointer_array_t *links = &mgr->custom.reference_links;
1212
const pointer_array_t *refs = &mgr->custom.references;
1313
if (links->len == 0) {
1414
return;
1515
}
1616

1717
mdd_pointer_array_for_each(links, link)
18-
mdd_node_data_t *link_node = &((pcc_ast_node_t *)link)->custom;
18+
mdd_node_data_t *link_node = &((mddi_ast_node_t *)link)->custom;
1919
mdd_node_type_link_data_t *link_node_data = &link_node->type_data->link_data;
2020
bool_t found = BOOL_FALSE;
2121
mdd_pointer_array_for_each(refs, ref)
22-
mdd_node_data_t *ref_node = &((pcc_ast_node_t *)ref)->custom;
22+
mdd_node_data_t *ref_node = &((mddi_ast_node_t *)ref)->custom;
2323
mdd_node_type_reference_data_t *ref_node_data = &ref_node->type_data->reference_data;
2424
assert(link_node_data->is_reference == BOOL_TRUE);
2525
if (mdd_char_array_equal(&link_node_data->reference, &ref_node_data->id)) {
@@ -33,8 +33,8 @@ static void validate_reference_links(pcc_ast_manager_t *mgr) {
3333
end_for_each
3434
if (!found) {
3535
// Transform invalid ref links to dummy elements.
36-
pcc_ast_node_t *node = (pcc_ast_node_t *)link;
37-
assert(node->type == PCC_AST_NODE_TYPE_VARIADIC);
36+
mddi_ast_node_t *node = (mddi_ast_node_t *)link;
37+
assert(node->type == MDDI_AST_NODE_TYPE_VARIADIC);
3838
for (size_t i = 0; i < node->data.variadic.len; ++i) {
3939
mdd_node_data_t *child_data = &node->data.variadic.node[i]->custom;
4040
mdd_node_type_t type = child_data->type;
@@ -54,19 +54,19 @@ static void validate_reference_links(pcc_ast_manager_t *mgr) {
5454
end_for_each
5555
}
5656

57-
static void validate_note_references(pcc_ast_manager_t *mgr) {
57+
static void validate_note_references(mddi_ast_manager_t *mgr) {
5858
const pointer_array_t *refs = &mgr->custom.note_references;
5959
const pointer_array_t *notes = &mgr->custom.notes;
6060
if (refs->len == 0) {
6161
return;
6262
}
6363

6464
mdd_pointer_array_for_each(refs, ref)
65-
mdd_node_data_t *ref_node = &((pcc_ast_node_t *)ref)->custom;
65+
mdd_node_data_t *ref_node = &((mddi_ast_node_t *)ref)->custom;
6666
mdd_node_type_note_data_t *ref_node_data = &ref_node->type_data->note_data;
6767
bool_t found = BOOL_FALSE;
6868
mdd_pointer_array_for_each(notes, note)
69-
mdd_node_data_t *note_node = &((pcc_ast_node_t *)note)->custom;
69+
mdd_node_data_t *note_node = &((mddi_ast_node_t *)note)->custom;
7070
mdd_node_type_note_data_t *note_node_data = &note_node->type_data->note_data;
7171
if (mdd_char_array_equal(&ref_node_data->reference, &note_node_data->reference)) {
7272
assert(mdd_char_array_empty(&ref_node_data->note));
@@ -77,8 +77,8 @@ static void validate_note_references(pcc_ast_manager_t *mgr) {
7777
end_for_each
7878
if (!found) {
7979
// Transform invalid note refs to dummy elements.
80-
pcc_ast_node_t *node = (pcc_ast_node_t *)ref;
81-
assert(node->type == PCC_AST_NODE_TYPE_NULLARY);
80+
mddi_ast_node_t *node = (mddi_ast_node_t *)ref;
81+
assert(node->type == MDDI_AST_NODE_TYPE_NULLARY);
8282
mdd_node_type_data_finalize(ref_node->type, ref_node->type_data);
8383
ref_node->type_data = NULL;
8484
ref_node->type = MDD_NODE_TYPE_DUMMY;
@@ -87,23 +87,23 @@ static void validate_note_references(pcc_ast_manager_t *mgr) {
8787
}
8888

8989

90-
pcc_ast_node_t *mdd_parse(pcc_ast_manager_t *mgr, const unsigned char *buf, size_t len) {
91-
pcc_ast_manager__initialize(mgr);
90+
mddi_ast_node_t *mdd_parse(mddi_ast_manager_t *mgr, const unsigned char *buf, size_t len) {
91+
mddi_ast_manager__initialize(mgr);
9292
mgr->custom.input = buf;
9393
mgr->custom.len = len;
9494

9595
mddi_context_t *ctx = mddi_create(mgr);
96-
pcc_ast_node_t *ast = NULL;
96+
mddi_ast_node_t *ast = NULL;
9797
mddi_parse(ctx, &ast);
9898
validate_reference_links(mgr);
9999
validate_note_references(mgr);
100100
mddi_destroy(ctx);
101101
return ast;
102102
}
103103

104-
void mdd_clear(pcc_ast_manager_t *mgr, pcc_ast_node_t *ast) {
104+
void mdd_clear(mddi_ast_manager_t *mgr, mddi_ast_node_t *ast) {
105105
if (ast) {
106-
pcc_ast_node__destroy(ast);
106+
mddi_ast_node__destroy(ast);
107107
}
108-
pcc_ast_manager__finalize(mgr);
108+
mddi_ast_manager__finalize(mgr);
109109
}

src/interface.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
// Parse |buf| with length |len|.
77
// Should call |mdd_clear(ast)| after the use of the AST.
8-
pcc_ast_node_t *mdd_parse(pcc_ast_manager_t *mgr, const unsigned char *buf, size_t len);
8+
mddi_ast_node_t *mdd_parse(mddi_ast_manager_t *mgr, const unsigned char *buf, size_t len);
99

10-
void mdd_clear(pcc_ast_manager_t *mgr, pcc_ast_node_t *ast);
10+
void mdd_clear(mddi_ast_manager_t *mgr, mddi_ast_node_t *ast);
1111

1212
#endif

src/markdowndown.peg

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@
55
%header {
66
#include "data.h"
77

8-
#define PCC_AST_MANAGER_CUSTOM_DATA_DEFINED
9-
typedef mdd_data_t pcc_ast_manager_custom_data_t;
8+
// Do not capture the string for each node by default.
9+
#define MDDI_AST_NODE_STRING_CAPTURING_DISABLED
1010

11-
#define PCC_AST_NODE_CUSTOM_DATA_DEFINED
12-
typedef mdd_node_data_t pcc_ast_node_custom_data_t;
11+
#define MDDI_AST_MANAGER_CUSTOM_DATA_DEFINED
12+
typedef mdd_data_t mddi_ast_manager_custom_data_t;
13+
14+
#define MDDI_AST_NODE_CUSTOM_DATA_DEFINED
15+
typedef mdd_node_data_t mddi_ast_node_custom_data_t;
1316
}
1417

1518
%import "code/pcc_ast.peg"
1619

1720
# Output data type as `$$`.
18-
%value "pcc_ast_node_t *"
21+
%value "mddi_ast_node_t *"
1922
# User-defined data type passed to `pcc_create`.
20-
%auxil "pcc_ast_manager_t *"
23+
%auxil "mddi_ast_manager_t *"
2124

2225
# Copied verbatim to the C source file before the generated parser implementation code.
2326
%source {
2427
#include <stdio.h>
2528
#include <stdlib.h>
29+
#include <assert.h>
2630

2731
#include "utils.h"
2832

@@ -204,7 +208,7 @@ mark <- '==' !whitespace
204208
{
205209
mdd_node_append_child($$, e);
206210
}
207-
)+ &{ look_back_nonspace(ctx) } '=='
211+
)+ &{ @@ = look_back_nonspace(pcc_ctx); } '=='
208212
{
209213
mdd_node_data_set_range(&$$->custom, $0s, $0e);
210214
}
@@ -227,9 +231,9 @@ inline_equation <- (inline_equation_single / inline_equation_multiple)
227231
$$ = mdd_node_create_0(MDD_NODE_TYPE_INLINE_EQUATION, $0s, $0e);
228232
}
229233

230-
inline_equation_single <- '$' &{ inline_equation_pre(ctx) } !'$' !'\\' nonspace_char '$' ![0-9]
234+
inline_equation_single <- '$' &{ @@ = inline_equation_pre(pcc_ctx); } !'$' !'\\' nonspace_char '$' ![0-9]
231235

232-
inline_equation_multiple <- '$' &{ inline_equation_pre(ctx) } !'$' nonspace_char (!'$' !new_line .)+ '$' &{ inline_equation_post(ctx) } ![0-9]
236+
inline_equation_multiple <- '$' &{ @@ = inline_equation_pre(pcc_ctx); } !'$' nonspace_char (!'$' !new_line .)+ '$' &{ @@ = inline_equation_post(pcc_ctx); } ![0-9]
233237

234238
ticks1 <- '`' !'`'
235239
ticks2 <- '``' !'`'
@@ -340,7 +344,7 @@ auto_link <-
340344
image <- '!' (e:explicit_link / e:explicit_link_size / e:reference_link)
341345
{
342346
assert(e);
343-
assert(e->type == PCC_AST_NODE_TYPE_VARIADIC);
347+
assert(e->type == MDDI_AST_NODE_TYPE_VARIADIC);
344348
$$ = e;
345349
mdd_node_data_assign(&$$->custom, MDD_NODE_TYPE_IMAGE, $0s, $0e);
346350
}
@@ -418,12 +422,14 @@ size <-
418422
/ '=' < [0-9]+ > 'x'
419423
{
420424
$$ = mdd_node_create_0(MDD_NODE_TYPE_LINK_SIZE, $0s, $0e);
421-
mdd_node_type_size_data_new(&$$->custom, atoi($3), -1);
425+
// FIXME
426+
mdd_node_type_size_data_new(&$$->custom, atoi(pcc_capture__3), -1);
422427
}
423428
/ '=x' < [0-9]+ >
424429
{
425430
$$ = mdd_node_create_0(MDD_NODE_TYPE_LINK_SIZE, $0s, $0e);
426-
mdd_node_type_size_data_new(&$$->custom, -1, atoi($4));
431+
// FIXME
432+
mdd_node_type_size_data_new(&$$->custom, -1, atoi(pcc_capture__4));
427433
}
428434

429435
explicit_link <- l:label '(' space s:source space_newline t:title? space ')'
@@ -534,7 +540,7 @@ strike <- '~~' !whitespace
534540
{
535541
mdd_node_append_child($$, e);
536542
}
537-
)+ &{ look_back_nonspace(ctx) } '~~'
543+
)+ &{ @@ = look_back_nonspace(pcc_ctx); } '~~'
538544
{
539545
mdd_node_data_set_range(&$$->custom, $0s, $0e);
540546
}
@@ -552,7 +558,7 @@ strong_star <- '**' !whitespace
552558
{
553559
mdd_node_append_child($$, e);
554560
}
555-
)+ &{ look_back_nonspace(ctx) } '**'
561+
)+ &{ @@ = look_back_nonspace(pcc_ctx); } '**'
556562
{
557563
mdd_node_data_set_range(&$$->custom, $0s, $0e);
558564
}
@@ -565,7 +571,7 @@ strong_ul <- '__' !whitespace
565571
{
566572
mdd_node_append_child($$, e);
567573
}
568-
)+ &{ look_back_nonspace(ctx) } '__'
574+
)+ &{ @@ = look_back_nonspace(pcc_ctx); } '__'
569575
{
570576
mdd_node_data_set_range(&$$->custom, $0s, $0e);
571577
}
@@ -583,7 +589,7 @@ emph_star <- '*' !whitespace
583589
{
584590
mdd_node_append_child($$, e);
585591
}
586-
)+ &{ look_back_nonspace(ctx) } '*'
592+
)+ &{ @@ = look_back_nonspace(pcc_ctx); } '*'
587593
{
588594
mdd_node_data_set_range(&$$->custom, $0s, $0e);
589595
}
@@ -596,7 +602,7 @@ emph_ul <- '_' !whitespace
596602
{
597603
mdd_node_append_child($$, e);
598604
}
599-
)+ &{ look_back_nonspace(ctx) } '_'
605+
)+ &{ @@ = look_back_nonspace(pcc_ctx); } '_'
600606
{
601607
mdd_node_data_set_range(&$$->custom, $0s, $0e);
602608
}
@@ -670,29 +676,29 @@ html_block_script <- html_block_script_open (!html_block_script_close .)* html_b
670676
######
671677

672678
%import "char/ascii_character_group.peg"
673-
%import "unicode_general_category_fix.peg"
679+
%import "char/unicode_general_category.peg"
674680

675681
# End of the rules. All codes below will be copied to the C source file.
676682
%%
677683

678-
void pcc_ast_manager_custom_data__initialize(pcc_ast_manager_custom_data_t *obj) {
684+
void mddi_ast_manager_custom_data__initialize(mddi_ast_manager_custom_data_t *obj) {
679685
mdd_data_initialize(obj);
680686
}
681687

682-
void pcc_ast_manager_custom_data__finalize(pcc_ast_manager_custom_data_t *obj) {
688+
void mddi_ast_manager_custom_data__finalize(mddi_ast_manager_custom_data_t *obj) {
683689
mdd_data_finalize(obj);
684690
}
685691

686-
void pcc_ast_node_custom_data__initialize(pcc_ast_node_custom_data_t *obj) {
692+
void mddi_ast_node_custom_data__initialize(mddi_ast_node_custom_data_t *obj) {
687693
mdd_node_data_initialize(obj);
688694
}
689695

690-
void pcc_ast_node_custom_data__finalize(pcc_ast_node_custom_data_t *obj) {
696+
void mddi_ast_node_custom_data__finalize(mddi_ast_node_custom_data_t *obj) {
691697
mdd_node_data_finalize(obj);
692698
}
693699

694700
int look_back_nonspace(mddi_context_t *ctx) {
695-
const char *buf = ctx->buffer.buf + ctx->cur;
701+
const char *buf = ctx->buffer.p + ctx->cur;
696702
if (ctx->cur == 0) {
697703
return 1;
698704
}
@@ -704,7 +710,7 @@ int look_back_nonspace(mddi_context_t *ctx) {
704710
}
705711

706712
int inline_equation_pre(mddi_context_t *ctx) {
707-
const char *buf = ctx->buffer.buf + ctx->cur;
713+
const char *buf = ctx->buffer.p + ctx->cur;
708714
assert(ctx->cur > 0);
709715
assert(*(buf - 1) == '$');
710716
if (ctx->cur < 2) {
@@ -718,7 +724,7 @@ int inline_equation_pre(mddi_context_t *ctx) {
718724
}
719725

720726
int inline_equation_post(mddi_context_t *ctx) {
721-
const char *buf = ctx->buffer.buf + ctx->cur;
727+
const char *buf = ctx->buffer.p + ctx->cur;
722728
assert(ctx->cur >= 2);
723729
const unsigned char ch = *(buf - 2);
724730
if (ch != ' ' && ch != '\t' && ch != '\\') {

0 commit comments

Comments
 (0)