scansion/parser.lua

Sat, 06 Feb 2016 14:38:17 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 06 Feb 2016 14:38:17 +0000
changeset 77
38066f635004
parent 72
4fde8d4a4c76
child 78
54bb54fe9ed2
permissions
-rw-r--r--

Ignore shebangs at the top of a script

0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local function parse(data)
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local parsed = {
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 objects = {};
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 actions = {};
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 };
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local line_number = 0;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local last_object;
43
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
9 local annotation;
72
4fde8d4a4c76 scansion.parser: Whitespace fix
Matthew Wild <mwild1@gmail.com>
parents: 70
diff changeset
10
42
67d50889b7fe scansion.parser: Don't skip blank lines (otherwise line numbers get skewed)
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
11 for line in data:gmatch("([^\r\n]*)\r?\n") do
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 line_number = line_number + 1;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 if line:sub(1,1) == "[" then
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local obj_type, name, extra = line:match("^%[(%a+)%] (.+)$");
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 if parsed.objects[name] then
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 return nil, "Duplicate definition of "..name.." on line "..line_number;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 parsed.objects[name] = {
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 type = obj_type:lower();
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 name = name;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 defined_line = line_number;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 };
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 last_object = parsed.objects[name];
47
1cbc0d9d132d scansion.parser: Allow property names to contain underscore
Kim Alvefur <zash@zash.se>
parents: 44
diff changeset
25 elseif line:match("^%s+[%a_]+:.+$") then
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if not last_object then
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 return nil, "Line "..line_number.. "unexpected outside of an object definition";
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
47
1cbc0d9d132d scansion.parser: Allow property names to contain underscore
Kim Alvefur <zash@zash.se>
parents: 44
diff changeset
29 local k, v = line:match("^%s+([%a_]+):%s*(.+)$")
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 last_object[k] = v;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 elseif #parsed.actions > 0 and line:sub(1,1) == "\t" then
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 table.insert(parsed.actions[#parsed.actions].extra, line:sub(2));
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 elseif line:match("^%s*$") or line:match("^#") or line:match("^([/-])%1") then
70
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
34 -- Blank line or comment
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
35 local in_header = (next(parsed.objects) == nil) and (next(parsed.actions) == nil);
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
36 if in_header and #line > 0 then
77
38066f635004 Ignore shebangs at the top of a script
Matthew Wild <mwild1@gmail.com>
parents: 72
diff changeset
37 if not parsed.title and not line:match("^#!") then
70
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
38 parsed.title = line:gsub("^[#-]+%s*", "");
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
39 else
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
40 parsed.summary = (parsed.summary and parsed.summary.."\n" or "")..line:gsub("^[#-]+%s*", "");
43
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
41 end
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
42 else
70
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
43 -- Save as annotation for the following action
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
44 if #line == 0 then
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
45 if annotation then
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
46 annotation.closed = true;
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
47 end
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
48 elseif annotation or not line:match("^%-+$") then
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
49 if (not annotation) or annotation.closed then
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
50 annotation = { line };
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
51 else
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
52 table.insert(annotation, line);
2b62a714b5df scansion.parser: Read any comments at top of the file as script title (first line) and summary (following lines)
Matthew Wild <mwild1@gmail.com>
parents: 47
diff changeset
53 end
43
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
54 end
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
55 end
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 else
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 last_object = nil;
44
0dab1dc183c1 parser, objects.client: Experimental support for more liberal object names
Matthew Wild <mwild1@gmail.com>
parents: 43
diff changeset
58 local name, action, extra = line:match("^([^:]+) (%a+):?%s?(.*)$");
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 if not name then
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 return nil, "Unable to parse action on line "..line_number;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 if not parsed.objects[name] then
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 return nil, "The object '"..name.."' used on line "..line_number.." was not declared";
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 table.insert(parsed.actions, {
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 object_name = name;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 action = action:lower();
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 extra = {#extra>0 and extra or nil};
43
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
69 annotation = annotation and table.concat(annotation, "\n") or nil;
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 });
43
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
71 annotation = nil;
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 return parsed;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 return {
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 parse = parse;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 };

mercurial