Skip to content

De-globalize all input/parsing state#261

Merged
jpco merged 13 commits intowryun:masterfrom
jpco:parserstruct
Mar 10, 2026
Merged

De-globalize all input/parsing state#261
jpco merged 13 commits intowryun:masterfrom
jpco:parserstruct

Conversation

@jpco
Copy link
Collaborator

@jpco jpco commented Mar 9, 2026

This PR introduces a new struct Parser defined in input.h. This struct is created and destroyed once per call to parse(), and is meant to contain any state with a lifetime equal to (or less than) that call.

Longer-term, I would like to simplify and shrink Input as much as possible, moving some of its state into Parser and some of it elsewhere. Input is completely invisible, often long-lived state with dynamic scope, which make it really difficult to reason about, and which directly determines how $&parse works. It is possible that with enough work Input could turn into the internal representation of a "file handle" like in #252. That's all down the line, though.

Shorter term, this PR makes it possible to have multiple parsers safely running concurrently, which is the final prerequisite for running es code while reading input. So while this PR doesn't itself change anything for a user, it should open up a few pathways for significant future changes to the shell's input behaviors.

Performance note: This PR slows down the shell's reading and parsing behavior a bit. I will (optimistically) assert that this is okay, at least temporarily. There are some potential optimizations we can make to fix it, but I would like to do some refactoring and code-moving-around before then.

Portability note: This PR makes use of %code requires, %parse-param and %lex-param in parse.y. This is supported by both byacc and bison.

jpco added 13 commits February 20, 2026 07:48
Mostly empty now, but as much input and parsing state as possible should
be moved into Parser soon, for two reasons:

 1. Parser is properly allocated (on the stack, for now, but can easily
    change), while global variables make it impossible to run multiple
    parsers at once.

 2. A Parser's lifetime is very well scoped: one call to $&parse.
    Inputs are long-lived and induce spooky, invisible behavior in the
    shell.  The more that Input can be shrunk, the more of that spooky,
    invisible behavior is removed, the more orthogonal and predictable
    the shell becomes.
Situation is still a little suboptimal; there are too many allocations
for memory that can be reused (pspace, tokenbuf), and the code is
generally less tidy than it ought to be.
@jpco jpco merged commit 17b0c62 into wryun:master Mar 10, 2026
1 check passed
@jpco jpco deleted the parserstruct branch March 15, 2026 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant