Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support identifier enum variant #70

Merged
merged 1 commit into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions emmylua.md
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,8 @@ You can define a (pseudo) enum using [`---@alias`](#alias).

```lua
---@alias <name> <type>
---| '<value>' [# description]
---| '<literal>' [# description]
---| `<identifier>` [# description]
```

- Input
Expand All @@ -660,7 +661,7 @@ local U = {}
---| '"line"' # Vertical motion
---| '"char"' # Horizontal motion
---| 'v'
---| 'V' # Visual Line Mode
---| `some.ident` # Some identifier

---Global vim mode
---@type VMode
Expand All @@ -678,10 +679,10 @@ VMode *VMode*
Read `:h map-operator`

Variants: ~
("line") Vertical motion
("char") Horizontal motion
(v)
(V) Visual Line Mode
("line") Vertical motion
("char") Horizontal motion
("v")
(some.ident) Some identifier


U.VMODE *U.VMODE*
Expand Down
18 changes: 13 additions & 5 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ impl Lexer {
)))
.ignored();

let union_literal = just('\'')
.ignore_then(filter(|c| c != &'\'').repeated())
.then_ignore(just('\''))
.collect();
let union_literal = choice((
just('\'')
.ignore_then(filter(|c| c != &'\'').repeated())
.then_ignore(just('\''))
.collect()
.map(Member::Literal),
just('`')
.ignore_then(filter(|c| c != &'`').repeated())
.then_ignore(just('`'))
.collect()
.map(Member::Ident),
));

let variant = just('|')
.then_ignore(space)
Expand Down Expand Up @@ -149,7 +157,7 @@ impl Lexer {
.delimited_by(just('(').padded(), just(')').padded());

// Union of string literals: '"g@"'|'"g@$"'
let string_literal = union_literal.map(Ty::Ref);
let string_literal = union_literal.map(Ty::Member);

choice((
array_union(any, inner.clone()),
Expand Down
28 changes: 26 additions & 2 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
use std::fmt::Display;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Member {
Literal(String),
Ident(String),
}

impl Display for Member {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Literal(lit) => f.write_str(&format!(
r#""{}""#,
lit.trim_start_matches('"').trim_end_matches('"')
)),
Self::Ident(ident) => f.write_str(ident),
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TagType {
/// ```lua
Expand Down Expand Up @@ -63,9 +81,13 @@ pub enum TagType {
/// ```
Alias(String, Option<Ty>),
/// ```lua
/// ---| '<value>' [# description]
/// ---| '<literal>' [# description]
///
/// -- or
///
/// ---| `<ident>` [# description]
/// ```
Variant(String, Option<String>),
Variant(Member, Option<String>),
/// ```lua
/// ---@type <type> [desc]
/// ```
Expand Down Expand Up @@ -166,6 +188,7 @@ pub enum Ty {
Userdata,
Lightuserdata,
Ref(String),
Member(Member),
Array(Box<Ty>),
Table(Option<(Box<Ty>, Box<Ty>)>),
Fun(Vec<(Name, Ty)>, Option<Vec<Ty>>),
Expand Down Expand Up @@ -234,6 +257,7 @@ impl Display for Ty {
f.write_str("|")?;
f.write_str(&lhs.to_string())
}
Self::Member(mem) => mem.fmt(f),
}
}
}
4 changes: 2 additions & 2 deletions src/parser/tags/alias.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use chumsky::{prelude::choice, select, Parser};

use crate::{
lexer::{TagType, Ty},
lexer::{Member, TagType, Ty},
parser::{impl_parse, Prefix},
};

#[derive(Debug, Clone)]
pub enum AliasKind {
Type(Ty),
Enum(Vec<(String, Option<String>)>),
Enum(Vec<(Member, Option<String>)>),
}

#[derive(Debug, Clone)]
Expand Down
10 changes: 5 additions & 5 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ fn alias_and_type() {
---| '"line"' # Vertical motion
---| '"char"' # Horizontal motion
---| 'v'
---| 'V' # Visual Line Mode
---| `some.ident` # Some identifier

---Returns all the content of the buffer
---@return Lines
Expand Down Expand Up @@ -619,10 +619,10 @@ VMode *VMode*
Read `:h map-operator`

Variants: ~
("line") Vertical motion
("char") Horizontal motion
(v)
(V) Visual Line Mode
("line") Vertical motion
("char") Horizontal motion
("v")
(some.ident) Some identifier


U.get_all() *U.get_all*
Expand Down
9 changes: 6 additions & 3 deletions tests/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chumsky::Parser;
use lemmy_help::lexer::{Lexer, Name, Ty};
use lemmy_help::lexer::{Lexer, Member, Name, Ty};

macro_rules! b {
($t:expr) => {
Expand Down Expand Up @@ -201,10 +201,13 @@ fn types() {
check!(
r#"'"g@"'|string[]|'"g@$"'|number"#,
Ty::Union(
b!(Ty::Ref(r#""g@""#.into())),
b!(Ty::Member(Member::Literal(r#""g@""#.into()))),
b!(Ty::Union(
b!(Ty::Array(b!(Ty::String))),
b!(Ty::Union(b!(Ty::Ref(r#""g@$""#.into())), b!(Ty::Number)))
b!(Ty::Union(
b!(Ty::Member(Member::Literal(r#""g@$""#.into()))),
b!(Ty::Number)
))
))
)
);
Expand Down