Skip to main content

Crate ptx_parser

Crate ptx_parser 

Source
Expand description

PTX (Parallel Thread Execution) parser for NVIDIA GPU assembly language.

This library provides a complete parser for PTX assembly code, including:

  • Lexical analysis (tokenization)
  • Syntactic parsing into structured types
  • Unparsing back to PTX source code

§Quick Start

use ptx_parser::{parse_ptx};
use ptx_parser::r#type::{Module, ModuleDirective, Instruction};

let source = r#"
    .version 8.5
    .target sm_90
    .address_size 64
     
    .entry kernel() {
        add.s32 %r1, %r2, %r3;
        ret;
    }
"#;

let module: Module = parse_ptx(source).expect("Failed to parse PTX");
println!("Parsed {} directives", module.directives.len());

§Type Organization

All types are re-exported at ptx_parser::r#type::* for easy access:

use ptx_parser::r#type::{
    Module,              // Root AST node
    Instruction,         // Instruction with label/predicate
    Predicate,           // Predicate guard
    Operand,             // Operand types
    EntryFunctionDirective,
    FuncFunctionDirective,
    // ... all other types
};

Instruction variants are under instruction:::

use ptx_parser::r#type::instruction::{Inst, add, mov};

Modules§

pretty_print
span
type
PTX type definitions - Abstract Syntax Tree (AST) nodes.

Macros§

alt
Chain together multiple alternative parsers using the binary alt combinator.
c
Constructor macro - builds a struct with automatic span field
cclosure
Constructor mapping macro - eliminates field repetition
err
Error constructor macro - builds a PtxParseError with automatic span field
func
Function macro - adds span parameter to closures
mapc
Macro to simplify map with cclosure!.
ok
Ok wrapper macro - wraps result in Ok(…) with automatic span field
okmap
Constructor mapping macro with Ok wrapper - like cclosure! but wraps result with Ok
seq_n
Compose multiple parsers at once by selecting the appropriate seqN combinator.
span
try_mapc
Similar as mapc!, but with try_map.
unexpected_token
Macro to create an UnexpectedToken error with expected and found values.
unexpected_value
Macro to build a standard unexpected-value parse error.

Structs§

LexError
Lexical analysis error type.
PtxParseError
PTX parsing error with location information.
PtxTokenStream
Token stream wrapper for parsing PTX tokens.
PtxUnlexer
Utility that performs the inverse of the lexer: it writes textual PTX for a sequence of [PtxSpecToken] values.
Span

Enums§

ParseErrorKind
Kinds of parse errors that can occur during PTX parsing.
PtxToken
PTX specification token types for lexical analysis.

Traits§

PtxParser
Trait for types that can be parsed from a PTX token stream.
PtxUnparser
Trait that mirrors crate::parser::PtxParser but for emitting PTX source text from the structured representation.

Functions§

parse_ptx
run_with_large_stack
Execute f on a dedicated thread with a larger stack in debug builds to avoid overflows from deep recursion. In release builds, run directly without the extra thread to reduce overhead.
tokenize
Tokenize a PTX source string into a sequence of tokens with their spans.

Type Aliases§

StreamPosition
Represents a position in the token stream, index of the token and optional char offset within the token.

Derive Macros§

Spanned