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

Basic handling of relative type-paths to procs #266

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions src/dreamchecker/tests/static_type_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,44 @@ fn return_type() {
"##.trim();
check_errors_match(code, RETURN_TYPE_ERRORS);
}

pub const RELATIVE_PROC_PATHS_ERRORS: &[(u32, u16, &str)] = &[
(13, 17, "failed to resolve path .fake"),
];

// In particular we'll test the examples documented in SS13's callback.dm. None should error.
#[test]
fn relative_proc_paths() {
let code = r##"
/proc/global_one()
return /proc/global_two // absolute paths should always work

/proc/global_two()
return .global_one // global proc while in another global proc

/mob/proc/test()
return

/mob/proc/other()
var/proc_path
proc_path = .test // proc defined on current(src) object (when in a /proc/ and not an override)
proc_path = .fake // Doesn't exist, should error
return proc_path

/mob/subtype/test()
. = .proc/foo // Proc defined on current object (when in an override not a /proc/)
return ..()

/mob/subtype/proc/foo()
var/proc_path
proc_path = /proc/global_one // global proc while in a datum proc
proc_path = .test // Proc overridden at src
proc_path = .proc/other // Proc defined in parent
proc_path = .proc/test // Proc defined in parent and also overridden in current.
return proc_path

/mob/subtype/deepertype/proc/bar()
return .test // Proc overridden in parent type (but not current type)
"##.trim();
check_errors_match(code, RELATIVE_PROC_PATHS_ERRORS);
}
34 changes: 23 additions & 11 deletions src/dreammaker/objtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,27 @@ impl<'a> TypeRef<'a> {
}
}

// Get child node by name whether its a type, proc, or verb.
pub fn get_child_type_or_proc(self, name: &str) -> Option<NavigatePathResult<'a>> {
if let Some(child) = self.child(name) {
return Some(NavigatePathResult::Type(child));
}
if let Some(proc_ref) = self.get_proc(name) {
return Some(NavigatePathResult::ProcPath(proc_ref, ProcDeclKind::Proc));
}
return None
}

/// Navigate the tree according to the given path operator.
pub fn navigate(self, op: PathOp, name: &str) -> Option<TypeRef<'a>> {
pub fn navigate(self, op: PathOp, name: &str) -> Option<NavigatePathResult<'a>> {
match op {
// '/' always looks for a direct child
PathOp::Slash => self.child(name),
PathOp::Slash => self.get_child_type_or_proc(name),
// '.' looks for a child of us or of any of our parents
PathOp::Dot => {
let mut next = Some(self);
while let Some(current) = next {
if let Some(child) = current.child(name) {
if let Some(child) = current.get_child_type_or_proc(name) {
return Some(child);
}
next = current.parent_path();
Expand All @@ -314,7 +325,7 @@ impl<'a> TypeRef<'a> {
},
// ':' looks for a child of us or of any of our children
PathOp::Colon => {
if let Some(child) = self.child(name) {
if let Some(child) = self.get_child_type_or_proc(name) {
return Some(child);
}
for &idx in self.children.values() {
Expand All @@ -341,11 +352,7 @@ impl<'a> TypeRef<'a> {
let name = s.as_ref();
if let Some(current) = next {
// Check if it's `proc` or `verb` in the path.
// Note that this doesn't catch this confusing corner case:
// /proc/foo()
// /proc/bar()
// return .foo
// It also doesn't yet handle the difference between `:proc`,
// Note that this doesn't yet handle the difference between `:proc`,
// `.proc`, and `/proc`, treating everything as `.proc`.
if let Some(kind) = ProcDeclKind::from_name(s.as_ref()) {
if let Some((_, name)) = iter.next() {
Expand All @@ -360,12 +367,17 @@ impl<'a> TypeRef<'a> {
}

// Otherwise keep navigating as normal.
next = current.navigate(op, name.as_ref());
let next_step = current.navigate(op, name.as_ref());
match next_step {
Some(ret @ NavigatePathResult::Type(_)) => next = Some(ret.ty()),
Some(_) => return next_step,
None => next = None,
}
} else {
return None;
}
}
next.map(NavigatePathResult::Type)
return next.map(NavigatePathResult::Type)
}

/// Checks whether this type is a subtype of the given type.
Expand Down
4 changes: 2 additions & 2 deletions src/langserver/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use lsp_types::*;

use dm::ast::PathOp;
use dm::annotation::Annotation;
use dm::objtree::{TypeRef, TypeVar, TypeProc, ProcValue};
use dm::objtree::{TypeRef, NavigatePathResult, TypeVar, TypeProc, ProcValue};

use crate::{Engine, Span, is_constructor_name};
use crate::symbol_search::contains;
Expand Down Expand Up @@ -206,7 +206,7 @@ impl<'a> Engine<'a> {
decl = Some("verb");
break;
}
if let Some(next) = ty.navigate(op, name) {
if let Some(next) = ty.navigate(op, name).map(NavigatePathResult::ty) {
ty = next;
} else {
break;
Expand Down