aboutsummaryrefslogtreecommitdiff
path: root/src/ast.h
blob: 6c77c68e73d185237f46876fb66e5c8416bd8d05 (plain)
  1. #ifndef CMARK_AST_H
  2. #define CMARK_AST_H
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include "buffer.h"
  6. #include "chunk.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #define CMARK_VERSION "0.1"
  11. #define CMARK_CODE_INDENT 4
  12. #define REFMAP_SIZE 16
  13. #define CMARK_MAX_LINK_LABEL_LENGTH 1000
  14. typedef enum {
  15. CMARK_INL_STRING,
  16. CMARK_INL_SOFTBREAK,
  17. CMARK_INL_LINEBREAK,
  18. CMARK_INL_CODE,
  19. CMARK_INL_RAW_HTML,
  20. CMARK_INL_EMPH,
  21. CMARK_INL_STRONG,
  22. CMARK_INL_LINK,
  23. CMARK_INL_IMAGE
  24. } cmark_inl_tag;
  25. struct cmark_node_inl {
  26. cmark_inl_tag tag;
  27. union {
  28. cmark_chunk literal;
  29. struct cmark_node_inl *inlines;
  30. struct {
  31. struct cmark_node_inl *label;
  32. unsigned char *url;
  33. unsigned char *title;
  34. } linkable;
  35. } content;
  36. struct cmark_node_inl *next;
  37. };
  38. struct cmark_reference {
  39. struct cmark_reference *next;
  40. unsigned char *label;
  41. unsigned char *url;
  42. unsigned char *title;
  43. unsigned int hash;
  44. };
  45. typedef struct cmark_reference cmark_reference;
  46. struct cmark_reference_map {
  47. cmark_reference *table[REFMAP_SIZE];
  48. };
  49. typedef struct cmark_reference_map cmark_reference_map;
  50. typedef enum {
  51. bullet,
  52. ordered
  53. } cmark_list_type;
  54. typedef enum {
  55. period,
  56. parens
  57. } cmark_delim_type;
  58. // Types for blocks
  59. struct cmark_ListData {
  60. cmark_list_type list_type;
  61. int marker_offset;
  62. int padding;
  63. int start;
  64. cmark_delim_type delimiter;
  65. unsigned char bullet_char;
  66. bool tight;
  67. };
  68. struct cmark_FencedCodeData {
  69. int fence_length;
  70. int fence_offset;
  71. unsigned char fence_char;
  72. cmark_strbuf info;
  73. };
  74. typedef enum {
  75. CMARK_BLOCK_DOCUMENT,
  76. CMARK_BLOCK_BQUOTE,
  77. CMARK_BLOCK_LIST,
  78. CMARK_BLOCK_LIST_ITEM,
  79. CMARK_BLOCK_FENCED_CODE,
  80. CMARK_BLOCK_INDENTED_CODE,
  81. CMARK_BLOCK_HTML,
  82. CMARK_BLOCK_PARAGRAPH,
  83. CMARK_BLOCK_ATX_HEADER,
  84. CMARK_BLOCK_SETEXT_HEADER,
  85. CMARK_BLOCK_HRULE,
  86. CMARK_BLOCK_REFERENCE_DEF
  87. } cmark_block_tag;
  88. struct cmark_node_block {
  89. cmark_block_tag tag;
  90. int start_line;
  91. int start_column;
  92. int end_line;
  93. bool open;
  94. bool last_line_blank;
  95. struct cmark_node_block* children;
  96. struct cmark_node_block* last_child;
  97. struct cmark_node_block* parent;
  98. struct cmark_node_block* top;
  99. cmark_strbuf string_content;
  100. struct cmark_node_inl* inline_content;
  101. union {
  102. struct cmark_ListData list;
  103. struct cmark_FencedCodeData code;
  104. struct {
  105. int level;
  106. } header;
  107. struct {
  108. cmark_reference_map *refmap;
  109. } document;
  110. } as;
  111. struct cmark_node_block *next;
  112. struct cmark_node_block *prev;
  113. };
  114. struct cmark_doc_parser {
  115. struct cmark_node_block* head;
  116. struct cmark_node_block* current;
  117. int line_number;
  118. cmark_strbuf *curline;
  119. };
  120. struct cmark_node_inl *cmark_make_link(struct cmark_node_inl *label, unsigned char *url, unsigned char *title);
  121. struct cmark_node_inl* cmark_make_autolink(struct cmark_node_inl* label, cmark_chunk url, int is_email);
  122. struct cmark_node_inl* cmark_make_inlines(cmark_inl_tag t, struct cmark_node_inl* contents);
  123. struct cmark_node_inl* cmark_make_literal(cmark_inl_tag t, cmark_chunk s);
  124. struct cmark_node_inl* cmark_make_simple(cmark_inl_tag t);
  125. // Macros for creating various kinds of simple.
  126. #define cmark_make_str(s) cmark_make_literal(INL_STRING, s)
  127. #define cmark_make_code(s) cmark_make_literal(INL_CODE, s)
  128. #define cmark_make_raw_html(s) cmark_make_literal(INL_RAW_HTML, s)
  129. #define cmark_make_linebreak() cmark_make_simple(INL_LINEBREAK)
  130. #define cmark_make_softbreak() cmark_make_simple(INL_SOFTBREAK)
  131. #define cmark_make_emph(contents) cmark_make_inlines(INL_EMPH, contents)
  132. #define cmark_make_strong(contents) cmark_make_inlines(INL_STRONG, contents)
  133. #ifndef CMARK_NO_SHORT_NAMES
  134. #define node_inl cmark_node_inl
  135. #define INL_STRING CMARK_INL_STRING
  136. #define INL_SOFTBREAK CMARK_INL_SOFTBREAK
  137. #define INL_LINEBREAK CMARK_INL_LINEBREAK
  138. #define INL_CODE CMARK_INL_CODE
  139. #define INL_RAW_HTML CMARK_INL_RAW_HTML
  140. #define INL_EMPH CMARK_INL_EMPH
  141. #define INL_STRONG CMARK_INL_STRONG
  142. #define INL_LINK CMARK_INL_LINK
  143. #define INL_IMAGE CMARK_INL_IMAGE
  144. #define ListData cmark_ListData
  145. #define FencedCodeData cmark_FencedCodeData
  146. #define node_block cmark_node_block
  147. #define BLOCK_DOCUMENT CMARK_BLOCK_DOCUMENT
  148. #define BLOCK_BQUOTE CMARK_BLOCK_BQUOTE
  149. #define BLOCK_LIST CMARK_BLOCK_LIST
  150. #define BLOCK_LIST_ITEM CMARK_BLOCK_LIST_ITEM
  151. #define BLOCK_FENCED_CODE CMARK_BLOCK_FENCED_CODE
  152. #define BLOCK_INDENTED_CODE CMARK_BLOCK_INDENTED_CODE
  153. #define BLOCK_HTML CMARK_BLOCK_HTML
  154. #define BLOCK_PARAGRAPH CMARK_BLOCK_PARAGRAPH
  155. #define BLOCK_ATX_HEADER CMARK_BLOCK_ATX_HEADER
  156. #define BLOCK_SETEXT_HEADER CMARK_BLOCK_SETEXT_HEADER
  157. #define BLOCK_HRULE CMARK_BLOCK_HRULE
  158. #define BLOCK_REFERENCE_DEF CMARK_BLOCK_REFERENCE_DEF
  159. #define make_link cmark_make_link
  160. #define make_autolink cmark_make_autolink
  161. #define make_str cmark_make_str
  162. #define make_code cmark_make_code
  163. #define make_raw_html cmark_make_raw_html
  164. #define make_linebreak cmark_make_linebreak
  165. #define make_softbreak cmark_make_softbreak
  166. #define make_emph cmark_make_emph
  167. #define make_strong cmark_make_strong
  168. #define make_simple cmark_make_simple
  169. #define make_literal cmark_make_literal
  170. #define make_inlines cmark_make_inlines
  171. #endif
  172. #ifdef __cplusplus
  173. }
  174. #endif
  175. #endif