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
727
728
729
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2018, Olof Kraigher olof.kraigher@gmail.com

use crate::diagnostic::{Diagnostic, ParseResult};
use crate::latin_1::{Latin1String, Utf8ToLatin1Error};
use pad;
use std::cmp::{max, min};
use std::collections::hash_map::DefaultHasher;
use std::collections::VecDeque;
use std::convert::AsRef;
use std::fmt;
use std::fmt::Write;
use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io;
use std::io::prelude::Read;
use std::io::BufRead;
use std::sync::Arc;

trait SourceDataProvider {
    fn contents(&self) -> io::Result<Arc<Latin1String>>;
}

struct SourceFile {
    file_name: String,
}

struct InlineSource {
    contents: Arc<Latin1String>,
}

impl SourceDataProvider for SourceFile {
    fn contents(&self) -> io::Result<Arc<Latin1String>> {
        let mut file = File::open(&self.file_name)?;
        let mut bytes = Vec::new();
        file.read_to_end(&mut bytes)?;
        Ok(Arc::new(Latin1String::from_vec(bytes)))
    }
}

impl SourceDataProvider for InlineSource {
    fn contents(&self) -> io::Result<Arc<Latin1String>> {
        Ok(self.contents.clone())
    }
}

struct FileId {
    name: String,
    hash: u64, // Hash of name
}

impl FileId {
    fn new(name: impl Into<String>) -> FileId {
        let name = name.into();
        let hash = hash(&name);
        Self { name, hash }
    }
}

impl PartialEq for FileId {
    fn eq(&self, other: &Self) -> bool {
        // Use file name hash to speedup comparison
        if self.hash == other.hash {
            self.name == other.name
        } else {
            false
        }
    }
}

fn hash(value: &str) -> u64 {
    let mut hasher = DefaultHasher::new();
    hasher.write(value.as_bytes());
    hasher.finish()
}

struct UniqueSource {
    file_id: FileId,
    data_provider: Box<dyn SourceDataProvider + Sync + Send>,
}

impl fmt::Debug for UniqueSource {
    /// Custom implementation to avoid large contents strings
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Source {{file_name: {:?}}}", self.file_name())
    }
}

impl UniqueSource {
    fn inline(file_name: impl Into<String>, contents: Arc<Latin1String>) -> Self {
        Self {
            file_id: FileId::new(file_name),
            data_provider: Box::new(InlineSource { contents }),
        }
    }

    fn from_file(file_name: impl Into<String>) -> Self {
        let data_provider = Box::new(SourceFile {
            file_name: file_name.into(),
        });
        Self {
            file_id: FileId::new(data_provider.file_name.clone()),
            data_provider,
        }
    }

    fn contents(&self) -> io::Result<Arc<Latin1String>> {
        self.data_provider.contents()
    }

    fn file_name(&self) -> &str {
        self.file_id.name.as_ref()
    }
}

#[derive(Debug, Clone)]
pub struct Source {
    source: Arc<UniqueSource>,
}

impl PartialEq for Source {
    fn eq(&self, other: &Self) -> bool {
        self.source.file_id == other.source.file_id
    }
}

impl Eq for Source {}

impl Hash for Source {
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        hasher.write_u64(self.source.file_id.hash)
    }
}

impl Source {
    pub fn inline(file_name: impl Into<String>, contents: Arc<Latin1String>) -> Source {
        Source {
            source: Arc::new(UniqueSource::inline(file_name, contents)),
        }
    }

    pub fn from_file(file_name: impl Into<String>) -> Source {
        Source {
            source: Arc::new(UniqueSource::from_file(file_name)),
        }
    }

    pub fn inline_utf8(
        file_name: impl Into<String>,
        contents: &str,
    ) -> Result<Self, Utf8ToLatin1Error> {
        let latin1 = Latin1String::from_utf8(contents)?;
        Ok(Self::inline(file_name, Arc::new(latin1)))
    }

    pub fn contents(&self) -> io::Result<Arc<Latin1String>> {
        self.source.contents()
    }

    pub fn file_name(&self) -> &str {
        self.source.file_name()
    }

    pub fn pos(&self, start: Position, end: Position) -> SrcPos {
        SrcPos {
            source: self.clone(),
            range: Range { start, end },
        }
    }
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Debug)]
pub struct Position {
    pub line: u64,
    pub character: u64,
}

impl Position {
    pub fn next_char(&self) -> Position {
        Position {
            line: self.line,
            character: self.character + 1,
        }
    }
}

#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug)]
pub struct Range {
    pub start: Position,
    pub end: Position,
}

impl Range {
    pub fn new(start: Position, end: Position) -> Range {
        Range { start, end }
    }
}

/// Lexical position in a file.
#[derive(PartialEq, Clone, Debug, Eq, Hash)]
pub struct SrcPos {
    /// The source
    pub source: Source,
    range: Range,
}

#[derive(PartialEq, Clone, Debug)]
pub struct WithPos<T> {
    pub item: T,
    pub pos: SrcPos,
}

impl<T> WithPos<T> {
    // Avoid clone in production code
    #[cfg(test)]
    pub fn new(item: T, pos: impl AsRef<SrcPos>) -> WithPos<T> {
        WithPos {
            item,
            pos: pos.as_ref().clone(),
        }
    }

    pub fn from(item: T, pos: impl Into<SrcPos>) -> WithPos<T> {
        WithPos {
            item,
            pos: pos.into(),
        }
    }

    pub fn map_into<F, U>(self, f: F) -> WithPos<U>
    where
        F: FnOnce(T) -> U,
    {
        WithPos {
            item: f(self.item),
            pos: self.pos,
        }
    }
    pub fn try_map_into<F, U>(self, f: F) -> ParseResult<WithPos<U>>
    where
        F: FnOnce(T) -> Result<U, String>,
    {
        match f(self.item) {
            Ok(item) => Ok(WithPos {
                item,
                pos: self.pos,
            }),
            Err(msg) => Err(Diagnostic::error(&self.pos, msg)),
        }
    }

    pub fn combine_pos_with(self, other: &dyn AsRef<SrcPos>) -> Self {
        WithPos {
            item: self.item,
            pos: self.pos.combine_into(other.as_ref()),
        }
    }
}

impl<T> AsRef<SrcPos> for WithPos<T> {
    fn as_ref(&self) -> &SrcPos {
        &self.pos
    }
}

impl AsRef<SrcPos> for SrcPos {
    fn as_ref(&self) -> &SrcPos {
        self
    }
}

impl<T> Into<SrcPos> for WithPos<T> {
    fn into(self) -> SrcPos {
        self.pos
    }
}

impl SrcPos {
    const LINE_CONTEXT: u64 = 2;

    pub fn new(source: Source, range: Range) -> SrcPos {
        SrcPos { source, range }
    }

    fn get_line_context(
        &self,
        context_lines: u64,
        reader: &mut dyn BufRead,
    ) -> VecDeque<(u64, Latin1String)> {
        let mut lines = VecDeque::new();
        let mut buf = String::new();
        let mut lineno = 0;

        while let Ok(bytes_read) = reader.read_line(&mut buf) {
            if bytes_read == 0 {
                break;
            }

            let line = Latin1String::from_utf8(&buf).unwrap();

            if lineno <= (self.range.end.line + context_lines) {
                if (lineno + context_lines) >= self.range.start.line {
                    lines.push_back((lineno, line.clone()));
                }
            } else {
                break;
            }

            lineno += 1;
            buf.clear();
        }
        if lines.is_empty() {
            lines.push_back((lineno, Latin1String::empty()));
        }
        lines
    }

    fn push_replicate(line: &mut String, chr: char, times: usize) {
        for _ in 0..times {
            line.push(chr);
        }
    }

    fn visual_width(chr: char) -> usize {
        if chr == '\t' {
            4
        } else {
            1
        }
    }

    /// Write ~~~ to underline symbol
    fn underline(&self, lineno_len: usize, lineno: u64, line: &str, into: &mut String) {
        const NEWLINE_SIZE: usize = 1;
        into.reserve("  |  ".len() + lineno_len + line.len() + NEWLINE_SIZE);

        // Prefix
        for _ in 0..lineno_len {
            into.push(' ');
        }
        into.push_str("  |  ");

        let mut pos = Position {
            line: lineno,
            character: 0,
        };

        // Padding before underline
        for chr in line.chars() {
            if pos < self.range.start {
                Self::push_replicate(into, ' ', Self::visual_width(chr));
            } else if pos < self.range.end {
                Self::push_replicate(into, '~', Self::visual_width(chr));
            } else {
                break;
            }
            pos.character += 1;
        }

        if lineno == self.range.end.line {
            while pos < self.range.end {
                into.push('~');
                pos.character += 1;
            }
        }

        // Newline
        into.push_str("\n");
    }

    fn code_context_from_reader(
        &self,
        reader: &mut dyn BufRead,
        context_lines: u64,
    ) -> (usize, String) {
        let lines = self.get_line_context(context_lines, reader);
        use self::pad::{Alignment, PadStr};
        // +1 since lines are shown with 1-index
        let lineno_len = (self.range.start.line + context_lines + 1)
            .to_string()
            .len();

        let mut result = String::new();

        for (lineno, line) in lines.iter() {
            let line = line.to_string();
            let line = line.trim_matches('\n');
            let lineno_str = (lineno + 1)
                .to_string()
                .pad_to_width_with_alignment(lineno_len, Alignment::Right);
            let overlaps = self.range.start.line <= *lineno && *lineno <= self.range.end.line;

            if overlaps {
                write!(result, "{} --> ", lineno_str).unwrap();
            } else {
                write!(result, "{}  |  ", lineno_str).unwrap();
            }

            for chr in line.trim_end().chars() {
                if chr == '\t' {
                    Self::push_replicate(&mut result, ' ', Self::visual_width(chr));
                } else {
                    result.push(chr);
                }
            }
            result.push('\n');

            if overlaps {
                self.underline(lineno_len, *lineno, line, &mut result);
            }
        }

        (lineno_len, result)
    }

    /// Create a string for pretty printing
    pub fn code_context(&self) -> String {
        self.lineno_len_and_code_context().1
    }

    fn lineno_len_and_code_context(&self) -> (usize, String) {
        let latin1 = self.source.contents().unwrap();
        self.code_context_from_reader(&mut latin1.to_string().as_bytes(), Self::LINE_CONTEXT)
    }

    pub fn show(&self, message: &str) -> String {
        let (lineno_len, pretty_str) = self.lineno_len_and_code_context();
        let file_name = self.source.file_name();
        let mut result = String::new();

        let lineno = self.range.start.line;
        writeln!(result, "{}", &message).unwrap();
        for _ in 0..lineno_len {
            result.push(' ');
        }
        writeln!(result, " --> {}:{}", file_name, lineno + 1).unwrap();
        for _ in 0..lineno_len {
            result.push(' ');
        }
        writeln!(result, "  |").unwrap();
        result.push_str(&pretty_str);
        result
    }

    /// Combines two lexical positions into a larger legical position overlapping both
    /// The file name is assumed to be the same
    pub fn combine_into(self, other: &dyn AsRef<Self>) -> Self {
        let other = other.as_ref();
        debug_assert!(self.source == other.source, "Assumes sources are equal");

        let start = min(self.range.start, other.range.start);
        let end = max(self.range.end, other.range.end);

        SrcPos {
            source: self.source,
            range: Range { start, end },
        }
    }

    pub fn start(&self) -> Position {
        self.range.start
    }

    pub fn end(&self) -> Position {
        self.range.end
    }

    pub fn range(&self) -> Range {
        self.range
    }

    pub fn combine(&self, other: &dyn AsRef<Self>) -> Self {
        self.clone().combine_into(other)
    }
}

pub trait HasSource {
    fn source(&self) -> &Source;
}

impl HasSource for Source {
    fn source(&self) -> &Source {
        &self
    }
}

pub trait HasSrcPos {
    fn pos(&self) -> &SrcPos;
}

impl HasSrcPos for SrcPos {
    fn pos(&self) -> &SrcPos {
        &self
    }
}

impl<T: HasSrcPos> HasSource for T {
    fn source(&self) -> &Source {
        &self.pos().source
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_util::{Code, CodeBuilder};
    use pretty_assertions::assert_eq;
    use tempfile;

    #[test]
    fn srcpos_combine() {
        let code = Code::new("hello world");

        assert_eq!(
            code.s1("hello").pos().combine(&code.s1("world").pos()),
            code.pos()
        );

        assert_eq!(code.s1("h").pos().combine(&code.s1("d").pos()), code.pos());

        assert_eq!(code.s1("d").pos().combine(&code.s1("h").pos()), code.pos());
    }

    fn with_code_from_file<F, R>(contents: &str, fun: F) -> R
    where
        F: Fn(Code) -> R,
    {
        use std::io::Write;
        let mut file = tempfile::NamedTempFile::new().unwrap();
        let file_name = file.path().to_str().unwrap().to_string();
        file.write(&Latin1String::from_utf8_unchecked(contents).bytes)
            .unwrap();
        fun(CodeBuilder::new().code_from_source(Source::from_file(file_name)))
    }

    #[test]
    fn code_context_pos_from_filename() {
        with_code_from_file("hello\nworld\n", |code: Code| {
            assert_eq!(
                code.s1("hello").pos().code_context(),
                "\
1 --> hello
   |  ~~~~~
2  |  world
"
            )
        });
    }

    #[test]
    fn code_context_pos_last_line_without_newline() {
        let code = Code::new("hello world");
        let pos = code.s1("hello").pos();
        assert_eq!(
            pos.code_context(),
            "\
1 --> hello world
   |  ~~~~~
"
        );
    }

    #[test]
    fn code_context_pos_with_indent() {
        let code = Code::new("    hello world");
        let pos = code.s1("hello").pos();
        assert_eq!(
            pos.code_context(),
            "\
1 -->     hello world
   |      ~~~~~
"
        );
    }

    #[test]
    fn code_context_eof() {
        let code = Code::new("h");
        assert_eq!(
            code.eof_pos().code_context(),
            "\
1 --> h
   |   ~
",
        );
    }

    #[test]
    fn code_context_eof_empty() {
        let code = Code::new("");
        assert_eq!(code.eof_pos().code_context(), "1 --> \n   |  ~\n",);
    }

    #[test]
    fn code_context_with_context() {
        let code = Code::new("hello\nworld");
        let pos = code.s1("hello").pos();
        assert_eq!(
            pos.code_context(),
            "\
1 --> hello
   |  ~~~~~
2  |  world
",
        );
    }

    #[test]
    fn code_context_with_tabs() {
        let code = Code::new("\thello\t");
        let pos = code.s1("hello\t").pos();
        assert_eq!(
            pos.code_context(),
            "\
1 -->     hello
   |      ~~~~~~~~~
",
        );
    }

    #[test]
    fn code_context_non_ascii() {
        let code = Code::new("åäö\nåäö\n__å_ä_ö__");
        let substr = code.s1("å_ä_ö");
        assert_eq!(substr.length(), 5);
        assert_eq!(
            substr.pos().code_context(),
            "\
1  |  åäö
2  |  åäö
3 --> __å_ä_ö__
   |    ~~~~~
",
        );
    }

    #[test]
    fn code_context_non_ascii_from_file() {
        with_code_from_file("åäö\nåäö\n__å_ä_ö__", |code: Code| {
            let substr = code.s1("å_ä_ö");
            assert_eq!(substr.length(), 5);
            assert_eq!(
                substr.pos().code_context(),
                "\
1  |  åäö
2  |  åäö
3 --> __å_ä_ö__
   |    ~~~~~
",
            );
        });
    }

    #[test]
    fn code_context_with_full_context() {
        let code = Code::new(
            "\
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
line11
line12
line13",
        );
        let pos = code.s1("line10").pos();
        assert_eq!(
            pos.code_context(),
            " \
 8  |  line8
 9  |  line9
10 --> line10
    |  ~~~~~~
11  |  line11
12  |  line12
",
        );
    }

    #[test]
    fn show_from_filename() {
        with_code_from_file("hello\nworld\nline\n", |code: Code| {
            assert_eq!(
                code.s1("world").pos().show("Greetings"),
                format!(
                    "\
Greetings
  --> {}:2
   |
1  |  hello
2 --> world
   |  ~~~~~
3  |  line
",
                    code.source().file_name()
                )
            )
        });
    }

    #[test]
    fn show_contents() {
        let code = Code::new("hello\nworld\nline\n");
        assert_eq!(
            code.s1("world").pos().show("Greetings"),
            format!(
                "\
Greetings
  --> {}:2
   |
1  |  hello
2 --> world
   |  ~~~~~
3  |  line
",
                code.source().file_name()
            )
        );
    }
}