a2kit 4.4.2

Retro disk image and language utility
Documentation
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
use std::collections::{HashSet,HashMap};
use std::ffi::OsString;
use std::path::PathBuf;
use crate::lang;
use lsp_types as lsp;
use tree_sitter::TreeCursor;
use super::super::{SourceType,Symbol,Workspace};
use crate::lang::{Document,Navigate,Navigation,node_text,lsp_range};
use crate::{DYNERR,STDRESULT};

const RCH: &str = "unreachable was reached";
const MAX_DIRS: usize = 1000;
const MAX_DEPTH: usize = 10;
const IGNORE_DIRS: [&str;4] = [
    "build",
    "node_modules",
    "target",
    ".git"
];

impl Workspace {
    pub fn new() -> Self {
        Self {
            ws_folders: Vec::new(),
            docs: Vec::new(),
            use_map: HashMap::new(),
            put_map: HashMap::new(),
            includes: HashSet::new(),
            entries: HashMap::new(),
            linker_frac: HashMap::new(),
            rel_modules: HashSet::new()
        }
    }
    pub fn get_ws_symbols(&self) -> Vec<lsp::WorkspaceSymbol> {
        let mut ans = Vec::new();
        let mut instances: Vec<(lsp::Location,String)> = Vec::new();
        for (name,sym) in &self.entries {
            for loc in &sym.decs {
                instances.push((loc.clone(),name.clone()));
            }
            for loc in &sym.defs {
                instances.push((loc.clone(),name.clone()));
            }
            for loc in &sym.refs {
                instances.push((loc.clone(),name.clone()));
            }
        }
        for (loc,name) in instances {
            ans.push(lsp::WorkspaceSymbol {
                name: name.to_owned(),
                kind: lsp::SymbolKind::CONSTANT,
                tags: None,
                container_name: None,
                location: lsp::OneOf::Left(loc),
                data: None
            });
        }
        ans
    }
    /// Get all masters of this URI
	pub fn get_masters(&self, uri: &lsp::Uri) -> HashSet<String> {
        let mut ans = HashSet::new();
        if let Some(masters) = self.put_map.get(&uri.to_string()) {
            for master in masters {
                ans.insert(master.to_owned());
            }
        }
        if let Some(masters) = self.use_map.get(&uri.to_string()) {
            for master in masters {
                ans.insert(master.to_owned());
            }
        }
		ans
	}
	/// find document's master based on what is in workspace and preference,
	/// but ignoring availability of labels and diagnostic status.
	pub fn get_master(&self, include: &Document, preferred_master: Option<String>) -> Document {
        let masters = self.get_masters(&include.uri);
        if masters.len() == 0 {
            return include.clone();
        }
        let preferred = match &preferred_master {
            Some(uri) => {
                match masters.get(uri.as_str()) {
                    Some(s) => s,
                    None => masters.iter().next().unwrap()
                }
            },
            None => masters.iter().next().unwrap()
        };
        for doc in &self.docs {
            if doc.uri.as_str() == preferred {
                return doc.clone();
            }
        }
		return include.clone();
	}
	/// Get the document URI that is the best match to the ProDOS path at the *next* cursor location.
    /// This may return an empty vector, or a vector with more than one match, where the latter
    /// means there were multiple equally good matches.
	pub fn get_include_doc(&self, node: &tree_sitter::Node, source: &str) -> Vec<lsp::Uri> {
		if let Some(file_node) = node.next_named_sibling() {
            let mut emu_path = super::super::node_text(&file_node,source);
            if !emu_path.ends_with(".S") && !emu_path.ends_with(".s") {
                emu_path.push_str(".S");
            }
            crate::lang::get_emulation_match(&self.docs, &emu_path, "/")
        } else {
            log::debug!("missing file node");
            vec![]
        }
	}
    pub fn source_type(&self, uri: &lsp::Uri, linker_threshold: f64) -> SourceType {
        let key = uri.to_string();
        if let Some(scheme) = uri.scheme() {
            if scheme.as_str() == "macro" {
                return SourceType::MacroRef;
            }
        }
        if let Some(frac) = self.linker_frac.get(&uri.to_string()) {
            if *frac >= linker_threshold {
                return SourceType::Linker;
            }
        }
        let is_put = self.put_map.contains_key(&key);
        let is_use = self.use_map.contains_key(&key);
        let is_rel = self.rel_modules.contains(&uri.to_string());
        match (is_put,is_use,is_rel) {
            (true,true,_) => SourceType::UseAndPut,
            (true,false,_) => SourceType::Put,
            (false,true,_) => SourceType::Use,
            (false,false,true) => SourceType::Module,
            (false,false,false) => SourceType::Master
        }
    }
}

pub struct WorkspaceScanner {
    parser: tree_sitter::Parser,
    line: String,
    curr_uri: Option<lsp::Uri>,
    curr_row: isize,
    curr_depth: usize,
    file_count: usize,
    dir_count: usize,
    ws: Workspace,
    running_docstring: String,
    scan_patt: regex::Regex,
    link_patt: regex::Regex
}

impl WorkspaceScanner {
    pub fn new() -> Self {
        let mut parser = tree_sitter::Parser::new();
        parser.set_language(&tree_sitter_merlin6502::LANGUAGE.into()).expect(RCH);
        Self {
            parser,
            line: String::new(),
            curr_uri: None,
            curr_row: 0,
            curr_depth: 0,
            file_count: 0,
            dir_count: 0,
            ws: Workspace::new(),
            running_docstring: String::new(),
            scan_patt: regex::Regex::new(r"(?i)^\S*\s+(ent|ext|exd|put|use|rel)(\s+|$)").expect(RCH),
            link_patt: regex::Regex::new(r"(?i)^\S*\s+(lnk|lkv|asm)\s+").expect(RCH)
        }
    }
    /// Workspace scanner has its own copies that are not always up to date.
    /// This should be called when there is a document change notification.
    /// When there is a full rescan, call this with all checkpoints
    /// right after the gather.
    pub fn update_doc(&mut self,doc: &Document) {
        for old in &mut self.ws.docs {
            if old.uri == doc.uri {
                old.text = doc.text.clone();
                old.version = doc.version;
            }
        }
    }
    /// Borrow the workspace data
    pub fn get_workspace(&self) -> &Workspace {
        &self.ws
    }
    /// Set the workspace data, source is probably another analysis object
    pub fn set_workspace(&mut self,ws: Workspace) {
        self.ws = ws;
    }
    /// Add volatile documents to the workspace set, useful for piped documents
    pub fn append_volatile_docs(&mut self, docs: Vec<Document>) {
        for doc in docs {
            self.ws.docs.push(doc);
        }
    }
    /// Recursively gather files from this directory
    fn gather_from_dir(&mut self, base: &PathBuf, max_files: usize) -> STDRESULT {
        log::debug!("gather from {}",base.display());
        self.dir_count += 1;
        self.curr_depth += 1;
        let opt = glob::MatchOptions {
            case_sensitive: false,
            require_literal_leading_dot: false,
            require_literal_separator: false
        };
        // first scan source files
        let patt = base.join("*.s");
        if let Some(globable) = patt.as_os_str().to_str() {
            if let Ok(paths) = glob::glob_with(globable,opt) {
                for entry in paths {
                    match &entry { Ok(path) => {
                        let full_path = base.join(path);
                        if let Some(path_str) = full_path.as_os_str().to_str() {
                            if let (Ok(uri),Ok(txt)) = (lang::uri_from_path_str(path_str),std::fs::read_to_string(path.clone())) {
                                log::trace!("{}",uri.as_str());
                                self.ws.docs.push(Document::new(uri, txt));
                            }
                        }
                    } Err(e) => log::warn!("glob error {}",e) }
                    self.file_count += 1;
                    if self.file_count >= max_files {
                        log::error!("aborting due to excessive source file count of {}",self.file_count);
                        return Err(Box::new(crate::lang::Error::OutOfRange));
                    }
                }
            } else {
                log::warn!("directory {} could not be globbed",base.display());
            }
        } else {
            log::warn!("directory {} has unknown encoding",base.display());
        }
        // now go into subdirectories
        if self.curr_depth < MAX_DEPTH {
            log::trace!("depth is {}",self.curr_depth);
            for entry in std::fs::read_dir(base)? {
                let entry = entry?;
                log::trace!("check {} for descent",entry.file_name().as_os_str().to_string_lossy());
                let ignore_dirs = IGNORE_DIRS.iter().map(|x| OsString::from(x)).collect::<Vec<OsString>>();
                if ignore_dirs.contains(&entry.file_name()) {
                    continue;
                }
                let path = entry.path();
                if path.is_dir() && self.dir_count < MAX_DIRS {
                    self.gather_from_dir(&path,max_files)?
                }
            }
        }
        self.curr_depth -= 1;
        Ok(())
    }
    /// Buffer all documents matching `*.s` in any of `dirs`, up to maximum count `max_files`,
    /// searching at most MAX_DIRS directories, using at most MAX_DEPTH recursions, and ignoring IGNORE_DIRS.
	pub fn gather_docs(&mut self, dirs: &Vec<lsp::Uri>, max_files: usize) -> STDRESULT {
        self.ws.ws_folders = Vec::new();
        self.ws.docs = Vec::new();
        self.curr_depth = 0;
        self.file_count = 0;
        self.dir_count = 0;
        // copy the workspace url's to the underlying workspace object
        for dir in dirs {
            self.ws.ws_folders.push(dir.clone());
        }
        for dir in dirs {
            let path = lang::pathbuf_from_uri(&dir)?;
            log::debug!("scanning {}",path.as_os_str().to_string_lossy());
            self.gather_from_dir(&path,max_files)?;
		}
        if self.dir_count >= MAX_DIRS {
            log::warn!("scan was aborted after {} directories",self.dir_count);
        }
        log::info!("there were {} sources in the workspace",self.file_count);
        Ok(())
	}
    /// Scan buffered documents for entries and includes.
    /// Assumes buffers are up to date.
    pub fn scan(&mut self) -> STDRESULT {
        self.ws.entries = HashMap::new();
        self.ws.use_map = HashMap::new();
        self.ws.put_map = HashMap::new();
        self.ws.includes = HashSet::new();
        self.ws.linker_frac = HashMap::new();
        self.ws.rel_modules = HashSet::new();
        for i in 0..self.ws.docs.len() {
            let doc = self.ws.docs[i].to_owned();
            log::debug!("Scanning {}...",doc.uri.as_str());
            self.curr_uri = Some(doc.uri.clone());
            self.curr_row = 0;
            let mut linker_count = 0.0;
            self.running_docstring = String::new();
            for line in doc.text.lines() {
                // use regex to skip most lines, this avoids the need to
                // deal with implicit macro call resolution, and may speed things up
                if !self.scan_patt.is_match(line) && !line.starts_with("*") {
                    if self.link_patt.is_match(line) {
                        linker_count += 1.0;
                    }
                    self.curr_row += 1;
                    self.running_docstring = String::new();
                    continue;
                }
                self.line = line.to_string() + "\n";
                if let Some(tree) = self.parser.parse(&self.line,None) {
                    self.walk(&tree)?;
                }
                self.curr_row += 1;
            }
            self.ws.linker_frac.insert(doc.uri.to_string(),linker_count/self.curr_row as f64);
        }
        // clean the include maps so that a master cannot also be an include.
        // it is possible to end up with no masters.
        for include in &self.ws.includes {
            for masters in self.ws.use_map.values_mut() {
                masters.remove(include);
            }
            for masters in self.ws.put_map.values_mut() {
                masters.remove(include);
            }
        }
        Ok(())
    }
}

impl Navigate for WorkspaceScanner {
    /// Visitor to build information about the overall workspace.
    /// Important that this be efficient since every file is scanned.
    /// The caller should skip over all lines but those matching `scan_patt`.
    /// For this scan we do not need or want to descend into includes (no recursive includes).
    fn visit(&mut self, curs: &TreeCursor) -> Result<Navigation,DYNERR> {
        // as an optimization, take swift action on certain high level nodes
        if curs.node().kind() == "operation" || curs.node().kind() == "macro_call" {
            self.running_docstring = String::new();
            return Ok(Navigation::Exit);
        }
        if curs.node().kind() == "source_file" {
            return Ok(Navigation::GotoChild);
        }
        if curs.node().kind() == "pseudo_operation" {
            return Ok(Navigation::GotoChild);
        }
        let curr = curs.node();
        let next = curr.next_named_sibling();
        let curr_uri = self.curr_uri.as_ref().unwrap().clone();
        let loc = lsp::Location::new(curr_uri.clone(), lsp_range(curr.range(), self.curr_row, 0));

        // Gather docstring

        if curr.kind() == "heading" {
            let temp = match curr.named_child(0) {
                Some(n) => node_text(&n, &self.line),
                None => String::new()
            };
            self.running_docstring += &temp;
            self.running_docstring += "\n";
            return Ok(Navigation::Exit);
        } else if curs.depth()>1 && curr.kind() != "label_def" {
            self.running_docstring = String::new();
        }

        // Identify REL modules

        if curr.kind() == "psop_rel" {
            self.ws.rel_modules.insert(curr_uri.to_string());
            return Ok(Navigation::Exit);
        }

        // Handle workspace labels including ENT, EXT, EXD

        if curr.kind() == "label_def" && next.is_some() && ["psop_ent","psop_ext","psop_exd"].contains(&next.unwrap().kind()) {
            let name = node_text(&curr,&self.line);
            if !self.ws.entries.contains_key(&name) {
                self.ws.entries.insert(name.clone(),Symbol::new(&name));
            }
            let sym = self.ws.entries.get_mut(&name).unwrap();
            sym.add_node_ws(loc, &curr, &self.line);
            if next.unwrap().kind() == "psop_ent" {
                sym.defining_code = Some(self.line.clone());
                sym.docstring = self.running_docstring.clone();
                self.running_docstring = String::new();
            }
            return Ok(Navigation::Exit);
        }
        if ["psop_ent","psop_ext","psop_exd"].contains(&curr.kind()) {
            return Ok(Navigation::GotoSibling);
        }
        if ["arg_ent","arg_ext","arg_exd"].contains(&curr.kind()) {
            let mut sib = curr.named_child(0);
            while sib.is_some() && ["label_ref","label_def"].contains(&sib.unwrap().kind()) {
                let name = node_text(&sib.unwrap(),&self.line);
                if !self.ws.entries.contains_key(&name) {
                    self.ws.entries.insert(name.clone(),Symbol::new(&name));
                }
                let sym = self.ws.entries.get_mut(&name).unwrap();
                sym.add_node_ws(loc.clone(), &sib.unwrap(), &self.line);
                sib = sib.unwrap().next_named_sibling();
            }
            return Ok(Navigation::Exit);
        }

        // Now check for includes.

        if curr.kind() == "label_def" {
            self.running_docstring = String::new();
            return Ok(Navigation::GotoSibling);
        }
        if curr.kind() == "psop_use" {
            let matching_docs = self.ws.get_include_doc(&curr,&self.line);
            if matching_docs.len()==1 {
                let include_uri = matching_docs[0].to_string();
                let mut masters = match self.ws.use_map.get(&include_uri) {
                    Some(m) => m.to_owned(),
                    None => HashSet::new()
                };
                masters.insert(curr_uri.to_string());
                self.ws.use_map.insert(include_uri.clone(),masters);
                self.ws.includes.insert(include_uri);
            } else {
                log::debug!("USE resulted in no unique match ({})",matching_docs.len());
            }
        }
        if curr.kind() == "psop_put" {
            let matching_docs = self.ws.get_include_doc(&curr,&self.line);
            if matching_docs.len()==1 {
                let include_uri = matching_docs[0].to_string();
                let mut masters = match self.ws.put_map.get(&include_uri) {
                    Some(m) => m.to_owned(),
                    None => HashSet::new()
                };
                masters.insert(curr_uri.to_string());
                self.ws.put_map.insert(include_uri.clone(),masters);
                self.ws.includes.insert(include_uri);
            } else {
                log::debug!("PUT resulted in no unique match ({})",matching_docs.len());
            }
        }
        // If none of the above we can go straight to the next line
        return Ok(Navigation::Exit);
    }
}