Skip to content

Commit 02222d3

Browse files
committed
extract export list and top level function definitions
1 parent 00397fd commit 02222d3

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

bin/moon-tags

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,30 @@ whitespace = S"\t " -- not including newline
3333
ignore_line = Ct until_end -- tag it for empty line
3434

3535
-- we have to do this double Ct to capture both the full line and the grouped captures
36-
Line = (p) -> Ct C Ct Cg(Indent, "indent") * p
3736
Type = (name) -> Cg Cc(name), "type"
37+
Line = (type_name, p) -> Ct C Ct Cg(Indent, "indent") * p * Type type_name
38+
39+
class_line = Line "class", P"class" * whitespace^1 * Cg(literals.Name, "tag") * until_end
3840

39-
class_line = Line P"class" * whitespace^1 * Cg(literals.Name, "tag") * until_end * Type "class"
4041
-- TODO: support lapis style routes
4142
-- class_property = Line P("@")^-1 * Cg(literals.Name, "tag") * P":" * until_end * Type "property"
4243

4344
method = P { P"=>" + P(1 - literals.Stop) * V(1) }
44-
class_method = Line P("@")^-1 * Cg(literals.Name, "tag") * P":" * method * until_end * Type "method"
45+
func = P { P"->" + P"=>" + P(1 - literals.Stop) * V(1) }
46+
47+
-- this matches end-of-file return table convention for module files to figure
48+
-- out what names are exported
49+
export_list = Ct P"{" * P {
50+
P"}" + ((P":" * literals.Name) + (P(1) - P"}")) * V(1)
51+
}
52+
53+
eof_exports = P { export_list * S(" \t\r\n")^0 * P(-1) + P(1) * V(1) }
54+
55+
class_method = Line("method", P("@")^-1 * Cg(literals.Name, "tag") * P":" * method) * until_end
56+
function_def = Line("function", Cg(literals.Name, "tag") * whitespace^0 * P"=" * func) * until_end
4557

4658
parse_lines = Ct P {
47-
(class_line + class_method + ignore_line) * (P(-1) + literals.Break * V(1))
59+
(class_line + class_method + function_def + ignore_line) * (P(-1) + literals.Break * V(1))
4860
}
4961

5062
escape_tagaddress = (line_text) ->
@@ -54,6 +66,8 @@ escape_tagaddress = (line_text) ->
5466
for fname in *args.files
5567
file = assert io.open fname
5668
contents = assert file\read "*a"
69+
exports = {e, true for e in *eof_exports\match(contents) or {}}
70+
5771
lines = assert parse_lines\match contents
5872

5973
class_stack = {}
@@ -95,13 +109,25 @@ for fname in *args.files
95109
table.insert fields, 1, "line:#{line_no}"
96110

97111
switch properties.type
112+
when "function"
113+
if exports[properties.tag] and properties.indent == 0
114+
table.insert TAGS, {
115+
properties.tag
116+
fname
117+
-- note we don't use $ here
118+
"/^#{escape_tagaddress line_text}$/;\""
119+
"f"
120+
table.concat fields, " "
121+
}
122+
98123
when "method"
99124
if cls = find_class properties
100125
table.insert fields, "class:#{cls.tag}"
101126

102127
table.insert TAGS, {
103128
properties.tag
104129
fname
130+
-- note we don't use $ here
105131
"/^#{escape_tagaddress line_text}$/;\""
106132
"f"
107133
table.concat fields, " "

0 commit comments

Comments
 (0)