aboutsummaryrefslogtreecommitdiff
path: root/src/ast.h
blob: 263b9cbef5b4ec3b93fe64d7877214420822e9d4 (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. struct cmark_node_inl {
  15. enum {
  16. CMARK_INL_STRING,
  17. CMARK_INL_SOFTBREAK,
  18. CMARK_INL_LINEBREAK,
  19. CMARK_INL_CODE,
  20. CMARK_INL_RAW_HTML,
  21. CMARK_INL_EMPH,
  22. CMARK_INL_STRONG,
  23. CMARK_INL_LINK,
  24. CMARK_INL_IMAGE
  25. } 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. // Types for blocks
  50. struct cmark_ListData {
  51. enum {
  52. bullet,
  53. ordered
  54. } list_type;
  55. int marker_offset;
  56. int padding;
  57. int start;
  58. enum {
  59. period,
  60. parens
  61. } delimiter;
  62. unsigned char bullet_char;
  63. bool tight;
  64. };
  65. struct cmark_FencedCodeData {
  66. int fence_length;
  67. int fence_offset;
  68. unsigned char fence_char;
  69. cmark_strbuf info;
  70. };
  71. struct cmark_node_block {
  72. enum {
  73. CMARK_BLOCK_DOCUMENT,
  74. CMARK_BLOCK_BQUOTE,
  75. CMARK_BLOCK_LIST,
  76. CMARK_BLOCK_LIST_ITEM,
  77. CMARK_BLOCK_FENCED_CODE,
  78. CMARK_BLOCK_INDENTED_CODE,
  79. CMARK_BLOCK_HTML,
  80. CMARK_BLOCK_PARAGRAPH,
  81. CMARK_BLOCK_ATX_HEADER,
  82. CMARK_BLOCK_SETEXT_HEADER,
  83. CMARK_BLOCK_HRULE,
  84. CMARK_BLOCK_REFERENCE_DEF
  85. } tag;
  86. int start_line;
  87. int start_column;
  88. int end_line;
  89. bool open;
  90. bool last_line_blank;
  91. struct cmark_node_block* children;
  92. struct cmark_node_block* last_child;
  93. struct cmark_node_block* parent;
  94. struct cmark_node_block* top;
  95. cmark_strbuf string_content;
  96. struct cmark_node_inl* inline_content;
  97. union {
  98. struct cmark_ListData list;
  99. struct cmark_FencedCodeData code;
  100. struct {
  101. int level;
  102. } header;
  103. struct {
  104. cmark_reference_map *refmap;
  105. } document;
  106. } as;
  107. struct cmark_node_block *next;
  108. struct cmark_node_block *prev;
  109. };
  110. struct cmark_doc_parser {
  111. struct cmark_node_block* head;
  112. struct cmark_node_block* current;
  113. int line_number;
  114. cmark_strbuf *curline;
  115. };
  116. struct cmark_node_inl *cmark_make_link(struct cmark_node_inl *label, unsigned char *url, unsigned char *title);
  117. struct cmark_node_inl* cmark_make_autolink(struct cmark_node_inl* label, cmark_chunk url, int is_email);
  118. struct cmark_node_inl* cmark_make_inlines(int t, struct cmark_node_inl* contents);
  119. struct cmark_node_inl* cmark_make_literal(int t, cmark_chunk s);
  120. struct cmark_node_inl* cmark_make_simple(int t);
  121. // Macros for creating various kinds of simple.
  122. #define cmark_make_str(s) cmark_make_literal(INL_STRING, s)
  123. #define cmark_make_code(s) cmark_make_literal(INL_CODE, s)
  124. #define cmark_make_raw_html(s) cmark_make_literal(INL_RAW_HTML, s)
  125. #define cmark_make_linebreak() cmark_make_simple(INL_LINEBREAK)
  126. #define cmark_make_softbreak() cmark_make_simple(INL_SOFTBREAK)
  127. #define cmark_make_emph(contents) cmark_make_inlines(INL_EMPH, contents)
  128. #define cmark_make_strong(contents) cmark_make_inlines(INL_STRONG, contents)
  129. #ifndef CMARK_NO_SHORT_NAMES
  130. #define node_inl cmark_node_inl
  131. #define INL_STRING CMARK_INL_STRING
  132. #define INL_SOFTBREAK CMARK_INL_SOFTBREAK
  133. #define INL_LINEBREAK CMARK_INL_LINEBREAK
  134. #define INL_CODE CMARK_INL_CODE
  135. #define INL_RAW_HTML CMARK_INL_RAW_HTML
  136. #define INL_EMPH CMARK_INL_EMPH
  137. #define INL_STRONG CMARK_INL_STRONG
  138. #define INL_LINK CMARK_INL_LINK
  139. #define INL_IMAGE CMARK_INL_IMAGE
  140. #define ListData cmark_ListData
  141. #define FencedCodeData cmark_FencedCodeData
  142. #define node_block cmark_node_block
  143. #define BLOCK_DOCUMENT CMARK_BLOCK_DOCUMENT
  144. #define BLOCK_BQUOTE CMARK_BLOCK_BQUOTE
  145. #define BLOCK_LIST CMARK_BLOCK_LIST
  146. #define BLOCK_LIST_ITEM CMARK_BLOCK_LIST_ITEM
  147. #define BLOCK_FENCED_CODE CMARK_BLOCK_FENCED_CODE
  148. #define BLOCK_INDENTED_CODE CMARK_BLOCK_INDENTED_CODE
  149. #define BLOCK_HTML CMARK_BLOCK_HTML
  150. #define BLOCK_PARAGRAPH CMARK_BLOCK_PARAGRAPH
  151. #define BLOCK_ATX_HEADER CMARK_BLOCK_ATX_HEADER
  152. #define BLOCK_SETEXT_HEADER CMARK_BLOCK_SETEXT_HEADER
  153. #define BLOCK_HRULE CMARK_BLOCK_HRULE
  154. #define BLOCK_REFERENCE_DEF CMARK_BLOCK_REFERENCE_DEF
  155. #define make_link cmark_make_link
  156. #define make_autolink cmark_make_autolink
  157. #define make_str cmark_make_str
  158. #define make_code cmark_make_code
  159. #define make_raw_html cmark_make_raw_html
  160. #define make_linebreak cmark_make_linebreak
  161. #define make_softbreak cmark_make_softbreak
  162. #define make_emph cmark_make_emph
  163. #define make_strong cmark_make_strong
  164. #define make_simple cmark_make_simple
  165. #define make_literal cmark_make_literal
  166. #define make_inlines cmark_make_inlines
  167. #endif
  168. #ifdef __cplusplus
  169. }
  170. #endif
  171. #endif