Skip to content

Commit

Permalink
parsers: split out from io library
Browse files Browse the repository at this point in the history
  • Loading branch information
esmil committed Jan 10, 2013
1 parent 3ae59df commit e9ffe6d
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 55 deletions.
2 changes: 2 additions & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ headers = @headers@

llibs = \
lem/repl.lua \
lem/parsers.lua \
lem/io.lua \
lem/io/queue.lua \
lem/lfs.lua \
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
6 changes: 3 additions & 3 deletions include/lem-io.h → include/lem-parsers.h
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,8 +16,8 @@
* along with LEM. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _LEM_IO_H
#define _LEM_IO_H
#ifndef _LEM_PARSERS_H
#define _LEM_PARSERS_H

#include <lem.h>

Expand Down
12 changes: 8 additions & 4 deletions lem/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lem/http/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with LEM. If not, see <http://www.gnu.org/licenses/>.
*/

#include <lem-io.h>
#include <lem-parsers.h>

#if !(LUA_VERSION_NUM >= 502)
#define lua_getuservalue lua_getfenv
Expand Down
30 changes: 5 additions & 25 deletions lem/io.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,18 @@
-- along with LEM. If not, see <http://www.gnu.org/licenses/>.
--

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
Expand Down
20 changes: 1 addition & 19 deletions lem/io/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <sys/sendfile.h>
#endif

#include <lem-io.h>
#include <lem-parsers.h>

static int
io_closed(lua_State *T)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 */
Expand Down
48 changes: 48 additions & 0 deletions lem/parsers.lua
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
--

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:
30 changes: 29 additions & 1 deletion lem/io/parsers.c → lem/parsers/core.c
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,6 +16,8 @@
* along with LEM. If not, see <http://www.gnu.org/licenses/>.
*/

#include <lem-parsers.h>

/*
* read available data
*/
Expand Down Expand Up @@ -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;
}

0 comments on commit e9ffe6d

Please sign in to comment.