-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from ErwannLesech/dev
Step2.2 - Add tests and last fixes
- Loading branch information
Showing
44 changed files
with
261 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.h token.h | ||
liblexer_a_SOURCES = lexer.c lexer_utils.c lexer_utils2.c lexer.h token.h | ||
liblexer_a_CFLAGS = -Wall -Wextra -Werror -Wvla -pedantic -std=c99 | ||
liblexer_a_CPPFLAGS = -I$(top_srcdir)/src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#include "lexer.h" | ||
|
||
void print_token(struct token token) | ||
{ | ||
char *tokens[] = { // Step1 | ||
[TOKEN_IF] = "TOKEN_IF", | ||
[TOKEN_THEN] = "TOKEN_THEN", | ||
[TOKEN_ELIF] = "TOKEN_ELIF", | ||
[TOKEN_ELSE] = "TOKEN_ELSE", | ||
[TOKEN_FI] = "TOKEN_FI", | ||
[TOKEN_SEMICOLON] = "TOKEN_SEMICOLON", | ||
[TOKEN_WORD] = "TOKEN_WORD", | ||
[TOKEN_EOL] = "TOKEN_EOL", | ||
[TOKEN_EOF] = "TOKEN_EOF", | ||
[TOKEN_ERROR] = "TOKEN_ERROR", | ||
// Step 2 | ||
[TOKEN_DONE] = "TOKEN_DONE", | ||
[TOKEN_AND] = "TOKEN_AND", | ||
[TOKEN_OR] = "TOKEN_OR", | ||
[TOKEN_PIPE] = "TOKEN_PIPE", | ||
[TOKEN_NEGATE] = "TOKEN_NEGATE", | ||
[TOKEN_IONUMBER] = "TOKEN_IONUMBER", | ||
[TOKEN_REDIR] = "TOKEN_REDIR", | ||
[TOKEN_DOUBLE_QUOTE] = "TOKEN_DOUBLE_QUOTE", | ||
[TOKEN_WORD_DOUBLE_QUOTE] = "TOKEN_WORD_DOUBLE_QUOTE", | ||
[TOKEN_WORD_ASSIGNMENT] = "TOKEN_WORD_ASSIGNMENT", | ||
[TOKEN_VARIABLE] = "TOKEN_VARIABLE", | ||
|
||
// Internal values for lexer | ||
[TOKEN_VARIABLE_VALUE] = "TOKEN_VARIABLE_VALUE", | ||
[TOKEN_VARIABLE_AND_DOUBLE_QUOTE] = | ||
"TOKEN_VARIABLE_AND_DOUBLE_QUOTE" | ||
}; | ||
printf("Token: %s\n", tokens[token.type]); | ||
} | ||
|
||
bool check_variable_name_simulated(const char *data, int index) | ||
{ | ||
bool is_in_braces = false; | ||
|
||
index += 1; | ||
|
||
// Check if it's a special variable (like $?, $*, $@, $# or $$) | ||
if (data[index] == '?' || data[index] == '*' || data[index] == '@' | ||
|| data[index] == '#' || data[index] == '$') | ||
{ | ||
return true; | ||
} | ||
|
||
// Chech if it's a special variable (like $n) | ||
else if (data[index] >= '0' && data[index] <= '9') | ||
{ | ||
return true; | ||
} | ||
|
||
else if (data[index] == '{') | ||
{ | ||
index += 1; | ||
is_in_braces = true; | ||
} | ||
|
||
// Classic variable name | ||
else if (data[index] == '_' || data[index] == '-' | ||
|| (data[index] >= 'a' && data[index] <= 'z') | ||
|| (data[index] >= 'A' && data[index] <= 'Z')) | ||
{ | ||
index += 1; | ||
} | ||
// Not a valid variable name | ||
else | ||
{ | ||
return false; | ||
} | ||
|
||
// Check the rest of the variable name break | ||
while (data[index] == '_' || data[index] == '-' | ||
|| (data[index] >= 'a' && data[index] <= 'z') | ||
|| (data[index] >= 'A' && data[index] <= 'Z') | ||
|| (data[index] >= '0' && data[index] <= '9')) | ||
{ | ||
index += 1; | ||
} | ||
|
||
if (is_in_braces) | ||
{ | ||
if (data[index] == '}') | ||
{ | ||
index += 1; | ||
return true; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void handle_back_slash_in_double_quote(struct lexer *lexer, char *word, | ||
unsigned *word_index) | ||
{ | ||
if (lexer->data[lexer->index] == '\"' || lexer->data[lexer->index] == '$' | ||
|| lexer->data[lexer->index] == '\\' | ||
|| lexer->data[lexer->index] == '\n' | ||
|| lexer->data[lexer->index] == '`') | ||
{ | ||
if (lexer->data[lexer->index] != '\n') | ||
{ | ||
word[*word_index] = lexer->data[lexer->index]; | ||
} | ||
else | ||
{ | ||
*word_index -= 1; | ||
} | ||
lexer->index += 1; | ||
} | ||
else | ||
{ | ||
word[*word_index] = '\\'; | ||
} | ||
*word_index += 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a=1;echo "$a\"$a" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
echo "Special characters: !@#%$^&*()" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bb=1; echo "$ddd\"" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a=1;echo "$a\"" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a=1;echo "idiej$a:jodejoj" "$a" "dikefok"' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a=1;echo "lpikopo"pojo^é$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
a=1;echo "lpikopo\ | ||
|
||
|
||
" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a=1;echo "lpikopo\$$\$$\\$\$\*\\*\\\*\*\\***\ " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
run_test double_quote1.test | ||
run_test double_quote2.test | ||
run_test double_quote3.test | ||
run_test double_quote4.test | ||
run_test double_quote4.test | ||
run_test double_quote5.test | ||
run_test double_quote6.test | ||
run_test double_quote7.test | ||
run_test double_quote8.test | ||
run_test double_quote9.test | ||
run_test double_quote10.test | ||
run_test double_quote11.test | ||
run_test double_quote12.test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bb=1; echo "$bb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bb=1; echo "$ddd\" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tata=1; echo "${tata} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tata=1; echo "${tata}\" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tata=1; echo "${tata" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tata=1; echo "$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tata=1; echo "$lff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tata=1; echo \"${tata}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
run_test double_quote_error1.test | ||
run_test double_quote_error2.test | ||
run_test double_quote_error3.test | ||
run_test double_quote_error4.test | ||
run_test double_quote_error5.test | ||
run_test double_quote_error6.test | ||
run_test double_quote_error7.test | ||
run_test double_quote_error8.test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
run_test single_quote_error1.test | ||
run_test single_quote_error2.test | ||
run_test single_quote_error3.test | ||
run_test single_quote_errorMouli1.test | ||
run_test single_quote_errorMouli2.test | ||
run_test single_quoteMouli1.test | ||
run_test single_quoteMouli2.test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
echo 'hello' > input.txt | ||
ls > output.txt 2<&1 < input.txt | ||
cat output.txt | ||
cat input.txt | ||
rm input.txt output.txt |
Oops, something went wrong.