KDevelop API Documentation

lib/antlr/src/ANTLRUtil.cpp

Go to the documentation of this file.
00001 /* ANTLR Translator Generator 00002 * Project led by Terence Parr at http://www.jGuru.com 00003 * Software rights: http://www.antlr.org/RIGHTS.html 00004 * 00005 * $Id: ANTLRUtil.cpp,v 1.1 2003/05/02 00:36:20 okellogg Exp $ 00006 */ 00007 00008 #include <antlr/config.hpp> 00009 #include <antlr/IOException.hpp> 00010 00011 #include <iostream> 00012 #include <cctype> 00013 #include <string> 00014 00015 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE 00016 namespace antlr { 00017 #endif 00018 00022 ANTLR_USE_NAMESPACE(std)istream& eatwhite( ANTLR_USE_NAMESPACE(std)istream& is ) 00023 { 00024 char c; 00025 while( is.get(c) ) 00026 { 00027 #ifdef ANTLR_CCTYPE_NEEDS_STD 00028 if( !ANTLR_USE_NAMESPACE(std)isspace(c) ) 00029 #else 00030 if( !isspace(c) ) 00031 #endif 00032 { 00033 is.putback(c); 00034 break; 00035 } 00036 } 00037 return is; 00038 } 00039 00046 ANTLR_USE_NAMESPACE(std)string read_string( ANTLR_USE_NAMESPACE(std)istream& in ) 00047 { 00048 char ch; 00049 ANTLR_USE_NAMESPACE(std)string ret(""); 00050 // States for a simple state machine... 00051 enum { START, READING, ESCAPE, FINISHED }; 00052 int state = START; 00053 00054 eatwhite(in); 00055 00056 while( state != FINISHED && in.get(ch) ) 00057 { 00058 switch( state ) 00059 { 00060 case START: 00061 // start state: check wether starting with " then switch to READING 00062 if( ch != '"' ) 00063 throw IOException("string must start with '\"'"); 00064 state = READING; 00065 continue; 00066 case READING: 00067 // reading state: look out for escape sequences and closing " 00068 if( ch == '\\' ) // got escape sequence 00069 { 00070 state = ESCAPE; 00071 continue; 00072 } 00073 if( ch == '"' ) // close quote -> stop 00074 { 00075 state = FINISHED; 00076 continue; 00077 } 00078 ret += ch; // else append... 00079 continue; 00080 case ESCAPE: 00081 switch(ch) 00082 { 00083 case '\\': 00084 ret += ch; 00085 state = READING; 00086 continue; 00087 case '"': 00088 ret += ch; 00089 state = READING; 00090 continue; 00091 case '0': 00092 ret += '\0'; 00093 state = READING; 00094 continue; 00095 default: // unrecognized escape is not mapped 00096 ret += '\\'; 00097 ret += ch; 00098 state = READING; 00099 continue; 00100 } 00101 } 00102 } 00103 if( state != FINISHED ) 00104 throw IOException("badly formatted string: "+ret); 00105 00106 return ret; 00107 } 00108 00109 /* Read a ([A-Z][0-9][a-z]_)* kindoff thing. Skips leading whitespace. 00110 * @param in the istream to read from. 00111 */ 00112 ANTLR_USE_NAMESPACE(std)string read_identifier( ANTLR_USE_NAMESPACE(std)istream& in ) 00113 { 00114 char ch; 00115 ANTLR_USE_NAMESPACE(std)string ret(""); 00116 00117 eatwhite(in); 00118 00119 while( in.get(ch) ) 00120 { 00121 #ifdef ANTLR_CCTYPE_NEEDS_STD 00122 if( ANTLR_USE_NAMESPACE(std)isupper(ch) || 00123 ANTLR_USE_NAMESPACE(std)islower(ch) || 00124 ANTLR_USE_NAMESPACE(std)isdigit(ch) || 00125 ch == '_' ) 00126 #else 00127 if( isupper(ch) || islower(ch) || isdigit(ch) || ch == '_' ) 00128 #endif 00129 ret += ch; 00130 else 00131 { 00132 in.putback(ch); 00133 break; 00134 } 00135 } 00136 return ret; 00137 } 00138 00148 void read_AttributeNValue( ANTLR_USE_NAMESPACE(std)istream& in, 00149 ANTLR_USE_NAMESPACE(std)string& attribute, 00150 ANTLR_USE_NAMESPACE(std)string& value ) 00151 { 00152 attribute = read_identifier(in); 00153 00154 char ch; 00155 if( in.get(ch) && ch == '=' ) 00156 value = read_string(in); 00157 else 00158 throw IOException("invalid attribute=value thing "+attribute); 00159 } 00160 00161 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE 00162 } 00163 #endif
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 6 17:39:06 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003