Struct yash_syntax::parser::Parser
source · [−]pub struct Parser<'a, 'b> { /* private fields */ }
Expand description
The shell syntax parser.
This struct
contains a set of data used in syntax parsing.
Because of the nature of here-document contents that cannot be parsed by a
standard recursive descent parser, most intrinsic functions of Parser
return an AST with MissingHereDoc
filled in place
of a proper here-document data. When the parser parses a here-document
content, the content is accumulated in the parser so that it can be merged
into the main AST in the final step of parsing.
Unless you are interested in parsing a specific syntactic construct that is
only part of source code, you will want to use a function that returns a
complete result filled with proper here-document contents if any.
Then the command_line
function is for you.
See also the module documentation.
Implementations
Creates a new parser based on the given lexer and alias set.
Returns a reference to the current token.
If the current token is not yet read from the underlying lexer, it is read.
Consumes the current token without performing alias substitution.
If the current token is not yet read from the underlying lexer, it is read.
This function does not perform alias substitution and therefore should be
used only in context where no alias substitution is expected. Otherwise,
you should use take_token_manual
or
take_token_auto
instead.
Consumes the current token after performing applicable alias substitution.
If the current token is not yet read from the underlying lexer, it is read.
This function checks if the token is the name of an alias. If it is,
alias substitution is performed on the token and the result is
Ok(AliasSubstituted)
. Otherwise, the token is consumed and returned.
Alias substitution is performed only if at least one of the following is true:
- The token is the first command word in a simple command, that is, it is
the word for the command name. (This condition should be specified by the
is_command_name
parameter.) - The token comes just after the replacement string of another alias substitution that ends with a blank character.
- The token names a global alias.
However, alias substitution should not be performed on a reserved word
in any case. It is your responsibility to check the token type and not to
call this function on a reserved word. That is why this function is named
manual
. To consume a reserved word without performing alias
substitution, you should call take_token_raw
or
take_token_auto
.
Consumes the current token after performing applicable alias substitution.
This function performs alias substitution unless the result is one of the reserved words specified in the argument.
Alias substitution is performed repeatedly until a non-alias token is
found. That is why this function is named auto
. This function should be
used only in contexts where no backtrack is needed after alias
substitution. If you need to backtrack or want to know whether alias
substitution was performed or not, you should use
Self::take_token_manual
, which performs
alias substitution at most once and returns Rec
.
Tests if there is a blank before the next token.
This function can be called to tell whether the previous and next tokens are separated by a blank or they are adjacent.
This function must be called after the previous token has been taken (by
one of take_token_raw
,
take_token_manual
and
take_token_auto
) and before the next token is
peeked. Otherwise, this function would panic.
Panics
If the previous token has not been taken or the next token has been peeked.
Remembers the given partial here-document for later parsing of its content.
Reads here-document contents that matches the remembered list of partial here-documents.
The results are accumulated in the internal list of (non-partial) here-documents.
This function must be called just after a newline token has been taken (either manual or auto). If there is a pending token that has been peeked but not yet taken, this function will panic!
Ensures that there is no pending partial here-document.
If there is any, this function returns a MissingHereDocContent
error.
Returns a list of here-documents with contents that have been read.
Parses an and-or list.
If there is no valid and-or list at the current position, this function
returns Ok(Rec::Parsed(None))
.
Parses a case item.
Does not parse the optional trailing double semicolon.
Returns None
if the next token is esac
.
Parses a case conditional construct.
The next token must be the case
reserved word.
Panics
If the first token is not case
.
Parses a do
clause, i.e., a compound list surrounded in do ... done
.
Returns Ok(None)
if the first token is not do
.
Parses a compound command.
pub async fn full_compound_command(
&mut self
) -> Result<Option<FullCompoundCommand<MissingHereDoc>>>
pub async fn full_compound_command(
&mut self
) -> Result<Option<FullCompoundCommand<MissingHereDoc>>>
Parses a compound command with optional redirections.
Parses a for loop.
The next token must be the for
reserved word.
Panics
If the first token is not for
.
pub async fn short_function_definition(
&mut self,
intro: SimpleCommand<MissingHereDoc>
) -> Result<Command<MissingHereDoc>>
pub async fn short_function_definition(
&mut self,
intro: SimpleCommand<MissingHereDoc>
) -> Result<Command<MissingHereDoc>>
Parses a function definition command that does not start with the
function
reserved word.
This function must be called just after a simple
command has been parsed.
The simple command must be passed as an argument.
If the simple command has only one word and the next token is (
, it is
parsed as a function definition command.
Otherwise, the simple command is returned intact.
Parses an if conditional construct.
The next token must be the if
reserved word.
Panics
If the first token is not if
.
Parses a list.
This function parses a sequence of and-or lists that are separated by ;
or &
. A newline token that delimits the list is not parsed.
If there is no valid command at the current position, this function returns a list with no items.
Parses an optional newline token and here-document contents.
If the current token is a newline, it is consumed and any pending here-document contents
are read starting from the next line. Otherwise, this function returns Ok(false)
without
any side effect.
Parses a complete command optionally delimited by a newline.
A complete command is a minimal sequence of and-or lists that can be executed in the shell environment. This function reads as many lines as needed to compose the complete command.
If the current line is empty (or containing only whitespaces and comments), the result is
an empty list. If the first token of the current line is the end of input, the result is
Ok(None)
.
Parses an optional compound list.
A compound list is a sequence of one or more and-or lists that are separated by newlines and optionally preceded and/or followed by newlines.
This function stops parsing on encountering an unexpected token that cannot be parsed as the beginning of an and-or list. The caller should check that the next token is an expected one.
pub fn maybe_compound_list_boxed(
&mut self
) -> Pin<Box<dyn Future<Output = Result<List<MissingHereDoc>>>>>
pub fn maybe_compound_list_boxed(
&mut self
) -> Pin<Box<dyn Future<Output = Result<List<MissingHereDoc>>>>>
Like maybe_compound_list
, but returns the future in a pinned box.
Parses a redirection.
If the current token is not a redirection operator, Ok(None)
is returned. If a word token
is missing after the operator, Err(Error{...})
is returned with a cause of
MissingRedirOperand
or
MissingHereDocDelimiter
.
Parses a (possibly empty) sequence of redirections.
Parses the value of an array assignment.
This function first consumes a (
token, then any number of words
separated by blanks and/or newlines, and finally a )
.
If the first token is not (
, the result is Ok(None)
.
If the last )
is missing, the result is
Err(ErrorCause::Syntax(SyntaxError::UnclosedArrayValue(_)))
.
Parses a simple command.
If there is no valid command at the current position, this function
returns Ok(Rec::Parsed(None))
.