Update: Work on AST

This commit is contained in:
Maix0 2024-06-09 22:27:00 +02:00
parent 8f00b8fd9b
commit 5759396eb2
9 changed files with 79 additions and 1297 deletions

View file

@ -1,570 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ast.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/25 19:30:30 by maiboyer #+# #+# */
/* Updated: 2024/05/28 14:49:05 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef AST_H
#define AST_H
#include "ast/forward.h"
#include "me/types.h"
/// @brief Node types enumeration
/// @details This enumeration is used to identify the type of a node
enum e_ast_type
{
TY_EMPTY = 0,
TY_AND_LIST,
TY_AND_OR_LIST,
TY_ARITHMETIC_EXPANSION,
TY_ASSIGNMENT,
TY_ASSIGNMENT_LIST,
TY_AST_STRING,
TY_ASYNC_COMMAND,
TY_BRACE_COMMAND,
TY_CASE_COMMAND,
TY_CASE_ITEM,
TY_COMMAND,
TY_COMMAND_BACKTICKS,
TY_COMMAND_SUBSTITUTION,
TY_COMPOUND_LIST,
TY_DOUBLE_QUOTE_STRING,
TY_ELIF_CLAUSE,
TY_ELSE_CLAUSE,
TY_FOR_COMMAND,
TY_FUNCTION_DEFINITION,
TY_IF_CLAUSE,
TY_IF_COMMAND,
TY_NAME,
TY_NOT,
TY_OR_LIST,
TY_PARAMETER_EXPANSION,
TY_PIPE_LIST,
TY_PROGRAM,
TY_REDIRECT_FILE,
TY_REDIRECT_HEREDOC,
TY_SEQUENTIAL_LIST,
TY_SIMPLE_COMMAND,
TY_SUBSHELL_COMMAND,
TY_UNTIL_COMMAND,
TY_WHILE_COMMAND,
TY_WORD,
};
/// Can be either a t_sequential_list, t_async_command, t_and_or_list, t_not,
/// t_pipe_list or an t_command
union u_program_body {
t_ast_type *type;
t_sequential_list *sequential_list;
t_async_command *async_command;
t_and_or_list *and_or_list;
t_not *not_;
t_pipe_list *pipe_list;
t_command *command;
};
/// @brief Entry point of the AST
struct s_program
{
t_ast_type type;
t_program_body *cmds;
t_usize cmds_len;
};
/// Can be either a t_sequential_list, t_async_command, t_and_or_list, t_not,
/// t_pipe_list or an t_command
union u_compound_list_body {
t_ast_type *type;
t_sequential_list *sequential_list;
t_async_command *async_command;
t_and_or_list *and_or_list;
t_not *not_;
t_pipe_list *pipe_list;
t_command *command;
};
struct s_compound_list
{
t_ast_type type;
t_compound_list_body *cmds;
t_usize cmds_len;
};
/// Can be either a t_async_command, t_and_or_list, t_not, t_pipe_list or an
/// t_command
union u_sequential_list_body {
t_ast_type *type;
t_async_command *async_command;
t_and_or_list *and_or_list;
t_not *not_;
t_pipe_list *pipe_list;
t_command *command;
};
/// @note Commands that are separated by a semicolon ; shall be executed
/// sequentially.
/// @note Read more:
/// http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_02_09_03_04
/// @note Format: `command1; command2 [; command3 ] ... [;]`
struct s_sequential_list
{
t_ast_type type;
t_sequential_list_body *cmds;
t_usize cmds_len;
};
/// Can be either a t_and_or_list, t_not t_pipe_list or t_command
union u_async_command_body {
t_ast_type *type;
t_and_or_list *and_or_list;
t_not *not_;
t_pipe_list *pipe_list;
t_command *command;
};
/// @note Commands that are ended by an & shall be executed asynchronously.
struct s_async_command
{
t_ast_type type;
t_async_command_body cmd;
};
/*
Part of AND_OR list
*/
/// Can be either a t_not t_pipe_list or t_command.
/// The first element CAN be an t_or_list
union u_and_list_body {
t_ast_type *type;
t_pipe_list *pipe_list;
t_command *cmd;
t_or_list *or_list;
};
struct s_and_list
{
t_ast_type type;
t_and_list_body *cmds;
t_usize cmds_len;
};
/// Can be either a t_not t_pipe_list or t_command.
/// The first element CAN be an t_and_list
union u_or_list_body {
t_ast_type *type;
t_pipe_list *pipe_list;
t_command *cmd;
t_and_list *or_list;
};
struct s_or_list
{
t_ast_type type;
t_or_list_body *cmds;
t_usize cmds_len;
};
/// Either an ast_node of type `t_and_list` or `t_or_list`
union u_and_or_list_body {
t_ast_type *type;
t_and_list *and_list;
t_or_list *or_list;
};
struct s_and_or_list
{
t_ast_type type;
t_and_or_list_body cmds;
};
/// Can be either a t_pipe_list or a t_command
union u_not_body {
t_ast_type *type;
t_pipe_list *pipe_list;
t_command *cmd;
};
struct s_not
{
t_ast_type type;
t_not_body cmd;
};
struct s_pipe_list
{
t_ast_type type;
t_ast_node **cmds;
t_usize cmds_len;
};
union u_command_inner {
t_ast_type *type;
t_simple_command *simple_command;
t_compound_command *compound_command;
t_assignment_list *assignments_list;
t_function_definition *function_definition;
};
/// Can be either an t_simple_command, t_compound_command, t_assignments_list or
/// t_function_definition
struct s_command
{
t_ast_type type;
t_command_inner inner;
};
union u_redirect_or_assign {
t_ast_type *type;
t_redirect *redirect;
t_assignment_list *assignments;
};
union u_redirect_or_word {
t_ast_type *type;
t_redirect *redirect;
t_word *word;
};
struct s_simple_command
{
t_ast_type type;
t_redirect_or_assign *prefix;
t_usize prefix_len;
t_word *_Nullable cmd;
t_redirect_or_word *suffix;
t_usize suffix_len;
};
enum e_assignement_modifier
{
AM_NONE = 0,
AM_EXPORT,
AM_LOCAL,
AM_READONLY,
};
struct s_assignment_list
{
t_ast_type type;
t_assignment *assignments;
t_usize assignments_len;
t_assignement_modifier modifier;
};
// Compound command
// Can be either a t_brace_command, t_subshell_command, t_if_command,
// t_for_command, t_case_command, t_while_command, t_until_command
union u_compound_command {
t_ast_type *type;
t_brace_command *brace_command;
t_subshell_command *subshell_command;
t_if_command *if_command;
t_for_command *for_command;
t_case_command *case_command;
t_while_command *while_command;
t_until_command *until_command;
};
struct s_brace_command
{
t_ast_type type;
t_compound_list *body;
t_redirect *redirects;
t_usize redirects_len;
};
struct s_subshell_command
{
t_ast_type type;
t_compound_list *_Nullable body;
t_redirect *redirects;
t_usize redirects_len;
};
union u_if_clauses {
t_ast_type *type;
t_if_clause *if_clause;
t_elif_clause *elif_clause;
t_else_clause *else_clause;
};
struct s_if_command
{
t_ast_type type;
t_if_clauses *clauses;
t_usize clauses_len;
t_redirect *redirect;
t_usize redirect_len;
};
struct s_if_clause
{
t_ast_type type;
t_compound_list *condition;
t_compound_list *body;
};
struct s_elif_clause
{
t_ast_type type;
t_compound_list *condition;
t_compound_list *body;
};
struct s_else_clause
{
t_ast_type type;
t_compound_list *body;
};
struct s_for_command
{
t_ast_type type;
t_name *var;
t_word *items;
t_usize items_len;
t_compound_list *body;
t_redirect *redirects;
t_usize redirects_len;
};
struct s_case_command
{
t_ast_type type;
t_name *var;
t_case_item *cases;
t_usize cases_len;
t_redirect *redirects;
t_usize redirects_len;
};
struct s_case_item
{
t_ast_type type;
t_word *pattern;
t_usize pattern_len;
t_compound_list *body;
};
struct s_while_command
{
t_ast_type type;
t_compound_list *condition;
t_compound_list *body;
t_redirect *redirects;
t_usize redirects_len;
};
struct s_until_command
{
t_ast_type type;
t_compound_list *condition;
t_compound_list *body;
t_redirect *redirects;
t_usize redirects_len;
};
struct s_function_definition
{
t_ast_type type;
t_name *name;
t_compound_command *body;
t_redirect *redirects;
t_usize redirects_len;
};
/// Can be either a t_redirect_file or t_redirect_heredoc
union u_redirect {
t_ast_type *type;
t_redirect_file *file;
t_redirect_heredoc *heredoc;
};
enum e_redirect_file_op
{
RF_INPUT, // <
RF_OUTPUT, // >
RF_APPEND, // >>
RF_DUP_INPUT, // <&
RF_DUP_OUTPUT, // >&
RF_CLOBBER, // >|
RF_INPUT_OUPUT, // <>
};
struct s_redirect_file
{
t_ast_type type;
t_isize fd;
t_redirect_file_op op;
t_word *file;
};
enum e_redirect_heredoc_op
{
RH_HEREDOC, // <<
RH_HEREDOC_IDENT // <<-
};
struct s_redirect_heredoc
{
t_ast_type type;
t_isize fd;
t_redirect_heredoc_op op;
t_word *delimiter;
t_word *content;
};
struct s_assignment
{
t_ast_type type;
t_ast_string *name;
t_word *_Nullable value;
};
struct s_ast_string
{
t_ast_type type;
t_str value;
};
struct s_name
{
t_ast_type type;
t_ast_string *value;
};
// Can be either a t_expension or a t_ast_string
union u_expension_or_string {
t_ast_type *type;
t_expension *expension;
t_ast_string *string;
};
struct s_word
{
t_ast_type type;
t_expension_or_string *contents;
t_usize contents_len;
};
#define OP_NONE 0
enum e_op_pre
{
OP_PRE_HASH = 1, // '#'
};
enum e_op_in
{
OP_IN_COLON_MINUS = 1, // ':-'
OP_IN_MINUS, // '-'
OP_IN_COLON_EQUAL, // ':='
OP_IN_EQUAL, // '='
OP_IN_COLON_QUESTION, // ':?'
OP_IN_QUESTION, // '?'
OP_IN_COLON_PLUS, // ':+'
OP_IN_PLUS, // '+'
OP_IN_PERCENT_PERCENT, // '%%'
OP_IN_PERCENT, // '%'
OP_IN_HASH_HASH, // '##'
OP_IN_HASH, // '#'
OP_IN_COLON, // ':'
OP_IN_SLASH_SLASH, // '//'
OP_IN_SLASH, // '/'
};
/// is either a t_parameter_expansion, t_arithmetic_expansion,
/// t_command_substitution or t_command_backticks
union u_expension {
t_ast_type *type;
t_parameter_expansion *parameter_expansion;
t_arithmetic_expansion *arithmetic_expansion;
t_command_substitution *command_substitution;
t_command_backticks *command_backticks;
t_double_quote_string *double_quote_string;
};
struct s_double_quote_string
{
t_ast_type type;
t_expension_or_string *parts;
t_usize parts_len;
};
struct s_parameter_expansion
{
t_ast_type type;
t_op_in op_pre;
t_ast_string *param;
t_op_in op_in;
t_word *_Nullable word;
};
struct s_arithmetic_expansion
{
t_ast_type type;
t_ast_string *expr;
};
struct s_command_substitution
{
t_ast_type type;
t_compound_list *cmds;
t_usize cmds_len;
};
struct s_command_backticks
{
t_ast_type type;
t_ast_string *text;
};
union u_ast_node {
t_ast_type type;
t_and_list and_list;
t_and_or_list and_or_list;
t_arithmetic_expansion arithmetic_expansion;
t_assignment assignment;
t_assignment_list assignment_list;
t_ast_string ast_string;
t_async_command async_command;
t_brace_command brace_command;
t_case_command case_command;
t_case_item case_item;
t_command command;
t_command_backticks command_backticks;
t_command_substitution command_substitution;
t_compound_list compound_list;
t_double_quote_string double_quote_string;
t_elif_clause elif_clause;
t_else_clause else_clause;
t_for_command for_command;
t_function_definition function_definition;
t_if_clause if_clause;
t_if_command if_command;
t_name name;
t_not not_;
t_or_list or_list;
t_parameter_expansion parameter_expansion;
t_pipe_list pipe_list;
t_program program;
t_redirect_file redirect_file;
t_redirect_heredoc redirect_heredoc;
t_sequential_list sequential_list;
t_simple_command simple_command;
t_subshell_command subshell_command;
t_until_command until_command;
t_while_command while_command;
t_word word;
};
#endif /* AST_H */

View file

@ -1,78 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* forward.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/28 13:24:34 by maiboyer #+# #+# */
/* Updated: 2024/05/28 13:51:40 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FORWARD_H
#define FORWARD_H
typedef union u_ast_node t_ast_node;
typedef enum e_assignement_modifier t_assignement_modifier;
typedef enum e_ast_type t_ast_type;
typedef enum e_op_in t_op_in;
typedef enum e_op_pre t_op_pre;
typedef enum e_redirect_file_op t_redirect_file_op;
typedef enum e_redirect_heredoc_op t_redirect_heredoc_op;
typedef struct s_and_list t_and_list;
typedef struct s_and_or_list t_and_or_list;
typedef struct s_arithmetic_expansion t_arithmetic_expansion;
typedef struct s_assignment_list t_assignment_list;
typedef struct s_assignment t_assignment;
typedef struct s_ast_string t_ast_string;
typedef struct s_async_command t_async_command;
typedef struct s_brace_command t_brace_command;
typedef struct s_case_command t_case_command;
typedef struct s_case_item t_case_item;
typedef struct s_command_backticks t_command_backticks;
typedef struct s_command_substitution t_command_substitution;
typedef struct s_command t_command;
typedef struct s_compound_list t_compound_list;
typedef struct s_double_quote_string t_double_quote_string;
typedef struct s_elif_clause t_elif_clause;
typedef struct s_else_clause t_else_clause;
typedef struct s_for_command t_for_command;
typedef struct s_function_definition t_function_definition;
typedef struct s_if_clause t_if_clause;
typedef struct s_if_command t_if_command;
typedef struct s_name t_name;
typedef struct s_not t_not;
typedef struct s_or_list t_or_list;
typedef struct s_parameter_expansion t_parameter_expansion;
typedef struct s_pipe_list t_pipe_list;
typedef struct s_program t_program;
typedef struct s_redirect_file t_redirect_file;
typedef struct s_redirect_heredoc t_redirect_heredoc;
typedef struct s_sequential_list t_sequential_list;
typedef struct s_simple_command t_simple_command;
typedef struct s_subshell_command t_subshell_command;
typedef struct s_until_command t_until_command;
typedef struct s_while_command t_while_command;
typedef struct s_word t_word;
typedef union u_and_list_body t_and_list_body;
typedef union u_and_or_list_body t_and_or_list_body;
typedef union u_async_command_body t_async_command_body;
typedef union u_command_inner t_command_inner;
typedef union u_compound_command t_compound_command;
typedef union u_compound_list_body t_compound_list_body;
typedef union u_expension_or_string t_expension_or_string;
typedef union u_expension t_expension;
typedef union u_if_clauses t_if_clauses;
typedef union u_not_body t_not_body;
typedef union u_or_list_body t_or_list_body;
typedef union u_program_body t_program_body;
typedef union u_redirect_or_assign t_redirect_or_assign;
typedef union u_redirect_or_word t_redirect_or_word;
typedef union u_redirect t_redirect;
typedef union u_sequential_list_body t_sequential_list_body;
#endif /* FORWARD_H */

View file

@ -1,204 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* from_node.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/28 13:14:10 by maiboyer #+# #+# */
/* Updated: 2024/05/29 00:54:00 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FROM_PROGRAM_C
#define FROM_PROGRAM_C
#include "forward.h"
#include "me/types.h"
#include "app/node.h"
#include "ast/ast.h"
t_ast_node *from_node(t_node *node);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_and_list *build_and_list(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_and_or_list *build_and_or_list(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_arithmetic_expansion *build_arithmetic_expansion(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_assignment_list *build_assignment_list(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_assignment *build_assignment(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_ast_string *build_ast_string(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_async_command *build_async_command(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_brace_command *build_brace_command(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_case_command *build_case_command(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_case_item *build_case_item(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_command_backticks *build_command_backticks(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_command_substitution *build_command_substitution(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_command *build_command(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_compound_list *build_compound_list(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_double_quote_string *build_double_quote_string(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_for_command *build_for_command(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_function_definition *build_function_definition(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_if_command *build_if_command(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_if_clause *build_if_clause(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_elif_clause *build_elif_clause(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_else_clause *build_else_clause(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_name *build_name(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_not *build_not(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_or_list *build_or_list(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_parameter_expansion *build_parameter_expansion(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_pipe_list *build_pipe_list(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_program *build_program(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_redirect_file *build_redirect_file(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_redirect_heredoc *build_redirect_heredoc(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_sequential_list *build_sequential_list(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_simple_command *build_simple_command(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_subshell_command *build_subshell_command(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_until_command *build_until_command(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_while_command *build_while_command(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_word *build_word(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_ast_node *build_comment(t_node *node, t_usize size);
#endif /* FROM_NODE_C */

View file

@ -1 +1 @@
from_node
empty

0
ast/src/empty.c Normal file
View file

View file

@ -1,341 +0,0 @@
// /* ************************************************************************** */
// /* */
// /* ::: :::::::: */
// /* from_node.c :+: :+: :+: */
// /* +:+ +:+ +:+ */
// /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
// /* +#+#+#+#+#+ +#+ */
// /* Created: 2024/05/28 13:18:44 by maiboyer #+# #+# */
// /* Updated: 2024/05/29 22:48:00 by maiboyer ### ########.fr */
// /* */
// /* ************************************************************************** */
// #include "me/str/str.h"
// #include "me/types.h"
// #include "app/node.h"
// #include "ast/from_node.h"
// #include "gmr/field_identifiers.h"
// #include "gmr/symbols.h"
// #include <stdio.h>
// t_ast_node *alloc_node(t_ast_type ty)
// {
// t_ast_node *ptr;
// ptr = mem_alloc(sizeof(*ptr));
// ptr->type = ty;
// return (ptr);
// }
// t_ast_node *build_comment(t_node *node, t_usize size)
// {
// t_ast_node *ptr;
// (void)(node);
// (void)(size);
// ptr = alloc_node(TY_EMPTY);
// return (ptr);
// }
// t_and_list *build_and_list(t_node *node, t_usize size)
// {
// t_and_list *ptr;
// t_usize i;
// t_usize j;
// (void)(size);
// if (node == NULL || node->kind != sym_list || size == 0 ||
// node->childs_count <= 1)
// me_abort("Invalid arguments to build ast!");
// ptr = (void *)alloc_node(TY_AND_LIST);
// ptr->cmds = mem_alloc_array(sizeof(t_ast_node *), node->childs_count);
// i = 0;
// j = 0;
// while (i < node->childs_count)
// {
// if ((node->childs[i].kind == anon_sym_AMP_AMP ||
// node->childs[i].kind == anon_sym_PIPE_PIPE) &&
// (i++, true))
// continue;
// ptr->cmds[j++].cmd = (void *)from_node(&node->childs[i]);
// i++;
// }
// ptr->cmds_len = j;
// return (ptr);
// }
// t_or_list *build_or_list(t_node *node, t_usize size)
// {
// t_or_list *ptr;
// t_usize i;
// t_usize j;
// (void)(size);
// if (node == NULL || node->kind != sym_list || size == 0 ||
// node->childs_count <= 1)
// me_abort("Invalid arguments to build ast!");
// ptr = (void *)alloc_node(TY_OR_LIST);
// ptr->cmds = mem_alloc_array(sizeof(t_ast_node *), node->childs_count);
// i = 0;
// j = 0;
// while (i < node->childs_count)
// {
// if ((node->childs[i].kind == anon_sym_AMP_AMP ||
// node->childs[i].kind == anon_sym_PIPE_PIPE) &&
// (i++, true))
// continue;
// ptr->cmds[j++].cmd = (void *)from_node(&node->childs[i]);
// i++;
// }
// ptr->cmds_len = j;
// return (ptr);
// }
// t_ast_string *build_ast_string(t_node *node, t_usize size)
// {
// t_ast_string *ptr;
// (void)(size);
// if (node == NULL ||
// (node->kind != sym_string_content && node->kind != sym_raw_string &&
// node->kind == sym_ansi_c_string) ||
// size == 0)
// me_abort("Invalid arguments to build ast!");
// ptr = (void *)alloc_node(TY_AST_STRING);
// ptr->value = str_clone(node_getstr(node));
// return (ptr);
// }
// t_and_or_list *build_and_or_list(t_node *node, t_usize size)
// {
// t_and_or_list *ptr;
// (void)(node);
// (void)(size);
// if (node == NULL || node->kind != sym_list || size == 0 ||
// node->childs_count <= 1)
// me_abort("Invalid arguments to build ast!");
// ptr = (void *)alloc_node(TY_AND_OR_LIST);
// if (node->childs[1].kind == anon_sym_PIPE_PIPE)
// ptr->cmds.or_list = build_or_list(node, 1);
// else if (node->childs[1].kind == anon_sym_AMP_AMP)
// ptr->cmds.and_list = build_and_list(node, 1);
// else
// return (mem_free(ptr), NULL);
// return (ptr);
// }
// t_pipe_list *build_pipe_list(t_node *node, t_usize size)
// {
// t_pipe_list *ptr;
// t_usize i;
// t_usize j;
// (void)(size);
// if (node == NULL || node->kind != sym_pipeline || size == 0 ||
// node->childs_count <= 1)
// me_abort("Invalid arguments to build ast!");
// ptr = (void *)alloc_node(TY_PIPE_LIST);
// ptr->cmds = mem_alloc_array(sizeof(t_ast_node *), node->childs_count);
// i = 0;
// j = 0;
// while (i < node->childs_count)
// {
// if (node->childs[i].kind == anon_sym_PIPE)
// i++;
// ptr->cmds[j++] = from_node(&node->childs[i]);
// i++;
// }
// return (ptr);
// }
// t_double_quote_string *build_double_qoute_string(t_node *node, t_usize size)
// {
// t_double_quote_string *ptr;
// t_usize i;
// t_usize j;
// (void)(size);
// if (node == NULL || node->kind != sym_string || size == 0)
// me_abort("Invalid arguments to build ast!");
// ptr = (void *)alloc_node(TY_DOUBLE_QUOTE_STRING);
// ptr->parts = mem_alloc_array(sizeof(*ptr->parts), node->childs_count);
// i = 0;
// j = 0;
// while (i < node->childs_count)
// ptr->parts[j++].expension->type = (void *)from_node(&node->childs[i++]);
// ptr->parts_len = j;
// return (ptr);
// }
// t_not *build_not(t_node *node, t_usize size)
// {
// t_not *ptr;
// (void)(size);
// if (node == NULL || node->kind != sym_negated_command || size == 0)
// me_abort("Invalid arguments to build ast!");
// ptr = (void *)alloc_node(TY_NOT);
// ptr->cmd.type = (void *)from_node(&node->childs[1]);
// return (ptr);
// }
// t_command *build_command(t_node *node, t_usize size)
// {
// t_command *ptr;
// t_usize i;
// t_usize j;
// (void)(size);
// if (node == NULL || size == 0 ||
// (node->kind != sym_redirected_statement && node->kind != sym_command &&
// node->kind != sym_test_command &&
// node->kind != sym_declaration_command &&
// node->kind != sym_unset_command))
// me_abort("Invalid arguments to build ast!");
// ptr = (void *)alloc_node(TY_COMMAND);
// if (node->kind == sym_redirected_statement)
// {
// if (node->childs[0].field == field_body)
// {
// ptr->inner.type = (void *)from_node(&node->childs[0]);
// }
// i = 0;
// j = 0;
// while (i < node->childs_count)
// {
// if (node->childs[i].field == field_redirect)
// {
// if (*ptr->inner.type == TY_SIMPLE_COMMAND)
// {
// ptr->inner.simple_command->suffix[j++].type = (void *)from_node(&node->childs[i]);
// ptr->inner.simple_command->suffix_len = j;
// }
// }
// i++;
// }
// }
// return (ptr);
// }
// /*
// */
// t_ast_node *from_node(t_node *node)
// {
// if (node == NULL)
// return (NULL);
// if (node->kind == sym_program)
// return ((t_ast_node *)build_program(node, 1));
// if (node->kind == sym_string)
// return ((t_ast_node *)build_double_quote_string(node, 1));
// if (node->kind == sym_word)
// return ((t_ast_node *)build_word(node, 1));
// if (node->kind == sym_concatenation)
// return ((t_ast_node *)build_word(node, 1));
// if (node->kind == sym_variable_name)
// return ((t_ast_node *)build_word(node, 1));
// if (node->kind == sym_command_name)
// return ((t_ast_node *)build_word(node, 1));
// if (node->kind == sym_translated_string)
// return ((t_ast_node *)build_word(node, 1));
// if (node->kind == sym_string_content)
// return ((t_ast_node *)build_ast_string(node, 1));
// if (node->kind == sym_raw_string)
// return ((t_ast_node *)build_ast_string(node, 1));
// if (node->kind == sym_ansi_c_string)
// return ((t_ast_node *)build_ast_string(node, 1));
// if (node->kind == sym_variable_assignments)
// return (t_ast_node *)build_assignment_list(node, 1);
// if (node->kind == sym_variable_assignment)
// return (t_ast_node *)build_assignment_list(node, 1);
// if (node->kind == sym_command_substitution)
// return (t_ast_node *)build_parameter_expansion(node, 1);
// if (node->kind == sym_expansion)
// return (t_ast_node *)build_parameter_expansion(node, 1);
// if (node->kind == sym_simple_expansion)
// return (t_ast_node *)build_parameter_expansion(node, 1);
// if (node->kind == sym_arithmetic_expansion)
// return ((t_ast_node *)build_arithmetic_expansion(node, 1));
// if (node->kind == sym_comment)
// return ((t_ast_node *)build_comment(node, 1));
// if (node->kind == sym_negated_command)
// return ((t_ast_node *)build_not(node, 1));
// if (node->kind == sym_redirected_statement)
// return ((t_ast_node *)build_command(node, 1));
// if (node->kind == sym_command)
// return ((t_ast_node *)build_command(node, 1));
// if (node->kind == sym_test_command)
// return ((t_ast_node *)build_command(node, 1));
// if (node->kind == sym_declaration_command)
// return ((t_ast_node *)build_command(node, 1));
// if (node->kind == sym_unset_command)
// return ((t_ast_node *)build_command(node, 1));
// if (node->kind == sym_file_redirect)
// return ((t_ast_node *)build_redirect_file(node, 1));
// if (node->kind == sym_heredoc_redirect)
// return ((t_ast_node *)build_redirect_heredoc(node, 1));
// if (node->kind == sym_list)
// return ((t_ast_node *)build_and_or_list(node, 1));
// if (node->kind == sym_pipeline)
// return ((t_ast_node *)build_pipe_list(node, 1));
// if (node->kind == sym_subshell)
// return ((t_ast_node *)build_subshell_command(node, 1));
// if (node->kind == sym_brace_expression)
// return ((t_ast_node *)build_brace_command(node, 1));
// if (node->kind == sym_compound_statement)
// return ((t_ast_node *)build_compound_list(node, 1));
// if (node->kind == sym_if_statement)
// return ((t_ast_node *)build_if_command(node, 1));
// if (node->kind == sym_else_clause)
// return ((t_ast_node *)build_else_clause(node, 1));
// if (node->kind == sym_elif_clause)
// return ((t_ast_node *)build_elif_clause(node, 1));
// if (node->kind == sym_case_statement)
// return ((t_ast_node *)build_case_command(node, 1));
// if (node->kind == sym_case_item)
// return ((t_ast_node *)build_case_item(node, 1));
// if (node->kind == sym_last_case_item)
// return ((t_ast_node *)build_case_item(node, 1));
// if (node->kind == sym_while_statement)
// return ((t_ast_node *)build_while_command(node, 1));
// if (node->kind == sym_for_statement)
// return ((t_ast_node *)build_for_command(node, 1));
// if (node->kind == sym_function_definition)
// return ((t_ast_node *)build_function_definition(node, 1));
// printf("unknown node of kind '%s'\n", node->kind_str);
// return (NULL);
// }

View file

@ -6,50 +6,48 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/25 16:13:52 by maiboyer #+# #+# */
/* Updated: 2024/05/01 17:38:14 by maiboyer ### ########.fr */
/* Updated: 2024/06/09 21:46:14 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "../parse_types.h"
#include "../static/headers/constants.h"
#include "../static/headers/symbols.h"
#include "../parse_types.h"
bool lex_keywords_main(t_lexer *lexer, t_state_id state);
bool lex_normal_main(t_lexer *lexer, t_state_id state);
bool tree_sitter_sh_external_scanner_scan(void *ctx, t_lexer *lexer, const bool *ret);
const bool *create_external_scanner_states(void);
const char *const *create_field_names(void);
const char *const *create_symbols_names(void);
const t_field_map_entry *create_field_map_entries(void);
const t_field_map_slice *create_field_map_slices(void);
const t_lex_modes *create_lex_modes(void);
const t_parse_action_entry *create_parse_actions_entries(void);
const t_state_id *create_primary_state_ids(void);
const t_symbol *create_alias_sequences(void);
const t_symbol *create_external_scanner_symbol_map(void);
const t_symbol *create_non_terminal_alias_map(void);
const t_symbol *create_unique_symbols_map(void);
const t_symbol_metadata *create_symbols_metadata(void);
const uint16_t *create_parse_table(void);
const uint16_t *create_small_parse_table(void);
const uint32_t *create_small_parse_table_map(void);
const t_parse_action_entry *create_parse_actions_entries(void);
const char *const *create_symbols_names(void);
const char *const *create_field_names(void);
const t_field_map_slice *create_field_map_slices(void);
const t_field_map_entry *create_field_map_entries(void);
const t_symbol_metadata *create_symbols_metadata(void);
const t_symbol *create_unique_symbols_map(void);
const t_symbol *create_non_terminal_alias_map(void);
const t_symbol *create_alias_sequences(void);
const t_lex_modes *create_lex_modes(void);
const t_state_id *create_primary_state_ids(void);
const bool *create_external_scanner_states(void);
const t_symbol *create_external_scanner_symbol_map(void);
bool lex_normal_main(t_lexer *lexer, t_state_id state);
bool lex_keywords_main(t_lexer *lexer, t_state_id state);
void *tree_sitter_bash_external_scanner_create(void);
void tree_sitter_bash_external_scanner_destroy(void *ctx);
bool tree_sitter_bash_external_scanner_scan(void *ctx, t_lexer *lexer,
const bool *ret);
uint32_t tree_sitter_bash_external_scanner_serialize(void *ctx, char *s);
void tree_sitter_bash_external_scanner_deserialize(void *ctx, const char *s,
uint32_t val);
uint32_t tree_sitter_sh_external_scanner_serialize(void *ctx, char *s);
void tree_sitter_sh_external_scanner_deserialize(void *ctx, const char *s, uint32_t val);
void tree_sitter_sh_external_scanner_destroy(void *ctx);
void *tree_sitter_sh_external_scanner_create(void);
static t_scanner init_scanner(void)
{
return ((t_scanner){
create_external_scanner_states(),
create_external_scanner_symbol_map(),
tree_sitter_bash_external_scanner_create,
tree_sitter_bash_external_scanner_destroy,
tree_sitter_bash_external_scanner_scan,
tree_sitter_bash_external_scanner_serialize,
tree_sitter_bash_external_scanner_deserialize,
tree_sitter_sh_external_scanner_create,
tree_sitter_sh_external_scanner_destroy,
tree_sitter_sh_external_scanner_scan,
tree_sitter_sh_external_scanner_serialize,
tree_sitter_sh_external_scanner_deserialize,
});
}

View file

@ -28,40 +28,38 @@ enum TokenType
ESAC,
ERROR_RECOVERY,
};
/*
enum TokenType {
HEREDOC_START,
SIMPLE_HEREDOC_BODY,
HEREDOC_BODY_BEGINNING,
HEREDOC_CONTENT,
HEREDOC_END,
FILE_DESCRIPTOR,
EMPTY_VALUE,
CONCAT,
VARIABLE_NAME,
TEST_OPERATOR,
REGEX,
REGEX_NO_SLASH,
REGEX_NO_SPACE,
EXPANSION_WORD,
EXTGLOB_PATTERN,
BARE_DOLLAR,
BRACE_START,
IMMEDIATE_DOUBLE_HASH,
EXTERNAL_EXPANSION_SYM_HASH,
EXTERNAL_EXPANSION_SYM_BANG,
EXTERNAL_EXPANSION_SYM_EQUAL,
CLOSING_BRACE,
CLOSING_BRACKET,
HEREDOC_ARROW,
HEREDOC_ARROW_DASH,
NEWLINE,
OPENING_PAREN,
ESAC,
ERROR_RECOVERY,
};
*/
// enum TokenType {
// HEREDOC_START,
// SIMPLE_HEREDOC_BODY,
// HEREDOC_BODY_BEGINNING,
// HEREDOC_CONTENT,
// HEREDOC_END,
// FILE_DESCRIPTOR,
// EMPTY_VALUE,
// CONCAT,
// VARIABLE_NAME,
// TEST_OPERATOR,
// REGEX,
// REGEX_NO_SLASH,
// REGEX_NO_SPACE,
// EXPANSION_WORD,
// EXTGLOB_PATTERN,
// BARE_DOLLAR,
// BRACE_START,
// IMMEDIATE_DOUBLE_HASH,
// EXTERNAL_EXPANSION_SYM_HASH,
// EXTERNAL_EXPANSION_SYM_BANG,
// EXTERNAL_EXPANSION_SYM_EQUAL,
// CLOSING_BRACE,
// CLOSING_BRACKET,
// HEREDOC_ARROW,
// HEREDOC_ARROW_DASH,
// NEWLINE,
// OPENING_PAREN,
// ESAC,
// ERROR_RECOVERY,
// };
typedef struct s_lexer_data TSLexer;
typedef Array(char) String;
@ -91,12 +89,12 @@ typedef struct
Array(Heredoc) heredocs;
} Scanner;
static inline void advance(t_lexer_data *lexer)
static inline void advance(TSLexer *lexer)
{
lexer->advance(lexer, false);
}
static inline void skip(t_lexer_data *lexer)
static inline void skip(TSLexer *lexer)
{
lexer->advance(lexer, true);
}
@ -215,11 +213,11 @@ static void deserialize(Scanner *scanner, const char *buffer, unsigned length)
* POSIX-mandated substitution, and assumes the default value for
* IFS.
*/
static bool advance_word(t_lexer_data *lexer, String *unquoted_word)
static bool advance_word(TSLexer *lexer, String *unquoted_word)
{
bool empty = true;
bool empty = true;
int32_t quote = 0;
if (lexer->lookahead == '\'' || lexer->lookahead == '"')
{
quote = lexer->lookahead;
@ -233,9 +231,7 @@ static bool advance_word(t_lexer_data *lexer, String *unquoted_word)
{
advance(lexer);
if (!lexer->lookahead)
{
return false;
}
}
empty = false;
array_push(unquoted_word, lexer->lookahead);
@ -244,32 +240,29 @@ static bool advance_word(t_lexer_data *lexer, String *unquoted_word)
array_push(unquoted_word, '\0');
if (quote && lexer->lookahead == quote)
{
advance(lexer);
}
return !empty;
}
static inline bool scan_bare_dollar(t_lexer_data *lexer)
static inline bool scan_bare_dollar(TSLexer *lexer)
{
while (iswspace(lexer->lookahead) && lexer->lookahead != '\n' && !lexer->eof(lexer))
{
skip(lexer);
}
if (lexer->lookahead == '$')
{
advance(lexer);
lexer->result_symbol = BARE_DOLLAR;
lexer->mark_end(lexer);
return iswspace(lexer->lookahead) || lexer->eof(lexer) || lexer->lookahead == '\"';
return (iswspace(lexer->lookahead) || lexer->eof(lexer) || lexer->lookahead == '\"');
}
return false;
}
static bool scan_heredoc_start(Heredoc *heredoc, t_lexer_data *lexer)
static bool scan_heredoc_start(Heredoc *heredoc, TSLexer *lexer)
{
while (iswspace(lexer->lookahead))
{
@ -288,7 +281,7 @@ static bool scan_heredoc_start(Heredoc *heredoc, t_lexer_data *lexer)
return found_delimiter;
}
static bool scan_heredoc_end_identifier(Heredoc *heredoc, t_lexer_data *lexer)
static bool scan_heredoc_end_identifier(Heredoc *heredoc, TSLexer *lexer)
{
reset_string(&heredoc->current_leading_word);
// Scan the first 'n' characters on this line, to see if they match the
@ -308,7 +301,7 @@ static bool scan_heredoc_end_identifier(Heredoc *heredoc, t_lexer_data *lexer)
return heredoc->delimiter.size == 0 ? false : strcmp(heredoc->current_leading_word.contents, heredoc->delimiter.contents) == 0;
}
static bool scan_heredoc_content(Scanner *scanner, t_lexer_data *lexer, enum TokenType middle_type, enum TokenType end_type)
static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer, enum TokenType middle_type, enum TokenType end_type)
{
bool did_advance = false;
Heredoc *heredoc = array_back(&scanner->heredocs);
@ -434,7 +427,7 @@ static bool scan_heredoc_content(Scanner *scanner, t_lexer_data *lexer, enum Tok
}
}
static bool scan(Scanner *scanner, t_lexer_data *lexer, const bool *valid_symbols)
static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols)
{
if (valid_symbols[CONCAT] && !in_error_recovery(valid_symbols))
{
@ -484,11 +477,6 @@ static bool scan(Scanner *scanner, t_lexer_data *lexer, const bool *valid_symbol
return true;
}
}
if (iswspace(lexer->lookahead) && !valid_symbols[EXPANSION_WORD])
{
lexer->result_symbol = CONCAT;
return true;
}
}
if (valid_symbols[IMMEDIATE_DOUBLE_HASH] && !in_error_recovery(valid_symbols))
@ -739,7 +727,7 @@ static bool scan(Scanner *scanner, t_lexer_data *lexer, const bool *valid_symbol
return true;
}
if (valid_symbols[REGEX] && !in_error_recovery(valid_symbols))
if ((valid_symbols[REGEX]) && !in_error_recovery(valid_symbols))
{
if (valid_symbols[REGEX])
{
@ -805,32 +793,24 @@ static bool scan(Scanner *scanner, t_lexer_data *lexer, const bool *valid_symbol
break;
case '{':
if (!state.last_was_escape)
{
state.brace_depth++;
}
state.last_was_escape = false;
break;
case ')':
if (state.paren_depth == 0)
{
state.done = true;
}
state.paren_depth--;
state.last_was_escape = false;
break;
case ']':
if (state.bracket_depth == 0)
{
state.done = true;
}
state.bracket_depth--;
state.last_was_escape = false;
break;
case '}':
if (state.brace_depth == 0)
{
state.done = true;
}
state.brace_depth--;
state.last_was_escape = false;
break;
@ -1220,36 +1200,35 @@ expansion_word:
}
brace_start:
return false;
}
void *tree_sitter_bash_external_scanner_create()
void *tree_sitter_sh_external_scanner_create()
{
Scanner *scanner = calloc(1, sizeof(Scanner));
array_init(&scanner->heredocs);
return scanner;
}
bool tree_sitter_bash_external_scanner_scan(void *payload, t_lexer_data *lexer, const bool *valid_symbols)
bool tree_sitter_sh_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols)
{
Scanner *scanner = (Scanner *)payload;
return scan(scanner, lexer, valid_symbols);
}
unsigned tree_sitter_bash_external_scanner_serialize(void *payload, char *state)
unsigned tree_sitter_sh_external_scanner_serialize(void *payload, char *state)
{
Scanner *scanner = (Scanner *)payload;
return serialize(scanner, state);
}
void tree_sitter_bash_external_scanner_deserialize(void *payload, const char *state, unsigned length)
void tree_sitter_sh_external_scanner_deserialize(void *payload, const char *state, unsigned length)
{
Scanner *scanner = (Scanner *)payload;
deserialize(scanner, state, length);
}
void tree_sitter_bash_external_scanner_destroy(void *payload)
void tree_sitter_sh_external_scanner_destroy(void *payload)
{
Scanner *scanner = (Scanner *)payload;
for (size_t i = 0; i < scanner->heredocs.size; i++)

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
/* Updated: 2024/05/30 16:05:17 by maiboyer ### ########.fr */
/* Updated: 2024/06/09 21:39:11 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,8 +23,6 @@
#include "parser/api.h"
#include <sys/types.h>
#include "ast/from_node.h"
#undef free
#undef malloc
#undef realloc