Skip to content

Commit 9a28c8a

Browse files
authored
chore: don't tokenize extraneous stuff (#57)
Also fixed a bug that slipped through in #56 :p
1 parent 3bd37b5 commit 9a28c8a

File tree

12 files changed

+77
-91
lines changed

12 files changed

+77
-91
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ To properly generate docs you should follow emmylua spec. The parser is capable
4040
Using the CLI is simple just give it the path to the lua files; it will parse them and prints the help doc to **stdout**
4141

4242
```bash
43-
lemmy-help /path/to/{first,second,third}.lua > doc.txt
43+
lemmy-help /path/to/{first,second,third}.lua > doc/PLUGIN_NAME.txt
4444
```
4545

4646
### Cli
@@ -55,17 +55,18 @@ ARGS:
5555
<FILES>... Path to the files
5656
5757
FLAGS:
58+
-h, --help Print help information
59+
-v, --version Print version information
5860
-M, --no-modeline Don't print modeline at the end
5961
-f, --prefix-func Prefix function name with ---@mod name
6062
-a, --prefix-alias Prefix ---@alias tag with return/---@mod name
6163
-c, --prefix-class Prefix ---@class tag with return/---@mod name
6264
-t, --prefix-type Prefix ---@type tag with ---@mod name
63-
-h, --help Print help information
64-
-v, --version Print version information
65+
--expand-opt Expand '?' (optional) to 'nil' type
6566
6667
USAGE:
67-
lemmy-help /path/to/first.lua /path/to/second.lua > doc.txt
68-
lemmy-help -c -a /path/to/{first,second,third}.lua > doc.txt
68+
lemmy-help /path/to/first.lua /path/to/second.lua > doc/PLUGIN_NAME.txt
69+
lemmy-help -c -a /path/to/{first,second,third}.lua > doc/PLUGIN_NAME.txt
6970
7071
NOTES:
7172
- The order of parsing + rendering is relative to the given files

src/cli.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ FLAGS:
104104
-a, --prefix-alias Prefix ---@alias tag with return/---@mod name
105105
-c, --prefix-class Prefix ---@class tag with return/---@mod name
106106
-t, --prefix-type Prefix ---@type tag with ---@mod name
107-
--expand-opt Expand ? (optional) to nil
107+
--expand-opt Expand '?' (optional) to 'nil' type
108108
109109
USAGE:
110-
{NAME} /path/to/first.lua /path/to/second.lua > doc.txt
111-
{NAME} -c -a /path/to/{{first,second,third}}.lua > doc.txt
110+
{NAME} /path/to/first.lua /path/to/second.lua > doc/PLUGIN_NAME.txt
111+
{NAME} -c -a /path/to/{{first,second,third}}.lua > doc/PLUGIN_NAME.txt
112112
113113
NOTES:
114114
- The order of parsing + rendering is relative to the given files

src/lexer.rs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,14 @@ impl Lexer {
258258
.map(TagType::Export),
259259
)));
260260

261-
let local = keyword("local").padded();
262261
let func = keyword("function").padded();
262+
let ret = keyword("return");
263263
let assign = just('=').padded();
264264

265-
let dotted = choice((
266-
ident()
267-
.then(choice((just('.').to(Kind::Dot), just(':').to(Kind::Colon))))
268-
.then(ident())
269-
.map(|((prefix, scope), name)| (Some(prefix), scope, name)),
270-
ident().map(|name| (None, Kind::Local, name)),
271-
));
265+
let dotted = ident()
266+
.then(choice((just('.').to(Kind::Dot), just(':').to(Kind::Colon))))
267+
.then(ident())
268+
.map(|((prefix, scope), name)| (Some(prefix), scope, name));
272269

273270
let expr = dotted.clone().then_ignore(assign);
274271

@@ -279,29 +276,15 @@ impl Lexer {
279276
newline().to(TagType::Comment(String::new())),
280277
comment.map(TagType::Comment),
281278
))),
282-
local.ignore_then(choice((
283-
func.clone().ignore_then(ident()).map(|name| TagType::Func {
284-
name,
285-
prefix: None,
286-
kind: Kind::Local,
287-
}),
288-
ident().then_ignore(assign).map(|name| TagType::Expr {
289-
name,
290-
prefix: None,
291-
kind: Kind::Local,
292-
}),
293-
))),
294279
func.clone()
295280
.ignore_then(dotted)
296281
.map(|(prefix, kind, name)| TagType::Func { prefix, name, kind }),
297-
choice((
298-
expr.clone()
299-
.then_ignore(func)
300-
.map(|(prefix, kind, name)| TagType::Func { prefix, name, kind }),
301-
expr.map(|(prefix, kind, name)| TagType::Expr { prefix, name, kind }),
302-
)),
303-
keyword("return")
304-
.ignore_then(ident().padded())
282+
expr.then(func.or_not())
283+
.map(|((prefix, kind, name), is_func)| match is_func {
284+
Some(_) => TagType::Func { prefix, name, kind },
285+
None => TagType::Expr { prefix, name, kind },
286+
}),
287+
ret.ignore_then(ident().padded())
305288
.then_ignore(end())
306289
.map(TagType::Export),
307290
till_eol.to(TagType::Skip),

src/lexer/token.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,13 @@ pub enum TagType {
110110
pub enum Kind {
111111
Dot,
112112
Colon,
113-
Local,
114113
}
115114

116115
impl Kind {
117116
pub fn as_char(&self) -> char {
118117
match self {
119118
Self::Dot => '.',
120119
Self::Colon => ':',
121-
Self::Local => '#',
122120
}
123121
}
124122
}
@@ -192,9 +190,18 @@ impl Display for Ty {
192190
Self::Userdata => f.write_str("userdata"),
193191
Self::Lightuserdata => f.write_str("lightuserdata"),
194192
Self::Ref(id) => f.write_str(id),
195-
Self::Array(ty) => write!(f, "{ty}[]"),
193+
Self::Array(ty) => {
194+
f.write_str(&ty.to_string())?;
195+
f.write_str("[]")
196+
}
196197
Self::Table(kv) => match kv {
197-
Some((k, v)) => write!(f, "table<{k},{v}>"),
198+
Some((k, v)) => {
199+
f.write_str("table<")?;
200+
f.write_str(&k.to_string())?;
201+
f.write_str(",")?;
202+
f.write_str(&v.to_string())?;
203+
f.write_str(">")
204+
}
198205
None => f.write_str("table"),
199206
},
200207
Self::Fun(args, ret) => {
@@ -217,7 +224,11 @@ impl Display for Ty {
217224
f.write_str(&list_like(kv))?;
218225
f.write_str("}")
219226
}
220-
Self::Union(rhs, lhs) => write!(f, "{rhs}|{lhs}"),
227+
Self::Union(rhs, lhs) => {
228+
f.write_str(&rhs.to_string())?;
229+
f.write_str("|")?;
230+
f.write_str(&lhs.to_string())
231+
}
221232
}
222233
}
223234
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ impl LemmyHelp {
114114
match ele {
115115
Node::Export(..) => {}
116116
Node::Func(mut func) => {
117-
if func.is_public(&export) {
117+
if func.prefix.left.as_deref() == Some(&export) {
118118
if settings.prefix_func {
119119
func.prefix.right = Some(module.to_owned());
120120
}
121121
self.nodes.push(Node::Func(func));
122122
}
123123
}
124124
Node::Type(mut typ) => {
125-
if typ.is_public(&export) {
125+
if typ.prefix.left.as_deref() == Some(&export) {
126126
if settings.prefix_type {
127127
typ.prefix.right = Some(module.to_owned());
128128
}

src/parser/node.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ impl_parse!(Node, Option<Self>, {
4545
});
4646

4747
impl Node {
48+
fn init() -> impl Parser<TagType, Vec<Node>, Error = Simple<TagType>> {
49+
Node::parse().repeated().flatten()
50+
}
51+
4852
/// Creates stream of AST nodes from emmylua
4953
///
5054
/// ```
@@ -68,6 +72,6 @@ impl Node {
6872
let tokens = Lexer::init().parse(src).unwrap();
6973
let stream = Stream::from_iter(src.len()..src.len() + 1, tokens.into_iter());
7074

71-
Node::parse().repeated().flatten().parse(stream)
75+
Node::init().parse(stream)
7276
}
7377
}

src/parser/tags/class.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ impl_parse!(Field, {
2323
})
2424
.map(|(header, (scope, name, ty, desc))| {
2525
let desc = match desc {
26-
Some(d) => vec![d],
26+
Some(d) => {
27+
let mut new_desc = Vec::with_capacity(header.len() + 1);
28+
new_desc.push(d);
29+
new_desc.extend(header);
30+
new_desc
31+
}
2732
None => header,
2833
};
2934

src/parser/tags/func.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ impl_parse!(Param, {
2121
.then(select! { TagType::Comment(x) => x }.repeated())
2222
.map(|((name, ty, desc), extra)| {
2323
let desc = match desc {
24-
Some(d) => Vec::from([d])
25-
.into_iter()
26-
.chain(extra.into_iter())
27-
.collect(),
24+
Some(d) => {
25+
let mut new_desc = Vec::with_capacity(extra.len() + 1);
26+
new_desc.push(d);
27+
new_desc.extend(extra);
28+
new_desc
29+
}
2830
None => extra,
2931
};
3032
Self { name, ty, desc }
@@ -45,10 +47,12 @@ impl_parse!(Return, {
4547
.then(select! { TagType::Comment(x) => x }.repeated())
4648
.map(|((ty, name, desc), extra)| {
4749
let desc = match desc {
48-
Some(d) => Vec::from([d])
49-
.into_iter()
50-
.chain(extra.into_iter())
51-
.collect(),
50+
Some(d) => {
51+
let mut new_desc = Vec::with_capacity(extra.len() + 1);
52+
new_desc.push(d);
53+
new_desc.extend(extra);
54+
new_desc
55+
}
5256
None => extra,
5357
};
5458

@@ -94,10 +98,3 @@ impl_parse!(Func, {
9498
},
9599
)
96100
});
97-
98-
impl Func {
99-
#[inline]
100-
pub fn is_public(&self, export: &str) -> bool {
101-
self.kind != Kind::Local && self.prefix.left.as_deref() == Some(export)
102-
}
103-
}

src/parser/tags/type.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,3 @@ impl_parse!(Type, {
4242
},
4343
)
4444
});
45-
46-
impl Type {
47-
#[inline]
48-
pub fn is_public(&self, export: &str) -> bool {
49-
self.kind != Kind::Local && self.prefix.left.as_deref() == Some(export)
50-
}
51-
}

src/vimdoc/class.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,13 @@ impl ToDoc for ClassDoc {
3030
let mut table = Table::new();
3131

3232
for field in &n.fields {
33-
let n = match (s.expand_opt, &field.name) {
34-
(true, Name::Req(n) | Name::Opt(n)) => format!("{{{n}}}"),
35-
(false, n) => format!("{{{n}}}"),
36-
};
37-
38-
let t = if s.expand_opt {
39-
format!("(nil|{})", field.ty)
40-
} else {
41-
format!("({})", field.ty)
33+
let (name, ty) = match (s.expand_opt, &field.name) {
34+
(true, Name::Opt(n)) => (format!("{{{n}}}"), format!("(nil|{})", field.ty)),
35+
(_, n) => (format!("{{{n}}}"), format!("({})", field.ty)),
4236
};
4337

4438
if field.scope == Scope::Public {
45-
table.add_row([n, t, field.desc.join("\n")]);
39+
table.add_row([name, ty, field.desc.join("\n")]);
4640
}
4741
}
4842

0 commit comments

Comments
 (0)