aboutsummaryrefslogtreecommitdiff
path: root/src/parse.h
blob: ee1e37d4678aa0bd4b90192c53eecd0a6b3dca58 (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
123
/*

    casm - Simple, portable assembler

    Copyright (C) 2003-2015  Ian Cowburn (ianc@noddybox.demon.co.uk)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    -------------------------------------------------------------------------

    Tokeniser and parser.

*/

#ifndef CASM_PARSE_H
#define CASM_PARSE_H

/* ---------------------------------------- TYPES
*/

/* Represents a tokenised line.
   
   no_tokens is the number of tokens that can be accessed via token[i].

   If quoted[i] is non-zero it contains the opening character that was used to
   quote argument [i].  Note that '(' counts as a quote in command arguments.

   If first_column is true then the first column held a parsable character.

*/
typedef struct Line
{
    int         no_tokens;
    int         first_column;
    int         *quoted;
    char        **token;
    char        *comment;
} Line;


/* Used to represent a table of string/value pairs.  The table is ended with
   a NULL in str.
*/
typedef struct ValueTable
{
    const char  *str;
    int         value;
} ValueTable;


/* ---------------------------------------- MACROS
*/


/* Defines the set of values allowed for boolean strings and generates table
   entries using the passed 'tv' and 'fv' for true and false respictively.
*/
#define YES_NO_ENTRIES(tv, fv)  \
    {"on",      tv},            \
    {"true",    tv},            \
    {"yes",     tv},            \
    {"off",     fv},            \
    {"false",   fv},            \
    {"no",      fv}

/* ---------------------------------------- INTERFACES
*/

/* Parses a line.  Returns TRUE if line parsed OK, otherwise returns FALSE.
   ParseError() will return the reason for any failures.

   Remember that it may be possible to get a line with no tokens.
*/
int             ParseLine(Line *line, const char *source);


/* Free up the dynamic parts of a tokenised line
*/
void            ParseFree(Line *line);


/* Returns a reason for the last failure.
*/
const char      *ParseError(void);


/* Return a value from a table.  The check is done case insensitive.  Returns
   the value item, or NULL if not found.
*/
const ValueTable *ParseTable(const char *str, const ValueTable *table);


/* Parse a true/false value from a built-in table.  Returns def if the value is
   invalid.

   The mappings generated from YES_NO_ENTRIES(TRUE, FALSE) are used.
};

*/
int             ParseTrueFalse(const char *str, int def);


/* Returns true if the passed text is in the passed NULL terminated array of
   names.
*/
int             ParseInTable(const char *str, const char *names[]);

#endif

/*
vim: ai sw=4 ts=8 expandtab
*/