Annotate

tools/erlparse.lua @ 482:b86082df0bc0

ejabberd db dump importer for Prosody
author Waqas Hussain <waqas20@gmail.com>
date Sat, 29 Nov 2008 23:59:27 +0500
child 489:237cddb1a785
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
482
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local file = nil;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 local last = nil;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 local function read(expected)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local ch;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 if last then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 ch = last; last = nil;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 else ch = file:read(1); end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 if expected and ch ~= expected then error("expected: "..expected.."; got: "..(ch or "nil")); end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 return ch;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local function pushback(ch)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 if last then error(); end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 last = ch;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 local function peek()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 if not last then last = read(); end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 return last;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 local _A, _a, _Z, _z, _0, _9, __, _space = string.byte("AaZz09_ ", 1, 8);
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 local function isAlpha(ch)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 ch = string.byte(ch) or 0;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 return (ch >= _A and ch <= _Z) or (ch >= _a and ch <= _z);
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 local function isNumeric(ch)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 ch = string.byte(ch) or 0;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 return (ch >= _0 and ch <= _9);
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 local function isVar(ch)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 ch = string.byte(ch) or 0;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 return (ch >= _A and ch <= _Z) or (ch >= _a and ch <= _z) or (ch >= _0 and ch <= _9) or ch == __;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 local function isSpace(ch)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 ch = string.byte(ch) or "x";
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 return ch <= _space;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 local function readString()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 read("\""); -- skip quote
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 local slash = nil;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 local str = "";
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 while true do
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 local ch = read();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 if ch == "\"" and not slash then break; end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 str = str..ch;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 str = str:gsub("\\.", {["\\b"]="\b", ["\\d"]="\d", ["\\e"]="\e", ["\\f"]="\f", ["\\n"]="\n", ["\\r"]="\r", ["\\s"]="\s", ["\\t"]="\t", ["\\v"]="\v", ["\\\""]="\"", ["\\'"]="'", ["\\\\"]="\\"});
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 return str;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 local function readSpecialString()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 read("<"); read("<"); -- read <<
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 local str = "";
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 if peek() == "\"" then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 local str = readString();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 elseif peek() ~= ">" then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 error();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 read(">"); read(">"); -- read >>
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 return str;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 local function readVar()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 local var = read();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 while isVar(peek()) do
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 var = var..read();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 return var;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 local function readNumber()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 local num = read();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 while isNumeric(peek()) do
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 num = num..read();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 return tonumber(num);
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 local readItem = nil;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 local function readTuple()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 local t = {};
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 read(); -- read { or [
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 while true do
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 local item = readItem();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 if not item then break; end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 table.insert(t, item);
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 read(); -- read } or ]
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 return t;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 readItem = function()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 local ch = peek();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 if ch == nil then return nil end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 if ch == "{" or ch == "[" then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 return readTuple();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 elseif isAlpha(ch) then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 return readVar();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 elseif isNumeric(ch) then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 return readNumber();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 elseif ch == "\"" then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 return readString();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 elseif ch == "<" then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 return readSpecialString();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 elseif isSpace(ch) or ch == "," then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 read();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 return readItem();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 else
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 --print("Unknown char: "..ch);
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 return nil;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 local function readChunk()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 local x = readItem();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 if x then read("."); end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 return x;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 local function readFile(filename)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 file = io.open(filename);
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 if not file then error("File not found: "..filename); os.exit(0); end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 return function()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 local x = readChunk();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 if not x and peek() then error("Invalid char: "..peek()); end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 return x;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 end;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 module "erlparse"
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 function parseFile(file)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 return readFile(file);
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 return _M;