Skip to main content

AstNode

Trait AstNode 

Source
pub trait AstNode {
    // Required methods
    fn append_source(&self, sink: &mut String, source: Option<&str>);
    fn byte_span(&self) -> ByteSpan;

    // Provided methods
    fn source_span(&self, source_map: &SourceMap<'_>) -> Option<SourceSpan> { ... }
    fn to_source(&self, source: Option<&str>) -> String { ... }
}
Expand description

Trait implemented by all AST node types. Provides source reconstruction and span access methods.

All AST node types implement this trait via #[inherent] impl AstNode, giving each node both inherent methods (no trait import needed) and a trait bound for generic utilities (error formatters, linters, etc.).

§Source Reconstruction Modes

  • Source-slice mode (fast, lossless): When source is Some(s), append_source slices &s[span.start.byte_offset..span.end.byte_offset]. This is the common path for string-based token sources. Zero allocation.

  • Synthetic-formatting mode (slower, lossy): When source is None, append_source walks the AST and emits keywords, names, values, and punctuation with standard spacing. The output is semantically equivalent but not formatting-identical.

§Span Access

Every AST node carries a ByteSpan recording its byte-offset range in the source text. byte_span() exposes this uniformly across all node types, and source_span() resolves it to line/column coordinates on demand via a SourceMap.

Required Methods§

Source

fn append_source(&self, sink: &mut String, source: Option<&str>)

Append this node’s source representation to sink.

When source is Some(s), slices the original source text directly via byte offsets (zero-copy, lossless). When source is None, reconstructs from semantic data with standard formatting (lossy but semantically equivalent).

Source

fn byte_span(&self) -> ByteSpan

Returns this node’s byte-offset span within the source text.

The returned ByteSpan is a compact [start, end) byte range that can be resolved to line/column positions via source_span() or ByteSpan::resolve().

Provided Methods§

Source

fn source_span(&self, source_map: &SourceMap<'_>) -> Option<SourceSpan>

Resolves this node’s position to line/column coordinates using the given SourceMap.

Returns None if the byte offsets cannot be resolved (e.g. the node was synthetically constructed without valid span data).

Source

fn to_source(&self, source: Option<&str>) -> String

Return this node as a source string.

Convenience wrapper around append_source.

Implementors§

Source§

impl AstNode for Definition<'_>

Source§

impl AstNode for Selection<'_>

Source§

impl AstNode for TypeAnnotation<'_>

Source§

impl AstNode for TypeDefinition<'_>

Source§

impl AstNode for TypeExtension<'_>

Source§

impl AstNode for Value<'_>

Source§

impl AstNode for Argument<'_>

Source§

impl AstNode for BooleanValue<'_>

Source§

impl AstNode for DirectiveAnnotation<'_>

Source§

impl AstNode for DirectiveDefinition<'_>

Source§

impl AstNode for DirectiveLocation<'_>

Source§

impl AstNode for Document<'_>

Source§

impl AstNode for EnumTypeDefinition<'_>

Source§

impl AstNode for EnumTypeExtension<'_>

Source§

impl AstNode for EnumValue<'_>

Source§

impl AstNode for EnumValueDefinition<'_>

Source§

impl AstNode for FieldDefinition<'_>

Source§

impl AstNode for FieldSelection<'_>

Source§

impl AstNode for FloatValue<'_>

Source§

impl AstNode for FragmentDefinition<'_>

Source§

impl AstNode for FragmentSpread<'_>

Source§

impl AstNode for InlineFragment<'_>

Source§

impl AstNode for InputObjectTypeDefinition<'_>

Source§

impl AstNode for InputObjectTypeExtension<'_>

Source§

impl AstNode for InputValueDefinition<'_>

Source§

impl AstNode for IntValue<'_>

Source§

impl AstNode for InterfaceTypeDefinition<'_>

Source§

impl AstNode for InterfaceTypeExtension<'_>

Source§

impl AstNode for ListTypeAnnotation<'_>

Source§

impl AstNode for ListValue<'_>

Source§

impl AstNode for Name<'_>

Source§

impl AstNode for NamedTypeAnnotation<'_>

Source§

impl AstNode for NullValue<'_>

Source§

impl AstNode for ObjectField<'_>

Source§

impl AstNode for ObjectTypeDefinition<'_>

Source§

impl AstNode for ObjectTypeExtension<'_>

Source§

impl AstNode for ObjectValue<'_>

Source§

impl AstNode for OperationDefinition<'_>

Source§

impl AstNode for RootOperationTypeDefinition<'_>

Source§

impl AstNode for ScalarTypeDefinition<'_>

Source§

impl AstNode for ScalarTypeExtension<'_>

Source§

impl AstNode for SchemaDefinition<'_>

Source§

impl AstNode for SchemaExtension<'_>

Source§

impl AstNode for SelectionSet<'_>

Source§

impl AstNode for StringValue<'_>

Source§

impl AstNode for TypeCondition<'_>

Source§

impl AstNode for UnionTypeDefinition<'_>

Source§

impl AstNode for UnionTypeExtension<'_>

Source§

impl AstNode for VariableDefinition<'_>

Source§

impl AstNode for VariableReference<'_>