scansion/parser.lua

Wed, 28 Oct 2015 02:11:36 +0100

author
Kim Alvefur <zash@zash.se>
date
Wed, 28 Oct 2015 02:11:36 +0100
changeset 47
1cbc0d9d132d
parent 44
0dab1dc183c1
child 70
2b62a714b5df
permissions
-rw-r--r--

scansion.parser: Allow property names to contain underscore

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;
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
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
43
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
34 -- Blank line or comment, save as annotation
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
35 if #line == 0 then
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
36 if annotation then
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
37 annotation.closed = true;
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
38 end
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
39 else
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
40 if (not annotation) or annotation.closed then
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
41 annotation = { line };
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
42 else
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
43 table.insert(annotation, line);
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
44 end
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
45 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
46 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
47 last_object = nil;
44
0dab1dc183c1 parser, objects.client: Experimental support for more liberal object names
Matthew Wild <mwild1@gmail.com>
parents: 43
diff changeset
48 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
49 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
50 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
51 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
52 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
53 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
54 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
55 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
56 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
57 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
58 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
59 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
60 });
43
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
61 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
62 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
63 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
64 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
65 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
66
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 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
68 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
69 };

mercurial