Skip to main content

ptx_parser/unparser/
mod.rs

1pub(crate) mod common;
2pub(crate) mod function;
3pub(crate) mod instruction;
4pub(crate) mod module;
5pub(crate) mod variable;
6
7use crate::lexer::PtxToken;
8
9#[allow(unused_imports)]
10pub(crate) use common::{
11    push_decimal, push_directive, push_identifier, push_opcode, push_register, push_token_from_str,
12};
13
14pub(crate) fn push_space(tokens: &mut Vec<PtxToken>, spaced: bool) {
15    if spaced {
16        tokens.push(PtxToken::Space);
17    }
18}
19
20pub(crate) fn push_newline(tokens: &mut Vec<PtxToken>, spaced: bool) {
21    if spaced {
22        tokens.push(PtxToken::Newline);
23    }
24}
25
26/// Trait that mirrors [`crate::parser::PtxParser`] but for emitting PTX source
27/// text from the structured representation.
28pub trait PtxUnparser {
29    /// Append the PTX token sequence representing `self` to `tokens`.
30    fn unparse_tokens(&self, tokens: &mut Vec<PtxToken>);
31
32    /// Append tokens, optionally inserting spacing tokens for readability.
33    fn unparse_tokens_mode(&self, tokens: &mut Vec<PtxToken>, spaced: bool) {
34        let _ = spaced;
35        self.unparse_tokens(tokens);
36    }
37
38    /// Convenience helper that returns the serialized PTX token stream.
39    fn to_tokens(&self) -> Vec<PtxToken> {
40        let mut tokens = Vec::new();
41        self.unparse_tokens_mode(&mut tokens, false);
42        tokens
43    }
44
45    /// Convenience helper that returns the serialized PTX token stream with
46    /// spacing/newlines inserted for readability.
47    fn to_tokens_spaced(&self) -> Vec<PtxToken> {
48        let mut tokens = Vec::new();
49        self.unparse_tokens_mode(&mut tokens, true);
50        tokens
51    }
52}