pythongrammar

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: AGPL-3.0, AGPL-3.0-or-later Imports: 3 Imported by: 0

Documentation

Overview

Package pythongrammar holds the closed Python 3.12 subset lexer and parser. It is deliberately neutral: it carries no request envelope, no fact-output budget, and no dependency on the separately buildable Python analyzer, so Core may reach the grammar without reaching that candidate (PNC-001).

Index

Constants

View Source
const (
	KindName byte = iota + 1
	KindNumber
	KindString
	KindSymbol
	KindNewline
	KindIndent
	KindDedent
)

Variables

This section is empty.

Functions

func ContainsPython314ExceptList

func ContainsPython314ExceptList(content []byte) bool

ContainsPython314ExceptList reports whether the source uses the 3.14 bare `except` list form, which this 3.12 subset does not accept.

func DelimiterPair

func DelimiterPair(open, close byte) bool

DelimiterPair reports whether close is the delimiter matching open.

func FString

func FString(prefix []byte) bool

FString reports whether the given literal prefix marks a formatted string.

func PythonKeyword

func PythonKeyword(value string) bool

func SkipString

func SkipString(content []byte, quoteAt int) int

SkipString returns the index just past the string literal opening at quoteAt.

func StringStart

func StringStart(content []byte, index int) (int, bool)

StringStart reports whether a string literal opens at index, returning the index of its opening quote.

func SyntaxName

func SyntaxName(value string) failure

Types

type DefinitionSpanSink

type DefinitionSpanSink interface {
	DefinitionEnd(startLine, endLine int)
}

DefinitionSpanSink is the optional half of FactSink for callers that need a definition's extent rather than only its position. A fact carries one line, which names where a definition starts and can never name where its suite ends; a collector that implements this interface is told both, and one that does not is unaffected -- the parser reports spans only when the assertion below succeeds.

type FactSink

type FactSink interface {
	Add(kind, subject, predicate, value string, line, column int) Failure
}

FactSink receives the grammar's facts. The grammar names only the subject it parsed; identity beyond that belongs to the caller, which closes over it.

type Failure

type Failure string

Failure is a typed rejection reason. The values match the analyzer's wire vocabulary so a caller can pass them straight through.

func ParseChecked

func ParseChecked(subject string, source []byte, collector FactSink, check SyntaxCheck) Failure

ParseChecked is ParsePython312Subset with an additional syntax check applied to the lexed source before the fact grammar runs. A nil check parses exactly as ParsePython312Subset does.

func ParsePython312Subset

func ParsePython312Subset(subject string, source []byte, collector FactSink) Failure

Balanced expressions outside the named fact grammar remain opaque. ParsePython312Subset parses the closed Python 3.12 subset, emitting facts to the sink. subject names the parsed unit in every fact it produces.

type Parser

type Parser struct {
	Source []byte
	Tokens []Token
	// contains filtered or unexported fields
}

func (*Parser) BinaryConnector

func (parser *Parser) BinaryConnector(token Token) bool

func (*Parser) CommaSeparated

func (parser *Parser) CommaSeparated(tokens []Token) ([][]Token, failure)

func (*Parser) Comparison

func (parser *Parser) Comparison(tokens []Token, index int) bool

func (*Parser) DanglingUnary

func (parser *Parser) DanglingUnary(tokens []Token) bool

func (*Parser) DefinitionStart

func (parser *Parser) DefinitionStart(tokens []Token) bool

func (*Parser) EllipsisAt

func (parser *Parser) EllipsisAt(tokens []Token, index int) bool

func (*Parser) ExpressionBracket

func (parser *Parser) ExpressionBracket(token Token) bool

func (*Parser) HasTopLevel

func (parser *Parser) HasTopLevel(tokens []Token, want string) bool

func (*Parser) HeaderColon

func (parser *Parser) HeaderColon(tokens []Token, start int) int

func (*Parser) KeywordOperator

func (parser *Parser) KeywordOperator(token Token) bool

func (*Parser) Matching

func (parser *Parser) Matching(tokens []Token, open int) int

func (*Parser) Name

func (parser *Parser) Name(token Token, want string) bool

func (*Parser) Operand

func (parser *Parser) Operand(token Token) bool

func (*Parser) SpanColon

func (parser *Parser) SpanColon(tokens []Token, start int) int

func (*Parser) StatementEnd

func (parser *Parser) StatementEnd(start int) int

func (*Parser) StaticCallee

func (parser *Parser) StaticCallee(tokens []Token) (string, int, bool, failure)

func (*Parser) Symbol

func (parser *Parser) Symbol(token Token, want string) bool

func (*Parser) SymbolByte

func (parser *Parser) SymbolByte(token Token, want byte) bool

func (*Parser) Text

func (parser *Parser) Text(token Token) string

func (*Parser) TopLevel

func (parser *Parser) TopLevel(tokens []Token, target int) bool

func (*Parser) TypeAliasStart

func (parser *Parser) TypeAliasStart(tokens []Token) bool

TypeAliasStart reports whether a statement opening with the soft keyword `type` is a PEP 695 alias rather than an ordinary use of the builtin. CPython commits to `type_alias: "type" NAME [type_params] '=' expression` only on that shape, so `type(x)`, `type = 5` and `type(self).calls += 1` stay expressions.

type SyntaxCheck

type SyntaxCheck func(*Parser) bool

SyntaxCheck rejects a lexed source whose shape the closed grammar treats as necessarily invalid. It is supplied by the caller rather than held here so this package — and every executable that reaches it for fact extraction alone — carries no syntax-validation route (ACP-009).

type Token

type Token struct {
	Kind       byte
	Start, End int
	// contains filtered or unexported fields
}

func LexPython312

func LexPython312(source []byte) ([]Token, failure)

Jump to

Keyboard shortcuts

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