Skip to content

Add attribute bag to AST nodes and a public typescope package - #10

Merged
TomasVotruba merged 2 commits into
mainfrom
node-attribute-bag-and-typescope
Nov 8, 2026
Merged

TomasVotruba merged 2 commits into
mainfrom
node-attribute-bag-and-typescope

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Foundation for carrying resolved types on the AST, reusable across projects on this parser.

1. Attribute bag on every node

  • AttributeBag (embedded in all 166 node.go nodes plus the transient parser nodes in internal/php5|7|8) gives every node SetAttribute(key, value) / GetAttribute(key).
  • Backing map is allocated lazily, so a node that never gets an attribute costs nothing.
  • Vertex gains the two methods.
  • AttributeBag.Equal keeps go-cmp node comparisons (the parser test suite) working past the bag's unexported map.

A resolver can now stamp an inferred type onto a property fetch, variable or method call node and read it back later, instead of recomputing it.

2. Public typescope package

  • pkg/typescope resolves the static type of a value expression by tracing it through the function scopes of a class subtree (typed params, single assignments, $this->property declared types, builtin call/arithmetic result types).
  • Promoted from an internal package so any project on the parser can reuse it.

Scope is single-object native types; docblocks and union types are deliberately out.

Tests

  • go test ./... green.
  • gofmt -s, golangci-lint (0 issues), modernize clean on changed packages.

Every node embeds AttributeBag, so a resolver can store post-parse
metadata (such as an inferred type) on a node via SetAttribute /
GetAttribute without widening node constructors. Equal keeps go-cmp
node comparisons working past the bag's unexported map.
Resolves the static type of a value expression by tracing it through the
function scopes of a class subtree. Promoted from an internal package so
any project on the parser can reuse it.
@TomasVotruba
TomasVotruba force-pushed the node-attribute-bag-and-typescope branch from 91be7cd to 7d3dc55 Compare November 8, 2026 19:40
@TomasVotruba
TomasVotruba merged commit c707b89 into main Nov 8, 2026
5 checks passed
@TomasVotruba
TomasVotruba deleted the node-attribute-bag-and-typescope branch November 8, 2026 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant