From 91642ceee7a2a2c05d9561c0e22b2f15111ac603 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 13 Nov 2014 09:29:51 -0800 Subject: Added ast.[c,h] for AST definitions and AST-manipulating functions. --- src/CMakeLists.txt | 2 + src/ast.c | 30 ++++++++++++ src/ast.h | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/blocks.c | 23 --------- src/cmark.h | 124 +------------------------------------------------ 5 files changed, 167 insertions(+), 146 deletions(-) create mode 100644 src/ast.c create mode 100644 src/ast.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6f30bf0..40efdf4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 2.8) set(LIBRARY "cmark-shared") set(HEADERS cmark.h + ast.h buffer.h chunk.h references.h @@ -14,6 +15,7 @@ set(HEADERS ) set(LIBRARY_SOURCES cmark.c + ast.c blocks.c inlines.c print.c diff --git a/src/ast.c b/src/ast.c new file mode 100644 index 0000000..1622568 --- /dev/null +++ b/src/ast.c @@ -0,0 +1,30 @@ +#include +#include +#include "buffer.h" +#include "ast.h" +#include "inlines.h" +#include "references.h" + +// Free a node_block list and any children. +void cmark_free_nodes(node_block *e) +{ + node_block * next; + while (e != NULL) { + free_inlines(e->inline_content); + strbuf_free(&e->string_content); + if (e->tag == BLOCK_FENCED_CODE) { + strbuf_free(&e->as.code.info); + } else if (e->tag == BLOCK_DOCUMENT) { + reference_map_free(e->as.document.refmap); + } + if (e->last_child) { + // Splice children into list + e->last_child->next = e->next; + e->next = e->children; + } + next = e->next; + free(e); + e = next; + } +} + diff --git a/src/ast.h b/src/ast.h new file mode 100644 index 0000000..decf5eb --- /dev/null +++ b/src/ast.h @@ -0,0 +1,134 @@ +#ifndef _AST_H_ +#define _AST_H_ + +#include +#include +#include "buffer.h" +#include "chunk.h" +#include "references.h" + +struct cmark_node_inl { + enum { + CMARK_INL_STRING, + CMARK_INL_SOFTBREAK, + CMARK_INL_LINEBREAK, + CMARK_INL_CODE, + CMARK_INL_RAW_HTML, + CMARK_INL_EMPH, + CMARK_INL_STRONG, + CMARK_INL_LINK, + CMARK_INL_IMAGE + } tag; + union { + cmark_chunk literal; + struct cmark_node_inl *inlines; + struct { + struct cmark_node_inl *label; + unsigned char *url; + unsigned char *title; + } linkable; + } content; + struct cmark_node_inl *next; +}; + +typedef struct cmark_node_inl cmark_node_inl; + +// Types for blocks +struct cmark_ListData { + enum { + bullet, + ordered + } list_type; + int marker_offset; + int padding; + int start; + enum { + period, + parens + } delimiter; + unsigned char bullet_char; + bool tight; +}; + +struct cmark_FencedCodeData { + int fence_length; + int fence_offset; + unsigned char fence_char; + cmark_strbuf info; +}; + +struct cmark_node_block { + enum { + CMARK_BLOCK_DOCUMENT, + CMARK_BLOCK_BQUOTE, + CMARK_BLOCK_LIST, + CMARK_BLOCK_LIST_ITEM, + CMARK_BLOCK_FENCED_CODE, + CMARK_BLOCK_INDENTED_CODE, + CMARK_BLOCK_HTML, + CMARK_BLOCK_PARAGRAPH, + CMARK_BLOCK_ATX_HEADER, + CMARK_BLOCK_SETEXT_HEADER, + CMARK_BLOCK_HRULE, + CMARK_BLOCK_REFERENCE_DEF + } tag; + int start_line; + int start_column; + int end_line; + bool open; + bool last_line_blank; + struct cmark_node_block* children; + struct cmark_node_block* last_child; + struct cmark_node_block* parent; + struct cmark_node_block* top; + cmark_strbuf string_content; + cmark_node_inl* inline_content; + + union { + struct cmark_ListData list; + struct cmark_FencedCodeData code; + struct { + int level; + } header; + struct { + cmark_reference_map *refmap; + } document; + } as; + + struct cmark_node_block *next; + struct cmark_node_block *prev; +}; + +typedef struct cmark_node_block cmark_node_block; + +void cmark_free_nodes(cmark_node_block *e); + +#ifndef CMARK_NO_SHORT_NAMES + #define node_inl cmark_node_inl + #define INL_STRING CMARK_INL_STRING + #define INL_SOFTBREAK CMARK_INL_SOFTBREAK + #define INL_LINEBREAK CMARK_INL_LINEBREAK + #define INL_CODE CMARK_INL_CODE + #define INL_RAW_HTML CMARK_INL_RAW_HTML + #define INL_EMPH CMARK_INL_EMPH + #define INL_STRONG CMARK_INL_STRONG + #define INL_LINK CMARK_INL_LINK + #define INL_IMAGE CMARK_INL_IMAGE + #define ListData cmark_ListData + #define FencedCodeData cmark_FencedCodeData + #define node_block cmark_node_block + #define BLOCK_DOCUMENT CMARK_BLOCK_DOCUMENT + #define BLOCK_BQUOTE CMARK_BLOCK_BQUOTE + #define BLOCK_LIST CMARK_BLOCK_LIST + #define BLOCK_LIST_ITEM CMARK_BLOCK_LIST_ITEM + #define BLOCK_FENCED_CODE CMARK_BLOCK_FENCED_CODE + #define BLOCK_INDENTED_CODE CMARK_BLOCK_INDENTED_CODE + #define BLOCK_HTML CMARK_BLOCK_HTML + #define BLOCK_PARAGRAPH CMARK_BLOCK_PARAGRAPH + #define BLOCK_ATX_HEADER CMARK_BLOCK_ATX_HEADER + #define BLOCK_SETEXT_HEADER CMARK_BLOCK_SETEXT_HEADER + #define BLOCK_HRULE CMARK_BLOCK_HRULE + #define BLOCK_REFERENCE_DEF CMARK_BLOCK_REFERENCE_DEF +#endif + +#endif diff --git a/src/blocks.c b/src/blocks.c index a70f5f8..615346d 100644 --- a/src/blocks.c +++ b/src/blocks.c @@ -253,29 +253,6 @@ static node_block* add_child(node_block* parent, } -// Free a node_block list and any children. -void cmark_free_nodes(node_block *e) -{ - node_block * next; - while (e != NULL) { - free_inlines(e->inline_content); - strbuf_free(&e->string_content); - if (e->tag == BLOCK_FENCED_CODE) { - strbuf_free(&e->as.code.info); - } else if (e->tag == BLOCK_DOCUMENT) { - reference_map_free(e->as.document.refmap); - } - if (e->last_child) { - // Splice children into list - e->last_child->next = e->next; - e->next = e->children; - } - next = e->next; - free(e); - e = next; - } -} - typedef struct BlockStack { struct BlockStack *previous; node_block *next_sibling; diff --git a/src/cmark.h b/src/cmark.h index 8b29934..07cee14 100644 --- a/src/cmark.h +++ b/src/cmark.h @@ -4,8 +4,7 @@ #include #include #include "buffer.h" -#include "chunk.h" -#include "references.h" +#include "ast.h" #ifdef __cplusplus extern "C" { @@ -16,105 +15,9 @@ extern "C" { #define CMARK_MAX_LINK_LABEL_LENGTH 1000 -struct cmark_node_inl { - enum { - CMARK_INL_STRING, - CMARK_INL_SOFTBREAK, - CMARK_INL_LINEBREAK, - CMARK_INL_CODE, - CMARK_INL_RAW_HTML, - CMARK_INL_EMPH, - CMARK_INL_STRONG, - CMARK_INL_LINK, - CMARK_INL_IMAGE - } tag; - union { - cmark_chunk literal; - struct cmark_node_inl *inlines; - struct { - struct cmark_node_inl *label; - unsigned char *url; - unsigned char *title; - } linkable; - } content; - struct cmark_node_inl *next; -}; - -typedef struct cmark_node_inl cmark_node_inl; - -// Types for blocks -struct cmark_ListData { - enum { - bullet, - ordered - } list_type; - int marker_offset; - int padding; - int start; - enum { - period, - parens - } delimiter; - unsigned char bullet_char; - bool tight; -}; - -struct cmark_FencedCodeData { - int fence_length; - int fence_offset; - unsigned char fence_char; - cmark_strbuf info; -}; - -struct cmark_node_block { - enum { - CMARK_BLOCK_DOCUMENT, - CMARK_BLOCK_BQUOTE, - CMARK_BLOCK_LIST, - CMARK_BLOCK_LIST_ITEM, - CMARK_BLOCK_FENCED_CODE, - CMARK_BLOCK_INDENTED_CODE, - CMARK_BLOCK_HTML, - CMARK_BLOCK_PARAGRAPH, - CMARK_BLOCK_ATX_HEADER, - CMARK_BLOCK_SETEXT_HEADER, - CMARK_BLOCK_HRULE, - CMARK_BLOCK_REFERENCE_DEF - } tag; - int start_line; - int start_column; - int end_line; - bool open; - bool last_line_blank; - struct cmark_node_block* children; - struct cmark_node_block* last_child; - struct cmark_node_block* parent; - struct cmark_node_block* top; - cmark_strbuf string_content; - cmark_node_inl* inline_content; - - union { - struct cmark_ListData list; - struct cmark_FencedCodeData code; - struct { - int level; - } header; - struct { - cmark_reference_map *refmap; - } document; - } as; - - struct cmark_node_block *next; - struct cmark_node_block *prev; -}; - -typedef struct cmark_node_block cmark_node_block; - cmark_node_block *cmark_parse_document(const unsigned char *buffer, size_t len); cmark_node_block *cmark_parse_file(FILE *f); -void cmark_free_nodes(cmark_node_block *e); - void cmark_debug_print(cmark_node_block *root); void cmark_render_html(cmark_strbuf *html, cmark_node_block *root); @@ -124,31 +27,6 @@ unsigned char *cmark_markdown_to_html(unsigned char *text, int len); #define VERSION CMARK_VERSION #define CODE_INDENT CMARK_CODE_INDENT #define MAX_LINK_LABEL_LENGTH CMARK_MAX_LINK_LABEL_LENGTH - #define node_inl cmark_node_inl - #define INL_STRING CMARK_INL_STRING - #define INL_SOFTBREAK CMARK_INL_SOFTBREAK - #define INL_LINEBREAK CMARK_INL_LINEBREAK - #define INL_CODE CMARK_INL_CODE - #define INL_RAW_HTML CMARK_INL_RAW_HTML - #define INL_EMPH CMARK_INL_EMPH - #define INL_STRONG CMARK_INL_STRONG - #define INL_LINK CMARK_INL_LINK - #define INL_IMAGE CMARK_INL_IMAGE - #define ListData cmark_ListData - #define FencedCodeData cmark_FencedCodeData - #define node_block cmark_node_block - #define BLOCK_DOCUMENT CMARK_BLOCK_DOCUMENT - #define BLOCK_BQUOTE CMARK_BLOCK_BQUOTE - #define BLOCK_LIST CMARK_BLOCK_LIST - #define BLOCK_LIST_ITEM CMARK_BLOCK_LIST_ITEM - #define BLOCK_FENCED_CODE CMARK_BLOCK_FENCED_CODE - #define BLOCK_INDENTED_CODE CMARK_BLOCK_INDENTED_CODE - #define BLOCK_HTML CMARK_BLOCK_HTML - #define BLOCK_PARAGRAPH CMARK_BLOCK_PARAGRAPH - #define BLOCK_ATX_HEADER CMARK_BLOCK_ATX_HEADER - #define BLOCK_SETEXT_HEADER CMARK_BLOCK_SETEXT_HEADER - #define BLOCK_HRULE CMARK_BLOCK_HRULE - #define BLOCK_REFERENCE_DEF CMARK_BLOCK_REFERENCE_DEF #endif #ifdef __cplusplus -- cgit v1.2.3