Skip to content

Commit 16c82f1

Browse files
authored
feat(types): add type annotation support (#31)
1 parent e242613 commit 16c82f1

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

Diff for: src/document.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ foo 1 2 \"three\" null true bar=\"baz\" {
254254
- 1
255255
- 2
256256
- \"three\"
257-
something \"else\"\r
257+
(mytype)something (\"name\")\"else\"\r
258258
}
259259
260260
null_id null_prop=null

Diff for: src/entry.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{KdlError, KdlErrorKind, KdlIdentifier, KdlValue};
1111
#[derive(Debug, Clone, PartialEq)]
1212
pub struct KdlEntry {
1313
pub(crate) leading: Option<String>,
14-
pub(crate) ty: Option<String>,
14+
pub(crate) ty: Option<KdlIdentifier>,
1515
pub(crate) value: KdlValue,
1616
pub(crate) value_repr: Option<String>,
1717
pub(crate) name: Option<KdlIdentifier>,
@@ -109,7 +109,7 @@ impl Display for KdlEntry {
109109
write!(f, "{}", leading)?;
110110
}
111111
if let Some(ty) = &self.ty {
112-
write!(f, "{}", ty)?;
112+
write!(f, "({})", ty)?;
113113
}
114114
if let Some(name) = &self.name {
115115
write!(f, "{}=", name)?;

Diff for: src/node.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{KdlDocument, KdlEntry, KdlError, KdlErrorKind, KdlIdentifier, KdlVal
1414
#[derive(Debug, Clone, PartialEq)]
1515
pub struct KdlNode {
1616
pub(crate) leading: Option<String>,
17-
pub(crate) ty: Option<String>,
17+
pub(crate) ty: Option<KdlIdentifier>,
1818
pub(crate) name: KdlIdentifier,
1919
// TODO: consider using `hashlink` for this instead, later.
2020
pub(crate) entries: Vec<KdlEntry>,
@@ -52,6 +52,20 @@ impl KdlNode {
5252
self.name = name.into();
5353
}
5454

55+
/// Gets the node's type, if any.
56+
pub fn ty(&self) -> Option<&KdlIdentifier> {
57+
self.ty.as_ref()
58+
}
59+
60+
/// Gets a mutable reference to the node's type.
61+
pub fn ty_mut(&mut self) -> &mut Option<KdlIdentifier> {
62+
&mut self.ty
63+
}
64+
65+
pub fn set_ty(&mut self, ty: impl Into<KdlIdentifier>) {
66+
self.ty = Some(ty.into());
67+
}
68+
5569
/// Returns a reference to this node's entries (arguments and properties).
5670
pub fn entries(&self) -> &[KdlEntry] {
5771
&self.entries

Diff for: src/parser.rs

+13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub(crate) fn document(input: &str) -> IResult<&str, KdlDocument, KdlParseError<
2323

2424
pub(crate) fn node(input: &str) -> IResult<&str, KdlNode, KdlParseError<&str>> {
2525
let (input, leading) = all_whitespace(input)?;
26+
let (input, ty) = opt(annotation)(input)?;
2627
let (input, name) = identifier(input)?;
2728
let (input, entries) = many0(entry)(input)?;
2829
let (input, children) = opt(children)(input)?;
@@ -33,6 +34,7 @@ pub(crate) fn node(input: &str) -> IResult<&str, KdlNode, KdlParseError<&str>> {
3334
let mut node = KdlNode::new(name);
3435
node.set_leading(leading);
3536
node.set_trailing(trailing);
37+
node.ty = ty;
3638
let ents = node.entries_mut();
3739
*ents = entries;
3840
if let Some((before, children)) = children {
@@ -70,19 +72,23 @@ pub(crate) fn entry(input: &str) -> IResult<&str, KdlEntry, KdlParseError<&str>>
7072

7173
fn property(input: &str) -> IResult<&str, KdlEntry, KdlParseError<&str>> {
7274
let (input, leading) = recognize(many0(node_space))(input)?;
75+
let (input, ty) = opt(annotation)(input)?;
7376
let (input, name) = identifier(input)?;
7477
let (input, _) = tag("=")(input)?;
7578
let (input, (raw, value)) = value(input)?;
7679
let mut entry = KdlEntry::new_prop(name, value);
80+
entry.ty = ty;
7781
entry.set_leading(if leading.is_empty() { " " } else { leading });
7882
entry.set_value_repr(raw);
7983
Ok((input, entry))
8084
}
8185

8286
fn argument(input: &str) -> IResult<&str, KdlEntry, KdlParseError<&str>> {
8387
let (input, leading) = recognize(many0(node_space))(input)?;
88+
let (input, ty) = opt(annotation)(input)?;
8489
let (input, (raw, value)) = value(input)?;
8590
let mut entry = KdlEntry::new(value);
91+
entry.ty = ty;
8692
entry.set_leading(if leading.is_empty() { " " } else { leading });
8793
entry.set_value_repr(raw);
8894
Ok((input, entry))
@@ -110,6 +116,13 @@ fn children(input: &str) -> IResult<&str, (&str, KdlDocument), KdlParseError<&st
110116
Ok((input, (before, children)))
111117
}
112118

119+
fn annotation(input: &str) -> IResult<&str, KdlIdentifier, KdlParseError<&str>> {
120+
let (input, _) = tag("(")(input)?;
121+
let (input, ty) = identifier(input)?;
122+
let (input, _) = tag(")")(input)?;
123+
Ok((input, ty))
124+
}
125+
113126
fn all_whitespace(input: &str) -> IResult<&str, &str, KdlParseError<&str>> {
114127
recognize(many0(alt((comment, unicode_space, newline))))(input)
115128
}

0 commit comments

Comments
 (0)