vhdl_lang 0.72.0

VHDL Language Frontend
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
// 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 std::cell::Cell;

use super::tokenizer::Kind::*;
use super::tokenizer::*;
use crate::ast::{AttributeDesignator, Ident, RangeAttribute, TypeAttribute};
use crate::data::{DiagnosticHandler, DiagnosticResult, WithPos};
use crate::{Diagnostic, SrcPos};

pub struct TokenStream<'a> {
    tokenizer: Tokenizer<'a>,
    idx: Cell<usize>,
    tokens: Vec<Token>,
    // This is the offset that a token's ID should be adapted
    // when getting it via `TokenStream::get_token_id()`
    // It is updated in the `slice_tokens` method
    token_offset: Cell<usize>,
}

impl<'a> TokenStream<'a> {
    /// Special handling for a tool directive of the form
    /// ```vhdl
    /// `identifier { any chars until newline }
    /// ```
    /// This needs special handling as the text that follows the identifier is arbitrary.
    fn handle_tool_directive(
        grave_accent: Token,
        tokenizer: &mut Tokenizer,
        diagnostics: &mut dyn DiagnosticHandler,
    ) {
        let start_pos = grave_accent.pos.clone();
        match tokenizer.pop() {
            Ok(Some(tok)) => {
                if tok.kind != Identifier {
                    diagnostics.error(tok, "Expecting identifier");
                    let _ = tokenizer.text_until_newline(); // skip potentially invalid tokens
                    return;
                }
            }
            Err(err) => diagnostics.push(err),
            Ok(None) => {
                diagnostics.error(start_pos, "Expecting identifier");
                return;
            }
        }
        match tokenizer.text_until_newline() {
            Ok(_) => {}
            Err(err) => diagnostics.push(err),
        }
    }

    pub fn new(
        mut tokenizer: Tokenizer<'a>,
        diagnostics: &mut dyn DiagnosticHandler,
    ) -> TokenStream<'a> {
        let mut tokens = Vec::new();
        loop {
            match tokenizer.pop() {
                Ok(Some(token)) if token.kind == GraveAccent => {
                    TokenStream::handle_tool_directive(token, &mut tokenizer, diagnostics)
                }
                Ok(Some(token)) => tokens.push(token),
                Ok(None) => break,
                Err(err) => diagnostics.push(err),
            }
        }
        TokenStream {
            tokenizer,
            idx: Cell::new(0),
            tokens,
            token_offset: Cell::new(0),
        }
    }

    pub fn state(&self) -> usize {
        self.get_idx()
    }

    pub fn set_state(&self, state: usize) {
        self.set_idx(state);
    }

    pub fn skip(&self) {
        self.set_idx(self.get_idx() + 1)
    }

    fn get_idx(&self) -> usize {
        self.idx.get()
    }

    fn set_idx(&self, idx: usize) {
        self.idx.replace(idx);
    }

    pub fn peek(&self) -> Option<&Token> {
        self.tokens.get(self.get_idx())
    }

    pub fn get_token_id(&self) -> TokenId {
        TokenId::new(self.get_idx() - self.token_offset.get())
    }

    pub fn last(&self) -> Option<&Token> {
        let last_idx = self.get_idx().checked_sub(1)?;
        self.tokens.get(last_idx)
    }

    fn eof_error(&self) -> Diagnostic {
        let end = self.tokenizer.source.contents().end();
        Diagnostic::error(
            self.tokenizer.source.pos(end, end.next_char()),
            "Unexpected EOF",
        )
    }

    fn idx_of(&self, token: &Token) -> Option<usize> {
        let base = self.tokens.as_ptr() as usize;
        let ptr = (token as *const Token) as usize;
        let idx = ptr.checked_sub(base)? / std::mem::size_of::<Token>();

        if idx < self.tokens.len() {
            Some(idx)
        } else {
            None
        }
    }

    fn token_before(&self, token: &Token) -> Option<&Token> {
        let idx = self.idx_of(token)?;
        self.tokens.get(idx.wrapping_sub(1))
    }

    /// A position that aligns with the previous token
    ///
    /// Example:
    ///  signal sig : natural
    ///                      ~ <- want semi colon error here
    ///  signal
    ///  ~~~~~~ <- not here
    pub fn pos_before(&self, token: &Token) -> SrcPos {
        if let Some(prev_token) = self.token_before(token) {
            let prev_pos = prev_token.pos.end();

            if prev_pos.line != token.pos.range.start.line {
                return prev_token.pos.pos_at_end();
            }
        }

        token.pos.clone()
    }

    pub fn expect_kind(&self, kind: Kind) -> DiagnosticResult<TokenId> {
        if let Some(token) = self.peek() {
            if token.kind == kind {
                let id = self.get_token_id();
                self.skip();
                Ok(id)
            } else {
                Err(kinds_error(self.pos_before(token), &[kind]))
            }
        } else {
            Err(self
                .eof_error()
                .when(format!("expecting {}", kinds_str(&[kind]))))
        }
    }

    pub fn peek_expect(&self) -> DiagnosticResult<&Token> {
        if let Some(token) = self.peek() {
            Ok(token)
        } else {
            Err(self.eof_error())
        }
    }

    pub fn peek_kind(&self) -> Option<Kind> {
        self.peek().map(|token| token.kind)
    }

    pub fn next_kind_is(&self, kind: Kind) -> bool {
        self.nth_kind_is(0, kind)
    }

    pub fn nth_kind_is(&self, idx: usize, kind: Kind) -> bool {
        if let Some(token) = self.tokens.get(self.get_idx() + idx) {
            token.kind == kind
        } else {
            false
        }
    }

    pub fn next_kinds_are(&self, kinds: &[Kind]) -> bool {
        kinds
            .iter()
            .enumerate()
            .all(|(idx, kind)| self.nth_kind_is(idx, *kind))
    }

    pub fn pop_if_kind(&self, kind: Kind) -> Option<TokenId> {
        if let Some(token) = self.peek() {
            if token.kind == kind {
                let id = self.get_token_id();
                self.skip();
                return Some(id);
            }
        }
        None
    }

    pub fn skip_if_kind(&self, kind: Kind) -> bool {
        self.pop_if_kind(kind).is_some()
    }

    pub fn skip_until<F>(&self, cond: F) -> DiagnosticResult<()>
    where
        F: Fn(Kind) -> bool,
    {
        loop {
            let token = self.peek_expect()?;
            if cond(token.kind) {
                return Ok(());
            }
            self.skip();
        }
    }

    pub fn pop_optional_ident(&self) -> Option<Ident> {
        self.pop_if_kind(Identifier)
            .map(|id| self.get_token(id).to_identifier_value().unwrap())
    }

    pub fn expect_ident(&self) -> DiagnosticResult<Ident> {
        expect_token!(self, token, Identifier => token.to_identifier_value())
    }

    /// Expect identifier or subtype/range keywords
    /// foo'subtype or foo'range
    pub fn expect_attribute_designator(&self) -> DiagnosticResult<WithPos<AttributeDesignator>> {
        let des = expect_token!(
            self,
            token,
            Identifier => {
                let ident = token.to_identifier_value()?;
                ident.map_into(|sym| self.tokenizer.attribute(sym))
            },
            Subtype => WithPos::new(AttributeDesignator::Type(TypeAttribute::Subtype), token.pos.clone()),
            Range => WithPos::new(AttributeDesignator::Range(RangeAttribute::Range), token.pos.clone())
        );
        Ok(des)
    }

    /// Slices the tokens until the current position.
    /// The token at the current position is not included.
    /// Subsequent calls to `slice_tokens` will start from the current position.
    ///
    /// Note that The function `TokenStream::get_token(TokenId)` only returns a token
    /// for `TokenId`s that are obtained after `slice_tokens` was called.
    ///
    /// # Example
    ///
    /// ```vhdl
    /// 1 2 abc; tok x
    ///          ^
    ///          current position
    /// ```
    /// After calling `slice_tokens`, the returned vec is `[1, 2, abc, ;]`.
    ///
    /// ```vhdl
    /// 1 2 abc; tok x
    ///                ^
    ///                current position (EOF)
    /// ```
    /// After calling `slice_tokens` again, the returned vec is `[tok x]`
    pub fn slice_tokens(&self) -> Vec<Token> {
        let vec = Vec::from(&self.tokens[self.token_offset.get()..self.state()]);
        self.token_offset.replace(self.state());
        vec
    }
}

impl<'a> TokenAccess for TokenStream<'a> {
    fn get_token(&self, id: TokenId) -> &Token {
        self.tokens[self.token_offset.get()..].get_token(id)
    }
}

pub trait Recover<T> {
    fn or_recover_until<F>(
        self,
        stream: &TokenStream,
        msgs: &mut dyn DiagnosticHandler,
        cond: F,
    ) -> DiagnosticResult<T>
    where
        F: Fn(Kind) -> bool;

    fn log(self, msgs: &mut dyn DiagnosticHandler);
}

impl<T> Recover<T> for DiagnosticResult<T> {
    fn or_recover_until<F>(
        self,
        stream: &TokenStream,
        msgs: &mut dyn DiagnosticHandler,
        cond: F,
    ) -> DiagnosticResult<T>
    where
        F: Fn(Kind) -> bool,
    {
        match self {
            Ok(res) => Ok(res),
            Err(ref original_err) => {
                let res = stream.skip_until(cond);
                match res {
                    Ok(_) => self,
                    Err(err) => {
                        msgs.push(original_err.clone());
                        Err(err)
                    }
                }
            }
        }
    }

    fn log(self, msgs: &mut dyn DiagnosticHandler) {
        if let Err(err) = self {
            msgs.push(err)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::{ContentReader, Diagnostic, NoDiagnostics};
    use crate::syntax::test::Code;
    use itertools::Itertools;

    macro_rules! new_stream {
        ($code:ident, $stream:ident) => {
            let source = $code.source();
            let contents = source.contents();
            let tokenizer = Tokenizer::new(&$code.symbols, source, ContentReader::new(&contents));
            let $stream = TokenStream::new(tokenizer, &mut NoDiagnostics);
        };
        ($code:ident, $stream:ident, $diagnostics:ident) => {
            let source = $code.source();
            let contents = source.contents();
            let tokenizer = Tokenizer::new(&$code.symbols, source, ContentReader::new(&contents));
            let $stream = TokenStream::new(tokenizer, &mut $diagnostics);
        };
    }

    #[test]
    fn pop_and_peek() {
        let code = Code::new("hello world again");
        let tokens = code.tokenize();
        new_stream!(code, stream);

        stream.skip();
        assert_eq!(stream.peek(), Some(&tokens[1]));
        stream.skip();
        assert_eq!(stream.peek(), Some(&tokens[2]));
        stream.skip();
        assert_eq!(stream.peek(), None);
        stream.skip();
        assert_eq!(stream.peek(), None);
    }

    #[test]
    fn idx_of() {
        let code = Code::new("hello world again");
        new_stream!(code, stream);

        let mut idx = 0;
        while let Some(token) = stream.peek() {
            assert_eq!(idx, stream.idx_of(token).unwrap());
            idx += 1;
            stream.skip();
        }
    }

    #[test]
    fn prev_token() {
        let code = Code::new("hello world again");
        new_stream!(code, stream);

        let mut prev = None;
        while let Some(token) = stream.peek() {
            if let Some(prev) = prev {
                assert_eq!(prev, stream.token_before(token).unwrap());
            }
            prev = Some(token);
            stream.skip();
        }
    }

    #[test]
    fn is_peek_kinds() {
        let code = Code::new("hello 1 +");
        new_stream!(code, stream);

        assert!(stream.next_kinds_are(&[Identifier, AbstractLiteral, Plus]),);
        assert!(stream.next_kinds_are(&[Identifier, AbstractLiteral]));
        assert!(stream.next_kinds_are(&[Identifier]));
        assert!(!stream.next_kinds_are(&[Identifier, AbstractLiteral, AbstractLiteral]),);
        assert!(!stream.next_kinds_are(&[AbstractLiteral]));
    }

    #[test]
    fn expect() {
        let code = Code::new("hello");
        let tokens = code.tokenize();
        new_stream!(code, stream);

        assert_eq!(stream.peek_expect(), Ok(&tokens[0]));
        stream.skip();
        assert_eq!(
            stream.peek_expect(),
            Err(Diagnostic::error(code.eof_pos(), "Unexpected EOF"))
        );
    }

    #[test]
    fn set_state_taken_before_peek() {
        let code = Code::new("hello world");
        let tokens = code.tokenize();
        new_stream!(code, stream);

        let state = stream.state();
        assert_eq!(stream.peek(), Some(&tokens[0]));
        stream.skip();
        assert_eq!(stream.peek(), Some(&tokens[1]));
        stream.set_state(state);
        assert_eq!(stream.peek(), Some(&tokens[0]));
    }

    #[test]
    fn expect_when_eof_empty() {
        let code = Code::new("");
        new_stream!(code, stream);

        assert_eq!(
            stream.peek_expect(),
            Err(Diagnostic::error(code.eof_pos(), "Unexpected EOF"))
        );
    }

    #[test]
    fn expect_eof_after_whitespace() {
        let code = Code::new("a  ");
        new_stream!(code, stream);

        stream.skip();
        assert_eq!(
            stream.peek_expect(),
            Err(Diagnostic::error(code.eof_pos(), "Unexpected EOF"))
        );
    }

    #[test]
    fn expect_eof_after_comment() {
        let code = Code::new("a  -- foo");
        new_stream!(code, stream);

        stream.skip();
        assert_eq!(
            stream.peek_expect(),
            Err(Diagnostic::error(code.eof_pos(), "Unexpected EOF"))
        );
    }

    #[test]
    fn skip_until() {
        let code = Code::new("a begin for + ;");
        new_stream!(code, stream);

        assert!(stream.skip_until(|ref k| matches!(k, Plus)).is_ok());
        assert_eq!(stream.peek().map(|t| t.kind), Some(Plus));
    }

    #[test]
    fn tokenize_simple_identifier_directive() {
        let code = Code::new("`protect begin");
        new_stream!(code, _stream);
    }

    #[test]
    fn tokenize_extended_identifier_directive() {
        let code = Code::new("`\\extended ident\\ begin other words");
        new_stream!(code, _stream);
    }

    #[test]
    fn tokenize_directive_illegal_identifier() {
        let code = Code::new("`123 begin other words");
        let mut diagnostics: Vec<Diagnostic> = vec![];
        new_stream!(code, _stream, diagnostics);
        assert_eq!(
            diagnostics,
            vec![Diagnostic::error(code.s1("123"), "Expecting identifier")]
        )
    }

    #[test]
    fn tokenize_directive_then_end_of_stream() {
        let code = Code::new("`");
        let mut diagnostics: Vec<Diagnostic> = vec![];
        new_stream!(code, _stream, diagnostics);
        assert_eq!(
            diagnostics,
            vec![Diagnostic::error(code.s1("`"), "Expecting identifier")]
        )
    }

    #[test]
    fn pop_tokens() {
        let code = Code::new(
            "\
entity my_ent is
end entity my_ent;

architecture arch of my_ent is
end arch;
        ",
        );
        new_stream!(code, stream);
        stream.skip_until(|it| it == SemiColon).expect("");
        stream.skip();
        assert_eq!(
            stream.slice_tokens().iter().map(|it| it.kind).collect_vec(),
            vec![Entity, Identifier, Is, End, Entity, Identifier, SemiColon]
        );
        stream.skip_until(|it| it == SemiColon).expect("");
        stream.skip();
        assert_eq!(
            stream.slice_tokens().iter().map(|it| it.kind).collect_vec(),
            vec![
                Architecture,
                Identifier,
                Of,
                Identifier,
                Is,
                End,
                Identifier,
                SemiColon
            ]
        );
    }

    #[test]
    fn indexing_tokens_after_slicing() {
        let code = Code::new("1 2 abc; () +");
        new_stream!(code, stream);
        let tokens = code.tokenize();
        assert_eq!(tokens[0], stream.get_token(stream.get_token_id()).clone());
        stream.skip();
        assert_eq!(tokens[1], stream.get_token(stream.get_token_id()).clone());
        stream
            .skip_until(|kind| kind == SemiColon)
            .expect("Unexpected EOF");
        stream.slice_tokens();
        assert_eq!(tokens[3], stream.get_token(stream.get_token_id()).clone());
        stream.skip();
        assert_eq!(tokens[4], stream.get_token(stream.get_token_id()).clone());
    }
}