aboutsummaryrefslogtreecommitdiff
path: root/src/ast.h
blob: 0370175d7880a338dc563cc90c3511d4aba3efd4 (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. typedef enum {
  14. CMARK_INL_STRING,
  15. CMARK_INL_SOFTBREAK,
  16. CMARK_INL_LINEBREAK,
  17. CMARK_INL_CODE,
  18. CMARK_INL_RAW_HTML,
  19. CMARK_INL_EMPH,
  20. CMARK_INL_STRONG,
  21. CMARK_INL_LINK,
  22. CMARK_INL_IMAGE
  23. } cmark_inl_tag;
  24. struct cmark_node_inl {
  25. cmark_inl_tag tag;
  26. union {
  27. cmark_chunk literal;
  28. struct cmark_node_inl *inlines;
  29. struct {
  30. struct cmark_node_inl *label;
  31. unsigned char *url;
  32. unsigned char *title;
  33. } linkable;
  34. } content;
  35. struct cmark_node_inl *next;
  36. };
  37. struct cmark_reference {
  38. struct cmark_reference *next;
  39. unsigned char *label;
  40. unsigned char *url;
  41. unsigned char *title;
  42. unsigned int hash;
  43. };
  44. typedef struct cmark_reference cmark_reference;
  45. struct cmark_reference_map {
  46. cmark_reference *table[REFMAP_SIZE];
  47. };
  48. typedef struct cmark_reference_map cmark_reference_map;
  49. typedef enum {
  50. bullet,
  51. ordered
  52. } cmark_list_type;
  53. typedef enum {
  54. period,
  55. parens
  56. } cmark_delim_type;
  57. // Types for blocks
  58. struct cmark_ListData {
  59. cmark_list_type list_type;
  60. int marker_offset;
  61. int padding;
  62. int start;
  63. cmark_delim_type delimiter;
  64. unsigned char bullet_char;
  65. bool tight;
  66. };
  67. struct cmark_FencedCodeData {
  68. int fence_length;
  69. int fence_offset;
  70. unsigned char fence_char;
  71. cmark_strbuf info;
  72. };
  73. typedef enum {
  74. CMARK_BLOCK_DOCUMENT,
  75. CMARK_BLOCK_BQUOTE,
  76. CMARK_BLOCK_LIST,
  77. CMARK_BLOCK_LIST_ITEM,
  78. CMARK_BLOCK_FENCED_CODE,
  79. CMARK_BLOCK_INDENTED_CODE,
  80. CMARK_BLOCK_HTML,
  81. CMARK_BLOCK_PARAGRAPH,
  82. CMARK_BLOCK_ATX_HEADER,
  83. CMARK_BLOCK_SETEXT_HEADER,
  84. CMARK_BLOCK_HRULE,
  85. CMARK_BLOCK_REFERENCE_DEF
  86. } cmark_block_tag;
  87. struct cmark_node_block {
  88. cmark_block_tag tag;
  89. int start_line;
  90. int start_column;
  91. int end_line;
  92. bool open;
  93. bool last_line_blank;
  94. struct cmark_node_block* children;
  95. struct cmark_node_block* last_child;
  96. struct cmark_node_block* parent;
  97. struct cmark_node_block* top;
  98. cmark_strbuf string_content;
  99. struct cmark_node_inl* inline_content;
  100. union {
  101. struct cmark_ListData list;
  102. struct cmark_FencedCodeData code;
  103. struct {
  104. int level;
  105. } header;
  106. struct {
  107. cmark_reference_map *refmap;
  108. } document;
  109. } as;
  110. struct cmark_node_block *next;
  111. struct cmark_node_block *prev;
  112. };
  113. struct cmark_doc_parser {
  114. struct cmark_node_block* head;
  115. struct cmark_node_block* current;
  116. int line_number;
  117. cmark_strbuf *curline;
  118. };
  119. unsigned char *cmark_clean_autolink(chunk *url, int is_email);
  120. static inline cmark_node_inl *cmark_make_link(cmark_node_inl *label, unsigned char *url, unsigned char *title)
  121. {
  122. cmark_node_inl* e = (cmark_node_inl *)calloc(1, sizeof(*e));
  123. if(e != NULL) {
  124. e->tag = CMARK_INL_LINK;
  125. e->content.linkable.label = label;
  126. e->content.linkable.url = url;
  127. e->content.linkable.title = title;
  128. e->next = NULL;
  129. }
  130. return e;
  131. }
  132. static inline cmark_node_inl* cmark_make_autolink(cmark_node_inl* label, cmark_chunk url, int is_email)
  133. {
  134. return cmark_make_link(label, cmark_clean_autolink(&url, is_email), NULL);
  135. }
  136. static inline cmark_node_inl* cmark_make_inlines(cmark_inl_tag t, cmark_node_inl* contents)
  137. {
  138. cmark_node_inl * e = (cmark_node_inl *)calloc(1, sizeof(*e));
  139. if(e != NULL) {
  140. e->tag = t;
  141. e->content.inlines = contents;
  142. e->next = NULL;
  143. }
  144. return e;
  145. }
  146. // Create an inline with a literal string value.
  147. static inline cmark_node_inl* cmark_make_literal(cmark_inl_tag t, cmark_chunk s)
  148. {
  149. cmark_node_inl * e = (cmark_node_inl *)calloc(1, sizeof(*e));
  150. if(e != NULL) {
  151. e->tag = t;
  152. e->content.literal = s;
  153. e->next = NULL;
  154. }
  155. return e;
  156. }
  157. // Create an inline with no value.
  158. static inline cmark_node_inl* cmark_make_simple(cmark_inl_tag t)
  159. {
  160. cmark_node_inl* e = (cmark_node_inl *)calloc(1, sizeof(*e));
  161. if(e != NULL) {
  162. e->tag = t;
  163. e->next = NULL;
  164. }
  165. return e;
  166. }
  167. // Macros for creating various kinds of simple.
  168. #define cmark_make_str(s) cmark_make_literal(INL_STRING, s)
  169. #define cmark_make_code(s) cmark_make_literal(INL_CODE, s)
  170. #define cmark_make_raw_html(s) cmark_make_literal(INL_RAW_HTML, s)
  171. #define cmark_make_linebreak() cmark_make_simple(INL_LINEBREAK)
  172. #define cmark_make_softbreak() cmark_make_simple(INL_SOFTBREAK)
  173. #define cmark_make_emph(contents) cmark_make_inlines(INL_EMPH, contents)
  174. #define cmark_make_strong(contents) cmark_make_inlines(INL_STRONG, contents)
  175. #ifndef CMARK_NO_SHORT_NAMES
  176. #define node_inl cmark_node_inl
  177. #define INL_STRING CMARK_INL_STRING
  178. #define INL_SOFTBREAK CMARK_INL_SOFTBREAK
  179. #define INL_LINEBREAK CMARK_INL_LINEBREAK
  180. #define INL_CODE CMARK_INL_CODE
  181. #define INL_RAW_HTML CMARK_INL_RAW_HTML
  182. #define INL_EMPH CMARK_INL_EMPH
  183. #define INL_STRONG CMARK_INL_STRONG
  184. #define INL_LINK CMARK_INL_LINK
  185. #define INL_IMAGE CMARK_INL_IMAGE
  186. #define ListData cmark_ListData
  187. #define FencedCodeData cmark_FencedCodeData
  188. #define node_block cmark_node_block
  189. #define BLOCK_DOCUMENT CMARK_BLOCK_DOCUMENT
  190. #define BLOCK_BQUOTE CMARK_BLOCK_BQUOTE
  191. #define BLOCK_LIST CMARK_BLOCK_LIST
  192. #define BLOCK_LIST_ITEM CMARK_BLOCK_LIST_ITEM
  193. #define BLOCK_FENCED_CODE CMARK_BLOCK_FENCED_CODE
  194. #define BLOCK_INDENTED_CODE CMARK_BLOCK_INDENTED_CODE
  195. #define BLOCK_HTML CMARK_BLOCK_HTML
  196. #define BLOCK_PARAGRAPH CMARK_BLOCK_PARAGRAPH
  197. #define BLOCK_ATX_HEADER CMARK_BLOCK_ATX_HEADER
  198. #define BLOCK_SETEXT_HEADER CMARK_BLOCK_SETEXT_HEADER
  199. #define BLOCK_HRULE CMARK_BLOCK_HRULE
  200. #define BLOCK_REFERENCE_DEF CMARK_BLOCK_REFERENCE_DEF
  201. #define make_link cmark_make_link
  202. #define make_autolink cmark_make_autolink
  203. #define make_str cmark_make_str
  204. #define make_code cmark_make_code
  205. #define make_raw_html cmark_make_raw_html
  206. #define make_linebreak cmark_make_linebreak
  207. #define make_softbreak cmark_make_softbreak
  208. #define make_emph cmark_make_emph
  209. #define make_strong cmark_make_strong
  210. #define make_simple cmark_make_simple
  211. #define make_literal cmark_make_literal
  212. #define make_inlines cmark_make_inlines
  213. #endif
  214. #ifdef __cplusplus
  215. }
  216. #endif
  217. #endif