1
/* ANTLR Translator Generator
2
* Project led by Terence Parr at http://www.jGuru.com
3
* Software rights: http://www.antlr.org/RIGHTS.html
7
#include <antlr/config.hpp>
8
#include <antlr/IOException.hpp>
14
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
18
/** Eat whitespace from the input stream
19
* @param is the stream to read from
21
ANTLR_USE_NAMESPACE(std)istream& eatwhite( ANTLR_USE_NAMESPACE(std)istream& is )
26
#ifdef ANTLR_CCTYPE_NEEDS_STD
27
if( !ANTLR_USE_NAMESPACE(std)isspace(c) )
39
/** Read a string enclosed by '"' from a stream. Also handles escaping of \".
40
* Skips leading whitespace.
41
* @param in the istream to read from.
42
* @returns the string read from file exclusive the '"'
43
* @throws IOException if string is badly formatted
45
ANTLR_USE_NAMESPACE(std)string read_string( ANTLR_USE_NAMESPACE(std)istream& in )
48
ANTLR_USE_NAMESPACE(std)string ret("");
49
// States for a simple state machine...
50
enum { START, READING, ESCAPE, FINISHED };
55
while( state != FINISHED && in.get(ch) )
60
// start state: check wether starting with " then switch to READING
62
throw IOException("string must start with '\"'");
66
// reading state: look out for escape sequences and closing "
67
if( ch == '\\' ) // got escape sequence
72
if( ch == '"' ) // close quote -> stop
77
ret += ch; // else append...
94
default: // unrecognized escape is not mapped
102
if( state != FINISHED )
103
throw IOException("badly formatted string: "+ret);
108
/* Read a ([A-Z][0-9][a-z]_)* kindoff thing. Skips leading whitespace.
109
* @param in the istream to read from.
111
ANTLR_USE_NAMESPACE(std)string read_identifier( ANTLR_USE_NAMESPACE(std)istream& in )
114
ANTLR_USE_NAMESPACE(std)string ret("");
120
#ifdef ANTLR_CCTYPE_NEEDS_STD
121
if( ANTLR_USE_NAMESPACE(std)isupper(ch) ||
122
ANTLR_USE_NAMESPACE(std)islower(ch) ||
123
ANTLR_USE_NAMESPACE(std)isdigit(ch) ||
126
if( isupper(ch) || islower(ch) || isdigit(ch) || ch == '_' )
138
/** Read a attribute="value" thing. Leading whitespace is skipped.
139
* Between attribute and '=' no whitespace is allowed. After the '=' it is
141
* @param in the istream to read from.
142
* @param attribute string the attribute name is put in
143
* @param value string the value of the attribute is put in
144
* @throws IOException if something is fishy. E.g. malformed quoting
147
void read_AttributeNValue( ANTLR_USE_NAMESPACE(std)istream& in,
148
ANTLR_USE_NAMESPACE(std)string& attribute,
149
ANTLR_USE_NAMESPACE(std)string& value )
151
attribute = read_identifier(in);
154
if( in.get(ch) && ch == '=' )
155
value = read_string(in);
157
throw IOException("invalid attribute=value thing "+attribute);
160
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE