summaryrefslogtreecommitdiffstats
path: root/lib/antlr/src/ANTLRUtil.cpp
blob: 5df8db6df49f05195916b861ffe1256f50ce3c34 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* ANTLR Translator Generator
 * Project led by Terence Parr at http://www.jGuru.com
 * Software rights: http://www.antlr.org/license.html
 *
 * $Id$
 */

#include <antlr/config.h>
#include <antlr/IOException.h>

#include <iostream>
#include <cctype>
#include <string>

#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
namespace antlr {
#endif

/** Eat whitespace from the input stream
 * @param is the stream to read from
 */
ANTLR_USE_NAMESPACE(std)istream& eatwhite( ANTLR_USE_NAMESPACE(std)istream& is )
{
	char c;
	while( is.get(c) )
	{
#ifdef ANTLR_CCTYPE_NEEDS_STD
		if( !ANTLR_USE_NAMESPACE(std)isspace(c) )
#else
		if( !isspace(c) )
#endif
		{
			is.putback(c);
			break;
		}
	}
	return is;
}

/** Read a string enclosed by '"' from a stream. Also handles escaping of \".
 * Skips leading whitespace.
 * @param in the istream to read from.
 * @returns the string read from file exclusive the '"'
 * @throws IOException if string is badly formatted
 */
ANTLR_USE_NAMESPACE(std)string read_string( ANTLR_USE_NAMESPACE(std)istream& in )
{
	char ch;
	ANTLR_USE_NAMESPACE(std)string ret("");
	// States for a simple state machine...
	enum { START, READING, ESCAPE, FINISHED };
	int state = START;

	eatwhite(in);

	while( state != FINISHED && in.get(ch) )
	{
		switch( state )
		{
		case START:
			// start state: check wether starting with " then switch to READING
			if( ch != '"' )
				throw IOException("string must start with '\"'");
			state = READING;
			continue;
		case READING:
			// reading state: look out for escape sequences and closing "
			if( ch == '\\' )		// got escape sequence
			{
				state = ESCAPE;
				continue;
			}
			if( ch == '"' ) 		// close quote -> stop
			{
				state = FINISHED;
				continue;
			}
			ret += ch;				// else append...
			continue;
		case ESCAPE:
			switch(ch)
			{
			case '\\':
				ret += ch;
				state = READING;
				continue;
			case '"':
				ret += ch;
				state = READING;
				continue;
			case '0':
				ret += '\0';
				state = READING;
				continue;
			default:						// unrecognized escape is not mapped
				ret += '\\';
				ret += ch;
				state = READING;
				continue;
			}
		}
	}
	if( state != FINISHED )
		throw IOException("badly formatted string: "+ret);

	return ret;
}

/* Read a ([A-Z][0-9][a-z]_)* kindoff thing. Skips leading whitespace.
 * @param in the istream to read from.
 */
ANTLR_USE_NAMESPACE(std)string read_identifier( ANTLR_USE_NAMESPACE(std)istream& in )
{
	char ch;
	ANTLR_USE_NAMESPACE(std)string ret("");

	eatwhite(in);

	while( in.get(ch) )
	{
#ifdef ANTLR_CCTYPE_NEEDS_STD
		if( ANTLR_USE_NAMESPACE(std)isupper(ch) ||
			 ANTLR_USE_NAMESPACE(std)islower(ch) ||
			 ANTLR_USE_NAMESPACE(std)isdigit(ch) ||
			 ch == '_' )
#else
		if( isupper(ch) || islower(ch) || isdigit(ch) || ch == '_' )
#endif
			ret += ch;
		else
		{
			in.putback(ch);
			break;
		}
	}
	return ret;
}

/** Read a attribute="value" thing. Leading whitespace is skipped.
 * Between attribute and '=' no whitespace is allowed. After the '=' it is
 * permitted.
 * @param in the istream to read from.
 * @param attribute string the attribute name is put in
 * @param value string the value of the attribute is put in
 * @throws IOException if something is fishy. E.g. malformed quoting
 * or missing '='
 */
void read_AttributeNValue( ANTLR_USE_NAMESPACE(std)istream& in,
									ANTLR_USE_NAMESPACE(std)string& attribute,
									ANTLR_USE_NAMESPACE(std)string& value )
{
	attribute = read_identifier(in);

	char ch;
	if( in.get(ch) && ch == '=' )
		value = read_string(in);
	else
		throw IOException("invalid attribute=value thing "+attribute);
}

#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
}
#endif