~ubuntu-branches/ubuntu/quantal/aspectc++/quantal

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
// This file is part of PUMA.
// Copyright (C) 1999-2003  The PUMA developer team.
//                                                                
// This program is free software;  you can redistribute it and/or 
// modify it under the terms of the GNU General Public License as 
// published by the Free Software Foundation; either version 2 of 
// the License, or (at your option) any later version.            
//                                                                
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
// GNU General Public License for more details.                   
//                                                                
// You should have received a copy of the GNU General Public      
// License along with this program; if not, write to the Free     
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
// MA  02111-1307  USA                                            

#include "Puma/SimpleParser.h"

namespace Puma {


string SimpleParser::strip (const string &str) {
  if (str[0] == '"' && str[str.length () - 1] == '"')
    return str.substr (1, str.length () - 2);
  return str;
}


string::size_type SimpleParser::findBeginOfNextToken (const string& line, const string& delimiters, string::size_type pos) {
  bool escaped = false;
  string::size_type lastPos = string::npos;
  if (pos != string::npos) {
    for (string::size_type i = pos; i < line.length() && lastPos == string::npos; i++) {
      char c = line[i];
      if (delimiters.find(c) == string::npos) {
        lastPos = escaped ? i-1 : i;
      }
      if (c == '\\') {
        escaped = ! escaped;
      } else {
        escaped = false;
      }
    }
  }
  return lastPos;
}


string::size_type SimpleParser::findEndOfNextToken (const string& line, const string& delimiters, string::size_type lastPos) {
  bool in_string = false, escaped = false;
  string::size_type endpos = line.length();
  if (lastPos != string::npos) {
    for (string::size_type i = lastPos; i < line.length(); i++) {
      char c = line[i];    
      if (! in_string && delimiters.find(c) != string::npos) {
        endpos = i;
        break;
      }
      if (! escaped && c == '"') {
        in_string = ! in_string;
      }    
      if (c == '\\') {
        escaped = ! escaped;
      } else {
        escaped = false;
      }
    }
  }
  return endpos;
}


string::size_type SimpleParser::next (const string& line, const string& delimiters, string::size_type pos, string::size_type& lastPos) {
  // find begin of next token
  lastPos = findBeginOfNextToken(line, delimiters, pos);
  // find end of the token
  return findEndOfNextToken(line, delimiters, lastPos);
}


int SimpleParser::tokenize (const std::string& line, std::vector<std::string>& words, const std::string& delimiters) {
  int res = 0;
  string::size_type pos = 0, lastPos = 0;
    
  // get first token position
  pos = next (line, delimiters, 0, lastPos);
    
  // while not at the end of the line
  while (pos != string::npos && lastPos != string::npos) {
    // get current token
    string token = line.substr (lastPos, pos-lastPos);
    // add current token to vector
    words.push_back (token);
    res++;
    // get next token position
    pos = next (line, delimiters, pos, lastPos);
  }
  return res;
}


} // namespace Puma