scansion/parser.lua

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

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 06 Feb 2016 14:38:40 +0000
changeset 79
63d573be3f91
parent 78
54bb54fe9ed2
child 147
db39e8e9146c
permissions
-rw-r--r--

scansion.parser: Remove unused variable (thanks luacheck)

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
79
63d573be3f91 scansion.parser: Remove unused variable (thanks luacheck)
Matthew Wild <mwild1@gmail.com>
parents: 78
diff changeset
14 local obj_type, name = line:match("^%[(%a+)%] (.+)$");
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
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*", "");
78
54bb54fe9ed2 main, scansion.parser: Allow scripts to include tags in comments at the beginning, which are included in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 77
diff changeset
39 elseif line:match("^##") then
54bb54fe9ed2 main, scansion.parser: Allow scripts to include tags in comments at the beginning, which are included in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 77
diff changeset
40 if not parsed.tags then
54bb54fe9ed2 main, scansion.parser: Allow scripts to include tags in comments at the beginning, which are included in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 77
diff changeset
41 parsed.tags = {};
54bb54fe9ed2 main, scansion.parser: Allow scripts to include tags in comments at the beginning, which are included in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 77
diff changeset
42 end
54bb54fe9ed2 main, scansion.parser: Allow scripts to include tags in comments at the beginning, which are included in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 77
diff changeset
43 local tag = line:gsub("^##%s*", "");
54bb54fe9ed2 main, scansion.parser: Allow scripts to include tags in comments at the beginning, which are included in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 77
diff changeset
44 local k, v = tag:match("^([^:]+):%s*(.+)$");
54bb54fe9ed2 main, scansion.parser: Allow scripts to include tags in comments at the beginning, which are included in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 77
diff changeset
45 if k then
54bb54fe9ed2 main, scansion.parser: Allow scripts to include tags in comments at the beginning, which are included in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 77
diff changeset
46 -- Tag format: ## tagkey:tagvalue
54bb54fe9ed2 main, scansion.parser: Allow scripts to include tags in comments at the beginning, which are included in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 77
diff changeset
47 parsed.tags[k] = v;
54bb54fe9ed2 main, scansion.parser: Allow scripts to include tags in comments at the beginning, which are included in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 77
diff changeset
48 else
54bb54fe9ed2 main, scansion.parser: Allow scripts to include tags in comments at the beginning, which are included in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 77
diff changeset
49 -- Tag format: ## tagfoobar
54bb54fe9ed2 main, scansion.parser: Allow scripts to include tags in comments at the beginning, which are included in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 77
diff changeset
50 parsed.tags[tag] = true;
54bb54fe9ed2 main, scansion.parser: Allow scripts to include tags in comments at the beginning, which are included in JSON output
Matthew Wild <mwild1@gmail.com>
parents: 77
diff changeset
51 end
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
52 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
53 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
54 end
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
55 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
56 -- 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
57 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
58 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
59 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
60 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
61 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
62 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
63 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
64 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
65 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
66 end
43
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
67 end
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
68 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
69 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
70 last_object = nil;
44
0dab1dc183c1 parser, objects.client: Experimental support for more liberal object names
Matthew Wild <mwild1@gmail.com>
parents: 43
diff changeset
71 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
72 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
73 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
74 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
75 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
76 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
77 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
78 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
79 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
80 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
81 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
82 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
83 });
43
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
84 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
85 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
86 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
87 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
88 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
89
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
90 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
91 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
92 };

mercurial