aboutsummaryrefslogtreecommitdiff
path: root/src/ast.h
blob: baeaa3f4b9d82b34afe6935ed34c74099095ee03 (plain)
  1. #ifndef CMARK_AST_H
  2. #define CMARK_AST_H
  3. #include <stdio.h>
  4. #include "config.h"
  5. #include "buffer.h"
  6. #include "chunk.h"
  7. #include "cmark.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #define REFMAP_SIZE 16
  12. #define MAX_LINK_LABEL_LENGTH 1000
  13. struct cmark_node_inl {
  14. cmark_inl_tag tag;
  15. union {
  16. cmark_chunk literal;
  17. struct cmark_node_inl *inlines;
  18. struct {
  19. struct cmark_node_inl *label;
  20. unsigned char *url;
  21. unsigned char *title;
  22. } linkable;
  23. } content;
  24. struct cmark_node_inl *next;
  25. };
  26. struct cmark_reference {
  27. struct cmark_reference *next;
  28. unsigned char *label;
  29. unsigned char *url;
  30. unsigned char *title;
  31. unsigned int hash;
  32. };
  33. typedef struct cmark_reference cmark_reference;
  34. struct cmark_reference_map {
  35. cmark_reference *table[REFMAP_SIZE];
  36. };
  37. typedef struct cmark_reference_map cmark_reference_map;
  38. // Types for blocks
  39. struct cmark_ListData {
  40. cmark_list_type list_type;
  41. int marker_offset;
  42. int padding;
  43. int start;
  44. cmark_delim_type delimiter;
  45. unsigned char bullet_char;
  46. bool tight;
  47. };
  48. struct cmark_FencedCodeData {
  49. int fence_length;
  50. int fence_offset;
  51. unsigned char fence_char;
  52. cmark_strbuf info;
  53. };
  54. struct cmark_node_block {
  55. cmark_block_tag tag;
  56. int start_line;
  57. int start_column;
  58. int end_line;
  59. bool open;
  60. bool last_line_blank;
  61. struct cmark_node_block* children;
  62. struct cmark_node_block* last_child;
  63. struct cmark_node_block* parent;
  64. cmark_strbuf string_content;
  65. struct cmark_node_inl* inline_content;
  66. union {
  67. struct cmark_ListData list;
  68. struct cmark_FencedCodeData code;
  69. struct {
  70. int level;
  71. } header;
  72. } as;
  73. struct cmark_node_block *next;
  74. struct cmark_node_block *prev;
  75. };
  76. struct cmark_doc_parser {
  77. struct cmark_reference_map *refmap;
  78. struct cmark_node_block* root;
  79. struct cmark_node_block* current;
  80. int line_number;
  81. cmark_strbuf *curline;
  82. };
  83. unsigned char *cmark_clean_autolink(chunk *url, int is_email);
  84. static inline cmark_node_inl *cmark_make_link(cmark_node_inl *label, unsigned char *url, unsigned char *title)
  85. {
  86. cmark_node_inl* e = (cmark_node_inl *)calloc(1, sizeof(*e));
  87. if(e != NULL) {
  88. e->tag = CMARK_INL_LINK;
  89. e->content.linkable.label = label;
  90. e->content.linkable.url = url;
  91. e->content.linkable.title = title;
  92. e->next = NULL;
  93. }
  94. return e;
  95. }
  96. static inline cmark_node_inl* cmark_make_autolink(cmark_node_inl* label, cmark_chunk url, int is_email)
  97. {
  98. return cmark_make_link(label, cmark_clean_autolink(&url, is_email), NULL);
  99. }
  100. static inline cmark_node_inl* cmark_make_inlines(cmark_inl_tag t, cmark_node_inl* contents)
  101. {
  102. cmark_node_inl * e = (cmark_node_inl *)calloc(1, sizeof(*e));
  103. if(e != NULL) {
  104. e->tag = t;
  105. e->content.inlines = contents;
  106. e->next = NULL;
  107. }
  108. return e;
  109. }
  110. // Create an inline with a literal string value.
  111. static inline cmark_node_inl* cmark_make_literal(cmark_inl_tag t, cmark_chunk s)
  112. {
  113. cmark_node_inl * e = (cmark_node_inl *)calloc(1, sizeof(*e));
  114. if(e != NULL) {
  115. e->tag = t;
  116. e->content.literal = s;
  117. e->next = NULL;
  118. }
  119. return e;
  120. }
  121. // Create an inline with no value.
  122. static inline cmark_node_inl* cmark_make_simple(cmark_inl_tag t)
  123. {
  124. cmark_node_inl* e = (cmark_node_inl *)calloc(1, sizeof(*e));
  125. if(e != NULL) {
  126. e->tag = t;
  127. e->next = NULL;
  128. }
  129. return e;
  130. }
  131. // Macros for creating various kinds of simple.
  132. #define cmark_make_str(s) cmark_make_literal(INL_STRING, s)
  133. #define cmark_make_code(s) cmark_make_literal(INL_CODE, s)
  134. #define cmark_make_raw_html(s) cmark_make_literal(INL_RAW_HTML, s)
  135. #define cmark_make_linebreak() cmark_make_simple(INL_LINEBREAK)
  136. #define cmark_make_softbreak() cmark_make_simple(INL_SOFTBREAK)
  137. #define cmark_make_emph(contents) cmark_make_inlines(INL_EMPH, contents)
  138. #define cmark_make_strong(contents) cmark_make_inlines(INL_STRONG, contents)
  139. #ifndef CMARK_NO_SHORT_NAMES
  140. #define node_inl cmark_node_inl
  141. #define ListData cmark_ListData
  142. #define FencedCodeData cmark_FencedCodeData
  143. #define node_block cmark_node_block
  144. #define make_link cmark_make_link
  145. #define make_autolink cmark_make_autolink
  146. #define make_str cmark_make_str
  147. #define make_code cmark_make_code
  148. #define make_raw_html cmark_make_raw_html
  149. #define make_linebreak cmark_make_linebreak
  150. #define make_softbreak cmark_make_softbreak
  151. #define make_emph cmark_make_emph
  152. #define make_strong cmark_make_strong
  153. #define make_simple cmark_make_simple
  154. #define make_literal cmark_make_literal
  155. #define make_inlines cmark_make_inlines
  156. #endif
  157. #ifdef __cplusplus
  158. }
  159. #endif
  160. #endif