2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* syck.h
|
|
|
|
*
|
|
|
|
* $Author$
|
|
|
|
* $Date$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 why the lucky stiff
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
#ifndef SYCK_H
|
|
|
|
#define SYCK_H
|
|
|
|
|
2003-07-11 18:52:14 -04:00
|
|
|
#define SYCK_YAML_MAJOR 1
|
|
|
|
#define SYCK_YAML_MINOR 0
|
|
|
|
|
2004-04-26 19:24:46 -04:00
|
|
|
#define SYCK_VERSION "0.44"
|
2003-05-10 15:55:18 -04:00
|
|
|
#define YAML_DOMAIN "yaml.org,2002"
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-10-14 20:32:44 -04:00
|
|
|
#include <stdio.h>
|
2003-10-17 16:53:39 -04:00
|
|
|
#include <ctype.h>
|
2003-05-20 02:48:04 -04:00
|
|
|
#include "st.h"
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* Memory Allocation
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
#if defined(HAVE_ALLOCA_H) && !defined(__GNUC__)
|
|
|
|
#include <alloca.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
void syck_assert( char *, unsigned );
|
|
|
|
# define ASSERT(f) \
|
|
|
|
if ( f ) \
|
|
|
|
{} \
|
|
|
|
else \
|
|
|
|
syck_assert( __FILE__, __LINE__ )
|
|
|
|
#else
|
|
|
|
# define ASSERT(f)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef NULL
|
|
|
|
# define NULL (void *)0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ALLOC_CT 8
|
2003-11-25 14:51:38 -05:00
|
|
|
#define SYCK_BUFFERSIZE 4096
|
2003-05-09 17:25:50 -04:00
|
|
|
#define S_ALLOC_N(type,n) (type*)malloc(sizeof(type)*(n))
|
|
|
|
#define S_ALLOC(type) (type*)malloc(sizeof(type))
|
|
|
|
#define S_REALLOC_N(var,type,n) (var)=(type*)realloc((char*)(var),sizeof(type)*(n))
|
|
|
|
#define S_FREE(n) free(n); n = NULL;
|
|
|
|
|
|
|
|
#define S_ALLOCA_N(type,n) (type*)alloca(sizeof(type)*(n))
|
|
|
|
|
|
|
|
#define S_MEMZERO(p,type,n) memset((p), 0, sizeof(type)*(n))
|
|
|
|
#define S_MEMCPY(p1,p2,type,n) memcpy((p1), (p2), sizeof(type)*(n))
|
|
|
|
#define S_MEMMOVE(p1,p2,type,n) memmove((p1), (p2), sizeof(type)*(n))
|
|
|
|
#define S_MEMCMP(p1,p2,type,n) memcmp((p1), (p2), sizeof(type)*(n))
|
|
|
|
|
|
|
|
#define BLOCK_FOLD 10
|
|
|
|
#define BLOCK_LIT 20
|
|
|
|
#define BLOCK_PLAIN 30
|
|
|
|
#define NL_CHOMP 130
|
|
|
|
#define NL_KEEP 140
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* Node definitions
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
#define SYMID unsigned long
|
|
|
|
|
|
|
|
typedef struct _syck_node SyckNode;
|
|
|
|
|
|
|
|
enum syck_kind_tag {
|
|
|
|
syck_map_kind,
|
|
|
|
syck_seq_kind,
|
|
|
|
syck_str_kind
|
|
|
|
};
|
|
|
|
|
|
|
|
enum map_part {
|
|
|
|
map_key,
|
|
|
|
map_value
|
|
|
|
};
|
|
|
|
|
2003-07-11 18:52:14 -04:00
|
|
|
/*
|
|
|
|
* Node metadata struct
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
struct _syck_node {
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Symbol table ID */
|
2003-05-09 17:25:50 -04:00
|
|
|
SYMID id;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Underlying kind */
|
2003-05-09 17:25:50 -04:00
|
|
|
enum syck_kind_tag kind;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Fully qualified tag-uri for type */
|
2003-05-09 17:25:50 -04:00
|
|
|
char *type_id;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Anchor name */
|
2003-05-09 17:25:50 -04:00
|
|
|
char *anchor;
|
|
|
|
union {
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Storage for map data */
|
2003-05-09 17:25:50 -04:00
|
|
|
struct SyckMap {
|
|
|
|
SYMID *keys;
|
|
|
|
SYMID *values;
|
|
|
|
long capa;
|
|
|
|
long idx;
|
|
|
|
} *pairs;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Storage for sequence data */
|
2003-05-09 17:25:50 -04:00
|
|
|
struct SyckSeq {
|
|
|
|
SYMID *items;
|
|
|
|
long capa;
|
|
|
|
long idx;
|
|
|
|
} *list;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Storage for string data */
|
2003-05-09 17:25:50 -04:00
|
|
|
struct SyckStr {
|
|
|
|
char *ptr;
|
|
|
|
long len;
|
|
|
|
} *str;
|
|
|
|
} data;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Shortcut node */
|
2003-05-14 16:01:09 -04:00
|
|
|
void *shortcut;
|
2003-05-09 17:25:50 -04:00
|
|
|
};
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* Parser definitions
|
|
|
|
*/
|
2003-07-11 18:52:14 -04:00
|
|
|
typedef struct _syck_parser SyckParser;
|
|
|
|
typedef struct _syck_file SyckIoFile;
|
|
|
|
typedef struct _syck_str SyckIoStr;
|
|
|
|
typedef struct _syck_level SyckLevel;
|
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
typedef SYMID (*SyckNodeHandler)(SyckParser *, SyckNode *);
|
|
|
|
typedef void (*SyckErrorHandler)(SyckParser *, char *);
|
2003-06-17 12:55:54 -04:00
|
|
|
typedef SyckNode * (*SyckBadAnchorHandler)(SyckParser *, char *);
|
2003-05-09 17:25:50 -04:00
|
|
|
typedef long (*SyckIoFileRead)(char *, SyckIoFile *, long, long);
|
|
|
|
typedef long (*SyckIoStrRead)(char *, SyckIoStr *, long, long);
|
|
|
|
|
|
|
|
enum syck_io_type {
|
|
|
|
syck_io_str,
|
|
|
|
syck_io_file
|
|
|
|
};
|
|
|
|
|
2003-10-14 19:15:19 -04:00
|
|
|
enum syck_parser_input {
|
|
|
|
syck_yaml_utf8,
|
|
|
|
syck_yaml_utf16,
|
|
|
|
syck_yaml_utf32,
|
|
|
|
syck_bytecode_utf8
|
|
|
|
};
|
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
enum syck_level_status {
|
|
|
|
syck_lvl_header,
|
|
|
|
syck_lvl_doc,
|
2003-10-14 19:15:19 -04:00
|
|
|
syck_lvl_open,
|
2003-05-09 17:25:50 -04:00
|
|
|
syck_lvl_seq,
|
2003-05-13 02:34:18 -04:00
|
|
|
syck_lvl_map,
|
2003-05-09 17:25:50 -04:00
|
|
|
syck_lvl_block,
|
|
|
|
syck_lvl_str,
|
|
|
|
syck_lvl_inline,
|
|
|
|
syck_lvl_end,
|
|
|
|
syck_lvl_pause
|
|
|
|
};
|
|
|
|
|
2003-07-11 18:52:14 -04:00
|
|
|
/*
|
2003-08-19 12:38:02 -04:00
|
|
|
* Parser structs
|
2003-07-11 18:52:14 -04:00
|
|
|
*/
|
2003-08-19 12:38:02 -04:00
|
|
|
struct _syck_file {
|
|
|
|
/* File pointer */
|
|
|
|
FILE *ptr;
|
|
|
|
/* Function which FILE -> buffer */
|
|
|
|
SyckIoFileRead read;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _syck_str {
|
|
|
|
/* String buffer pointers */
|
|
|
|
char *beg, *ptr, *end;
|
|
|
|
/* Function which string -> buffer */
|
|
|
|
SyckIoStrRead read;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _syck_level {
|
|
|
|
int spaces;
|
2003-10-14 19:15:19 -04:00
|
|
|
int ncount;
|
2003-08-19 12:38:02 -04:00
|
|
|
char *domain;
|
|
|
|
enum syck_level_status status;
|
|
|
|
};
|
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
struct _syck_parser {
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Root node */
|
2003-05-15 17:46:47 -04:00
|
|
|
SYMID root, root_on_error;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Implicit typing flag */
|
2003-05-09 17:25:50 -04:00
|
|
|
int implicit_typing, taguri_expansion;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Scripting language function to handle nodes */
|
2003-05-09 17:25:50 -04:00
|
|
|
SyckNodeHandler handler;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Error handler */
|
2003-05-09 17:25:50 -04:00
|
|
|
SyckErrorHandler error_handler;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* InvalidAnchor handler */
|
2003-06-17 12:55:54 -04:00
|
|
|
SyckBadAnchorHandler bad_anchor_handler;
|
2003-10-14 19:15:19 -04:00
|
|
|
/* Parser input type */
|
|
|
|
enum syck_parser_input input_type;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* IO type */
|
2003-05-09 17:25:50 -04:00
|
|
|
enum syck_io_type io_type;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Custom buffer size */
|
2003-05-09 17:25:50 -04:00
|
|
|
size_t bufsize;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Buffer pointers */
|
2003-05-19 12:37:20 -04:00
|
|
|
char *buffer, *linectptr, *lineptr, *toktmp, *token, *cursor, *marker, *limit;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Line counter */
|
2003-05-09 17:25:50 -04:00
|
|
|
int linect;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Last token from yylex() */
|
2003-05-09 17:25:50 -04:00
|
|
|
int last_token;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Force a token upon next call to yylex() */
|
2003-05-09 17:25:50 -04:00
|
|
|
int force_token;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* EOF flag */
|
2003-05-09 17:25:50 -04:00
|
|
|
int eof;
|
|
|
|
union {
|
2003-08-19 12:38:02 -04:00
|
|
|
SyckIoFile *file;
|
|
|
|
SyckIoStr *str;
|
2003-05-09 17:25:50 -04:00
|
|
|
} io;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Symbol table for anchors */
|
2003-06-17 12:55:54 -04:00
|
|
|
st_table *anchors, *bad_anchors;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Optional symbol table for SYMIDs */
|
2003-05-09 17:25:50 -04:00
|
|
|
st_table *syms;
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Levels of indentation */
|
2003-08-19 12:38:02 -04:00
|
|
|
SyckLevel *levels;
|
2003-05-09 17:25:50 -04:00
|
|
|
int lvl_idx;
|
|
|
|
int lvl_capa;
|
|
|
|
void *bonus;
|
|
|
|
};
|
|
|
|
|
2003-07-11 18:52:14 -04:00
|
|
|
/*
|
|
|
|
* Emitter definitions
|
|
|
|
*/
|
|
|
|
typedef struct _syck_emitter SyckEmitter;
|
|
|
|
typedef struct _syck_emitter_node SyckEmitterNode;
|
|
|
|
|
|
|
|
typedef void (*SyckOutputHandler)(SyckEmitter *, char *, long);
|
|
|
|
|
|
|
|
enum doc_stage {
|
|
|
|
doc_open,
|
|
|
|
doc_need_header,
|
|
|
|
doc_processing
|
|
|
|
};
|
|
|
|
|
|
|
|
enum block_styles {
|
|
|
|
block_arbitrary,
|
|
|
|
block_fold,
|
|
|
|
block_literal
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emitter struct
|
|
|
|
*/
|
|
|
|
struct _syck_emitter {
|
|
|
|
/* Headerless doc flag */
|
|
|
|
int headless;
|
|
|
|
/* Sequence map shortcut flag */
|
|
|
|
int seq_map;
|
|
|
|
/* Force header? */
|
|
|
|
int use_header;
|
|
|
|
/* Force version? */
|
|
|
|
int use_version;
|
|
|
|
/* Sort hash keys */
|
|
|
|
int sort_keys;
|
|
|
|
/* Anchor format */
|
|
|
|
char *anchor_format;
|
|
|
|
/* Explicit typing on all collections? */
|
|
|
|
int explicit_typing;
|
|
|
|
/* Best width on folded scalars */
|
|
|
|
int best_width;
|
|
|
|
/* Use literal[1] or folded[2] blocks on all text? */
|
|
|
|
enum block_styles block_style;
|
|
|
|
/* Stage of written document */
|
|
|
|
enum doc_stage stage;
|
|
|
|
/* Level counter */
|
|
|
|
int level;
|
|
|
|
/* Default indentation */
|
|
|
|
int indent;
|
|
|
|
/* Object ignore ID */
|
|
|
|
SYMID ignore_id;
|
|
|
|
/* Symbol table for anchors */
|
|
|
|
st_table *markers, *anchors;
|
|
|
|
/* Custom buffer size */
|
|
|
|
size_t bufsize;
|
|
|
|
/* Buffer */
|
|
|
|
char *buffer, *marker;
|
|
|
|
/* Absolute position of the buffer */
|
|
|
|
long bufpos;
|
|
|
|
/* Handler for output */
|
|
|
|
SyckOutputHandler handler;
|
|
|
|
/* Pointer for extension's use */
|
|
|
|
void *bonus;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emitter node metadata struct
|
|
|
|
*/
|
|
|
|
struct _syck_emitter_node {
|
|
|
|
/* Node buffer position */
|
|
|
|
long pos;
|
|
|
|
/* Current indent */
|
|
|
|
long indent;
|
|
|
|
/* Collection? */
|
|
|
|
int is_shortcut;
|
|
|
|
};
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* Handler prototypes
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
SYMID syck_hdlr_add_node( SyckParser *, SyckNode * );
|
|
|
|
SyckNode *syck_hdlr_add_anchor( SyckParser *, char *, SyckNode * );
|
2003-06-17 12:55:54 -04:00
|
|
|
void syck_hdlr_remove_anchor( SyckParser *, char * );
|
|
|
|
SyckNode *syck_hdlr_get_anchor( SyckParser *, char * );
|
2003-05-09 17:25:50 -04:00
|
|
|
void syck_add_transfer( char *, SyckNode *, int );
|
2003-05-10 15:55:18 -04:00
|
|
|
char *syck_xprivate( char *, int );
|
|
|
|
char *syck_taguri( char *, char *, int );
|
2003-05-09 17:25:50 -04:00
|
|
|
int syck_add_sym( SyckParser *, char * );
|
|
|
|
int syck_lookup_sym( SyckParser *, SYMID, char ** );
|
|
|
|
int syck_try_implicit( SyckNode * );
|
2003-05-10 15:55:18 -04:00
|
|
|
char *syck_type_id_to_uri( char * );
|
2003-05-09 17:25:50 -04:00
|
|
|
void try_tag_implicit( SyckNode *, int );
|
2003-05-10 15:55:18 -04:00
|
|
|
char *syck_match_implicit( char *, size_t );
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* API prototypes
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
char *syck_strndup( char *, long );
|
|
|
|
long syck_io_file_read( char *, SyckIoFile *, long, long );
|
|
|
|
long syck_io_str_read( char *, SyckIoStr *, long, long );
|
2003-07-28 15:27:42 -04:00
|
|
|
char *syck_base64enc( char *, long );
|
|
|
|
char *syck_base64dec( char *, long );
|
2003-07-11 18:52:14 -04:00
|
|
|
SyckEmitter *syck_new_emitter();
|
|
|
|
void syck_emitter_ignore_id( SyckEmitter *, SYMID );
|
|
|
|
void syck_emitter_handler( SyckEmitter *, SyckOutputHandler );
|
|
|
|
void syck_free_emitter( SyckEmitter * );
|
|
|
|
void syck_emitter_clear( SyckEmitter * );
|
2003-07-28 15:27:42 -04:00
|
|
|
void syck_emitter_simple( SyckEmitter *, char *, long );
|
2003-07-11 18:52:14 -04:00
|
|
|
void syck_emitter_write( SyckEmitter *, char *, long );
|
2003-07-24 12:30:43 -04:00
|
|
|
void syck_emitter_flush( SyckEmitter *, long );
|
2003-07-11 18:52:14 -04:00
|
|
|
char *syck_emitter_start_obj( SyckEmitter *, SYMID );
|
|
|
|
void syck_emitter_end_obj( SyckEmitter * );
|
2003-05-09 17:25:50 -04:00
|
|
|
SyckParser *syck_new_parser();
|
|
|
|
void syck_free_parser( SyckParser * );
|
2003-05-15 17:46:47 -04:00
|
|
|
void syck_parser_set_root_on_error( SyckParser *, SYMID );
|
2003-05-09 17:25:50 -04:00
|
|
|
void syck_parser_implicit_typing( SyckParser *, int );
|
|
|
|
void syck_parser_taguri_expansion( SyckParser *, int );
|
|
|
|
void syck_parser_handler( SyckParser *, SyckNodeHandler );
|
|
|
|
void syck_parser_error_handler( SyckParser *, SyckErrorHandler );
|
2003-06-17 12:55:54 -04:00
|
|
|
void syck_parser_bad_anchor_handler( SyckParser *, SyckBadAnchorHandler );
|
2003-05-09 17:25:50 -04:00
|
|
|
void syck_parser_file( SyckParser *, FILE *, SyckIoFileRead );
|
|
|
|
void syck_parser_str( SyckParser *, char *, long, SyckIoStrRead );
|
|
|
|
void syck_parser_str_auto( SyckParser *, char *, SyckIoStrRead );
|
|
|
|
SyckLevel *syck_parser_current_level( SyckParser * );
|
|
|
|
void syck_parser_add_level( SyckParser *, int, enum syck_level_status );
|
2003-05-15 17:46:47 -04:00
|
|
|
void syck_parser_pop_level( SyckParser * );
|
2003-05-09 17:25:50 -04:00
|
|
|
void free_any_io( SyckParser * );
|
|
|
|
long syck_parser_read( SyckParser * );
|
|
|
|
long syck_parser_readlen( SyckParser *, long );
|
|
|
|
void syck_parser_init( SyckParser *, int );
|
|
|
|
SYMID syck_parse( SyckParser * );
|
|
|
|
void syck_default_error_handler( SyckParser *, char * );
|
2003-10-17 16:53:39 -04:00
|
|
|
SYMID syck_yaml2byte_handler( SyckParser *, SyckNode * );
|
|
|
|
char *syck_yaml2byte( char * );
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* Allocation prototypes
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
SyckNode *syck_alloc_map();
|
|
|
|
SyckNode *syck_alloc_seq();
|
|
|
|
SyckNode *syck_alloc_str();
|
|
|
|
void syck_free_node( SyckNode * );
|
|
|
|
void syck_free_members( SyckNode * );
|
|
|
|
SyckNode *syck_new_str( char * );
|
|
|
|
SyckNode *syck_new_str2( char *, long );
|
|
|
|
void syck_str_blow_away_commas( SyckNode * );
|
|
|
|
char *syck_str_read( SyckNode * );
|
|
|
|
SyckNode *syck_new_map( SYMID, SYMID );
|
|
|
|
void syck_map_add( SyckNode *, SYMID, SYMID );
|
|
|
|
SYMID syck_map_read( SyckNode *, enum map_part, long );
|
2003-05-14 16:01:09 -04:00
|
|
|
void syck_map_assign( SyckNode *, enum map_part, long, SYMID );
|
2003-05-15 17:46:47 -04:00
|
|
|
long syck_map_count( SyckNode * );
|
2003-05-09 17:25:50 -04:00
|
|
|
void syck_map_update( SyckNode *, SyckNode * );
|
|
|
|
SyckNode *syck_new_seq( SYMID );
|
|
|
|
void syck_seq_add( SyckNode *, SYMID );
|
|
|
|
SYMID syck_seq_read( SyckNode *, long );
|
|
|
|
long syck_seq_count( SyckNode * );
|
2003-05-15 17:46:47 -04:00
|
|
|
|
2003-05-14 16:01:09 -04:00
|
|
|
void apply_seq_in_map( SyckParser *, SyckNode * );
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-07-28 15:27:42 -04:00
|
|
|
/*
|
|
|
|
* Lexer prototypes
|
|
|
|
*/
|
2003-10-14 19:15:19 -04:00
|
|
|
void syckerror( char * );
|
2003-07-28 15:27:42 -04:00
|
|
|
|
2003-05-20 02:48:04 -04:00
|
|
|
#ifndef ST_DATA_T_DEFINED
|
|
|
|
typedef long st_data_t;
|
|
|
|
#endif
|
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
} /* extern "C" { */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* ifndef SYCK_H */
|