aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/lexis.h
blob: 7d8fe7bc63ca3aecf4ef18684bd6c754e99111f8 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or  (at your option) any later version. This library is 
 * distributed in the  hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#ifndef _SP_LEXIS_H_
#define _SP_LEXIS_H_

#include "sys_string.h"
#include "sys_ctype.h"

/** @file
 * Lexical analysis.
 */

/** Class of characters treated as space. */
#define space_class ((char []){ '\n', '\r', '\t', ' ', '\f' , 0 })

/** Class of separator characters. */
#define sep_class "{}()<>[]@!;"

#define comment_class "#"

/** Determine if a character is in a given class.
 * 
 * @param c character to test
 * @param s null-terminated string of characters in the class
 * @return 1 if c is in the class, 0 otherwise.
 */
static inline int in_class(int c, const char *s){
  return s && (strchr(s, c) != 0);
}

/** Determine if a character is in the space class.
 * 
 * @param c character to test
 * @return 1 if c is in the class, 0 otherwise.
 */
static inline int in_space_class(int c){
    return in_class(c, space_class);
}

static inline int in_comment_class(int c){
    return in_class(c, comment_class);
}

/** Determine if a character is in the separator class.
 * Separator characters terminate tokens, and do not need space
 * to separate them.
 * 
 * @param c character to test
 * @return 1 if c is in the class, 0 otherwise.
 */
static inline int in_sep_class(int c){
    return in_class(c, sep_class);
}

/** Determine if a character is in the alpha class.
 * 
 * @param c character to test
 * @return 1 if c is in the class, 0 otherwise.
 */
static inline int in_alpha_class(int c){
    return isalpha(c);
}

/** Determine if a character is in the octal digit class.
 * 
 * @param c character to test
 * @return 1 if c is in the class, 0 otherwise.
 */
static inline int in_octal_digit_class(int c){
    return '0' <= c && c <= '7';
}

/** Determine if a character is in the decimal digit class.
 * 
 * @param c character to test
 * @return 1 if c is in the class, 0 otherwise.
 */
static inline int in_decimal_digit_class(int c){
    return isdigit(c);
}

/** Determine if a character is in the hex digit class.
 * 
 * @param c character to test
 * @return 1 if c is in the class, 0 otherwise.
 */
static inline int in_hex_digit_class(int c){
    return isdigit(c) || in_class(c, "abcdefABCDEF");
}


static inline int in_string_quote_class(int c){
    return in_class(c, "'\"");
}

static inline int in_printable_class(int c){
    return ('A' <= c && c <= 'Z')
        || ('a' <= c && c <= 'z')
        || ('0' <= c && c <= '9')
        || in_class(c, "!$%&*+,-./:;<=>?@^_`{|}~");
}

extern int is_decimal_number(const char *s, int n);
extern int is_hex_number(const char *s, int n);
extern int is_keyword(const char *s, const char *k);
extern int is_keychar(const char *s, char c);

#endif /* !_SP_LEXIS_H_ */