]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_codesigning/antlr2/src/ANTLRUtil.cpp
1 /* ANTLR Translator Generator
2 * Project led by Terence Parr at http://www.jGuru.com
3 * Software rights: http://www.antlr.org/license.html
8 #include <antlr/config.hpp>
9 #include <antlr/IOException.hpp>
10 #include <antlr/ANTLRUtil.hpp>
16 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
20 /** Eat whitespace from the input stream
21 * @param is the stream to read from
23 ANTLR_USE_NAMESPACE(std
)istream
& eatwhite( ANTLR_USE_NAMESPACE(std
)istream
& is
)
28 #ifdef ANTLR_CCTYPE_NEEDS_STD
29 if( !ANTLR_USE_NAMESPACE(std
)isspace(c
) )
41 /** Read a string enclosed by '"' from a stream. Also handles escaping of \".
42 * Skips leading whitespace.
43 * @param in the istream to read from.
44 * @returns the string read from file exclusive the '"'
45 * @throws IOException if string is badly formatted
47 ANTLR_USE_NAMESPACE(std
)string
read_string( ANTLR_USE_NAMESPACE(std
)istream
& in
)
50 ANTLR_USE_NAMESPACE(std
)string
ret("");
51 // States for a simple state machine...
52 enum { START
, READING
, ESCAPE
, FINISHED
};
57 while( state
!= FINISHED
&& in
.get(ch
) )
62 // start state: check wether starting with " then switch to READING
64 throw IOException("string must start with '\"'");
68 // reading state: look out for escape sequences and closing "
69 if( ch
== '\\' ) // got escape sequence
74 if( ch
== '"' ) // close quote -> stop
79 ret
+= ch
; // else append...
96 default: // unrecognized escape is not mapped
104 if( state
!= FINISHED
)
105 throw IOException("badly formatted string: "+ret
);
110 /* Read a ([A-Z][0-9][a-z]_)* kindoff thing. Skips leading whitespace.
111 * @param in the istream to read from.
113 ANTLR_USE_NAMESPACE(std
)string
read_identifier( ANTLR_USE_NAMESPACE(std
)istream
& in
)
116 ANTLR_USE_NAMESPACE(std
)string
ret("");
122 #ifdef ANTLR_CCTYPE_NEEDS_STD
123 if( ANTLR_USE_NAMESPACE(std
)isupper(ch
) ||
124 ANTLR_USE_NAMESPACE(std
)islower(ch
) ||
125 ANTLR_USE_NAMESPACE(std
)isdigit(ch
) ||
128 if( isupper(ch
) || islower(ch
) || isdigit(ch
) || ch
== '_' )
140 /** Read a attribute="value" thing. Leading whitespace is skipped.
141 * Between attribute and '=' no whitespace is allowed. After the '=' it is
143 * @param in the istream to read from.
144 * @param attribute string the attribute name is put in
145 * @param value string the value of the attribute is put in
146 * @throws IOException if something is fishy. E.g. malformed quoting
149 void read_AttributeNValue( ANTLR_USE_NAMESPACE(std
)istream
& in
,
150 ANTLR_USE_NAMESPACE(std
)string
& attribute
,
151 ANTLR_USE_NAMESPACE(std
)string
& value
)
153 attribute
= read_identifier(in
);
156 if( in
.get(ch
) && ch
== '=' )
157 value
= read_string(in
);
159 throw IOException("invalid attribute=value thing "+attribute
);
162 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE