pub struct ZshParser<'a> { /* private fields */ }Expand description
The Zsh Parser
Implementations§
Source§impl<'a> ZshParser<'a>
impl<'a> ZshParser<'a>
Sourcepub fn parse_context_save(&mut self, ps: &mut ParseStack)
pub fn parse_context_save(&mut self, ps: &mut ParseStack)
Save parse context onto a ParseStack. Direct port of
zsh/Src/parse.c:295-320 parse_context_save. Pushes
recursion_depth + global_iterations and resets to zero so
a nested parse can’t trigger the outer parse’s limits.
Lexer-side state (incmdpos / incond / etc.) saves via the
lexer’s own LexStack since those fields live on ZshLexer.
Sourcepub fn parse_context_restore(&mut self, ps: &ParseStack)
pub fn parse_context_restore(&mut self, ps: &ParseStack)
Restore parse context from a ParseStack. Direct port of
zsh/Src/parse.c:326-355 parse_context_restore. Inverse of
parse_context_save. Also clears any half-built AST state
to prevent leaking into the outer parse.
Sourcepub fn init_parse_status(&mut self)
pub fn init_parse_status(&mut self)
Initialize parser status. Direct port of zsh/Src/parse.c:489-503
init_parse_status. Clears the per-parse-call lexer flags
so a fresh parse starts from cmd-position with no nesting
state inherited from a prior parse.
Sourcepub fn init_parse(&mut self)
pub fn init_parse(&mut self)
Initialize parser for a fresh parse. Direct port of
zsh/Src/parse.c:507-525 init_parse. C source allocates a
fresh wordcode buffer (ecbuf) sized EC_INIT_SIZE, resets the
per-parse-call counters, and calls init_parse_status. zshrs
has no flat wordcode buffer (AST is built inline) so this
function reduces to init_parse_status + recursion_depth/
global_iterations clear.
Sourcepub fn empty_eprog(prog: &ZshProgram) -> bool
pub fn empty_eprog(prog: &ZshProgram) -> bool
Check whether the parsed program is empty. Direct port of
zsh/Src/parse.c:583-587 empty_eprog. C version checks
*p->prog == WCB_END() (single end-of-wordcode marker).
zshrs version checks the AST node count.
Sourcepub fn clear_hdocs(&mut self)
pub fn clear_hdocs(&mut self)
Clear pending here-document list. Direct port of
zsh/Src/parse.c:589-600 clear_hdocs. The C version walks
the global hdocs linked list and frees each node. zshrs
stores pending heredocs on the lexer’s heredocs Vec —
truncating it has the same effect.
Sourcepub fn parse_event(&mut self, endtok: LexTok) -> Option<ZshProgram>
pub fn parse_event(&mut self, endtok: LexTok) -> Option<ZshProgram>
Top-level parse-event entry. Direct port of zsh/Src/parse.c:
612-631 parse_event. Reads one event from the lexer (a
sublist optionally followed by SEPER/AMPER/AMPERBANG) and
returns the resulting ZshProgram.
endtok is the token that terminates the event — usually
ENDINPUT, but for command-style substitutions the closing
) (zsh’s CMD_SUBST_CLOSE).
zshrs port note: zsh’s parse_event returns an Eprog (heap-
allocated wordcode program). zshrs returns a ZshProgram
(AST root). Same role at the parse-output boundary.
Sourcepub fn par_event(&mut self, endtok: LexTok) -> bool
pub fn par_event(&mut self, endtok: LexTok) -> bool
Parse one event (sublist with optional separator). Direct
port of zsh/Src/parse.c:633-695 par_event. Returns true if
an event was successfully parsed, false on EOF / endtok.
zshrs port note: the C version emits wordcodes via ecadd/ set_list_code; zshrs’s parser builds AST nodes via parse_sublist + parse_list. Same flow, different output.
Sourcepub fn par_list1(&mut self) -> Option<ZshSublist>
pub fn par_list1(&mut self) -> Option<ZshSublist>
Parse one list — non-recursing variant. Direct port of
zsh/Src/parse.c:807-817 par_list1. Like par_list but
doesn’t recurse on the trailing-separator path; used by
callers that only want one statement (e.g. each arm of a
case body).
Sourcepub fn setheredoc(
&mut self,
_pc: usize,
_redir_type: i32,
_doc: &str,
_term: &str,
_munged_term: &str,
)
pub fn setheredoc( &mut self, _pc: usize, _redir_type: i32, _doc: &str, _term: &str, _munged_term: &str, )
Wire a here-document body onto the redirection token that
requested it. Direct port of zsh/Src/parse.c:2347-2361
setheredoc. Called when a heredoc terminator has been
matched and the body is ready to be attached to the redir.
zshrs port note: zsh’s setheredoc patches the wordcode
in-place via pc[1] = ecstrcode(doc); pc[2] = ecstrcode(term);.
zshrs threads heredoc bodies through HereDocInfo structs
that resolve_redir applies during the post-parse fill_in pass.
This method is the AST-side equivalent: writes back to the
matching redir node by index.
Sourcepub fn par_wordlist(&mut self) -> Vec<String>
pub fn par_wordlist(&mut self) -> Vec<String>
Parse a wordlist for for ... in WORDS;. Direct port of
zsh/Src/parse.c:2362-2378 par_wordlist. Reads STRING tokens
until the next SEPER / SEMI / NEWLIN.
Sourcepub fn par_nl_wordlist(&mut self) -> Vec<String>
pub fn par_nl_wordlist(&mut self) -> Vec<String>
Parse a newline-separated wordlist. Direct port of
zsh/Src/parse.c:2379-2398 par_nl_wordlist. Like
par_wordlist but tolerates leading/trailing newlines.
Sourcepub fn get_cond_num(&mut self) -> Option<i64>
pub fn get_cond_num(&mut self) -> Option<i64>
Get the integer value of the next token in a cond expression.
Direct port of zsh/Src/parse.c:2643-2658 get_cond_num.
Used for [[ N OP M ]] numeric tests where N/M are integer
literals or variable references.
Sourcepub fn yyerror(&mut self, msg: &str)
pub fn yyerror(&mut self, msg: &str)
Emit a parser-level error. Direct port of zsh/Src/parse.c
2733-2766 yyerror. C version fills a per-event error buffer
and sets errflag. zshrs pushes onto self.errors which the
caller drains via parse()’s Result return.
Sourcepub fn set_list_code(_p: usize, _type_code: i32, _cmplx: bool)
pub fn set_list_code(_p: usize, _type_code: i32, _cmplx: bool)
Patch a list-placeholder wordcode with its actual opcode +
jump distance. Direct port of zsh/Src/parse.c:736-749
set_list_code. zsh emits an ecadd(0) placeholder before
par_sublist runs, then comes back through set_list_code to
rewrite the slot with WCB_LIST(type, distance) once the
sublist’s final length is known.
zshrs port note: zshrs builds AST nodes inline so there’s no placeholder to patch. The ZshList { sublist, flags } node is created with the right flags from the start. Stub provided for port-surface completeness.
Sourcepub fn set_sublist_code(
_p: usize,
_type_code: i32,
_flags: i32,
_skip: i32,
_cmplx: bool,
)
pub fn set_sublist_code( _p: usize, _type_code: i32, _flags: i32, _skip: i32, _cmplx: bool, )
Patch a sublist-placeholder wordcode with its actual opcode.
Direct port of zsh/Src/parse.c:753-763 set_sublist_code.
Same role as set_list_code at the sublist level.
Sourcepub fn ecadd(_c: u32) -> usize
pub fn ecadd(_c: u32) -> usize
Add one wordcode opcode to the buffer. Direct port of
zsh/Src/parse.c:396-408 ecadd. Returns the index of the
new opcode. zshrs no-op since the AST is built inline.
Sourcepub fn ecdel(_p: usize)
pub fn ecdel(_p: usize)
Delete a wordcode at position p. Direct port of
zsh/Src/parse.c:412-421 ecdel. zshrs no-op.
Sourcepub fn ecstrcode(_s: &str) -> u32
pub fn ecstrcode(_s: &str) -> u32
Encode a string into a wordcode value. Direct port of
zsh/Src/parse.c:425-471 ecstrcode. C source packs short
strings (≤4 chars) into a single wordcode + uses a binary
tree (Eccstr) for longer strings; long-string slots are
de-duplicated via hasher + strcmp. zshrs no-op since the
AST stores strings directly.
Sourcepub fn ecispace(_p: usize, _n: usize)
pub fn ecispace(_p: usize, _n: usize)
Insert N empty wordcode slots at position p. Direct port of
zsh/Src/parse.c:371-388 ecispace. Used to reserve space
for a forward-jump opcode that will be patched once the
jump target is known. zshrs no-op since AST jumps are
resolved at compile_zsh time.
Sourcepub fn ecadjusthere(_p: usize, _d: i32)
pub fn ecadjusthere(_p: usize, _d: i32)
Adjust pending heredoc pointers when wordcodes shift. Direct
port of zsh/Src/parse.c:359-367 ecadjusthere. Called
internally by ecispace / ecdel after they shift the buffer.
zshrs no-op since heredocs are tracked by index in the
lexer’s Vec, not by absolute wordcode offset.
Sourcepub fn dupeprog(prog: &ZshProgram) -> ZshProgram
pub fn dupeprog(prog: &ZshProgram) -> ZshProgram
Duplicate an Eprog. Direct port of zsh/Src/parse.c:2767-2812
dupeprog. C version deep-copies the wordcode array + string
table + pattern progs. zshrs uses Clone on the AST.
Sourcepub fn useeprog(_prog: &ZshProgram)
pub fn useeprog(_prog: &ZshProgram)
Increment an Eprog’s reference count. Direct port of
zsh/Src/parse.c:2813-2822 useeprog. zshrs no-op (Rust
ownership).
Sourcepub fn freeeprog(_prog: ZshProgram)
pub fn freeeprog(_prog: ZshProgram)
Decrement / free an Eprog. Direct port of
zsh/Src/parse.c:2823-2854 freeeprog. zshrs no-op (drop on
scope-exit).
Sourcepub fn ecgetstr(_dup: bool) -> String
pub fn ecgetstr(_dup: bool) -> String
Read a packed string from the wordcode stream. Direct port of
zsh/Src/parse.c:2853-2887 ecgetstr. C version unpacks
4-char inline strings + indexes into the strs table for
longer ones. zshrs no-op (AST stores strings directly).
Sourcepub fn ecrawstr() -> String
pub fn ecrawstr() -> String
Read a packed string without consuming the wordcode pointer.
Direct port of zsh/Src/parse.c:2890-2913 ecrawstr. zshrs
no-op.
Sourcepub fn ecgetarr(_num: usize, _dup: bool) -> Vec<String>
pub fn ecgetarr(_num: usize, _dup: bool) -> Vec<String>
Read a NUL-terminated string array from wordcode. Direct port
of zsh/Src/parse.c:2916-2933 ecgetarr. zshrs no-op.
Sourcepub fn ecgetlist(_num: usize, _dup: bool) -> Vec<String>
pub fn ecgetlist(_num: usize, _dup: bool) -> Vec<String>
Read a linked-list of strings from wordcode. Direct port of
zsh/Src/parse.c:2936-2955 ecgetlist. zshrs no-op.
Sourcepub fn ecgetredirs() -> Vec<ZshRedir>
pub fn ecgetredirs() -> Vec<ZshRedir>
Read a sequence of redirection wordcodes. Direct port of
zsh/Src/parse.c:2958-2991 ecgetredirs. zshrs no-op
(redirections live as AST ZshRedir nodes).
Sourcepub fn eccopyredirs() -> Option<ZshProgram>
pub fn eccopyredirs() -> Option<ZshProgram>
Copy consecutive redirection wordcodes into a new Eprog.
Direct port of zsh/Src/parse.c:3001-3060 eccopyredirs.
zshrs no-op.
Sourcepub fn init_eprog()
pub fn init_eprog()
Initialize the dummy Eprog used as a placeholder. Direct port
of zsh/Src/parse.c:3068-3075 init_eprog. zshrs no-op since
the AST has no equivalent dummy node — empty programs are
just ZshProgram { lists: vec![] }.
Sourcepub fn parse(&mut self) -> Result<ZshProgram, Vec<ParseError>>
pub fn parse(&mut self) -> Result<ZshProgram, Vec<ParseError>>
Parse the complete input