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

« back to all changes in this revision

Viewing changes to Puma/gen-release/step2/src/SimpleParser.cc

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2008-04-10 17:40:52 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080410174052-xdnsm7oi8hauyyf1
Tags: 1.0pre4~svn.20080409+dfsg-3
Fix another missing include, this time in Ag++/StdSystem.cc

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
}
29
29
 
30
30
 
31
 
int SimpleParser::tokenize (const string &line, vector<string> &words, 
32
 
 const string &delimiters) {
33
 
  int res=0;
34
 
  // Skip delimiters at beginning.
35
 
  string::size_type lastPos = line.find_first_not_of (delimiters, 0);
36
 
  // Find first "delimiter".
37
 
  string::size_type pos     = line.find_first_of (delimiters, lastPos);
38
 
  // Find first '"'
39
 
  string::size_type hpos    = line.find_first_of ('"', lastPos);
40
 
 
41
 
  if (hpos != string::npos && hpos < pos)
42
 
    // Find next "delimiter" after closing '"'
43
 
    pos = line.find_first_of (delimiters, line.find_first_of ('"', hpos + 1) + 1);
44
 
 
45
 
  while ((string::npos != pos) || (string::npos != lastPos)) {
46
 
    string token = line.substr (lastPos, pos - lastPos);
47
 
    // strip '"'
48
 
    //if (token[0] == '"' && token[token.length () - 1] == '"')
49
 
    //  token = token.substr (1, token.length () - 2);
50
 
    // Found a token, add it to the vector.
 
31
string::size_type SimpleParser::findBeginOfNextToken (const string& line, const string& delimiters, string::size_type pos) {
 
32
  bool escaped = false;
 
33
  string::size_type lastPos = string::npos;
 
34
  if (pos != string::npos) {
 
35
    for (string::size_type i = pos; i < line.length() && lastPos == string::npos; i++) {
 
36
      char c = line[i];
 
37
      if (delimiters.find(c) == string::npos) {
 
38
        lastPos = escaped ? i-1 : i;
 
39
      }
 
40
      if (c == '\\') {
 
41
        escaped = ! escaped;
 
42
      } else {
 
43
        escaped = false;
 
44
      }
 
45
    }
 
46
  }
 
47
  return lastPos;
 
48
}
 
49
 
 
50
 
 
51
string::size_type SimpleParser::findEndOfNextToken (const string& line, const string& delimiters, string::size_type lastPos) {
 
52
  bool in_string = false, escaped = false;
 
53
  string::size_type endpos = line.length();
 
54
  if (lastPos != string::npos) {
 
55
    for (string::size_type i = lastPos; i < line.length(); i++) {
 
56
      char c = line[i];    
 
57
      if (! in_string && delimiters.find(c) != string::npos) {
 
58
        endpos = i;
 
59
        break;
 
60
      }
 
61
      if (! escaped && c == '"') {
 
62
        in_string = ! in_string;
 
63
      }    
 
64
      if (c == '\\') {
 
65
        escaped = ! escaped;
 
66
      } else {
 
67
        escaped = false;
 
68
      }
 
69
    }
 
70
  }
 
71
  return endpos;
 
72
}
 
73
 
 
74
 
 
75
string::size_type SimpleParser::next (const string& line, const string& delimiters, string::size_type pos, string::size_type& lastPos) {
 
76
  // find begin of next token
 
77
  lastPos = findBeginOfNextToken(line, delimiters, pos);
 
78
  // find end of the token
 
79
  return findEndOfNextToken(line, delimiters, lastPos);
 
80
}
 
81
 
 
82
 
 
83
int SimpleParser::tokenize (const std::string& line, std::vector<std::string>& words, const std::string& delimiters) {
 
84
  int res = 0;
 
85
  string::size_type pos = 0, lastPos = 0;
 
86
    
 
87
  // get first token position
 
88
  pos = next (line, delimiters, 0, lastPos);
 
89
    
 
90
  // while not at the end of the line
 
91
  while (pos != string::npos && lastPos != string::npos) {
 
92
    // get current token
 
93
    string token = line.substr (lastPos, pos-lastPos);
 
94
    // add current token to vector
51
95
    words.push_back (token);
52
96
    res++;
53
 
    // Skip delimiters.  Note the "not_of"
54
 
    lastPos = line.find_first_not_of (delimiters, pos);
55
 
    // Find next "non-delimiter"
56
 
    pos = line.find_first_of (delimiters, lastPos);
57
 
    // Find first '"'
58
 
    hpos = line.find_first_of ('"', lastPos);
59
 
    if (hpos != string::npos && hpos < pos)
60
 
      // Find next "delimiter" after closing '"'
61
 
      pos = line.find_first_of (delimiters, line.find_first_of ('"', hpos + 1) + 1);
 
97
    // get next token position
 
98
    pos = next (line, delimiters, pos, lastPos);
62
99
  }
63
100
  return res;
64
101
}