monotone

monotone Mtn Source Tree

Root/luaext_parse_basic_io.cc

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

Archive Download this file

Branches

Tags

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