aboutsummaryrefslogtreecommitdiff
path: root/src/lexer/lexer_utils.c
blob: 64a25b16571d75debbe567fbab7c4dba6e626892 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   lexer_utils.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: nahaddac <nahaddac@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/07/16 08:18:15 by nahaddac          #+#    #+#             */
/*   Updated: 2020/09/13 08:38:27 by nahaddac         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "lexer.h"

// check for append tag
enum e_tok                ret_token_sep_redir_append(char *input, int i)
{
    if (input[i + 1] ==  '>')
        return(TAG_REDIR_APPEND);
    return (TAG_REDIR_OUT);

}

// return token tag corresponding to string id
enum e_tok                ret_token(char *input, int  i)
{
    if (input[i] == ';')
        return(TAG_END);
    if (input[i] == '&' && input[i + 1] == '&')
        return(TAG_AND);
    if (input[i] == '|' && input[i + 1] == '|')
        return(TAG_OR);
    if(input[i]  == '|')
        return(TAG_PIPE);
    if (input[i] == '>')
        return(ret_token_sep_redir_append(input,i));
    if (input[i] == '<')
        return(TAG_REDIR_IN);
    if (input[i] ==  '(')
        return(TAG_PARENT_OPEN);
    if (input[i] == ')')
        return(TAG_PARENT_CLOSE);
    return(0);

}

// check is char is separator
// /!\ can be replaced by ft_strchr(";&|><()", input) == NULL
int                 	lexer_sep(char input)
{
    char            *sep;
    int             i;

    i = 0;
    sep = ";&|><()";
    while(sep[i] != '\0')
    {
        if(sep[i] == input)
            return(1);
        i++;
    }
    return (0);
}

// skip spaces
// /!\ can be replaced by strspn
int             		lexer_space(char *input)
{
    int i;

    i=0;
    while(input[i] == ' ')
        i++;
    return(i);
}

static int             lex_check_single_quote(char *input, int i)
{
    i++;
    while(input[i] != '\0')
    {
        if(input[i] == '\\')
            i+=1;
        if(input[i] == '\'')
            break;
        ++i;
    }
    if (input[i + 1] == ' ')
        while(input[i + 1] == ' ')
            i++;
    return(i + 1);
}

int             		lexer_check_between_quote(char *input, int i)
{
    if(input[i] == '\'')
        return(lex_check_single_quote(input, i));
    i++;
    while(input[i] != '"' && (input[i] != '\0'))
    {
        if (input[i] == '\\')
            i += 1;
        ++i;
    }
    if (input[i + 1] == ' ')
        while(input[i + 1] == ' ')
            i++;
    return(i + 1);
}