monotone

monotone Mtn Source Tree

Root/luaext_parse_basic_io.cc

1
2#include "base.hh"
3#include "lua.hh"
4
5#include "basic_io.hh"
6
7using std::vector;
8using std::string;
9using std::pair;
10using std::make_pair;
11
12LUAEXT(parse_basic_io, )
13{
14 vector<pair<string, vector<string> > > res;
15 const string str(luaL_checkstring(L, -1), lua_strlen(L, -1));
16 basic_io::input_source in(str, "monotone_parse_basic_io_for_lua");
17 basic_io::tokenizer tok(in);
18 try
19 {
20 string got;
21 basic_io::token_type tt;
22 do
23 {
24 tt = tok.get_token(got);
25 switch (tt)
26 {
27 case basic_io::TOK_SYMBOL:
28 res.push_back(make_pair(got, vector<string>()));
29 break;
30 case basic_io::TOK_STRING:
31 case basic_io::TOK_HEX:
32 E(!res.empty(), F("bad input to parse_basic_io"));
33 res.back().second.push_back(got);
34 break;
35 default:
36 break;
37 }
38 }
39 while (tt != basic_io::TOK_NONE);
40 }
41 catch (informative_failure & e)
42 {// there was a syntax error in our string
43 lua_pushnil(L);
44 return 1;
45 }
46 lua_newtable(L);
47 int n = 1;
48 for (vector<pair<string, vector<string> > >::const_iterator i = res.begin();
49 i != res.end(); ++i)
50 {
51 lua_newtable(L);
52 lua_pushstring(L, i->first.c_str());
53 lua_setfield(L, -2, "name");
54 lua_newtable(L);
55 int m = 1;
56 for (vector<string>::const_iterator j = i->second.begin();
57 j != i->second.end(); ++j)
58 {
59 lua_pushstring(L, j->c_str());
60 lua_rawseti(L, -2, m++);
61 }
62 lua_setfield(L, -2, "values");
63 lua_rawseti(L, -2, n++);
64 }
65 return 1;
66}
67
68
69// Local Variables:
70// mode: C++
71// fill-column: 76
72// c-file-style: "gnu"
73// indent-tabs-mode: nil
74// End:
75// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
76

Archive Download this file

Branches

Tags

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