Skip to content

Commit

Permalink
style<clang-tidy>: refactor all lexer function so it's clang-tidy com…
Browse files Browse the repository at this point in the history
…pliant
  • Loading branch information
ErwannLesech committed Jan 27, 2024
1 parent 78af69a commit 3116fd0
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 124 deletions.
2 changes: 1 addition & 1 deletion src/execute/utils/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ int dot_fun(struct ast_node *node)
{
return return_val;
}

if (doted)
{
int r = check_file(node, first_arg, path);
Expand Down
2 changes: 1 addition & 1 deletion src/lexer/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
lib_LIBRARIES = liblexer.a

liblexer_a_SOURCES = lexer.c lexer_utils.c lexer_utils2.c lexer.h token.h
liblexer_a_SOURCES = lexer.c lexer_utils.c lexer_utils2.c lexer_utils3.c lexer.h token.h
liblexer_a_CFLAGS = -Wall -Wextra -Werror -Wvla -pedantic -std=c99
liblexer_a_CPPFLAGS = -I$(top_srcdir)/src
101 changes: 11 additions & 90 deletions src/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,38 +150,18 @@ char *get_word(struct lexer *lexer, bool *is_diactivated)
if (!first_char_check(lexer, &word, &word_index))
{
// Handle the word
while (lexer->data[lexer->index] != ' '
&& lexer->data[lexer->index] != '\0'
&& lexer->data[lexer->index] != ';'
&& lexer->data[lexer->index] != '\n'
&& lexer->data[lexer->index] != '\t'
&& lexer->data[lexer->index] != '>'
&& lexer->data[lexer->index] != '<'
&& lexer->data[lexer->index] != '|'
&& lexer->data[lexer->index] != '&'
&& lexer->data[lexer->index] != '('
&& lexer->data[lexer->index] != ')')
while (word_separator_check(lexer))
{
word = append_end_of_word(word, word_index);

// Handle the word assignement if it's contain '=' and it's not the
// first character

if (lexer->data[lexer->index] == '=' && word_index > 0
&& lexer->curr_tok.type != TOKEN_DOUBLE_QUOTE
&& lexer->curr_tok.type != TOKEN_VARIABLE_VALUE
&& check_variable_assignement(word))
if (handle_egal(lexer, word, word_index))
{
lexer->curr_tok.type = TOKEN_WORD_ASSIGNMENT;
break;
}

else if (lexer->data[lexer->index] == '=' && word_index == 0
&& lexer->curr_tok.type == TOKEN_VARIABLE_VALUE)
{
lexer->index += 1;
}

// Handle the variable
if (lexer->data[lexer->index] == '$')
{
Expand All @@ -206,15 +186,10 @@ char *get_word(struct lexer *lexer, bool *is_diactivated)
}
}
// Take next char and put it in the word
word = realloc(word, sizeof(char) * (word_index + 1));
word[word_index] = lexer->data[lexer->index];
++word_index;
++lexer->index;
append_char_to_word(lexer, &word, &word_index);

// Handle the double quote
if (lexer->data[lexer->index - 1] == '\"'
|| lexer->curr_tok.type == TOKEN_DOUBLE_QUOTE
|| lexer->curr_tok.type == TOKEN_VARIABLE_AND_DOUBLE_QUOTE)
if (check_double_quote(lexer))
{
// Handle the end of the double quote
if (word_index > 0 && lexer->data[lexer->index - 1] == '\"')
Expand All @@ -241,31 +216,16 @@ char *get_word(struct lexer *lexer, bool *is_diactivated)
{
return NULL;
}
if (lexer->curr_tok.type == TOKEN_VARIABLE_AND_DOUBLE_QUOTE
|| lexer->curr_tok.type == TOKEN_SUB_AND_DOUBLE_QUOTE)

if (is_sub_or_var_and_double_quote(lexer))
{
return word;
}
}

// Handle backslash
else if (lexer->data[lexer->index - 1] == '\\')
{
handle_backslash(lexer, is_diactivated, word, word_index);
}

// Handle simple quote
else if (lexer->data[lexer->index - 1] == '\'')
else if (NULL == hbsq(lexer, word, &word_index, is_diactivated))
{
word = handle_simple_quote(lexer, is_diactivated, word,
&word_index);

// Missing closing simple quote
if (!word)
{
return NULL;
}
lexer->index += 1;
return NULL;
}
}
}
Expand Down Expand Up @@ -299,49 +259,10 @@ struct token parse_input_for_tok(struct lexer *lexer)
return token;
}

// Check if the word is a word_assignement (contains a '=') and if it's a
// variable name is valid
if (lexer->curr_tok.type == TOKEN_WORD_ASSIGNMENT
&& check_variable_assignement(word))
{
token.type = TOKEN_WORD_ASSIGNMENT;
token.data = word;
// Usefull to have the next word token
lexer->curr_tok.type = TOKEN_VARIABLE_VALUE;
return token;
}

if (lexer->curr_tok.type == TOKEN_IONUMBER)
{
token.type = TOKEN_IONUMBER;
token.data = word;
lexer->curr_tok.type = TOKEN_EOL;
return token;
}

// Check if the word is a variable name
if (lexer->curr_tok.type == TOKEN_VARIABLE
|| lexer->curr_tok.type == TOKEN_VARIABLE_AND_DOUBLE_QUOTE)
{
token.type = TOKEN_VARIABLE;
token.data = word;
if (lexer->curr_tok.type == TOKEN_VARIABLE_AND_DOUBLE_QUOTE)
{
lexer->curr_tok.type = TOKEN_DOUBLE_QUOTE;
}
else
{
lexer->curr_tok.type = TOKEN_EOL;
}
return token;
}

if (lexer->curr_tok.type == TOKEN_SUBSTITUTION
|| lexer->curr_tok.type == TOKEN_SUB_AND_DOUBLE_QUOTE)
// Check if the word is a special case
token = check_special_cases(lexer, word, token);
if (token.type != TOKEN_ERROR)
{
token.type = TOKEN_SUBSTITUTION;
token.data = word;
lexer->curr_tok.type = TOKEN_EOL;
return token;
}

Expand Down
61 changes: 61 additions & 0 deletions src/lexer/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,65 @@ char *handle_redir(struct lexer *lexer, unsigned *word_index);
*/
char *get_word(struct lexer *lexer, bool *is_diactivated);

// clang_tidy functions

/**
* \brief replace if of line 101 in check_variable_name
*/
enum token_type affect_curr_tok_type_var_name(struct lexer *lexer);

/**
* \brief replace if of line 120 in check_variable_name
*/
bool not_valid_check_var(struct lexer *lexer, char **word, char *curr_word);

/**
* \brief replace elif of line 140 in check_variable_name
*/
bool elif_check_var(struct lexer *lexer);

/**
* \brief replace while of line 160 in check_variable_name
*/
bool while_check_var(struct lexer *lexer);

/**
* \brief replace check cases in parse_input_for_tok
*/
struct token check_special_cases(struct lexer *lexer, char *word,
struct token token);

/**
* \brief replace while separator condition of get_word
*/
bool word_separator_check(struct lexer *lexer);

/**
* \brief replace egal handling of get_word
*/
bool handle_egal(struct lexer *lexer, char *word, unsigned word_index);

/**
* \brief replace append char to word of get_word
*/
void append_char_to_word(struct lexer *lexer, char **word,
unsigned *word_index);

/**
* \brief replace backslash and simple quote handling of get_word
* handle backslash & single quote
*/
char *hbsq(struct lexer *lexer, char *word, unsigned *word_index,
bool *is_diactivated);

/**
* \brief replace if double quote handling of get_word
*/
bool check_double_quote(struct lexer *lexer);

/**
* \brief replace spacial curr_tok type handling of get_word
*/
bool is_sub_or_var_and_double_quote(struct lexer *lexer);

#endif /* !LEXER_H */
36 changes: 4 additions & 32 deletions src/lexer/lexer_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,7 @@ bool check_variable_name(struct lexer *lexer, char **word, unsigned *word_index,
char *curr_word = *word;
*is_in_braces = false;
// Handle variable in double quote
if (lexer->curr_tok.type == TOKEN_DOUBLE_QUOTE
|| lexer->curr_tok.type == TOKEN_VARIABLE_AND_DOUBLE_QUOTE)
{
lexer->curr_tok.type = TOKEN_VARIABLE_AND_DOUBLE_QUOTE;
}
else
{
lexer->curr_tok.type = TOKEN_VARIABLE;
}
lexer->curr_tok.type = affect_curr_tok_type_var_name(lexer);

// Check if it's a special variable (like $?, $*, $@, $# or $$)
if (lexer->data[lexer->index] == '?' || lexer->data[lexer->index] == '*'
Expand Down Expand Up @@ -138,38 +130,18 @@ bool check_variable_name(struct lexer *lexer, char **word, unsigned *word_index,
}

// Classic variable name
else if (lexer->data[lexer->index] == '_'
|| lexer->data[lexer->index] == '-'
|| (lexer->data[lexer->index] >= 'a'
&& lexer->data[lexer->index] <= 'z')
|| (lexer->data[lexer->index] >= 'A'
&& lexer->data[lexer->index] <= 'Z'))
else if (elif_check_var(lexer))
{
append_char_to_word(lexer, &curr_word, word_index);
}
// Not a valid variable name
else
{
if (lexer->curr_tok.type != TOKEN_VARIABLE_AND_DOUBLE_QUOTE)
{
lexer->curr_tok.type = TOKEN_WORD;
}
else
{
lexer->curr_tok.type = TOKEN_DOUBLE_QUOTE;
}
*word = curr_word;
return false;
return not_valid_check_var(lexer, word, curr_word);
}

// Check the rest of the variable name break
while (lexer->data[lexer->index] == '_' || lexer->data[lexer->index] == '-'
|| (lexer->data[lexer->index] >= 'a'
&& lexer->data[lexer->index] <= 'z')
|| (lexer->data[lexer->index] >= 'A'
&& lexer->data[lexer->index] <= 'Z')
|| (lexer->data[lexer->index] >= '0'
&& lexer->data[lexer->index] <= '9'))
while (while_check_var(lexer))
{
append_char_to_word(lexer, &curr_word, word_index);
}
Expand Down
60 changes: 60 additions & 0 deletions src/lexer/lexer_utils2.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,63 @@ void handle_back_slash_in_double_quote(struct lexer *lexer, char *word,
}
*word_index += 1;
}

enum token_type affect_curr_tok_type_var_name(struct lexer *lexer)
{
if (lexer->curr_tok.type == TOKEN_DOUBLE_QUOTE)
{
return TOKEN_VARIABLE_AND_DOUBLE_QUOTE;
}
else
{
return TOKEN_VARIABLE;
}
}

bool elif_check_var(struct lexer *lexer)
{
if (lexer->data[lexer->index] == '_' || lexer->data[lexer->index] == '-'
|| (lexer->data[lexer->index] >= 'a'
&& lexer->data[lexer->index] <= 'z')
|| (lexer->data[lexer->index] >= 'A'
&& lexer->data[lexer->index] <= 'Z'))
{
return true;
}
else
{
return false;
}
}

bool while_check_var(struct lexer *lexer)
{
if (lexer->data[lexer->index] == '_' || lexer->data[lexer->index] == '-'
|| (lexer->data[lexer->index] >= 'a'
&& lexer->data[lexer->index] <= 'z')
|| (lexer->data[lexer->index] >= 'A'
&& lexer->data[lexer->index] <= 'Z')
|| (lexer->data[lexer->index] >= '0'
&& lexer->data[lexer->index] <= '9'))
{
return true;
}
else
{
return false;
}
}

bool not_valid_check_var(struct lexer *lexer, char **word, char *curr_word)
{
if (lexer->curr_tok.type != TOKEN_VARIABLE_AND_DOUBLE_QUOTE)
{
lexer->curr_tok.type = TOKEN_WORD;
}
else
{
lexer->curr_tok.type = TOKEN_DOUBLE_QUOTE;
}
*word = curr_word;
return false;
}
Loading

0 comments on commit 3116fd0

Please sign in to comment.