bonsaipython

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package bonsaipython provides a tree-sitter Python parser built on the bonsai snapshot runtime.

Typical use:

p := bonsaipython.NewParser()
root, err := p.Parse(src)
if err != nil { ... }
for n := range root.Find("function_definition") { ... }

The Parser, Node, and Point types are aliases for the corresponding bonsai runtime types, so values flow naturally between bonsai-aware code and this package.

The wasm module + grammar are generated. See Dockerfile.builder at the repo root. `go generate ./bonsai-python` regenerates module_gen.{go,dat}, libc_gen.go, and meta_gen.go in this directory.

Grammar source: https://github.com/tree-sitter/tree-sitter-python

Index

Constants

View Source
const (
	KindAliasedImport           = "aliased_import"
	KindArgumentList            = "argument_list"
	KindAsPattern               = "as_pattern"
	KindAssertStatement         = "assert_statement"
	KindAssignment              = "assignment"
	KindAttribute               = "attribute"
	KindAugmentedAssignment     = "augmented_assignment"
	KindAwait                   = "await"
	KindBinaryOperator          = "binary_operator"
	KindBlock                   = "block"
	KindBooleanOperator         = "boolean_operator"
	KindBreakStatement          = "break_statement"
	KindCall                    = "call"
	KindCaseClause              = "case_clause"
	KindCasePattern             = "case_pattern"
	KindChevron                 = "chevron"
	KindClassDefinition         = "class_definition"
	KindClassPattern            = "class_pattern"
	KindComment                 = "comment"
	KindComparisonOperator      = "comparison_operator"
	KindComplexPattern          = "complex_pattern"
	KindConcatenatedString      = "concatenated_string"
	KindConditionalExpression   = "conditional_expression"
	KindConstrainedType         = "constrained_type"
	KindContinueStatement       = "continue_statement"
	KindDecoratedDefinition     = "decorated_definition"
	KindDecorator               = "decorator"
	KindDefaultParameter        = "default_parameter"
	KindDeleteStatement         = "delete_statement"
	KindDictPattern             = "dict_pattern"
	KindDictionary              = "dictionary"
	KindDictionaryComprehension = "dictionary_comprehension"
	KindDictionarySplat         = "dictionary_splat"
	KindDictionarySplatPattern  = "dictionary_splat_pattern"
	KindDottedName              = "dotted_name"
	KindElifClause              = "elif_clause"
	KindEllipsis                = "ellipsis"
	KindElseClause              = "else_clause"
	KindEscapeInterpolation     = "escape_interpolation"
	KindEscapeSequence          = "escape_sequence"
	KindExceptClause            = "except_clause"
	KindExecStatement           = "exec_statement"
	KindExpressionList          = "expression_list"
	KindExpressionStatement     = "expression_statement"
	KindFalse                   = "false"
	KindFinallyClause           = "finally_clause"
	KindFloat                   = "float"
	KindForInClause             = "for_in_clause"
	KindForStatement            = "for_statement"
	KindFormatExpression        = "format_expression"
	KindFormatSpecifier         = "format_specifier"
	KindFunctionDefinition      = "function_definition"
	KindFutureImportStatement   = "future_import_statement"
	KindGeneratorExpression     = "generator_expression"
	KindGenericType             = "generic_type"
	KindGlobalStatement         = "global_statement"
	KindIdentifier              = "identifier"
	KindIfClause                = "if_clause"
	KindIfStatement             = "if_statement"
	KindImportFromStatement     = "import_from_statement"
	KindImportPrefix            = "import_prefix"
	KindImportStatement         = "import_statement"
	KindInteger                 = "integer"
	KindInterpolation           = "interpolation"
	KindKeywordArgument         = "keyword_argument"
	KindKeywordPattern          = "keyword_pattern"
	KindKeywordSeparator        = "keyword_separator"
	KindLambda                  = "lambda"
	KindLambdaParameters        = "lambda_parameters"
	KindLineContinuation        = "line_continuation"
	KindList                    = "list"
	KindListComprehension       = "list_comprehension"
	KindListPattern             = "list_pattern"
	KindListSplat               = "list_splat"
	KindListSplatPattern        = "list_splat_pattern"
	KindMatchStatement          = "match_statement"
	KindMemberType              = "member_type"
	KindModule                  = "module"
	KindNamedExpression         = "named_expression"
	KindNone                    = "none"
	KindNonlocalStatement       = "nonlocal_statement"
	KindNotOperator             = "not_operator"
	KindPair                    = "pair"
	KindParameters              = "parameters"
	KindParenthesizedExpression = "parenthesized_expression"
	KindParenthesizedListSplat  = "parenthesized_list_splat"
	KindPassStatement           = "pass_statement"
	KindPatternList             = "pattern_list"
	KindPositionalSeparator     = "positional_separator"
	KindPrintStatement          = "print_statement"
	KindRaiseStatement          = "raise_statement"
	KindRelativeImport          = "relative_import"
	KindReturnStatement         = "return_statement"
	KindSet                     = "set"
	KindSetComprehension        = "set_comprehension"
	KindSlice                   = "slice"
	KindSplatPattern            = "splat_pattern"
	KindSplatType               = "splat_type"
	KindString                  = "string"
	KindStringContent           = "string_content"
	KindStringEnd               = "string_end"
	KindStringStart             = "string_start"
	KindSubscript               = "subscript"
	KindTrue                    = "true"
	KindTryStatement            = "try_statement"
	KindTuple                   = "tuple"
	KindTuplePattern            = "tuple_pattern"
	KindType                    = "type"
	KindTypeAliasStatement      = "type_alias_statement"
	KindTypeConversion          = "type_conversion"
	KindTypeParameter           = "type_parameter"
	KindTypedDefaultParameter   = "typed_default_parameter"
	KindTypedParameter          = "typed_parameter"
	KindUnaryOperator           = "unary_operator"
	KindUnionPattern            = "union_pattern"
	KindUnionType               = "union_type"
	KindWhileStatement          = "while_statement"
	KindWildcardImport          = "wildcard_import"
	KindWithClause              = "with_clause"
	KindWithItem                = "with_item"
	KindWithStatement           = "with_statement"
	KindYield                   = "yield"
)

KindX constants name every named, visible node type this grammar can produce as Node.Kind. Values are the raw tree-sitter kind strings, so they work anywhere a node kind string is expected, like Node.Find. Anonymous tokens (keywords, punctuation) and supertypes have no constants.

View Source
const (
	FieldAlias           = "alias"
	FieldAlternative     = "alternative"
	FieldArgument        = "argument"
	FieldArguments       = "arguments"
	FieldAttribute       = "attribute"
	FieldBody            = "body"
	FieldCause           = "cause"
	FieldCode            = "code"
	FieldCondition       = "condition"
	FieldConsequence     = "consequence"
	FieldDefinition      = "definition"
	FieldExpression      = "expression"
	FieldFormatSpecifier = "format_specifier"
	FieldFunction        = "function"
	FieldGuard           = "guard"
	FieldKey             = "key"
	FieldLeft            = "left"
	FieldModuleName      = "module_name"
	FieldName            = "name"
	FieldObject          = "object"
	FieldOperator        = "operator"
	FieldOperators       = "operators"
	FieldParameters      = "parameters"
	FieldReturnType      = "return_type"
	FieldRight           = "right"
	FieldSubject         = "subject"
	FieldSubscript       = "subscript"
	FieldSuperclasses    = "superclasses"
	FieldType            = "type"
	FieldTypeConversion  = "type_conversion"
	FieldTypeParameters  = "type_parameters"
	FieldValue           = "value"
)

FieldX constants name every field this grammar attaches to child nodes, for Node.ChildByField and Node.Field.

View Source
const GrammarVersion = "v0.25.0"

GrammarVersion is the upstream grammar ref (tag or branch) this module's parser was generated from. The exact commit is in the header above.

View Source
const InitialPages = 8

InitialPages is the wasm module's declared initial linear-memory size, in 64 KiB WebAssembly pages. Memory passed to New must already be sized to at least InitialPages * 65536 bytes before the call.

View Source
const TreeSitterVersion = "v0.25.10"

TreeSitterVersion is the tree-sitter runtime ref whose compiled form this module embeds.

Variables

View Source
var LicenseGrammar string

LicenseGrammar is the license of the grammar (https://github.com/tree-sitter/tree-sitter-python.git @ v0.25.0) whose compiled form this package embeds.

View Source
var LicenseTreeSitter string

LicenseTreeSitter is the license of the tree-sitter runtime (v0.25.10) whose compiled form this package embeds.

Functions

This section is empty.

Types

type Memory

type Memory = interface {
	Slice() *[]byte
	Grow(delta, max int64) int64
}

type Module

type Module struct {
	// contains filtered or unexported fields
}

func New

func New(v0 Xenv) *Module

func (*Module) X__indirect_function_table

func (m *Module) X__indirect_function_table() *[]any

func (*Module) X_initialize

func (m *Module) X_initialize()

func (*Module) Xfree

func (m *Module) Xfree(v0 int32)

func (*Module) Xmalloc

func (m *Module) Xmalloc(v0 int32) int32

func (*Module) Xtree_sitter_python

func (m *Module) Xtree_sitter_python() int32

func (*Module) Xts_node_end_byte

func (m *Module) Xts_node_end_byte(v0 int32) int32

func (*Module) Xts_node_end_point

func (m *Module) Xts_node_end_point(v0, v1 int32)

func (*Module) Xts_node_has_error

func (m *Module) Xts_node_has_error(v0 int32) int32

func (*Module) Xts_node_is_error

func (m *Module) Xts_node_is_error(v0 int32) int32

func (*Module) Xts_node_is_missing

func (m *Module) Xts_node_is_missing(v0 int32) int32

func (*Module) Xts_node_is_named

func (m *Module) Xts_node_is_named(v0 int32) int32

func (*Module) Xts_node_is_null

func (m *Module) Xts_node_is_null(v0 int32) int32

func (*Module) Xts_node_start_byte

func (m *Module) Xts_node_start_byte(v0 int32) int32

func (*Module) Xts_node_start_point

func (m *Module) Xts_node_start_point(v0, v1 int32)

func (*Module) Xts_node_string

func (m *Module) Xts_node_string(v0 int32) int32

func (*Module) Xts_node_type

func (m *Module) Xts_node_type(v0 int32) int32

func (*Module) Xts_parser_delete

func (m *Module) Xts_parser_delete(v0 int32)

func (*Module) Xts_parser_new

func (m *Module) Xts_parser_new() int32

func (*Module) Xts_parser_parse_string

func (m *Module) Xts_parser_parse_string(v0, v1, v2, v3 int32) int32

func (*Module) Xts_parser_set_language

func (m *Module) Xts_parser_set_language(v0, v1 int32) int32

func (*Module) Xts_tree_cursor_current_field_name

func (m *Module) Xts_tree_cursor_current_field_name(v0 int32) int32

func (*Module) Xts_tree_cursor_current_node

func (m *Module) Xts_tree_cursor_current_node(v0, v1 int32)

func (*Module) Xts_tree_cursor_delete

func (m *Module) Xts_tree_cursor_delete(v0 int32)

func (*Module) Xts_tree_cursor_goto_first_child

func (m *Module) Xts_tree_cursor_goto_first_child(v0 int32) int32

func (*Module) Xts_tree_cursor_goto_next_sibling

func (m *Module) Xts_tree_cursor_goto_next_sibling(v0 int32) int32

func (*Module) Xts_tree_cursor_goto_parent

func (m *Module) Xts_tree_cursor_goto_parent(v0 int32) int32

func (*Module) Xts_tree_cursor_new

func (m *Module) Xts_tree_cursor_new(v0, v1 int32)

func (*Module) Xts_tree_delete

func (m *Module) Xts_tree_delete(v0 int32)

func (*Module) Xts_tree_root_node

func (m *Module) Xts_tree_root_node(v0, v1 int32)

type Node

type Node = bonsai.Node

Type aliases so callers can import only this package.

type Parser

type Parser = bonsai.Parser

Type aliases so callers can import only this package.

func NewParser

func NewParser() *Parser

NewParser instantiates a tree-sitter Python parser.

The parser owns one wasm module instance and is NOT safe for concurrent use. Pool a Parser per goroutine. Reuse across files. Instantiation is the expensive part.

type Point

type Point = bonsai.Point

Type aliases so callers can import only this package.

type Xenv

type Xenv = interface {
	Xfputc(v0, v1 int32) int32
	Xmemory() Memory
	Xts_language_is_wasm(v0 int32) int32
	Xts_wasm_store_delete(v0 int32)
	Xvfprintf(v0, v1, v2 int32) int32
	Xvsnprintf(v0, v1, v2, v3 int32) int32
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL