Skip to content

Commit fa07e41

Browse files
committed
fix (typecheck): hash parsing for entry key
1 parent 2322d2d commit fa07e41

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

crates/ltk_ritobin/src/typecheck/visitor.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ltk_meta::{
1313

1414
use crate::{
1515
parse::{
16-
cst::{self, visitor::Visit, Cst, Kind, Visitor},
16+
cst::{self, visitor::Visit, Child, Cst, Kind, Visitor},
1717
Span, Token, TokenKind,
1818
},
1919
typecheck::RitobinName,
@@ -624,6 +624,38 @@ pub fn resolve_entry(
624624

625625
let key = c.expect_tree(Kind::EntryKey)?;
626626

627+
let (key, key_span) = match key.children.first().ok_or(InvalidHash(key.span))? {
628+
Child::Token(Token {
629+
kind: TokenKind::String,
630+
span,
631+
}) => (
632+
PropertyValueEnum::from(values::String::from(&ctx.text[span])),
633+
*span,
634+
),
635+
Child::Token(Token {
636+
kind: TokenKind::HexLit,
637+
span,
638+
}) => {
639+
// TODO: better err here?
640+
(
641+
PropertyValueEnum::Hash(
642+
u32::from_str_radix(
643+
ctx.text[span]
644+
.strip_prefix("0x")
645+
.ok_or(InvalidHash(*span))?,
646+
16,
647+
)
648+
.map_err(|_| InvalidHash(*span))?
649+
.into(),
650+
),
651+
*span,
652+
)
653+
}
654+
_ => {
655+
return Err(InvalidHash(key.span).into());
656+
}
657+
};
658+
627659
let parent_value_kind = parent_value_kind
628660
.and_then(|p| p.value_subtype())
629661
.map(RitoType::simple);
@@ -654,7 +686,7 @@ pub fn resolve_entry(
654686
.unwrap(),
655687
);
656688
return Ok(IrEntry {
657-
key: values::String::new_with_meta(ctx.text[key.span].into(), key.span).into(),
689+
key,
658690
value: parent.make_default(value.span),
659691
});
660692
}
@@ -667,7 +699,7 @@ pub fn resolve_entry(
667699

668700
let value = match (kind, resolved_val) {
669701
(None, Some(value)) => value,
670-
(None, None) => return Err(MissingType(key.span).into()),
702+
(None, None) => return Err(MissingType(key_span).into()),
671703
(Some(kind), Some(ivalue)) => match ivalue.kind() == kind.base {
672704
true => ivalue,
673705
false => {
@@ -683,10 +715,7 @@ pub fn resolve_entry(
683715
(Some(kind), _) => kind.make_default(value_span),
684716
};
685717

686-
Ok(IrEntry {
687-
key: values::String::new_with_meta(ctx.text[key.span].into(), key.span).into(),
688-
value,
689-
})
718+
Ok(IrEntry { key, value })
690719
}
691720

692721
pub fn resolve_list(ctx: &mut Ctx, tree: &Cst) -> Result<(), Diagnostic> {

0 commit comments

Comments
 (0)