Software /
code /
prosody
Annotate
tools/erlparse.lua @ 2367:22dc9005360f
net.server_event: Load ssl/socket libraries if not already available
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 17 Dec 2009 16:53:28 +0000 |
parent | 1783:f79972ad8965 |
child | 2923:b7049746bd29 |
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 | 5 -- This project is MIT/X11 licensed. Please see the |
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 |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
10 |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
11 local file = nil; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
12 local last = nil; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
13 local function read(expected) |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
14 local ch; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
15 if last then |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
16 ch = last; last = nil; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
17 else ch = file:read(1); end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
18 if expected and ch ~= expected then error("expected: "..expected.."; got: "..(ch or "nil")); end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
19 return ch; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
20 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
21 local function pushback(ch) |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
22 if last then error(); end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
23 last = ch; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
24 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
25 local function peek() |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
26 if not last then last = read(); end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
27 return last; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
28 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
29 |
1572
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
30 local _A, _a, _Z, _z, _0, _9, __, _at, _space = string.byte("AaZz09@_ ", 1, 9); |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
31 local function isLowerAlpha(ch) |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
32 ch = string.byte(ch) or 0; |
1572
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
33 return (ch >= _a and ch <= _z); |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
34 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
35 local function isNumeric(ch) |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
36 ch = string.byte(ch) or 0; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
37 return (ch >= _0 and ch <= _9); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
38 end |
1572
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
39 local function isAtom(ch) |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
40 ch = string.byte(ch) or 0; |
1572
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
41 return (ch >= _A and ch <= _Z) or (ch >= _a and ch <= _z) or (ch >= _0 and ch <= _9) or ch == __ or ch == _at; |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
42 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
43 local function isSpace(ch) |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
44 ch = string.byte(ch) or "x"; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
45 return ch <= _space; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
46 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
47 |
1783
f79972ad8965
ejabberd2prosody: Fixed escape code processing when parsing strings.
Waqas Hussain <waqas20@gmail.com>
parents:
1572
diff
changeset
|
48 local escapes = {["\\b"]="\b", ["\\d"]="\d", ["\\e"]="\e", ["\\f"]="\f", ["\\n"]="\n", ["\\r"]="\r", ["\\s"]="\s", ["\\t"]="\t", ["\\v"]="\v", ["\\\""]="\"", ["\\'"]="'", ["\\\\"]="\\"}; |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
49 local function readString() |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
50 read("\""); -- skip quote |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
51 local slash = nil; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
52 local str = ""; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
53 while true do |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
54 local ch = read(); |
1783
f79972ad8965
ejabberd2prosody: Fixed escape code processing when parsing strings.
Waqas Hussain <waqas20@gmail.com>
parents:
1572
diff
changeset
|
55 if slash then |
f79972ad8965
ejabberd2prosody: Fixed escape code processing when parsing strings.
Waqas Hussain <waqas20@gmail.com>
parents:
1572
diff
changeset
|
56 slash = slash..ch; |
f79972ad8965
ejabberd2prosody: Fixed escape code processing when parsing strings.
Waqas Hussain <waqas20@gmail.com>
parents:
1572
diff
changeset
|
57 if not escapes[slash] then error("Unknown escape sequence: "..slash); end |
f79972ad8965
ejabberd2prosody: Fixed escape code processing when parsing strings.
Waqas Hussain <waqas20@gmail.com>
parents:
1572
diff
changeset
|
58 str = str..escapes[slash]; |
f79972ad8965
ejabberd2prosody: Fixed escape code processing when parsing strings.
Waqas Hussain <waqas20@gmail.com>
parents:
1572
diff
changeset
|
59 slash = nil; |
f79972ad8965
ejabberd2prosody: Fixed escape code processing when parsing strings.
Waqas Hussain <waqas20@gmail.com>
parents:
1572
diff
changeset
|
60 elseif ch == "\"" then |
f79972ad8965
ejabberd2prosody: Fixed escape code processing when parsing strings.
Waqas Hussain <waqas20@gmail.com>
parents:
1572
diff
changeset
|
61 break; |
f79972ad8965
ejabberd2prosody: Fixed escape code processing when parsing strings.
Waqas Hussain <waqas20@gmail.com>
parents:
1572
diff
changeset
|
62 elseif ch == "\\" then |
f79972ad8965
ejabberd2prosody: Fixed escape code processing when parsing strings.
Waqas Hussain <waqas20@gmail.com>
parents:
1572
diff
changeset
|
63 slash = ch; |
f79972ad8965
ejabberd2prosody: Fixed escape code processing when parsing strings.
Waqas Hussain <waqas20@gmail.com>
parents:
1572
diff
changeset
|
64 else |
f79972ad8965
ejabberd2prosody: Fixed escape code processing when parsing strings.
Waqas Hussain <waqas20@gmail.com>
parents:
1572
diff
changeset
|
65 str = str..ch; |
f79972ad8965
ejabberd2prosody: Fixed escape code processing when parsing strings.
Waqas Hussain <waqas20@gmail.com>
parents:
1572
diff
changeset
|
66 end |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
67 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
68 return str; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
69 end |
1572
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
70 local function readAtom1() |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
71 local var = read(); |
1572
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
72 while isAtom(peek()) do |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
73 var = var..read(); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
74 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
75 return var; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
76 end |
1572
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
77 local function readAtom2() |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
78 local str = read("'"); |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
79 local slash = nil; |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
80 while true do |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
81 local ch = read(); |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
82 str = str..ch; |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
83 if ch == "'" and not slash then break; end |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
84 end |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
85 return str; |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
86 end |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
87 local function readNumber() |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
88 local num = read(); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
89 while isNumeric(peek()) do |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
90 num = num..read(); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
91 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
92 return tonumber(num); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
93 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
94 local readItem = nil; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
95 local function readTuple() |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
96 local t = {}; |
1572
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
97 local s = ""; -- string representation |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
98 read(); -- read {, or [, or < |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
99 while true do |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
100 local item = readItem(); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
101 if not item then break; end |
1572
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
102 if type(item) ~= type(0) or item > 255 then |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
103 s = nil; |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
104 elseif s then |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
105 s = s..string.char(item); |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
106 end |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
107 table.insert(t, item); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
108 end |
1572
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
109 read(); -- read }, or ], or > |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
110 if s and s ~= "" then |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
111 return s |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
112 else |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
113 return t |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
114 end; |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
115 end |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
116 local function readBinary() |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
117 read("<"); -- read < |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
118 local t = readTuple(); |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
119 read(">") -- read > |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
120 local ch = peek(); |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
121 if type(t) == type("") then |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
122 -- binary is a list of integers |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
123 return t; |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
124 elseif type(t) == type({}) then |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
125 if t[1] then |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
126 -- binary contains string |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
127 return t[1]; |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
128 else |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
129 -- binary is empty |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
130 return ""; |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
131 end; |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
132 else |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
133 error(); |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
134 end |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
135 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
136 readItem = function() |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
137 local ch = peek(); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
138 if ch == nil then return nil end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
139 if ch == "{" or ch == "[" then |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
140 return readTuple(); |
1572
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
141 elseif isLowerAlpha(ch) then |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
142 return readAtom1(); |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
143 elseif ch == "'" then |
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
144 return readAtom2(); |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
145 elseif isNumeric(ch) then |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
146 return readNumber(); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
147 elseif ch == "\"" then |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
148 return readString(); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
149 elseif ch == "<" then |
1572
1b87dfb76caa
ejabberd2prosody, erlparse: Add support for parsing non-ASCII strings and binaries, and atoms enclosed in single quotes
Sergei Golovan
parents:
1567
diff
changeset
|
150 return readBinary(); |
1567
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
151 elseif isSpace(ch) or ch == "," or ch == "|" then |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
152 read(); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
153 return readItem(); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
154 else |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
155 --print("Unknown char: "..ch); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
156 return nil; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
157 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
158 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
159 local function readChunk() |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
160 local x = readItem(); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
161 if x then read("."); end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
162 return x; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
163 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
164 local function readFile(filename) |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
165 file = io.open(filename); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
166 if not file then error("File not found: "..filename); os.exit(0); end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
167 return function() |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
168 local x = readChunk(); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
169 if not x and peek() then error("Invalid char: "..peek()); end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
170 return x; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
171 end; |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
172 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
173 |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
174 module "erlparse" |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
175 |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
176 function parseFile(file) |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
177 return readFile(file); |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
178 end |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
179 |
e052a3bdb8b5
ejabberd2prosody, erlparse.lua: Convert from Windows line endings (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
180 return _M; |