scansion/parser.lua

changeset 43
b37504fa3031
parent 42
67d50889b7fe
child 44
0dab1dc183c1
equal deleted inserted replaced
42:67d50889b7fe 43:b37504fa3031
4 actions = {}; 4 actions = {};
5 }; 5 };
6 6
7 local line_number = 0; 7 local line_number = 0;
8 local last_object; 8 local last_object;
9 local annotation;
9 10
10 for line in data:gmatch("([^\r\n]*)\r?\n") do 11 for line in data:gmatch("([^\r\n]*)\r?\n") do
11 line_number = line_number + 1; 12 line_number = line_number + 1;
12 if line:sub(1,1) == "[" then 13 if line:sub(1,1) == "[" then
13 local obj_type, name, extra = line:match("^%[(%a+)%] (.+)$"); 14 local obj_type, name, extra = line:match("^%[(%a+)%] (.+)$");
28 local k, v = line:match("^%s+(%a+):%s*(.+)$") 29 local k, v = line:match("^%s+(%a+):%s*(.+)$")
29 last_object[k] = v; 30 last_object[k] = v;
30 elseif #parsed.actions > 0 and line:sub(1,1) == "\t" then 31 elseif #parsed.actions > 0 and line:sub(1,1) == "\t" then
31 table.insert(parsed.actions[#parsed.actions].extra, line:sub(2)); 32 table.insert(parsed.actions[#parsed.actions].extra, line:sub(2));
32 elseif line:match("^%s*$") or line:match("^#") or line:match("^([/-])%1") then 33 elseif line:match("^%s*$") or line:match("^#") or line:match("^([/-])%1") then
33 -- Blank line or comment 34 -- Blank line or comment, save as annotation
35 if #line == 0 then
36 if annotation then
37 annotation.closed = true;
38 end
39 else
40 if (not annotation) or annotation.closed then
41 annotation = { line };
42 else
43 table.insert(annotation, line);
44 end
45 end
34 else 46 else
35 last_object = nil; 47 last_object = nil;
36 local name, action, extra = line:match("^(%a+) (%a+):?%s?(.*)$"); 48 local name, action, extra = line:match("^(%a+) (%a+):?%s?(.*)$");
37 if not name then 49 if not name then
38 return nil, "Unable to parse action on line "..line_number; 50 return nil, "Unable to parse action on line "..line_number;
42 end 54 end
43 table.insert(parsed.actions, { 55 table.insert(parsed.actions, {
44 object_name = name; 56 object_name = name;
45 action = action:lower(); 57 action = action:lower();
46 extra = {#extra>0 and extra or nil}; 58 extra = {#extra>0 and extra or nil};
59 annotation = annotation and table.concat(annotation, "\n") or nil;
47 }); 60 });
61 annotation = nil;
48 end 62 end
49 end 63 end
50 return parsed; 64 return parsed;
51 end 65 end
52 66

mercurial