1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
//! Implementation of scopes for WDL documents.

use std::fmt;
use std::sync::Arc;

use indexmap::IndexMap;
use petgraph::graph::NodeIndex;
use rowan::GreenNode;
use url::Url;
use wdl_ast::support::token;
use wdl_ast::v1::StructDefinition;
use wdl_ast::Ast;
use wdl_ast::AstNode;
use wdl_ast::AstToken;
use wdl_ast::Diagnostic;
use wdl_ast::Span;
use wdl_ast::SyntaxElement;
use wdl_ast::SyntaxKind;
use wdl_ast::SyntaxNode;
use wdl_ast::ToSpan;

use crate::graph::DocumentGraph;
use crate::graph::ParseState;
use crate::Type;
use crate::Types;

mod v1;

/// Represents the context of a name for diagnostic reporting.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum NameContext {
    /// The name is a workflow name.
    Workflow(Span),
    /// The name is a task name.
    Task(Span),
    /// The name is a struct name.
    Struct(Span),
    /// The name is a struct member name.
    StructMember(Span),
    /// A name local to a scope.
    Scoped(ScopedNameContext),
}

impl NameContext {
    /// Gets the span of the name.
    fn span(&self) -> Span {
        match self {
            Self::Workflow(s) => *s,
            Self::Task(s) => *s,
            Self::Struct(s) => *s,
            Self::StructMember(s) => *s,
            Self::Scoped(n) => n.span(),
        }
    }
}

impl fmt::Display for NameContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Workflow(_) => write!(f, "workflow"),
            Self::Task(_) => write!(f, "task"),
            Self::Struct(_) => write!(f, "struct"),
            Self::StructMember(_) => write!(f, "struct member"),
            Self::Scoped(n) => n.fmt(f),
        }
    }
}

/// Represents the context of a name in a workflow or task scope.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScopedNameContext {
    /// The name was introduced by an task or workflow input.
    Input(Span),
    /// The name was introduced by an task or workflow output.
    Output(Span),
    /// The name was introduced by a private declaration.
    Decl(Span),
    /// The name was introduced by a workflow call statement.
    Call(Span),
    /// The name was introduced by a variable in workflow scatter statement.
    ScatterVariable(Span),
}

impl ScopedNameContext {
    /// Gets the span of the name.
    pub fn span(&self) -> Span {
        match self {
            Self::Input(s) => *s,
            Self::Output(s) => *s,
            Self::Decl(s) => *s,
            Self::Call(s) => *s,
            Self::ScatterVariable(s) => *s,
        }
    }
}

impl fmt::Display for ScopedNameContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Input(_) => write!(f, "input"),
            Self::Output(_) => write!(f, "output"),
            Self::Decl(_) => write!(f, "declaration"),
            Self::Call(_) => write!(f, "call"),
            Self::ScatterVariable(_) => write!(f, "scatter variable"),
        }
    }
}

impl From<ScopedNameContext> for NameContext {
    fn from(context: ScopedNameContext) -> Self {
        Self::Scoped(context)
    }
}

/// Represents a name in a task or workflow scope.
#[derive(Debug, Clone)]
pub struct ScopedName {
    /// The context of the name.
    context: ScopedNameContext,
    /// The CST node that introduced the name.
    node: GreenNode,
    /// The type of the name.
    ///
    /// Initially this is `None` until a type check occurs.
    ty: Option<Type>,
    /// Whether or not the name was implicitly introduced.
    ///
    /// This is true for names introduced in outer scopes from workflow scatter
    /// and conditional statements.
    implicit: bool,
}

impl ScopedName {
    /// Constructs a new scoped name.
    pub(crate) fn new(context: ScopedNameContext, node: GreenNode, implicit: bool) -> Self {
        Self {
            context,
            node,
            ty: None,
            implicit,
        }
    }

    /// Gets the context of the scoped name.
    pub fn context(&self) -> ScopedNameContext {
        self.context
    }

    /// Gets the node of the scoped name.
    ///
    /// This may be a bound declaration, an unbound declaration, a workflow call
    /// statement, or a workflow scatter statement.
    pub fn node(&self) -> &GreenNode {
        &self.node
    }

    /// Gets the type of the name.
    ///
    /// A value of `None` indicates that the type could not be determined; this
    /// may occur if the type is a name reference to a struct that does not
    /// exist.
    pub fn ty(&self) -> Option<Type> {
        self.ty
    }

    /// Whether or not the name was introduced implicitly into the scope.
    ///
    /// This is true for names introduced in outer scopes from workflow scatter
    /// and conditional statements.
    pub fn implicit(&self) -> bool {
        self.implicit
    }

    /// Determines if the name was introduced for a scatter variable.
    fn is_scatter_variable(&self) -> bool {
        if !self.implicit {
            return matches!(self.context, ScopedNameContext::ScatterVariable(_));
        }

        false
    }
}

/// Represents a namespace introduced by an import.
#[derive(Debug)]
pub struct Namespace {
    /// The span of the import that introduced the namespace.
    span: Span,
    /// The CST node of the import that introduced the namespace.
    node: GreenNode,
    /// The URI of the imported document that introduced the namespace.
    source: Arc<Url>,
    /// The namespace's document scope.
    scope: Arc<DocumentScope>,
}

impl Namespace {
    /// Gets the CST node that introduced the namespace.
    ///
    /// The node is an import statement.
    pub fn node(&self) -> &GreenNode {
        &self.node
    }

    /// Gets the URI of the imported document that introduced the namespace.
    pub fn source(&self) -> &Arc<Url> {
        &self.source
    }

    /// Gets the scope of the imported document.
    pub fn scope(&self) -> &DocumentScope {
        &self.scope
    }
}

/// Represents a struct in a document.
#[derive(Debug, Clone)]
pub struct Struct {
    /// The span that introduced the struct.
    ///
    /// This is either the name of a struct definition (local) or an import's
    /// URI or alias (imported).
    span: Span,
    /// The namespace that defines the struct.
    ///
    /// This is `Some` only for imported structs.
    namespace: Option<String>,
    /// The CST node of the struct definition.
    node: GreenNode,
    /// The type of the struct.
    ///
    /// Initially this is `None` until a type check occurs.
    ty: Option<Type>,
    /// The index into the locally defined structs.
    ///
    /// This is `None` for imported structs.
    index: Option<usize>,
}

impl Struct {
    /// Gets the CST node of the struct definition.
    pub fn node(&self) -> &GreenNode {
        &self.node
    }

    /// Gets the namespace that defines this struct.
    ///
    /// Returns `None` for structs defined in the containing scope or `Some` for
    /// a struct introduced by an import.
    pub fn namespace(&self) -> Option<&str> {
        self.namespace.as_deref()
    }

    /// Gets the type of the struct.
    ///
    /// A value of `None` indicates that the type could not be determined for
    /// the struct; this may happen if the struct definition is recursive.
    pub fn ty(&self) -> Option<Type> {
        self.ty
    }

    /// Compares two structs for structural equality.
    fn is_equal(&self, other: &Self) -> bool {
        let a = StructDefinition::cast(SyntaxNode::new_root(self.node.clone()))
            .expect("node should cast");
        let b = StructDefinition::cast(SyntaxNode::new_root(other.node.clone()))
            .expect("node should cast");
        for (a, b) in a.members().zip(b.members()) {
            if a.name().as_str() != b.name().as_str() {
                return false;
            }

            if a.ty() != b.ty() {
                return false;
            }
        }

        true
    }
}

/// Represents a scope in a WDL document.
#[derive(Debug)]
pub struct Scope {
    /// The span in the document where the names of the scope are visible.
    span: Span,
    /// The CST node that introduced the scope.
    ///
    /// This may be a struct, task, workflow, conditional statement, or scatter
    /// statement.
    node: GreenNode,
    /// The names in the task scope.
    names: IndexMap<String, ScopedName>,
    /// The child scopes of this scope.
    ///
    /// Child scopes are from workflow conditional and scatter statements.
    children: Vec<Scope>,
}

impl Scope {
    /// Gets the span where the names of the scope are visible.
    pub fn span(&self) -> Span {
        self.span
    }

    /// Gets the CST node that introduced the scope.
    ///
    /// This may be a struct, task, workflow, conditional statement, or scatter
    /// statement.
    pub fn node(&self) -> &GreenNode {
        &self.node
    }

    /// Gets the names in the scope.
    pub fn names(&self) -> impl Iterator<Item = (&String, &ScopedName)> {
        self.names.iter()
    }

    /// Gets a name within the scope.
    pub fn get(&self, name: &str) -> Option<&ScopedName> {
        self.names.get(name)
    }

    /// Gets the child scopes of this scope.
    ///
    /// Child scopes may exist in workflows when conditional or scatter
    /// statements are present.
    pub fn children(&self) -> impl Iterator<Item = &Scope> {
        self.children.iter()
    }

    /// Finds the deepest child scope by position within the document.
    pub fn find_child_scope(&self, position: usize) -> Option<&Scope> {
        let scope = match self
            .children
            .binary_search_by_key(&position, |s| s.span.start())
        {
            Ok(index) => &self.children[index],
            Err(insertion) => {
                // This indicates that we couldn't find a match and the match would go _before_
                // the first child scope, so there is no corresponding scope.
                if insertion == 0 {
                    return None;
                }

                // Check to see if the span before the insertion point actually contains the
                // position.
                let child = &self.children[insertion - 1];
                if position - child.span.start() < child.span.len() {
                    return None;
                }

                child
            }
        };

        Some(scope.find_child_scope(position).unwrap_or(scope))
    }
}

/// Represents context about a scope in a document.
#[derive(Debug, Clone, Copy)]
enum ScopeContext {
    /// The scope is a task.
    ///
    /// The value is an index into the document's `tasks` collection.
    Task(usize),
    /// The scope is a workflow.
    Workflow,
}

/// Represents a task scope.
#[derive(Debug)]
struct TaskScope {
    /// The span of the task name.
    name_span: Span,
    /// The scope of the task.
    scope: Scope,
}

/// Represents a workflow scope.
#[derive(Debug)]
struct WorkflowScope {
    /// The span of the workflow name.
    name_span: Span,
    /// The name of the workflow.
    name: String,
    /// The scope of the workflow.
    scope: Scope,
}

/// Represents the scope of a document.
#[derive(Debug, Default)]
pub struct DocumentScope {
    /// The namespaces in the document.
    namespaces: IndexMap<String, Namespace>,
    /// The tasks in the document.
    tasks: IndexMap<String, TaskScope>,
    /// The singular workflow in the document.
    workflow: Option<WorkflowScope>,
    /// The structs in the document.
    structs: IndexMap<String, Struct>,
    /// A sorted list of scopes within the document.
    ///
    /// This can be used to quickly search for a scope by span.
    scopes: Vec<(Span, ScopeContext)>,
    /// The collection of types for the document.
    types: Types,
}

impl DocumentScope {
    /// Creates a new document scope for a given document.
    pub(crate) fn new(graph: &DocumentGraph, index: NodeIndex) -> (Self, Vec<Diagnostic>) {
        let node = graph.get(index);

        let mut diagnostics = match node.parse_state() {
            ParseState::NotParsed => panic!("node should have been parsed"),
            ParseState::Error(_) => return (Default::default(), Default::default()),
            ParseState::Parsed { diagnostics, .. } => {
                Vec::from_iter(diagnostics.as_ref().iter().cloned())
            }
        };

        let document = node.document().expect("node should have been parsed");

        let version = match document.version_statement() {
            Some(stmt) => stmt.version(),
            None => {
                // Don't process a document with a missing version
                return (Default::default(), diagnostics);
            }
        };

        let scope = match document.ast() {
            Ast::Unsupported => Default::default(),
            Ast::V1(ast) => Self::from_ast_v1(graph, index, &ast, &version, &mut diagnostics),
        };

        (scope, diagnostics)
    }

    /// Gets the namespaces in the document scope.
    pub fn namespaces(&self) -> impl Iterator<Item = (&String, &Namespace)> {
        self.namespaces.iter()
    }

    /// Gets a namespace in the document scope by name.
    pub fn namespace(&self, name: &str) -> Option<&Namespace> {
        self.namespaces.get(name)
    }

    /// Gets the task scopes in the document scope.
    pub fn task_scopes(&self) -> impl Iterator<Item = (&String, &Scope)> {
        self.tasks.iter().map(|(n, s)| (n, &s.scope))
    }

    /// Gets a task scope in the document scope by name.
    pub fn task_scope(&self, name: &str) -> Option<&Scope> {
        self.tasks.get(name).map(|s| &s.scope)
    }

    /// Gets the workflow scope in the document scope.
    pub fn workflow_scope(&self) -> Option<&Scope> {
        self.workflow.as_ref().map(|s| &s.scope)
    }

    /// Gets the structs in the document scope.
    pub fn structs(&self) -> impl Iterator<Item = (&String, &Struct)> {
        self.structs.iter()
    }

    /// Gets a struct in the document scope by name.
    pub fn struct_(&self, name: &str) -> Option<&Struct> {
        self.structs.get(name)
    }

    /// Gets the types of the document.
    pub fn types(&self) -> &Types {
        &self.types
    }

    /// Finds the deepest scope based on a position within the document.
    pub fn find_scope_by_position(&self, position: usize) -> Option<&Scope> {
        let context = match self
            .scopes
            .binary_search_by_key(&position, |(s, _)| s.start())
        {
            Ok(index) => self.scopes[index].1,
            Err(insertion) => {
                // This indicates that we couldn't find a match and the match would go _before_
                // the first scope, so there is no corresponding scope.
                if insertion == 0 {
                    return None;
                }

                // Check to see if the span before the insertion point actually contains the
                // position.
                let (span, context) = &self.scopes[insertion - 1];
                if position - span.start() < span.len() {
                    return None;
                }

                *context
            }
        };

        let scope = match context {
            ScopeContext::Task(index) => &self.tasks[index].scope,
            ScopeContext::Workflow => {
                &self
                    .workflow
                    .as_ref()
                    .expect("expected a workflow scope")
                    .scope
            }
        };

        Some(scope.find_child_scope(position).unwrap_or(scope))
    }

    /// Calculates the span of a scope given a node which uses braces to
    /// delineate the scope.
    fn scope_span(parent: &SyntaxNode) -> Span {
        let open = token(parent, SyntaxKind::OpenBrace).expect("task must have an opening brace");
        let close = parent
            .last_child_or_token()
            .and_then(SyntaxElement::into_token)
            .expect("task must have a last token");
        assert_eq!(
            close.kind(),
            SyntaxKind::CloseBrace,
            "the last token of a task should be a close brace"
        );
        let open = open.text_range().to_span();
        let close = close.text_range().to_span();

        // The span starts after the opening brace and before the closing brace
        Span::new(open.end(), close.start() - open.end())
    }
}