2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local string_byte, string_char = string.byte, string.char;
10 local t_concat, t_insert = table.concat, table.insert;
11 local type, tonumber, tostring = type, tonumber, tostring;
15 local function read(expected)
18 ch = last; last = nil;
19 else ch = file:read(1); end
20 if expected and ch ~= expected then error("expected: "..expected.."; got: "..(ch or "nil")); end
23 local function pushback(ch)
24 if last then error(); end
28 if not last then last = read(); end
32 local _A, _a, _Z, _z, _0, _9, __, _at, _space, _minus = string_byte("AaZz09@_ -", 1, 10);
33 local function isLowerAlpha(ch)
34 ch = string_byte(ch) or 0;
35 return (ch >= _a and ch <= _z);
37 local function isNumeric(ch)
38 ch = string_byte(ch) or 0;
39 return (ch >= _0 and ch <= _9) or ch == _minus;
41 local function isAtom(ch)
42 ch = string_byte(ch) or 0;
43 return (ch >= _A and ch <= _Z) or (ch >= _a and ch <= _z) or (ch >= _0 and ch <= _9) or ch == __ or ch == _at;
45 local function isSpace(ch)
46 ch = string_byte(ch) or "x";
50 local escapes = {["\\b"]="\b", ["\\d"]="\d", ["\\e"]="\e", ["\\f"]="\f", ["\\n"]="\n", ["\\r"]="\r", ["\\s"]="\s", ["\\t"]="\t", ["\\v"]="\v", ["\\\""]="\"", ["\\'"]="'", ["\\\\"]="\\"};
51 local function readString()
52 read("\""); -- skip quote
59 if not escapes[slash] then error("Unknown escape sequence: "..slash); end
60 str[#str+1] = escapes[slash];
62 elseif ch == "\"" then
64 elseif ch == "\\" then
72 local function readAtom1()
73 local var = { read() };
74 while isAtom(peek()) do
79 local function readAtom2()
80 local str = { read("'") };
85 if ch == "'" and not slash then break; end
89 local function readNumber()
90 local num = { read() };
91 while isNumeric(peek()) do
94 return tonumber(t_concat(num));
97 local function readTuple()
99 local s = {}; -- string representation
100 read(); -- read {, or [, or <
102 local item = readItem();
103 if not item then break; end
104 if type(item) ~= "number" or item > 255 then
107 s[#s+1] = string_char(item);
111 read(); -- read }, or ], or >
118 local function readBinary()
120 local t = readTuple();
123 if type(t) == "string" then
124 -- binary is a list of integers
126 elseif type(t) == "table" then
128 -- binary contains string
138 readItem = function()
140 if ch == nil then return nil end
141 if ch == "{" or ch == "[" then
143 elseif isLowerAlpha(ch) then
145 elseif ch == "'" then
147 elseif isNumeric(ch) then
149 elseif ch == "\"" then
151 elseif ch == "<" then
153 elseif isSpace(ch) or ch == "," or ch == "|" then
157 --print("Unknown char: "..ch);
161 local function readChunk()
162 local x = readItem();
163 if x then read("."); end
166 local function readFile(filename)
167 file = io.open(filename);
168 if not file then error("File not found: "..filename); os.exit(0); end
170 local x = readChunk();
171 if not x and peek() then error("Invalid char: "..peek()); end
178 function parseFile(file)
179 return readFile(file);