vhdl_parser 0.13.0

VHDL Parser
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
// 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 super::common::ParseResult;
use super::concurrent_statement::parse_labeled_concurrent_statement;
use super::context::{parse_library_clause, parse_use_clause};
use super::declarative_part::parse_declarative_part_leave_end_token;
use super::design_unit::parse_design_file;
use super::expression::{parse_aggregate, parse_choices, parse_expression};
use super::interface_declaration::{parse_generic, parse_parameter, parse_port};
use super::names::{parse_association_list, parse_designator, parse_name, parse_selected_name};
use super::range::{parse_discrete_range, parse_range};
use super::sequential_statement::parse_sequential_statement;
use super::subprogram::{parse_signature, parse_subprogram_declaration_no_semi};
use super::subtype_indication::parse_subtype_indication;
use super::tokens::{Comment, Symbols, Token, TokenStream, Tokenizer};
use super::waveform::parse_waveform;
use crate::ast;
use crate::ast::*;
use crate::data::Range;
use crate::data::*;
use std::collections::hash_map::DefaultHasher;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt::Debug;
use std::hash::Hasher;
use std::sync::Arc;

pub struct CodeBuilder {
    pub symbols: Arc<Symbols>,
}

impl CodeBuilder {
    pub fn new() -> CodeBuilder {
        CodeBuilder {
            symbols: Arc::new(Symbols::new()),
        }
    }

    pub fn code_from_source(&self, source: Source) -> Code {
        let contents = source.contents();

        let pos = SrcPos::new(source.clone(), contents.range());

        let code = Code {
            symbols: self.symbols.clone(),
            pos,
        };

        // Ensure symbol table is populated
        code.tokenize_result();
        code
    }

    pub fn code_with_file_name(&self, file_name: &Path, code: &str) -> Code {
        self.code_from_source(Source::inline(file_name, code))
    }

    pub fn code(&self, code: &str) -> Code {
        let mut hasher = DefaultHasher::new();
        hasher.write(code.as_bytes());
        let file_name: PathBuf = format!("<unknown file with hash {}>", hasher.finish()).into();
        self.code_with_file_name(&file_name, code)
    }

    pub fn symbol(&self, name: &str) -> Symbol {
        self.symbols.symtab().insert_utf8(name)
    }
}

#[derive(Clone)]
pub struct Code {
    pub symbols: Arc<Symbols>,
    pos: SrcPos,
}

impl Code {
    pub fn new(code: &str) -> Code {
        CodeBuilder::new().code(code)
    }

    pub fn new_with_file_name(file_name: &Path, code: &str) -> Code {
        CodeBuilder::new().code_with_file_name(file_name, code)
    }

    fn in_range(&self, range: Range) -> Code {
        Code {
            symbols: self.symbols.clone(),
            pos: SrcPos::new(self.pos.source.clone(), range),
        }
    }

    /// Create new Code from n:th occurence of substr
    pub fn s(&self, substr: &str, occurence: usize) -> Code {
        self.in_range(substr_range(
            &self.pos.source,
            self.pos.range(),
            substr,
            occurence,
        ))
    }

    /// Create new Code from first n:th occurence of substr
    pub fn s1(&self, substr: &str) -> Code {
        self.s(substr, 1)
    }

    pub fn pos(&self) -> SrcPos {
        self.pos.clone()
    }

    // Position after code
    pub fn eof_pos(&self) -> SrcPos {
        SrcPos::new(
            self.source().clone(),
            Range::new(self.end(), self.end().next_char()),
        )
    }

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

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

    pub fn source(&self) -> &Source {
        &self.pos.source
    }

    /// Helper method to test tokenization functions
    pub fn tokenize_result(&self) -> (Vec<Result<Token, Diagnostic>>, Vec<Comment>) {
        let mut tokens = Vec::new();
        let final_comments: Vec<Comment>;
        {
            let contents = self.pos.source.contents();
            let reader = ContentReader::new(&contents);
            let mut tokenizer = Tokenizer::new(&self.symbols, &self.pos.source, reader);
            loop {
                let token = tokenizer.pop();

                match token {
                    Ok(None) => break,
                    Ok(Some(token)) => tokens.push(Ok(token)),
                    Err(err) => tokens.push(Err(err)),
                }
            }
            match tokenizer.get_final_comments() {
                Some(comments) => final_comments = comments,
                None => panic!("Tokenizer failed to check for final comments."),
            }
        }
        (tokens, final_comments)
    }

    /// Tokenize and check that there are no errors, ignore final comments
    pub fn tokenize(&self) -> Vec<Token> {
        let tokens = self.tokenize_result().0;
        tokens.into_iter().map(|tok| tok.unwrap()).collect()
    }

    /// Helper method to run lower level parsing function at specific substring
    pub fn parse<F, R>(&self, parse_fun: F) -> R
    where
        F: FnOnce(&mut TokenStream) -> R,
    {
        let contents = self.pos.source.contents();
        let source = Source::from_contents(
            self.pos.file_name(),
            contents.crop(Range::new(Position::default(), self.pos.end())),
        );
        let contents = source.contents();
        let reader = ContentReader::new(&contents);
        let tokenizer = Tokenizer::new(&self.symbols, &source, reader);
        let mut stream = TokenStream::new(tokenizer);
        forward(&mut stream, self.pos.start());
        parse_fun(&mut stream)
    }

    /// Expect Ok() value
    pub fn parse_ok<F, R>(&self, parse_fun: F) -> R
    where
        F: FnOnce(&mut TokenStream) -> ParseResult<R>,
    {
        match self.parse(parse_fun) {
            Ok(res) => res,
            Err(diagnostic) => {
                panic!("{}", diagnostic.show());
            }
        }
    }

    pub fn with_partial_stream<F, R>(&self, parse_fun: F) -> R
    where
        F: FnOnce(&mut TokenStream) -> R,
    {
        let contents = self.pos.source.contents();
        let reader = ContentReader::new(&contents);
        let tokenizer = Tokenizer::new(&self.symbols, &self.pos.source, reader);
        let mut stream = TokenStream::new(tokenizer);
        parse_fun(&mut stream)
    }

    pub fn with_stream<F, R>(&self, parse_fun: F) -> R
    where
        R: Debug,
        F: FnOnce(&mut TokenStream) -> ParseResult<R>,
    {
        let parse_fun_eof = |stream: &mut TokenStream| {
            let result = parse_fun(stream);
            match result {
                Err(err) => {
                    println!("{:#?}", err);
                    println!("{}", err.show());
                    panic!("Got Err()");
                }
                Ok(result) => {
                    if let Some(token) = stream.peek().unwrap() {
                        println!("result = {:#?}", result);
                        panic!("Expected EOF got {:?}", token);
                    }
                    result
                }
            }
        };

        self.with_partial_stream(parse_fun_eof)
    }

    pub fn with_stream_err<F, R>(&self, parse_fun: F) -> Diagnostic
    where
        R: Debug,
        F: FnOnce(&mut TokenStream) -> ParseResult<R>,
    {
        let parse_fun_eof = |stream: &mut TokenStream| {
            let result = parse_fun(stream);
            match result {
                Err(err) => {
                    if let Some(token) = stream.peek().unwrap() {
                        println!("err = {:#?}", err);
                        panic!("Expected EOF got {:?}", token);
                    }
                    err
                }
                Ok(result) => {
                    panic!("Expected error got {:?}", result);
                }
            }
        };

        self.with_partial_stream(parse_fun_eof)
    }

    pub fn with_partial_stream_diagnostics<F, R>(&self, parse_fun: F) -> (R, Vec<Diagnostic>)
    where
        R: Debug,
        F: FnOnce(&mut TokenStream, &mut dyn DiagnosticHandler) -> R,
    {
        let mut diagnostics = Vec::new();
        let result = self
            .with_partial_stream(|stream: &mut TokenStream| parse_fun(stream, &mut diagnostics));
        (result, diagnostics)
    }

    pub fn with_stream_diagnostics<F, R>(&self, parse_fun: F) -> (R, Vec<Diagnostic>)
    where
        R: Debug,
        F: FnOnce(&mut TokenStream, &mut dyn DiagnosticHandler) -> ParseResult<R>,
    {
        let mut diagnostics = Vec::new();
        let result =
            self.with_stream(|stream: &mut TokenStream| parse_fun(stream, &mut diagnostics));
        (result, diagnostics)
    }

    pub fn with_stream_no_diagnostics<F, R>(&self, parse_fun: F) -> R
    where
        R: Debug,
        F: FnOnce(&mut TokenStream, &mut dyn DiagnosticHandler) -> ParseResult<R>,
    {
        let (result, diagnostics) = self.with_stream_diagnostics(parse_fun);
        check_no_diagnostics(&diagnostics);
        result
    }

    pub fn declarative_part(&self) -> Vec<Declaration> {
        let mut diagnostics = Vec::new();
        let res = self
            .parse_ok(|stream| parse_declarative_part_leave_end_token(stream, &mut diagnostics));
        check_no_diagnostics(&diagnostics);
        res
    }
    /// Helper to create a identifier at first occurence of name
    pub fn ident(&self) -> Ident {
        self.parse_ok(|stream: &mut TokenStream| stream.expect_ident())
    }

    pub fn designator(&self) -> WithPos<Designator> {
        self.parse_ok(parse_designator)
    }

    pub fn designator_ref(&self) -> WithPos<WithRef<Designator>> {
        self.parse_ok(parse_designator).into_ref()
    }

    pub fn character(&self) -> WithPos<u8> {
        self.parse_ok(|stream: &mut TokenStream| stream.expect()?.expect_character())
    }

    /// Helper method to create expression from first occurence of substr
    /// Can be used to test all but expression parsing
    pub fn expr(&self) -> WithPos<Expression> {
        self.parse_ok(parse_expression)
    }

    pub fn name(&self) -> WithPos<Name> {
        self.parse_ok(parse_name)
    }

    pub fn selected_name(&self) -> WithPos<SelectedName> {
        self.parse_ok(parse_selected_name)
    }

    pub fn signature(&self) -> Signature {
        self.parse_ok(parse_signature)
    }

    /// Return symbol from symbol table
    pub fn symbol(&self, name: &str) -> Symbol {
        self.symbols.symtab().insert_utf8(name)
    }

    pub fn subtype_indication(&self) -> SubtypeIndication {
        self.parse_ok(parse_subtype_indication)
    }

    pub fn port(&self) -> InterfaceDeclaration {
        self.parse_ok(parse_port)
    }

    pub fn generic(&self) -> InterfaceDeclaration {
        self.parse_ok(parse_generic)
    }

    pub fn parameter(&self) -> InterfaceDeclaration {
        self.parse_ok(parse_parameter)
    }

    pub fn function_call(&self) -> FunctionCall {
        let name = self.name();
        match name.item {
            Name::FunctionCall(call) => *call,
            _ => FunctionCall {
                name,
                parameters: vec![],
            },
        }
    }

    pub fn parse_ok_no_diagnostics<F, R>(&self, parse_fun: F) -> R
    where
        F: FnOnce(&mut TokenStream, &mut dyn DiagnosticHandler) -> ParseResult<R>,
    {
        let mut diagnostics = Vec::new();
        let res = self.parse_ok(|stream| parse_fun(stream, &mut diagnostics));
        check_no_diagnostics(&diagnostics);
        res
    }

    pub fn sequential_statement(&self) -> LabeledSequentialStatement {
        self.parse_ok_no_diagnostics(parse_sequential_statement)
    }

    pub fn concurrent_statement(&self) -> LabeledConcurrentStatement {
        self.parse_ok_no_diagnostics(parse_labeled_concurrent_statement)
    }

    pub fn association_list(&self) -> Vec<AssociationElement> {
        self.parse_ok(parse_association_list)
    }

    pub fn waveform(&self) -> Waveform {
        self.parse_ok(parse_waveform)
    }

    pub fn aggregate(&self) -> WithPos<Vec<ElementAssociation>> {
        self.parse_ok(|stream| parse_aggregate(stream))
    }

    pub fn range(&self) -> ast::Range {
        self.parse_ok(parse_range).item
    }

    pub fn discrete_range(&self) -> DiscreteRange {
        self.parse_ok(parse_discrete_range)
    }

    pub fn choices(&self) -> Vec<Choice> {
        self.parse_ok(parse_choices)
    }

    pub fn use_clause(&self) -> WithPos<UseClause> {
        self.parse_ok(parse_use_clause)
    }

    pub fn library_clause(&self) -> WithPos<LibraryClause> {
        self.parse_ok(parse_library_clause)
    }

    pub fn design_file(&self) -> DesignFile {
        self.parse_ok_no_diagnostics(parse_design_file)
    }

    pub fn subprogram_decl(&self) -> SubprogramDeclaration {
        self.parse_ok_no_diagnostics(parse_subprogram_declaration_no_semi)
    }

    pub fn attribute_name(&self) -> AttributeName {
        match self.parse_ok(parse_name).item {
            Name::Attribute(attr) => *attr,
            name => panic!("Expected attribute got {:?}", name),
        }
    }
}

fn substr_range(source: &Source, range: Range, substr: &str, occurence: usize) -> Range {
    let contents = source.contents();
    let mut reader = ContentReader::new(&contents);
    let mut count = occurence;

    reader.seek_pos(range.start);

    while reader.pos() < range.end {
        if reader.matches(&substr) {
            count -= 1;
            if count == 0 {
                let start = reader.pos();
                for _ in substr.chars() {
                    reader.skip();
                }
                if reader.pos() <= range.end {
                    return Range::new(start, reader.pos());
                }
            }
        }

        reader.skip();
    }

    panic!(
        "Could not find occurence {} of substring {:?}",
        occurence, substr
    );
}

/// Fast forward tokenstream until position
fn forward(stream: &mut TokenStream, start: Position) {
    loop {
        let token = stream.peek_expect().unwrap();
        if token.pos.start() >= start {
            break;
        }
        stream.move_after(&token);
    }
}

/// Check that no errors where found
pub fn check_no_diagnostics(diagnostics: &Vec<Diagnostic>) {
    for err in diagnostics.iter() {
        println!("{}", err.show());
    }
    if diagnostics.len() > 0 {
        panic!("Found errors");
    }
}

/// Create map from diagnostic -> count
fn diagnostics_to_map(diagnostics: Vec<Diagnostic>) -> HashMap<Diagnostic, usize> {
    let mut map = HashMap::new();
    for diagnostic in diagnostics {
        match map.entry(diagnostic) {
            Entry::Occupied(mut entry) => {
                let count = *entry.get() + 1;
                entry.insert(count);
            }
            Entry::Vacant(entry) => {
                entry.insert(1);
            }
        }
    }
    map
}

/// Check diagnostics are equal without considering order
pub fn check_diagnostics(got: Vec<Diagnostic>, expected: Vec<Diagnostic>) {
    let mut expected = diagnostics_to_map(expected);
    let mut got = diagnostics_to_map(got);

    let mut found_errors = false;

    for (diagnostic, count) in expected.drain() {
        match got.remove(&diagnostic) {
            Some(got_count) => {
                if count != got_count {
                    found_errors = true;
                    println!("-------------------------------------------------------");
                    println!(
                        "Got right diagnostic but wrong count {}, expected {}",
                        got_count, count
                    );
                    println!("-------------------------------------------------------");
                    println!("{}", diagnostic.show());
                }
            }
            None => {
                found_errors = true;
                println!("-------------------------------------------------------");
                println!("Got no diagnostic, expected {}", count);
                println!("-------------------------------------------------------");
                println!("{}", diagnostic.show());
            }
        }
    }

    for (diagnostic, _) in got.drain() {
        found_errors = true;
        println!("-------------------------------------------------------");
        println!("Got unexpected diagnostic");
        println!("-------------------------------------------------------");
        println!("{}", diagnostic.show());
    }

    if found_errors {
        panic!("Found diagnostic mismatch");
    }
}

fn compare_unordered<T: PartialEq + Debug>(got: &[T], expected: &[T]) -> bool {
    if got.len() != expected.len() {
        return false;
    }
    for exp in expected.iter() {
        if !got.contains(exp) {
            return false;
        }
    }
    true
}

pub fn assert_eq_unordered<T: PartialEq + Debug>(got: &[T], expected: &[T]) {
    if !compare_unordered(got, expected) {
        panic!(
            "\ngot(len={}): {:?}\nexp(len={}): {:?}",
            got.len(),
            got,
            expected.len(),
            expected
        );
    }
}

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

mod tests {
    use super::*;

    #[test]
    fn check_diagnostics_ok() {
        let code = Code::new("foo bar");
        check_diagnostics(
            vec![Diagnostic::error(code.s1("foo"), "hello")],
            vec![Diagnostic::error(code.s1("foo"), "hello")],
        )
    }

    #[test]
    fn check_diagnostics_ok_out_of_order() {
        let code = Code::new("foo bar");
        check_diagnostics(
            vec![
                Diagnostic::error(code.s1("foo"), "hello"),
                Diagnostic::error(code.s1("bar"), "msg"),
            ],
            vec![
                Diagnostic::error(code.s1("bar"), "msg"),
                Diagnostic::error(code.s1("foo"), "hello"),
            ],
        )
    }

    #[test]
    #[should_panic]
    fn check_diagnostics_not_ok_mismatch() {
        let code = Code::new("foo bar");
        check_diagnostics(
            vec![Diagnostic::error(code.s1("bar"), "msg")],
            vec![Diagnostic::error(code.s1("foo"), "hello")],
        )
    }

    #[test]
    #[should_panic]
    fn check_diagnostics_not_ok_count_mismatch() {
        let code = Code::new("foo bar");
        check_diagnostics(
            vec![
                Diagnostic::error(code.s1("bar"), "msg"),
                Diagnostic::error(code.s1("bar"), "msg"),
            ],
            vec![Diagnostic::error(code.s1("bar"), "msg")],
        )
    }

    #[test]
    #[should_panic]
    fn check_diagnostics_not_ok_missing() {
        let code = Code::new("foo bar");
        check_diagnostics(
            vec![Diagnostic::error(code.s1("bar"), "msg")],
            vec![
                Diagnostic::error(code.s1("bar"), "msg"),
                Diagnostic::error(code.s1("bar"), "missing"),
            ],
        )
    }

    #[test]
    #[should_panic]
    fn check_diagnostics_not_ok_unexpected() {
        let code = Code::new("foo bar");
        check_diagnostics(
            vec![
                Diagnostic::error(code.s1("bar"), "msg"),
                Diagnostic::error(code.s1("bar"), "unexpected"),
            ],
            vec![Diagnostic::error(code.s1("bar"), "msg")],
        )
    }
}