Skip to content

Commit

Permalink
chore: don't tokenize extraneous stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
numToStr committed Oct 26, 2022
1 parent 3bd37b5 commit 287c1e2
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 83 deletions.
39 changes: 11 additions & 28 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,14 @@ impl Lexer {
.map(TagType::Export),
)));

let local = keyword("local").padded();
let func = keyword("function").padded();
let ret = keyword("return");
let assign = just('=').padded();

let dotted = choice((
ident()
.then(choice((just('.').to(Kind::Dot), just(':').to(Kind::Colon))))
.then(ident())
.map(|((prefix, scope), name)| (Some(prefix), scope, name)),
ident().map(|name| (None, Kind::Local, name)),
));
let dotted = ident()
.then(choice((just('.').to(Kind::Dot), just(':').to(Kind::Colon))))
.then(ident())
.map(|((prefix, scope), name)| (Some(prefix), scope, name));

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

Expand All @@ -279,29 +276,15 @@ impl Lexer {
newline().to(TagType::Comment(String::new())),
comment.map(TagType::Comment),
))),
local.ignore_then(choice((
func.clone().ignore_then(ident()).map(|name| TagType::Func {
name,
prefix: None,
kind: Kind::Local,
}),
ident().then_ignore(assign).map(|name| TagType::Expr {
name,
prefix: None,
kind: Kind::Local,
}),
))),
func.clone()
.ignore_then(dotted)
.map(|(prefix, kind, name)| TagType::Func { prefix, name, kind }),
choice((
expr.clone()
.then_ignore(func)
.map(|(prefix, kind, name)| TagType::Func { prefix, name, kind }),
expr.map(|(prefix, kind, name)| TagType::Expr { prefix, name, kind }),
)),
keyword("return")
.ignore_then(ident().padded())
expr.then(func.or_not())
.map(|((prefix, kind, name), is_func)| match is_func {
Some(_) => TagType::Func { prefix, name, kind },
None => TagType::Expr { prefix, name, kind },
}),
ret.ignore_then(ident().padded())
.then_ignore(end())
.map(TagType::Export),
till_eol.to(TagType::Skip),
Expand Down
21 changes: 16 additions & 5 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,13 @@ pub enum TagType {
pub enum Kind {
Dot,
Colon,
Local,
}

impl Kind {
pub fn as_char(&self) -> char {
match self {
Self::Dot => '.',
Self::Colon => ':',
Self::Local => '#',
}
}
}
Expand Down Expand Up @@ -192,9 +190,18 @@ impl Display for Ty {
Self::Userdata => f.write_str("userdata"),
Self::Lightuserdata => f.write_str("lightuserdata"),
Self::Ref(id) => f.write_str(id),
Self::Array(ty) => write!(f, "{ty}[]"),
Self::Array(ty) => {
f.write_str(&ty.to_string())?;
f.write_str("[]")
}
Self::Table(kv) => match kv {
Some((k, v)) => write!(f, "table<{k},{v}>"),
Some((k, v)) => {
f.write_str("table<")?;
f.write_str(&k.to_string())?;
f.write_str(",")?;
f.write_str(&v.to_string())?;
f.write_str(">")
}
None => f.write_str("table"),
},
Self::Fun(args, ret) => {
Expand All @@ -217,7 +224,11 @@ impl Display for Ty {
f.write_str(&list_like(kv))?;
f.write_str("}")
}
Self::Union(rhs, lhs) => write!(f, "{rhs}|{lhs}"),
Self::Union(rhs, lhs) => {
f.write_str(&rhs.to_string())?;
f.write_str("|")?;
f.write_str(&lhs.to_string())
}
}
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ impl LemmyHelp {
match ele {
Node::Export(..) => {}
Node::Func(mut func) => {
if func.is_public(&export) {
if func.prefix.left.as_deref() == Some(&export) {
if settings.prefix_func {
func.prefix.right = Some(module.to_owned());
}
self.nodes.push(Node::Func(func));
}
}
Node::Type(mut typ) => {
if typ.is_public(&export) {
if typ.prefix.left.as_deref() == Some(&export) {
if settings.prefix_type {
typ.prefix.right = Some(module.to_owned());
}
Expand Down
6 changes: 5 additions & 1 deletion src/parser/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ impl_parse!(Node, Option<Self>, {
});

impl Node {
fn init() -> impl Parser<TagType, Vec<Node>, Error = Simple<TagType>> {
Node::parse().repeated().flatten()
}

/// Creates stream of AST nodes from emmylua
///
/// ```
Expand All @@ -68,6 +72,6 @@ impl Node {
let tokens = Lexer::init().parse(src).unwrap();
let stream = Stream::from_iter(src.len()..src.len() + 1, tokens.into_iter());

Node::parse().repeated().flatten().parse(stream)
Node::init().parse(stream)
}
}
7 changes: 6 additions & 1 deletion src/parser/tags/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ impl_parse!(Field, {
})
.map(|(header, (scope, name, ty, desc))| {
let desc = match desc {
Some(d) => vec![d],
Some(d) => {
let mut new_desc = Vec::with_capacity(header.len() + 1);
new_desc.push(d);
new_desc.extend(header);
new_desc
}
None => header,
};

Expand Down
27 changes: 12 additions & 15 deletions src/parser/tags/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ impl_parse!(Param, {
.then(select! { TagType::Comment(x) => x }.repeated())
.map(|((name, ty, desc), extra)| {
let desc = match desc {
Some(d) => Vec::from([d])
.into_iter()
.chain(extra.into_iter())
.collect(),
Some(d) => {
let mut new_desc = Vec::with_capacity(extra.len() + 1);
new_desc.push(d);
new_desc.extend(extra);
new_desc
}
None => extra,
};
Self { name, ty, desc }
Expand All @@ -45,10 +47,12 @@ impl_parse!(Return, {
.then(select! { TagType::Comment(x) => x }.repeated())
.map(|((ty, name, desc), extra)| {
let desc = match desc {
Some(d) => Vec::from([d])
.into_iter()
.chain(extra.into_iter())
.collect(),
Some(d) => {
let mut new_desc = Vec::with_capacity(extra.len() + 1);
new_desc.push(d);
new_desc.extend(extra);
new_desc
}
None => extra,
};

Expand Down Expand Up @@ -94,10 +98,3 @@ impl_parse!(Func, {
},
)
});

impl Func {
#[inline]
pub fn is_public(&self, export: &str) -> bool {
self.kind != Kind::Local && self.prefix.left.as_deref() == Some(export)
}
}
7 changes: 0 additions & 7 deletions src/parser/tags/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,3 @@ impl_parse!(Type, {
},
)
});

impl Type {
#[inline]
pub fn is_public(&self, export: &str) -> bool {
self.kind != Kind::Local && self.prefix.left.as_deref() == Some(export)
}
}
14 changes: 4 additions & 10 deletions src/vimdoc/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,13 @@ impl ToDoc for ClassDoc {
let mut table = Table::new();

for field in &n.fields {
let n = match (s.expand_opt, &field.name) {
(true, Name::Req(n) | Name::Opt(n)) => format!("{{{n}}}"),
(false, n) => format!("{{{n}}}"),
};

let t = if s.expand_opt {
format!("(nil|{})", field.ty)
} else {
format!("({})", field.ty)
let (name, ty) = match (s.expand_opt, &field.name) {
(true, Name::Opt(n)) => (format!("{{{n}}}"), format!("(nil|{})", field.ty)),
(_, n) => (format!("{{{n}}}"), format!("({})", field.ty)),
};

if field.scope == Scope::Public {
table.add_row([n, t, field.desc.join("\n")]);
table.add_row([name, ty, field.desc.join("\n")]);
}
}

Expand Down
14 changes: 4 additions & 10 deletions src/vimdoc/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,12 @@ impl ToDoc for FuncDoc {
let mut table = Table::new();

for param in &n.params {
let n = match (s.expand_opt, &param.name) {
(true, Name::Req(n) | Name::Opt(n)) => format!("{{{n}}}"),
(false, n) => format!("{{{n}}}"),
let (name, ty) = match (s.expand_opt, &param.name) {
(true, Name::Opt(n)) => (format!("{{{n}}}"), format!("(nil|{})", param.ty)),
(_, n) => (format!("{{{n}}}"), format!("({})", param.ty)),
};

let t = if s.expand_opt {
format!("(nil|{})", param.ty)
} else {
format!("({})", param.ty)
};

table.add_row([n, t, param.desc.join("\n")]);
table.add_row([name, ty, param.desc.join("\n")]);
}

doc.push_str(&table.to_string());
Expand Down
12 changes: 8 additions & 4 deletions tests/with_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,12 @@ local M = {}
---@class HelloWorld
---@field message? string First message to the world
---@field private opts? table
---@field secret table Sauce
---Prints given value
---@param message? string
function M.echo(message)
---@param message string
---@param opts? table
function M.echo(message, opts)
return print(message)
end
Expand All @@ -168,13 +170,15 @@ HelloWorld *HelloWorld*
Fields: ~
{message} (nil|string) First message to the world
{secret} (table) Sauce
M.echo({message?}) *M.echo*
M.echo({message}, {opts?}) *M.echo*
Prints given value
Parameters: ~
{message} (nil|string)
{message} (string)
{opts} (nil|table)
"
Expand Down

0 comments on commit 287c1e2

Please sign in to comment.