From e9ffe6dd0d0ec71e73ec39cdeddd09757fb242e6 Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Thu, 10 Jan 2013 17:55:12 +0100 Subject: [PATCH] parsers: split out from io library --- Makefile.in | 2 ++ configure | 2 +- configure.ac | 2 +- include/{lem-io.h => lem-parsers.h} | 6 ++-- lem/http.lua | 12 ++++--- lem/http/core.c | 2 +- lem/io.lua | 30 +++-------------- lem/io/core.c | 20 +----------- lem/parsers.lua | 48 ++++++++++++++++++++++++++++ lem/{io/parsers.c => parsers/core.c} | 30 ++++++++++++++++- 10 files changed, 99 insertions(+), 55 deletions(-) rename include/{lem-io.h => lem-parsers.h} (93%) create mode 100644 lem/parsers.lua rename lem/{io/parsers.c => parsers/core.c} (83%) diff --git a/Makefile.in b/Makefile.in index 7e928b6..f384a8f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -25,6 +25,7 @@ headers = @headers@ llibs = \ lem/repl.lua \ + lem/parsers.lua \ lem/io.lua \ lem/io/queue.lua \ lem/lfs.lua \ @@ -34,6 +35,7 @@ llibs = \ clibs = \ lem/utils.so \ + lem/parsers/core.so \ lem/io/core.so \ lem/lfs/core.so \ lem/http/core.so diff --git a/configure b/configure index 076551b..41b9432 100755 --- a/configure +++ b/configure @@ -2159,7 +2159,7 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu ac_config_headers="$ac_config_headers libev/ev-config.h:ev-config.h.in" -headers='lem.h lem-io.h' +headers='lem.h lem-parsers.h' objects='bin/lem.o' diff --git a/configure.ac b/configure.ac index 59f9ff0..b6b60b0 100644 --- a/configure.ac +++ b/configure.ac @@ -5,7 +5,7 @@ builtin_lua_version='5.2' AC_LANG(C) AC_CONFIG_HEADERS([libev/ev-config.h:ev-config.h.in]) -AC_SUBST([headers], ['lem.h lem-io.h']) +AC_SUBST([headers], ['lem.h lem-parsers.h']) AC_SUBST([objects], ['bin/lem.o']) AC_SUBST([SHARED], ['-shared']) AC_SUBST([CPPFLAGS_PRIVATE], ['-Iinclude']) diff --git a/include/lem-io.h b/include/lem-parsers.h similarity index 93% rename from include/lem-io.h rename to include/lem-parsers.h index 9ad2b48..bc4a782 100644 --- a/include/lem-io.h +++ b/include/lem-parsers.h @@ -1,6 +1,6 @@ /* * This file is part of LEM, a Lua Event Machine. - * Copyright 2011-2012 Emil Renner Berthing + * Copyright 2011-2013 Emil Renner Berthing * * LEM is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -16,8 +16,8 @@ * along with LEM. If not, see . */ -#ifndef _LEM_IO_H -#define _LEM_IO_H +#ifndef _LEM_PARSERS_H +#define _LEM_PARSERS_H #include diff --git a/lem/http.lua b/lem/http.lua index f754b09..190b80e 100644 --- a/lem/http.lua +++ b/lem/http.lua @@ -19,10 +19,14 @@ local io = require 'lem.io' local http = require 'lem.http.core' -io.parsers['HTTPRequest'] = http.HTTPRequest -http.HTTPRequest = nil -io.parsers['HTTPResponse'] = http.HTTPResponse -http.HTTPResponse = nil +do + local parsers = require 'lem.parsers' + + parsers.lookup['HTTPRequest'] = http.HTTPRequest + http.HTTPRequest = nil + parsers.lookup['HTTPResponse'] = http.HTTPResponse + http.HTTPResponse = nil +end local tonumber = tonumber local concat = table.concat diff --git a/lem/http/core.c b/lem/http/core.c index bfaa99b..ed4ef0e 100644 --- a/lem/http/core.c +++ b/lem/http/core.c @@ -16,7 +16,7 @@ * along with LEM. If not, see . */ -#include +#include #if !(LUA_VERSION_NUM >= 502) #define lua_getuservalue lua_getfenv diff --git a/lem/io.lua b/lem/io.lua index 4f7d31d..f6f5236 100644 --- a/lem/io.lua +++ b/lem/io.lua @@ -16,38 +16,18 @@ -- along with LEM. If not, see . -- -local utils = require 'lem.utils' -local io = require 'lem.io.core' +local utils = require 'lem.utils' +local io = require 'lem.io.core' local type = type local assert = assert local error = error do - local parsers = io.parsers - local parser_available = parsers.available - parsers.available = nil - local parser_target = parsers.target - parsers.target = nil - - function io.reader(readp) - return function(self, fmt, ...) - if fmt == nil then - return readp(self, parser_available) - end - if type(fmt) == 'number' then - return readp(self, parser_target, fmt) - end - local parser = parsers[fmt] - if parser == nil then - error('invalid format', 2) - end - return readp(self, parser, ...) - end - end + local parsers = require 'lem.parsers' - io.Stream.read = io.reader(io.Stream.readp) - io.File.read = io.reader(io.File.readp) + io.Stream.read = parsers.newreader(io.Stream.readp) + io.File.read = parsers.newreader(io.File.readp) end do diff --git a/lem/io/core.c b/lem/io/core.c index 83ac5bf..26552d3 100644 --- a/lem/io/core.c +++ b/lem/io/core.c @@ -36,7 +36,7 @@ #include #endif -#include +#include static int io_closed(lua_State *T) @@ -66,7 +66,6 @@ io_strerror(lua_State *T, int err) #include "stream.c" #include "server.c" #include "tcp.c" -#include "parsers.c" static int module_index(lua_State *T) @@ -362,23 +361,6 @@ luaopen_lem_io_core(lua_State *L) /* insert the tcp table */ lua_setfield(L, -2, "tcp"); - /* create parser table */ - lua_createtable(L, 0, 4); - /* push parser_line */ - lua_pushlightuserdata(L, (void *)&parser_available); - lua_setfield(L, -2, "available"); - /* push parser_target */ - lua_pushlightuserdata(L, (void *)&parser_target); - lua_setfield(L, -2, "target"); - /* push parser_all */ - lua_pushlightuserdata(L, (void *)&parser_all); - lua_setfield(L, -2, "*a"); - /* push parser_line */ - lua_pushlightuserdata(L, (void *)&parser_line); - lua_setfield(L, -2, "*l"); - /* insert parser table */ - lua_setfield(L, -2, "parsers"); - /* create metatable for the module */ lua_newtable(L); /* insert the index function */ diff --git a/lem/parsers.lua b/lem/parsers.lua new file mode 100644 index 0000000..cdf345c --- /dev/null +++ b/lem/parsers.lua @@ -0,0 +1,48 @@ +-- +-- This file is part of LEM, a Lua Event Machine. +-- Copyright 2011-2012 Emil Renner Berthing +-- +-- LEM is free software: you can redistribute it and/or +-- modify it under the terms of the GNU General Public License as +-- published by the Free Software Foundation, either version 3 of +-- the License, or (at your option) any later version. +-- +-- LEM is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with LEM. If not, see . +-- + +local parsers = require 'lem.parsers.core' + +local type = type +local error = error + +local lookup = parsers.lookup +local available = lookup.available +lookup.available = nil +local target = lookup.target +lookup.target = nil + +function parsers.newreader(readp) + return function(self, fmt, ...) + if fmt == nil then + return readp(self, available) + end + if type(fmt) == 'number' then + return readp(self, target, fmt) + end + local parser = lookup[fmt] + if parser == nil then + error('invalid format', 2) + end + return readp(self, parser, ...) + end +end + +return parsers + +-- vim: ts=2 sw=2 noet: diff --git a/lem/io/parsers.c b/lem/parsers/core.c similarity index 83% rename from lem/io/parsers.c rename to lem/parsers/core.c index 72cd671..8154381 100644 --- a/lem/io/parsers.c +++ b/lem/parsers/core.c @@ -1,6 +1,6 @@ /* * This file is part of LEM, a Lua Event Machine. - * Copyright 2011-2012 Emil Renner Berthing + * Copyright 2011-2013 Emil Renner Berthing * * LEM is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -16,6 +16,8 @@ * along with LEM. If not, see . */ +#include + /* * read available data */ @@ -173,3 +175,29 @@ static const struct lem_parser parser_line = { .init = parse_line_init, .process = parse_line_process, }; + +int +luaopen_lem_parsers_core(lua_State *L) +{ + /* create module table */ + lua_newtable(L); + + /* create lookup table */ + lua_createtable(L, 0, 4); + /* push parser_line */ + lua_pushlightuserdata(L, (void *)&parser_available); + lua_setfield(L, -2, "available"); + /* push parser_target */ + lua_pushlightuserdata(L, (void *)&parser_target); + lua_setfield(L, -2, "target"); + /* push parser_all */ + lua_pushlightuserdata(L, (void *)&parser_all); + lua_setfield(L, -2, "*a"); + /* push parser_line */ + lua_pushlightuserdata(L, (void *)&parser_line); + lua_setfield(L, -2, "*l"); + /* insert lookup table */ + lua_setfield(L, -2, "lookup"); + + return 1; +}