1 /* 2 * lyaml.h, libyaml parser binding for Lua 3 * Written by Gary V. Vaughan, 2013 4 * 5 * Copyright (C) 2013-2022 Gary V. Vaughan 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 */ 25 26 #ifndef LYAML_H 27 #define LYAML_H 1 28 29 #include <yaml.h> 30 31 #include <lua.h> 32 #include <lauxlib.h> 33 34 #include "lyaml.h" 35 36 #if LUA_VERSION_NUM == 502 || LUA_VERSION_NUM == 503 || LUA_VERSION_NUM == 504 37 # define lua_objlen lua_rawlen 38 # define lua_strlen lua_rawlen 39 # define luaL_openlib(L,n,l,nup) luaL_setfuncs((L),(l),(nup)) 40 # define luaL_register(L,n,l) (luaL_newlib(L,l)) 41 #endif 42 43 #ifndef STREQ 44 #define STREQ !strcmp 45 #endif 46 #ifndef STRNEQ 47 #define STRNEQ strcmp 48 #endif 49 50 /* NOTE: Make sure L is in scope before using these macros. 51 lua_pushyamlstr casts away the impedance mismatch between Lua's 52 signed char APIs and libYAML's unsigned char APIs. */ 53 54 #define lua_pushyamlstr(_s) lua_pushstring (L, (char *)(_s)) 55 56 #define RAWSET_BOOLEAN(_k, _v) \ 57 lua_pushyamlstr (_k); \ 58 lua_pushboolean (L, (_v) != 0); \ 59 lua_rawset (L, -3) 60 61 #define RAWSET_INTEGER(_k, _v) \ 62 lua_pushyamlstr (_k); \ 63 lua_pushinteger (L, (_v)); \ 64 lua_rawset (L, -3) 65 66 #define RAWSET_STRING(_k, _v) \ 67 lua_pushyamlstr (_k); \ 68 lua_pushyamlstr (_v); \ 69 lua_rawset (L, -3) 70 71 #define RAWSET_EVENTF(_k) \ 72 lua_pushstring (L, #_k); \ 73 lua_pushyamlstr (EVENTF(_k)); \ 74 lua_rawset (L, -3) 75 76 77 /* NOTE: Make sure L is in scope before using these macros. 78 The table value at _k is not popped from the stack for strings 79 or tables, so that we can check for an empty table entry with 80 lua_isnil (L, -1), or get the length of a string with 81 lua_objlen (L, -1) before popping. */ 82 83 #define RAWGET_BOOLEAN(_k) \ 84 lua_pushstring (L, #_k); \ 85 lua_rawget (L, -2); \ 86 if (!lua_isnil (L, -1)) { \ 87 _k = lua_toboolean (L, -1); \ 88 } \ 89 lua_pop (L, 1) 90 91 #define RAWGET_INTEGER(_k) \ 92 lua_pushstring (L, #_k); \ 93 lua_rawget (L, -2); \ 94 if (!lua_isnil (L, -1)) { \ 95 _k = lua_tointeger (L, -1); \ 96 } \ 97 lua_pop (L, 1) 98 99 #define RAWGET_STRING(_k) \ 100 lua_pushstring (L, #_k); \ 101 lua_rawget (L, -2); \ 102 if (!lua_isnil (L, -1)) { \ 103 _k = lua_tostring (L, -1); \ 104 } else { _k = NULL; } 105 106 #define RAWGET_STRDUP(_k) \ 107 lua_pushstring (L, #_k); \ 108 lua_rawget (L, -2); \ 109 if (!lua_isnil (L, -1)) { \ 110 _k = strdup (lua_tostring (L, -1)); \ 111 } else { _k = NULL; } 112 113 #define RAWGET_YAML_CHARP(_k) \ 114 lua_pushstring (L, #_k); \ 115 lua_rawget (L, -2); \ 116 if (!lua_isnil (L, -1)) { \ 117 _k = (yaml_char_t *) lua_tostring (L, -1); \ 118 } else { _k = NULL; } 119 120 #define RAWGETS_INTEGER(_v, _s) \ 121 lua_pushstring (L, _s); \ 122 lua_rawget (L, -2); \ 123 if (!lua_isnil (L, -1)) { \ 124 _v = lua_tointeger (L, -1); \ 125 } \ 126 lua_pop (L, 1) 127 128 #define RAWGETS_STRDUP( _v, _s) \ 129 lua_pushstring (L, _s); \ 130 lua_rawget (L, -2); \ 131 if (!lua_isnil (L, -1)) { \ 132 _v = (yaml_char_t *) strdup (lua_tostring (L, -1)); \ 133 } else { _v = NULL; } 134 135 #define RAWGET_PUSHTABLE(_k) \ 136 lua_pushstring (L, _k); \ 137 lua_rawget (L, -2); \ 138 if ((lua_type (L, -1) != LUA_TTABLE) && !lua_isnil (L, -1)) { \ 139 lua_pop (L, 1); \ 140 return luaL_error (L, "%s must be a table", _k); \ 141 } 142 143 #define ERROR_IFNIL(_err) \ 144 if (lua_isnil (L, -1)) { \ 145 emitter->error++; \ 146 luaL_addstring (&emitter->errbuff, _err); \ 147 } 148 149 150 /* from emitter.c */ 151 extern int Pemitter (lua_State *L); 152 153 /* from parser.c */ 154 extern void parser_init (lua_State *L); 155 extern int Pparser (lua_State *L); 156 157 /* from scanner.c */ 158 extern void scanner_init (lua_State *L); 159 extern int Pscanner (lua_State *L); 160 161 #endif 162