aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/lexis.c
blob: 26d2ec4d5b2da976d4327b69cf892e5e488d2e0e (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
/*
 *
 * 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
 */

/** @file
 * Lexical analysis.
 */

#include "sys_string.h"
#include "lexis.h"
#include <errno.h>

/** Check if a value lies in a (closed) range.
 *
 * @param x value to test
 * @param lo low end of the range
 * @param hi high end of the range
 * @return 1 if x is in the interval [lo, hi], 0 otherwise
 */
inline static int in_range(int x, int lo, int hi){
    return (lo <= x) && (x <= hi);
}

/** Determine if a string is an (unsigned) decimal number.
 * 
 * @param s pointer to characters to test
 * @param n length of string
 * @return 1 if s is a decimal number, 0 otherwise.
 */
int is_decimal_number(const char *s, int n){
    int i;
    if(n <= 0)return 0;
    for(i = 0; i < n; i++){
        if(!in_decimal_digit_class(s[i])) return 0;
    }
    return 1;
}

/** Determine if a string is a hex number.
 * Hex numbers are 0, or start with 0x or 0X followed
 * by a non-zero number of hex digits (0-9,a-f,A-F).
 * 
 * @param s pointer to characters to test
 * @param n length of string
 * @return 1 if s is a hex number, 0 otherwise.
 */
int is_hex_number(const char *s, int n){
    int i;
    if(n <= 0) return 0;
    if(n == 1){
        return s[0]=='0';
    }
    if(n <= 3) return 0;
    if(s[0] != '0' || (s[1] != 'x' && s[1] != 'X')) return 0;
    for(i = 2; i < n; i++){
        if(!in_hex_digit_class(s[i])) return 0;
    }
    return 1;
}

/** Test if a string matches a keyword.
 * The comparison is case-insensitive.
 * The comparison fails if either argument is null.
 *
 * @param s string
 * @param k keyword
 * @return 1 if they match, 0 otherwise
 */
int is_keyword(const char *s, const char *k){
  return s && k && !strcasecmp(s, k);
}

/** Test if a string matches a character.
 *
 * @param s string
 * @param c character (non-null)
 * @return 1 if s contains exactly c, 0 otherwise
 */
int is_keychar(const char *s, char c){
  return c && (s[0] == c) && !s[1];
}