monotone

monotone Mtn Source Tree

Root/src/luaext_parse_basic_io.cc

1// Copyright (C) 2006 Timothy Brownawell <tbrownaw@gmail.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#include "base.hh"
11#include "lua.hh"
12
13#include "basic_io.hh"
14
15using std::vector;
16using std::string;
17using std::pair;
18using std::make_pair;
19
20LUAEXT(parse_basic_io, )
21{
22 // This has no notion of a 'stanza'. It assumes a 'line' is a symbol
23 // followed by one or more string or hex values. It returns a table of
24 // lines.
25 vector<pair<string, vector<string> > > res;
26#ifdef lua_strlen
27 const string str(luaL_checkstring(LS, -1), lua_strlen(LS, -1));
28#else
29 const string str(luaL_checkstring(LS, -1), lua_rawlen(LS, -1));
30#endif
31 basic_io::input_source in(str, "monotone_parse_basic_io_for_lua");
32 in.made_from = origin::user;
33 basic_io::tokenizer tok(in);
34 try
35 {
36 string got;
37 basic_io::token_type tt;
38 do
39 {
40 tt = tok.get_token(got);
41 switch (tt)
42 {
43 case basic_io::TOK_SYMBOL:
44 res.push_back(make_pair(got, vector<string>()));
45 break;
46 case basic_io::TOK_STRING:
47 case basic_io::TOK_HEX:
48 E(!res.empty(), origin::user, F("bad input to parse_basic_io"));
49 res.back().second.push_back(got);
50 break;
51 default:
52 break;
53 }
54 }
55 while (tt != basic_io::TOK_NONE);
56 }
57 catch (recoverable_failure & e)
58 {// there was a syntax error in our string
59 lua_pushnil(LS);
60 return 1;
61 }
62 lua_newtable(LS);
63 int n = 1;
64 for (vector<pair<string, vector<string> > >::const_iterator i = res.begin();
65 i != res.end(); ++i)
66 {
67 lua_newtable(LS);
68 lua_pushstring(LS, i->first.c_str());
69 lua_setfield(LS, -2, "name");
70 lua_newtable(LS);
71 int m = 1;
72 for (vector<string>::const_iterator j = i->second.begin();
73 j != i->second.end(); ++j)
74 {
75 lua_pushstring(LS, j->c_str());
76 lua_rawseti(LS, -2, m++);
77 }
78 lua_setfield(LS, -2, "values");
79 lua_rawseti(LS, -2, n++);
80 }
81 return 1;
82}
83
84// Local Variables:
85// mode: C++
86// fill-column: 76
87// c-file-style: "gnu"
88// indent-tabs-mode: nil
89// End:
90// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status