Skip to main content

ZshParser

Struct ZshParser 

Source
pub struct ZshParser<'a> { /* private fields */ }
Expand description

The Zsh Parser

Implementations§

Source§

impl<'a> ZshParser<'a>

Source

pub fn new(input: &'a str) -> ZshParser<'a>

Create a new parser

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

  • sets errflag. zshrs pushes onto self.errors which the caller drains via parse()’s Result return.
Source

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.

Source

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.

Source

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.

Source

pub fn ecdel(_p: usize)

Delete a wordcode at position p. Direct port of zsh/Src/parse.c:412-421 ecdel. zshrs no-op.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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).

Source

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).

Source

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).

Source

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.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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![] }.

Source

pub fn parse(&mut self) -> Result<ZshProgram, Vec<ParseError>>

Parse the complete input

Auto Trait Implementations§

§

impl<'a> Freeze for ZshParser<'a>

§

impl<'a> RefUnwindSafe for ZshParser<'a>

§

impl<'a> Send for ZshParser<'a>

§

impl<'a> Sync for ZshParser<'a>

§

impl<'a> Unpin for ZshParser<'a>

§

impl<'a> UnsafeUnpin for ZshParser<'a>

§

impl<'a> UnwindSafe for ZshParser<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<F, W, T, D> Deserialize<With<T, W>, D> for F
where W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

Source§

fn deserialize( &self, deserializer: &mut D, ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
Source§

impl<T> Finish for T

Source§

fn finish(self)

Does nothing but move self, equivalent to drop.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Gets the layout of the type.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The type for metadata in pointers and references to Self.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

Source§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
Source§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

Source§

fn to_owned_table(&self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more