MiniDevil As beautiful as a shell
parser.h File Reference

Parser entry point & sub parser prototypes. More...

#include "structs.h"
+ Include dependency graph for parser.h:
+ This graph shows which files directly or indirectly include this file:

Detailed Description

Parser entry point & sub parser prototypes.

Definition in file parser.h.

Functions

t_astparse (t_token *tokens)
 Parse a token list into an AST. More...
 
t_astparse_pipeline (t_token **tokens)
 Parse a pipeline of commands connected by pipes. More...
 
t_astparse_command (t_token **tokens)
 Parse a command with all its redirections. More...
 
t_astparse_simple_command (t_token **tokens)
 Parse a simple command from consecutive word tokens. More...
 
char ** collect_args (t_token **tokens, int *argc)
 Collect consecutive word tokens into an arguments array. More...
 
int print_syntax_error (t_syntax_error err)
 Print an error message to STDERR. More...
 
int is_redirection (t_token_type type)
 Check if a token type is a redirection operator. More...
 
int validate_syntax (t_token *tokens)
 Run all validation checks on a token list. More...
 
int count_word_tokens (t_token *tokens)
 Count consecutive word token groups. More...
 
t_astreverse_redir_chain (t_ast *node)
 Reverse a redirection chain for left to right execution order. More...
 
char * join_connected_delim (t_token **tokens, int *quoted)
 Join connected tokens in a single string. More...
 

Function Documentation

◆ parse()

t_ast* parse ( t_token tokens)

Parse a token list into an AST.

Validates syntax and then delegates to parse_pipeline() which handles the recursive descent

Parameters
tokensHead of the token list
Returns
Root of the AST or NULL on syntax error / empty input
Warning
The caller has to free the returned AST with free_ast()

Definition at line 26 of file parser.c.

References parse_pipeline(), and validate_syntax().

Referenced by process_input(), and process_ui_input().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_pipeline()

t_ast* parse_pipeline ( t_token **  tokens)

Parse a pipeline of commands connected by pipes.

Left associative tree: "A | B | C" -> PIPE(PIPE(A, B), C)

Parameters
tokensPointer to current token pointer
Returns
Pipeline AST or NULL on failure

Definition at line 25 of file parser_pipeline.c.

References create_pipe_node(), free_ast(), t_token::next, parse_command(), TOKEN_PIPE, and t_token::type.

Referenced by parse().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_command()

t_ast* parse_command ( t_token **  tokens)

Parse a command with all its redirections.

Parses a simple command and wraps it in redirection nodes as long as redirection tokens follow. After each redirection it collects any trailing word tokens and adds them to the command's args

Parameters
tokensPointer to current token pointer (advanced)
Returns
Command AST or NULL on error
Note
It reverses the redirection chain for left to right execution

Definition at line 134 of file parser_redir.c.

References collect_and_merge_remaining_args(), get_command_node(), is_redirection(), parse_one_redirection(), parse_simple_command(), and reverse_redir_chain().

Referenced by parse_pipeline().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_simple_command()

t_ast* parse_simple_command ( t_token **  tokens)

Parse a simple command from consecutive word tokens.

Collects the arguments via collect_args() and wraps them in a NODE_COMMAND AST node

Parameters
tokensPointer to current token pointer
Returns
NODE_COMMAND AST node or NULL if no words were found

Definition at line 117 of file parser_cmd.c.

References collect_args(), and create_cmd_node().

Referenced by parse_command().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ collect_args()

char** collect_args ( t_token **  tokens,
int *  argc 
)

Collect consecutive word tokens into an arguments array.

Parameters
tokensPointer to current token pointer
argcNumber of arguments collected
Returns
NULL terminated arguments array or NULL if none

Definition at line 88 of file parser_cmd.c.

References count_word_tokens(), and fill_args_array().

Referenced by collect_and_merge_remaining_args(), and parse_simple_command().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ print_syntax_error()

int print_syntax_error ( t_syntax_error  err)

Print an error message to STDERR.

Parameters
errError code
Returns
-1 always (value that will be return by callers)

Definition at line 44 of file parser_grammar_error.c.

References ERR_NONE, and get_error_msg().

Referenced by validate_empty(), validate_pipe_position(), and validate_redir_syntax().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ is_redirection()

int is_redirection ( t_token_type  type)

Check if a token type is a redirection operator.

Parameters
typeToken type to check
Returns
1 if redirection and 0 if not

Definition at line 62 of file parser_grammar_error.c.

References TOKEN_APPEND, TOKEN_HEREDOC, TOKEN_REDIR_IN, and TOKEN_REDIR_OUT.

Referenced by parse_command(), and validate_redir_syntax().

+ Here is the caller graph for this function:

◆ validate_syntax()

int validate_syntax ( t_token tokens)

Run all validation checks on a token list.

Parameters
tokensToken list to validate
Returns
0 if valid and -1 on error (with message printed to STDERR)

Definition at line 88 of file parser_grammar.c.

References validate_empty(), validate_pipe_position(), and validate_redir_syntax().

Referenced by parse().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ count_word_tokens()

int count_word_tokens ( t_token tokens)

Count consecutive word token groups.

Connected tokens count as 1 group

Parameters
tokensToken list starting position
Returns
Number of word groups

Definition at line 25 of file parser_cmd.c.

References t_token::connected, t_token::next, TOKEN_WORD, and t_token::type.

Referenced by collect_args().

+ Here is the caller graph for this function:

◆ reverse_redir_chain()

t_ast* reverse_redir_chain ( t_ast node)

Reverse a redirection chain for left to right execution order.

The parser builds redirections with the outermost being last, so this reverses the chain so that the first parsed redirection is outermost, matching BASH's left to right behavior

Parameters
nodeOutermost redirection
Returns
New outermost node of the reverse chain

Definition at line 39 of file parser_redir_utils.c.

References t_redir_node::cmd, t_ast::data, is_redir(), and t_ast_data::redir.

Referenced by parse_command().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ join_connected_delim()

char* join_connected_delim ( t_token **  tokens,
int *  quoted 
)

Join connected tokens in a single string.

  • Walks the connected tokens, accumulating their values
  • Sets quoted to 1 if any token in the chain was quoted
Parameters
tokensPointer to current token pointer
quotedSet to 1 if any was quoted
Returns
Joined string or NULL on allocation failure

Definition at line 69 of file parser_redir_utils.c.

References t_token::next, and QUOTE_NONE.

Referenced by parse_one_redirection().

+ Here is the caller graph for this function:

Go to the source code of this file.