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 // This has no notion of a 'stanza'. It assumes a 'line' is a symbol
15 // followed by one or more string or hex values. It returns a table of
16 // lines.
17 vector<pair<string, vector<string> > > res;
18 const string str(luaL_checkstring(L, -1), lua_strlen(L, -1));
19 basic_io::input_source in(str, "monotone_parse_basic_io_for_lua");
20 basic_io::tokenizer tok(in);
21 try
22 {
23 string got;
24 basic_io::token_type tt;
25 do
26 {
27 tt = tok.get_token(got);
28 switch (tt)
29 {
30 case basic_io::TOK_SYMBOL:
31 res.push_back(make_pair(got, vector<string>()));
32 break;
33 case basic_io::TOK_STRING:
34 case basic_io::TOK_HEX:
35 E(!res.empty(), F("bad input to parse_basic_io"));
36 res.back().second.push_back(got);
37 break;
38 default:
39 break;
40 }
41 }
42 while (tt != basic_io::TOK_NONE);
43 }
44 catch (informative_failure & e)
45 {// there was a syntax error in our string
46 lua_pushnil(L);
47 return 1;
48 }
49 lua_newtable(L);
50 int n = 1;
51 for (vector<pair<string, vector<string> > >::const_iterator i = res.begin();
52 i != res.end(); ++i)
53 {
54 lua_newtable(L);
55 lua_pushstring(L, i->first.c_str());
56 lua_setfield(L, -2, "name");
57 lua_newtable(L);
58 int m = 1;
59 for (vector<string>::const_iterator j = i->second.begin();
60 j != i->second.end(); ++j)
61 {
62 lua_pushstring(L, j->c_str());
63 lua_rawseti(L, -2, m++);
64 }
65 lua_setfield(L, -2, "values");
66 lua_rawseti(L, -2, n++);
67 }
68 return 1;
69}
70
71
72// Local Variables:
73// mode: C++
74// fill-column: 76
75// c-file-style: "gnu"
76// indent-tabs-mode: nil
77// End:
78// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
79

Archive Download this file

Branches

Tags

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