Skip to main content

Crate treesitter_types_rust

Crate treesitter_types_rust 

Source
Expand description

Strongly-typed AST types for Rust, auto-generated from tree-sitter-rust’s node-types.json.

This crate is generated by treesitter-types and is automatically kept up to date when a new version of the grammar crate is released.

These types have been tested by parsing the Rust compiler source code.

See the Tree-sitter project for more information about the underlying parser framework.

§Example

use treesitter_types_rust::*;

// A minimal Rust hello-world program.
let src = b"\
fn main() {
    println!(\"Hello, World!\");
}
";

// Parse the source with tree-sitter and convert into typed AST.
let mut parser = tree_sitter::Parser::new();
parser.set_language(&tree_sitter_rust::LANGUAGE.into()).unwrap();
let tree = parser.parse(src, None).unwrap();
let source_file = SourceFile::from_node(tree.root_node(), src).unwrap();

// The source file has one top-level child: a function definition.
assert_eq!(source_file.children.len(), 1);

// Unwrap the function item through the declaration statement wrapper.
let SourceFileChildren::DeclarationStatement(decl) = &source_file.children[0] else {
    panic!("expected a declaration statement");
};
let DeclarationStatement::FunctionItem(func) = decl.as_ref() else {
    panic!("expected a function item");
};

// Check function metadata.
let FunctionItemName::Identifier(name) = &func.name else {
    panic!("expected an identifier");
};
assert_eq!(name.text(), "main");
assert!(func.parameters.children.is_empty()); // no parameters
assert!(func.return_type.is_none());           // no return type

// The body contains one statement: the `println!` macro invocation.
assert_eq!(func.body.children.len(), 1);

Re-exports§

pub use tree_sitter_rust;
pub use treesitter_types::tree_sitter;

Structs§

AbstractType
Arguments
ArrayExpression
ArrayType
AssignmentExpression
AssociatedType
AsyncBlock
Attribute
AttributeItem
AwaitExpression
BaseFieldInitializer
BinaryExpression
Block
BlockComment
BooleanLiteral
BoundedType
BracketedType
BreakExpression
CallExpression
CapturedPattern
CharLiteral
ClosureExpression
ClosureParameters
CompoundAssignmentExpr
ConstBlock
ConstItem
ConstParameter
ContinueExpression
Crate
DeclarationList
DocComment
DynamicType
ElseClause
EmptyStatement
EnumItem
EnumVariant
EnumVariantList
EscapeSequence
ExpressionStatement
ExternCrateDeclaration
ExternModifier
FieldDeclaration
FieldDeclarationList
FieldExpression
FieldIdentifier
FieldInitializer
FieldInitializerList
FieldPattern
FloatLiteral
ForExpression
ForLifetimes
ForeignModItem
FragmentSpecifier
FunctionItem
FunctionModifiers
FunctionSignatureItem
FunctionType
GenBlock
GenericFunction
GenericPattern
GenericType
GenericTypeWithTurbofish
HigherRankedTraitBound
Identifier
IfExpression
ImplItem
IndexExpression
InnerAttributeItem
InnerDocCommentMarker
IntegerLiteral
Label
LetChain
LetCondition
LetDeclaration
Lifetime
LifetimeParameter
LineComment
LoopExpression
MacroDefinition
MacroInvocation
MacroRule
MatchArm
MatchBlock
MatchExpression
MatchPattern
Metavariable
ModItem
MutPattern
MutableSpecifier
NegativeLiteral
NeverType
OrPattern
OrderedFieldDeclarationList
OuterDocCommentMarker
Parameter
Parameters
ParenthesizedExpression
PointerType
PrimitiveType
QualifiedType
RangeExpression
RangePattern
RawStringLiteral
RefPattern
ReferenceExpression
ReferencePattern
ReferenceType
RemainingFieldPattern
RemovedTraitBound
ReturnExpression
ScopedIdentifier
ScopedTypeIdentifier
ScopedUseList
SelfParameter
SelfType
Shebang
ShorthandFieldIdentifier
ShorthandFieldInitializer
SlicePattern
SourceFile
Span
StaticItem
StringContent
StringLiteral
StructExpression
StructItem
StructPattern
Super
TokenBindingPattern
TokenRepetition
TokenRepetitionPattern
TokenTree
TokenTreePattern
TraitBounds
TraitItem
TryBlock
TryExpression
TupleExpression
TuplePattern
TupleStructPattern
TupleType
TypeArguments
TypeBinding
TypeCastExpression
TypeIdentifier
TypeItem
TypeParameter
TypeParameters
UnaryExpression
UnionItem
UnitExpression
UnitType
UnsafeBlock
UseAsClause
UseBounds
UseDeclaration
UseList
UseWildcard
VariadicParameter
VisibilityModifier
WhereClause
WherePredicate
WhileExpression
YieldExpression

Enums§

AbstractTypeTrait
AnyNode
ArgumentsChildren
ArrayExpressionChildren
AttributeChildren
BinaryExpressionOperator
BlockChildren
BoundedTypeChildren
BracketedTypeChildren
BreakExpressionChildren
CallExpressionFunction
ClosureExpressionBody
ClosureParametersChildren
CompoundAssignmentExprOperator
ConstParameterValue
DeclarationStatement
DynamicTypeTrait
ElseClauseChildren
EnumItemChildren
EnumVariantBody
EnumVariantListChildren
Expression
ExternCrateDeclarationChildren
FieldDeclarationListChildren
FieldExpressionField
FieldInitializerField
FieldInitializerListChildren
FieldPatternName
FunctionItemChildren
FunctionItemName
FunctionSignatureItemChildren
FunctionSignatureItemName
FunctionTypeChildren
FunctionTypeTrait
GenericFunctionFunction
GenericPatternChildren
GenericTypeType
GenericTypeWithTurbofishType
IfExpressionCondition
ImplItemTrait
LetChainChildren
Literal
LiteralPattern
MacroInvocationMacro
MatchArmChildren
MatchPatternCondition
MutPatternChildren
NegativeLiteralChildren
OrderedFieldDeclarationListChildren
ParameterPattern
ParametersChildren
ParseError
Pattern
RangePatternLeft
RangePatternRight
ReferencePatternChildren
ReferenceTypeChildren
ScopedIdentifierName
ScopedIdentifierPath
ScopedTypeIdentifierPath
ScopedUseListPath
SelfParameterChildren
ShorthandFieldInitializerChildren
SourceFileChildren
StaticItemChildren
StringLiteralChildren
StructExpressionName
StructItemBody
StructItemChildren
StructPatternChildren
StructPatternType
TokenRepetitionChildren
TokenRepetitionPatternChildren
TokenTreeChildren
TokenTreePatternChildren
TraitBoundsChildren
TraitItemChildren
TupleExpressionChildren
TuplePatternChildren
TupleStructPatternType
Type
TypeArgumentsChildren
TypeItemChildren
TypeParametersChildren
UnionItemChildren
UseAsClausePath
UseBoundsChildren
UseDeclarationArgument
UseListChildren
UseWildcardChildren
VisibilityModifierChildren
WherePredicateLeft
WhileExpressionCondition

Traits§

FromNode
Every generated struct and enum implements this.
LeafNode
Implemented by every generated leaf type (identifiers, literals, etc.)
Spanned
Implemented by every generated type that has a source location.