Annotate

tools/erlparse.lua @ 1523:841d61be198f

Remove version number from copyright headers
author Matthew Wild <mwild1@gmail.com>
date Fri, 10 Jul 2009 03:11:45 +0100
parent 896:2c0b9e3c11c3
child 1567:e052a3bdb8b5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
1 -- Prosody IM
760
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
4 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
6 -- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
7 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
9
482
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local file = nil;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local last = nil;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local function read(expected)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local ch;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 if last then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 ch = last; last = nil;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 else ch = file:read(1); end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 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
19 return ch;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 local function pushback(ch)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 if last then error(); end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 last = ch;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local function peek()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 if not last then last = read(); end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 return last;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 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
31 local function isAlpha(ch)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 ch = string.byte(ch) or 0;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 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
34 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 local function isNumeric(ch)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 ch = string.byte(ch) or 0;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 return (ch >= _0 and ch <= _9);
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 local function isVar(ch)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 ch = string.byte(ch) or 0;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 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
42 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 local function isSpace(ch)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 ch = string.byte(ch) or "x";
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 return ch <= _space;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 local function readString()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 read("\""); -- skip quote
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 local slash = nil;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 local str = "";
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 while true do
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 local ch = read();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 if ch == "\"" and not slash then break; end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 str = str..ch;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 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
58 return str;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 local function readSpecialString()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 read("<"); read("<"); -- read <<
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 local str = "";
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 if peek() == "\"" then
607
7d1493fc524a Fixed a variable redeclaration
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
64 str = readString();
482
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 elseif peek() ~= ">" then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 error();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 read(">"); read(">"); -- read >>
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 return str;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 local function readVar()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 local var = read();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 while isVar(peek()) do
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 var = var..read();
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 return var;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 local function readNumber()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 local num = read();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 while isNumeric(peek()) do
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 num = num..read();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 return tonumber(num);
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 local readItem = nil;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 local function readTuple()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 local t = {};
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 read(); -- read { or [
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 while true do
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 local item = readItem();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 if not item then break; end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 table.insert(t, item);
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 read(); -- read } or ]
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 return t;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 readItem = function()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 local ch = peek();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 if ch == nil then return nil end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 if ch == "{" or ch == "[" then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 return readTuple();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 elseif isAlpha(ch) then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 return readVar();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 elseif isNumeric(ch) then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 return readNumber();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 elseif ch == "\"" then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 return readString();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 elseif ch == "<" then
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 return readSpecialString();
489
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 482
diff changeset
110 elseif isSpace(ch) or ch == "," or ch == "|" then
482
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 read();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 return readItem();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 else
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 --print("Unknown char: "..ch);
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 return nil;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 local function readChunk()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 local x = readItem();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 if x then read("."); end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 return x;
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 local function readFile(filename)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 file = io.open(filename);
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 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
126 return function()
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 local x = readChunk();
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 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
129 return x;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 end;
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 module "erlparse"
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 function parseFile(file)
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 return readFile(file);
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 end
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138
b86082df0bc0 ejabberd db dump importer for Prosody
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 return _M;