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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! # Language Module
//! 
//! This module contains facilities for language transformations and analysis.
//! The root module `lang` contains code for navigating any syntax tree, and
//! general code for interacting with the CLI or the language servers.
//! 
//! The syntax trees are generated using Tree-sitter parsers, which reside in their own crates.
//! 
//! The submodules contain the specific language transformations and analysis.
//!
//! The language servers themselves are compiled to separate executables, and as
//! such, per rust convention, are in src/bin.  In particular, communication with a
//! language client is handled there, not here.

pub mod applesoft;
pub mod integer;
pub mod merlin;
mod linenum;
pub mod server;
pub mod disk_server;

use tree_sitter;
use lsp_types as lsp;
use colored::*;
use thiserror::Error;
use std::{collections::HashSet, io};
use std::io::Write;
use num_traits::Num;
use std::str::FromStr;
use std::collections::BTreeMap;
use atty;

use crate::{STDRESULT,DYNERR};
const RCH: &str = "unreachable was reached";

pub enum Navigation {
    GotoSelf,
    GotoChild,
    GotoSibling,
    GotoParentSibling,
    Descend,
    Exit,
    Abort
}

#[derive(Error,Debug)]
pub enum Error {
    #[error("Syntax error")]
    Syntax,
    #[error("Invalid Line Number")]
    LineNumber,
    #[error("Tokenization error")]
    Tokenization,
    #[error("Detokenization error")]
    Detokenization,
    #[error("Parsing error")]
    ParsingError,
    #[error("Path not found")]
    PathNotFound,
    #[error("Out of range")]
    OutOfRange,
    #[error("Could not parse URL")]
    BadUrl
}

/// Take a URI from the client and recreate it using the server's conventions.
/// This is needed in order to make reliable file system comparisons.
/// If the URI cannot be interpreted by `std::path` the input is returned unchanged.
pub fn normalize_client_uri(uri: lsp::Uri) -> lsp::Uri {
    match pathbuf_from_uri(&uri) {
        Ok(path) => match uri_from_path(&path) {
            Ok(ans) => return ans,
            Err(_) => {}
        },
        Err(_) => {}
    }
    uri
}

/// Convenience function calling `normalize_client_uri`
pub fn normalize_client_uri_str(uri: &str) -> Result<lsp::Uri,DYNERR> {
    Ok(normalize_client_uri(lsp::Uri::from_str(uri)?))
}

pub fn uri_from_path(path: &std::path::Path) -> Result<lsp::Uri,DYNERR> {
    let url = match url::Url::from_file_path(path) {
        Ok(ans) => ans,
        Err(()) => return Err(Box::new(Error::PathNotFound))
    };
    let uri = fluent_uri::Uri::from_str(url.as_str())?.normalize();
    Ok(lsp::Uri::from_str(uri.as_str())?)
}

/// N.b. `path_str` should be a full path
pub fn uri_from_path_str(path_str: &str) -> Result<lsp::Uri,DYNERR> {
    uri_from_path(&std::path::PathBuf::from_str(path_str)?)
}

pub fn pathbuf_from_uri(uri: &lsp::Uri) -> Result<std::path::PathBuf,DYNERR> {
    let url_crate_uri = match url::Url::from_str(uri.as_str()) {
        Ok(ans) => ans,
        Err(e) => return Err(Box::new(e))
    };
    match url_crate_uri.to_file_path() {
        Ok(ans) => Ok(ans),
        Err(_) => Err(Box::new(Error::BadUrl))
    }
}

/// Return a value indicating the quality of the match of an emulation path to a document in the
/// host file system.  Any value >0 means the filename itself matched case insensitively.
/// Higher values mean there were additional matches, such as parent directories.
fn match_emulation_path(emu_path: &str, sep: &str, doc: &Document) -> usize {
    let mut quality = 0;
    let Some(scheme) = doc.uri.scheme() else { return quality; };
    if scheme.as_str() != "file" { return quality; }
    let Ok(doc_path) = crate::lang::pathbuf_from_uri(&doc.uri) else {
        log::trace!("error while parsing {}",doc.uri.as_str());
        return quality;
    };
    let mut doc_segs = doc_path.iter().rev();
    let emu_segs = emu_path.split(sep).map(|x| x.to_string()).collect::<Vec<String>>();
    for emu_seg in emu_segs.iter().rev() {
        if let Some(doc_seg) = doc_segs.next() {
            if let Some(s) = doc_seg.to_str() {
                if s.to_lowercase() == emu_seg.to_lowercase() {
                    quality += 1;
                } else {
                    break;
                }
            }
        }
    }
    quality
}

/// Get the document URI from the given set that is the best match to the given emulation path.
/// This may return an empty set, or a set with more than one match, where the latter
/// means there were multiple equally good matches.  It is OK for the document vector to contain
/// duplicate URI (the returned Vec is formed from a HashSet).
fn get_emulation_match(docs: &Vec<Document>, emu_path: &str, sep: &str) -> Vec<lsp::Uri> {
    let mut set = HashSet::new();
    let mut best_quality = 0;
    let mut shortest = usize::MAX; // secondary quality measure
    log::debug!("search for file `{}`",emu_path);
    for doc in docs {
        let quality = match_emulation_path(&emu_path, sep, &doc);
        let l = doc.uri.as_str().len();
        log::trace!("match {} to {} Q={}",emu_path,doc.uri.as_str(),quality);
        if quality > best_quality {
            set = HashSet::new();
            set.insert(doc.uri.clone());
            best_quality = quality;
            shortest = l;
        } else if quality > 0 && quality == best_quality {
            if l < shortest {
                set = HashSet::new();
                set.insert(doc.uri.clone());
                shortest = l;
            } else if l == shortest {
                set.insert(doc.uri.clone());
            }
        }
    }
    log::debug!("found {} URI candidates",set.len());
    let mut ans = Vec::new();
    for uri in &set {
        log::trace!("  {}",uri.as_str());
        ans.push(uri.clone())
    }
    ans
    //ans.iter().map(|x| x.clone()).collect()
}

/// Text document packed up with URI string and version information.
/// This is similar to the LSP `TextDocumentItem`, except that it originates
/// on the server side, or from the CLI.
/// There are internally defined URI's for strings and macros.
#[derive(Clone)]
pub struct Document {
    pub uri: lsp::Uri,
    pub version: Option<i32>,
    pub text: String
}

impl Document {
    pub fn new(uri: lsp::Uri,text: String) -> Self {
        Self {
            uri,
            version: None,
            text
        }
    }
    pub fn from_string(text: String, id: u64) -> Self {
        Self {
            uri: lsp::Uri::from_str(&format!("string:{}",id)).expect(RCH),
            version: None,
            text
        }
    }
    pub fn from_macro(text: String, label: String) -> Self {
        Self {
            uri: lsp::Uri::from_str(&format!("macro:{}",label)).expect(RCH),
            version: None,
            text
        }
    }
    pub fn from_file_path(path: &std::path::Path) -> Result<Self,DYNERR> {
        let by = std::fs::read(path)?;
        Ok(Self {
            uri: uri_from_path(path)?,
            version: None,
            text: String::from_utf8(by)?
        })
    }
}

pub fn range_contains_pos(rng: &lsp::Range, pos: &lsp::Position) -> bool
{
	if pos.line < rng.start.line || pos.line > rng.end.line {
		return false;
    }
	if pos.line == rng.start.line && pos.character < rng.start.character {
		return false;
    }
	if pos.line == rng.end.line && pos.character > rng.end.character {
		return false;
    }
	return true;
}

pub fn range_contains_range(outer: &lsp::Range, inner: &lsp::Range) -> bool
{
	if inner.start.line < outer.start.line || inner.end.line > outer.end.line {
		return false;
    }
	if inner.start.line == outer.start.line && inner.start.character < outer.start.character {
		return false;
    }
	if inner.end.line == outer.end.line && inner.end.character > outer.end.character {
		return false;
    }
	return true;
}

pub fn translate_pos(pos: &lsp::Position, dl: isize, dc: isize) -> lsp::Position {
    let mut ans = lsp::Position::new(0,0);
    ans.line = match pos.line as isize + dl < 0 {
        true => 0,
        false => (pos.line as isize + dl) as u32
    };
    ans.character = match pos.character as isize + dc < 0 {
        true => 0,
        false => (pos.character as isize + dc) as u32
    };
    ans
}

pub fn range_union(r1: &lsp::Range,r2: &lsp::Range) -> lsp::Range {
    lsp::Range::new(
        lsp::Position::new(
            match r1.start.line < r2.start.line { true => r1.start.line, false => r2.start.line },
            match r1.start.line < r2.start.line || r1.start.line == r2.start.line && r1.start.character < r2.start.character {
                true => r1.start.character,
                false => r2.start.character
            }
        ),
        lsp::Position::new(
            match r2.end.line > r1.end.line { true => r2.end.line, false => r1.end.line },
            match r2.end.line > r1.end.line || r2.end.line == r1.end.line && r2.end.character > r1.end.character {
                true => r2.end.character,
                false => r1.end.character
            }
        )
    )
}

/// Take a range from a tree-sitter parser and convert it to an LSP range.
/// The `row` argument is used when we are parsing line by line.
/// The `col` argument is only needed to subtract out parsing hints.
pub fn lsp_range(rng: tree_sitter::Range,row: isize,col: isize) -> lsp::Range {
    lsp::Range {
        start: lsp::Position { line: (row + rng.start_point.row as isize) as u32, character: (col + rng.start_point.column as isize) as u32 },
        end: lsp::Position { line: (row + rng.end_point.row as isize) as u32, character: (col + rng.end_point.column as isize) as u32}
    }
}

/// Get text of the node, returning null string if there is any error
pub fn node_text(node: &tree_sitter::Node,source: &str) -> String {
    if let Ok(ans) = node.utf8_text(source.as_bytes()) {
        return ans.to_string();
    }
    return "".to_string();
}

/// Parse a node that is expected to be an integer literal and put into generic type,
/// if node cannot be parsed return None.  This will ignore all spaces.
/// Actually this will work for floating point types as well.
pub fn node_integer<T: FromStr>(node: &tree_sitter::Node,source: &str) -> Option<T> {
    let txt = node_text(&node,source).replace(" ","");
    match txt.parse::<T>() {
        Ok(num) => Some(num),
        Err(_) => None
    }
}

/// Parse a node that may use a prefix to indicate radix, e.g., `$0F` or `%00001111`.
/// This will ignore all spaces and underscores, except for an underscore prefix.
pub fn node_radix<T: Num>(node: &tree_sitter::Node, source: &str, hex: &str, bin: &str) -> Option<T> {
    if let Ok(s) = node.utf8_text(source.as_bytes()) {
        let mut trimmed = s.to_string().replace(" ","").replace("_","");
        if s.starts_with("_") && (hex=="_" || bin=="_") {
            trimmed = ["_",&trimmed].concat();
        }
        if trimmed.starts_with(hex) {
            match T::from_str_radix(&trimmed[1..],16) {
                Ok(ans) => Some(ans),
                Err(_) => None
            }
        } else if trimmed.starts_with(bin) {
            match T::from_str_radix(&trimmed[1..],2) {
                Ok(ans) => Some(ans),
                Err(_) => None
            }
        } else {
            match T::from_str_radix(&trimmed,10) {
                Ok(ans) => Some(ans),
                Err(_) => None
            }
        }
    } else {
        None
    }
}

pub fn named_sibling(node: tree_sitter::Node,skip: usize) -> Option<tree_sitter::Node> {

    let mut maybe = Some(node);
    for _i in 0..skip {
        maybe = maybe.unwrap().next_named_sibling();
        if maybe.is_none() {
            return None;
        }
    }
    maybe
}

/// Extend a zero range one code point in either direction, if possible.
/// Finite ranges are not modified.
/// The byte range is not modified in any case.
pub fn extended_range(node: &tree_sitter::Node,end_col: usize) -> tree_sitter::Range {
    let mut ans = node.range();
    if ans.start_point.column == ans.end_point.column {
        if ans.start_point.column > 0 {
            ans.start_point.column -= 1;
        }
        if ans.end_point.column + 1 < end_col {
            ans.end_point.column += 1;
        }
    }
    ans
}

/// Try to update a named boolean from a serde `Value` presumed to be an object.
/// If there is any error do not change the value.
pub fn update_json_bool(maybe_obj: &serde_json::Value, key: &str, curr: &mut bool) {
    if let Some(outer) = maybe_obj.as_object() {
        if let Some(x) = outer.get(key) {
            match x.as_bool() { Some(x) => *curr = x, _ => {} };
        }
    }
}

/// Try to update a named integer from a serde `Value` presumed to be an object.
/// If there is any error do not change the value.
pub fn update_json_i64(maybe_obj: &serde_json::Value, key: &str, curr: &mut i64) {
    if let Some(outer) = maybe_obj.as_object() {
        if let Some(x) = outer.get(key) {
            match x.as_i64() { Some(x) => *curr = x, _ => {} };
        }
    }
}

/// Try to update a named float from a serde `Value` presumed to be an object.
/// If there is any error do not change the value.
pub fn update_json_f64(maybe_obj: &serde_json::Value, key: &str, curr: &mut f64) {
    if let Some(outer) = maybe_obj.as_object() {
        if let Some(x) = outer.get(key) {
            match x.as_f64() { Some(x) => *curr = x, _ => {} };
        }
    }
}

/// Try to update a named optional string from a serde `Value` presumed to be an object.
/// If there is any error do not change the value.
pub fn update_json_string_opt(maybe_obj: &serde_json::Value, key: &str, curr: &mut Option<String>) {
    if let Some(outer) = maybe_obj.as_object() {
        if let Some(x) = outer.get(key) {
            match x.as_str() { Some(x) => *curr = Some(x.to_string()), _ => {} };
        }
    }
}

/// Try to update a named string from a serde `Value` presumed to be an object.
/// If there is any error do not change the value.
pub fn update_json_string(maybe_obj: &serde_json::Value, key: &str, curr: &mut String) {
    if let Some(outer) = maybe_obj.as_object() {
        if let Some(x) = outer.get(key) {
            match x.as_str() { Some(x) => *curr = x.to_string(), _ => {} };
        }
    }
}

/// Try to update a named severity from a serde `Value` presumed to be an object.
/// If there is any error do not change the value.
pub fn update_json_severity(maybe_obj: &serde_json::Value, key: &str, curr: &mut Option<lsp::DiagnosticSeverity>) {
    if let Some(outer) = maybe_obj.as_object() {
        if let Some(x) = outer.get(key) {
            match x.as_str() {
                Some("ignore") => *curr = None,
                Some("hint") => *curr = Some(lsp::DiagnosticSeverity::HINT),
                Some("info") => *curr = Some(lsp::DiagnosticSeverity::INFORMATION),
                Some("warn") => *curr = Some(lsp::DiagnosticSeverity::WARNING),
                Some("error") => *curr = Some(lsp::DiagnosticSeverity::ERROR),
                _ => {}
            }
        }
    }
}

/// Try to update a named list of integers from a serde `Value` presumed to be an object.
/// If there is any error do not change the value.
pub fn update_json_vec(maybe_obj: &serde_json::Value, key: &str, curr: &mut Vec<i64>) {
    if let Some(outer) = maybe_obj.as_object() {
        if let Some(x) = outer.get(key) {
            let mut ans: Vec<i64> = Vec::new();
            if let Some(a) = x.as_array() {
                for v in a {
                    match v.as_i64() {
                        Some(i) => ans.push(i),
                        None => return
                    }
                }
                *curr = ans;
            }
        }
    }
}

/// Try to update a named list of strings from a serde `Value` presumed to be an object.
/// If there is any error do not change the value.
pub fn update_json_vec_str(maybe_obj: &serde_json::Value, key: &str, curr: &mut Vec<String>) {
    if let Some(outer) = maybe_obj.as_object() {
        if let Some(x) = outer.get(key) {
            let mut ans: Vec<String> = Vec::new();
            if let Some(a) = x.as_array() {
                for v in a {
                    match v.as_str() {
                        Some(s) => ans.push(s.to_owned()),
                        None => return
                    }
                }
                *curr = ans;
            }
        }
    }
}

/// Trait for navigating a syntax tree in any language.
pub trait Navigate {
    fn visit(&mut self,curs: &tree_sitter::TreeCursor) -> Result<Navigation,DYNERR>;
    fn descend(&mut self,_curs: &tree_sitter::TreeCursor) -> Result<Navigation,DYNERR> {
        Ok(Navigation::GotoSibling)
    }
    fn walk(&mut self,tree: &tree_sitter::Tree) -> Result<(),DYNERR>
    {
        let mut curs = tree.walk();
        let mut choice = Navigation::GotoSelf;
        while ! matches!(choice,Navigation::Exit | Navigation::Abort)
        {
            if matches!(choice,Navigation::GotoSelf) {
                choice = self.visit(&curs)?;
            } else if matches!(choice,Navigation::Descend) {
                choice = self.descend(&curs)?;
            } else if matches!(choice,Navigation::GotoChild) && curs.goto_first_child() {
                choice = self.visit(&curs)?;
            } else if matches!(choice,Navigation::GotoParentSibling) && curs.goto_parent() && curs.goto_next_sibling() {
                choice = self.visit(&curs)?;
            } else if matches!(choice,Navigation::GotoSibling) && curs.goto_next_sibling() {
                choice = self.visit(&curs)?;
            } else if curs.goto_next_sibling() {
                choice = self.visit(&curs)?;
            } else if curs.goto_parent() {
                choice = Navigation::GotoSibling;
            } else {
                choice = Navigation::Exit;
            }
        }
        Ok(())
    }
}


/// Test for the given language, returns false if there is any syntax error,
/// does not always return true otherwise (additional criteria may be used).
/// Warnings may be emitted if the results are ambiguous.
/// Works for any language, provided it is line-oriented.
pub fn is_lang(lang: tree_sitter::Language,code: &str) -> bool {
    let mut parser = tree_sitter::Parser::new();
    parser.set_language(&lang).expect("language not found");
    let mut iter = code.lines();
    let mut line_count = 0;
    let mut good_lines = 0;
    let mut great_lines = 0;
    while let Some(line) = iter.next()
    {
        line_count += 1;
        if let Some(tree) = parser.parse(String::from(line) + "\n",None) {
            let curs = tree.walk();
            if !curs.node().has_error() {
                good_lines += 1;
                match lang.name() {
                    Some("merlin6502") => {
                        match curs.node().child(0) {
                            Some(child) => match child.kind() {
                                "operation" => great_lines += 1,
                                "pseudo_operation" => great_lines += 1,
                                _ => {}
                            },
                            None => {}
                        }
                    },
                    _ => great_lines += 1
                }
            }
        }
    }
    if line_count == 0 {
        log::warn!("encountered empty file");
        return false;
    }
    if (great_lines==0 || good_lines != line_count) && good_lines * 3 > line_count {
        log::warn!("{} {} parsed as {} but merit test failed",good_lines,match good_lines==1 { true => "line", false => "lines" },lang.name().unwrap_or("unknown"));
    }
    good_lines == line_count && great_lines > 0
}

/// Simple verify, returns an error if syntax check fails, but does not run full diagnostics.
/// This is used by the CLI to interrupt the pipeline when a bad language file is encountered.
/// Works for any language, provided it is line-oriented.
pub fn verify_str(lang: tree_sitter::Language,code: &str) -> STDRESULT {
    let mut parser = tree_sitter::Parser::new();
    parser.set_language(&lang)?;
    let mut iter = code.lines();
    let mut row = 0;
    while let Some(line) = iter.next()
    {
        match parser.parse(String::from(line) + "\n",None) {
            Some(tree) => {
                let curs = tree.walk();
                if curs.node().has_error() {
                    log::error!("syntax error in row {}, use `verify` for more details",row);
                    return Err(Box::new(Error::Syntax));
                }
            },
            None => {
                log::error!("unable to parse row {}",row);
                return Err(Box::new(Error::Syntax));
            }
        }
        row += 1;
    }
    Ok(())
}

pub fn eprint_lines_sexpr(lang: tree_sitter::Language, program: &str, unwraps: usize) {
    let mut parser = tree_sitter::Parser::new();
    parser.set_language(&lang).expect("Error loading grammar");
    let mut iter = program.lines();
    eprintln!();
    while let Some(line) = iter.next()
    {
        if let Some(tree) = parser.parse(String::from(line) + "\n",None) {
            let mut curs = tree.walk();
            for _i in 0..unwraps {
                curs.goto_first_child();
            }
            eprintln!("{}",line.to_string());
            eprintln!("{}",curs.node().to_sexp());
        }
    }
}

/// Gather program lines from the console, panics if stdin is not the console
pub fn line_entry(lang: tree_sitter::Language,prompt: &str) -> String
{
    let mut parser = tree_sitter::Parser::new();
    parser.set_language(&lang).expect("Error loading grammar");
    let mut code = String::new();
    if atty::is(atty::Stream::Stdin) {
        eprintln!("Line entry interface.");
        eprintln!("This is a blind accumulation of lines.");
        eprintln!("Verify occurs when entry is terminated.");
        eprintln!("Accumulated lines can be piped.");
        eprintln!("`bye` terminates.");
        loop {
            eprint!("{} ",prompt);
            let mut line = String::new();
            io::stderr().flush().expect("could not flush stderr");
            io::stdin().read_line(&mut line).expect("could not read stdin");
            if line=="bye\n" || line=="bye\r\n" {
                break;
            }
            code += &line;
        }
        return code;
    } else {
        panic!("line_entry was called with piped input");
    }
}

pub fn eprint_diagnostic(diag: &lsp::Diagnostic, program: &str) {
    // line search not very efficient, perhaps it will do...
    if let Some(sev) = diag.severity {
        if sev == lsp::DiagnosticSeverity::HINT {
            // at present this is used to dim conditional assembly,
            // and we don't want to flag it.
            return;
        }
    }
    let mut lines = program.lines();
    let mut maybe_line = None;
    for _i in 0..diag.range.start.line+1 {
        maybe_line = lines.next();
    }
    let [announcement,squiggle] = match diag.severity {
        Some(lsp::DiagnosticSeverity::ERROR) => ["Error".red(),"^".red()],
        Some(lsp::DiagnosticSeverity::WARNING) => ["Warning".bright_yellow(),"^".bright_yellow()],
        Some(lsp::DiagnosticSeverity::INFORMATION) => ["Information".bright_blue(),"^".bright_blue()],
        _ => ["Unexpected Notice".red(),"^".red()]
    };
    eprintln!("{} on line {}: {}",announcement,diag.range.start.line,diag.message);
    if let Some(line) = maybe_line {
        eprintln!("  {}",line);
        for _i in 0..diag.range.start.character+2 {
            eprint!(" ");
        }
        for _i in diag.range.start.character..diag.range.end.character {
            eprint!("{}",squiggle);
        }    
        eprintln!();
    } 
}

/// This assumes all CRLF have been filtered from `doc`.
/// CRLF in `raw_new` will be changed to LF.
fn replace_range(doc: &mut String, rng: lsp::Range, raw_new: &str) -> STDRESULT {
    let new = raw_new.replace("\r\n","\n");
    let mut start_char = 0;
    let mut end_char = 0;
    let mut curr_line = 0;
    let mut found_start = false;
    let mut found_end = false;
    for line in doc.lines() {
        if rng.start.line == curr_line {
            start_char += rng.start.character;
            found_start = true;
        }
        if !found_start {
            start_char += line.chars().count() as u32 + 1;
        }
        if rng.end.line == curr_line {
            end_char += rng.end.character;
            found_end = true;
            break;
        }
        if !found_end {
            end_char += line.chars().count() as u32 + 1;
        }
        curr_line += 1;
    }
    if found_start && found_end {
        doc.replace_range(start_char as usize..end_char as usize,&new);
        return Ok(());
    }
    // there still could be an insertion at the end
    let line_count = doc.lines().count() as u32;
    if rng.start.line==line_count && rng.start.character==0 && rng.end.line==line_count && rng.end.character==0 {
        doc.push_str(&new);
        return Ok(());
    }
    Err(Box::new(Error::LineNumber))
}

/// Strategy is to sort edits bottom to top and apply in that sequence, this way the
/// meaning of a row doesn't change as we make the replacements.  Overlaps not allowed.
/// This is consistent with the LSP.  Preserves CRLF or LF, unless there is a mixture,
/// in which case LF wins.  Panics if row is out of range.
pub fn apply_edits(doc: &str, edits: &Vec<lsp::TextEdit>, row: u32) -> Result<String,DYNERR> {
    // TODO: check for overlaps
    let line_sep = match doc.split("\r\n").count() == doc.split("\n").count() {
        true => "\r\n",
        false => "\n"
    };
    let mut ans = String::from(doc);
    ans = ans.replace("\r\n","\n");
    let mut sorted = BTreeMap::new();
    let mut idx: u32 = 0; // provide uniqueness in case of repeated insertions or deletions
    for edit in edits {
        let key = (edit.range.start.line,edit.range.start.character,idx);
        sorted.insert(key,edit.clone());
        idx += 1;
    }
    for edit in sorted.values().rev() {
        let offset_rng = lsp::Range::new(
            lsp::Position::new(edit.range.start.line - row,edit.range.start.character),
            lsp::Position::new(edit.range.end.line - row,edit.range.end.character)
        );
        log::trace!("replace {:?}",offset_rng);
        replace_range(&mut ans,offset_rng,&edit.new_text)?;
    }
    if line_sep == "\r\n" {
        ans = ans.replace("\n","\r\n");
    }
    Ok(ans)
}