001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.fusesource.hawtbuf.proto.compiler;
018    
019    import org.fusesource.hawtbuf.proto.compiler.parser.ParseException;
020    import org.fusesource.hawtbuf.proto.compiler.parser.Token;
021    
022    public class ParserSupport {
023    
024        public static String decodeString(Token token) throws ParseException {
025            
026    //        StringBuilder sb = new StringBuilder();
027    //        for (int i = 1; i < value.length() - 1; i++) {
028    //            char c = value.charAt(i);
029    //            if (c == '\'') {
030    //                if( i+1 < (value.length() - 1) ) {
031    //                    char e = value.charAt(i+1);
032    //                    switch(e) {
033    //                    case 'a': 
034    //                        sb.append((char)0x07);
035    //                        break;
036    //                    case 'b':
037    //                        sb.append("\b");
038    //                        break;
039    //                    case 'f':
040    //                        sb.append("\f");
041    //                        break;
042    //                    case 'n': 
043    //                        sb.append("\n");
044    //                        break;
045    //                    case 'r':
046    //                        sb.append("\r");
047    //                        break;
048    //                    case 't':
049    //                        sb.append("\t");
050    //                        break;
051    //                    case 'v':
052    //                        sb.append((char)0x0b);
053    //                        break;
054    //                    case '\\':
055    //                        sb.append("\\");
056    //                        break;
057    //                    case '\'':
058    //                        sb.append("'");
059    //                        break;
060    //                    case '\"':
061    //                        sb.append("\"");
062    //                        break;
063    //                    default:
064    //                        sb.append(e);
065    //                        break;
066    //                    }
067    //                } else {
068    //                    throw new RuntimeException("Invalid string litteral: "+value);
069    //                }
070    //            }
071    //            sb.append(c);
072    //        }
073    //        return sb.toString();
074            
075            try {
076                return TextFormat.unescapeText(token.image.substring(1, token.image.length()-1));
077            } catch (TextFormat.InvalidEscapeSequence e) {
078                throw new ParseException("Invalid string litteral at line " + token.next.beginLine + ", column " + token.next.beginColumn+": "+e.getMessage());
079            }
080        }
081    
082    }