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