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

« back to all changes in this revision

Viewing changes to Puma/gen-release/step1/src/CScanner.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:
17
17
// MA  02111-1307  USA                                            
18
18
 
19
19
#include "Puma/CScanner.h"
20
 
#include "Puma/SB_Sequential.h"
21
 
#include "Puma/SB_WholeFile.h"
22
 
#include "Puma/SB_String.h"
23
20
#include "Puma/ErrorStream.h"
24
21
#include "Puma/Source.h"
25
22
#include "Puma/Token.h"
26
23
#include "Puma/Location.h"
27
24
#include "Puma/Unit.h"
28
25
#include "Puma/CTokens.h"
 
26
#include "Puma/ScanBuffer.h"
 
27
 
 
28
#include <string.h>
29
29
 
30
30
namespace Puma {
31
31
 
44
44
}
45
45
 
46
46
 
47
 
Token *CScanner::scan () {
48
 
  CRecognizer::Lang lang;
49
 
  int expr, len;
50
 
  Token *result = 0;
51
 
 
52
 
  switch (buffer ().state ()) {
53
 
    case ScanBuffer::STATE_OK:
54
 
      switch (recognize (lang, expr, len)) {
55
 
        case -1: result = new Token (Token::ID_ERROR); break;
56
 
        case 0 :
57
 
        case 1 : result = make_token (lang, expr, len); break;
58
 
      }
59
 
      break;
60
 
    case ScanBuffer::STATE_END:
61
 
      result = new Token (Token::ID_END_OF_FILE);
62
 
      break;
63
 
    case ScanBuffer::STATE_ERROR:
64
 
      result = new Token (Token::ID_ERROR);
65
 
      break;
66
 
  }
67
 
 
68
 
  return result;
69
 
}
70
 
 
71
 
 
72
47
LanguageID CScanner::map_lang (CRecognizer::Lang lang) {
73
48
  switch (lang) {
74
49
    case CRecognizer::COMMENT: return Token::comment_id;
76
51
    case CRecognizer::COMP_DIR: return Token::dir_id;
77
52
    case CRecognizer::WHITE: return Token::white_id;
78
53
    case CRecognizer::PRE: return Token::macro_op_id;
79
 
    case CRecognizer::STRING: return Token::cpp_id;
80
54
    case CRecognizer::CORE: return Token::cpp_id;
81
55
    case CRecognizer::KEYWORD: return Token::keyword_id;
82
56
    case CRecognizer::ID: return Token::identifier_id;
91
65
  Token *result = 0;
92
66
  char short_buffer[512];
93
67
  char *tok_buffer = short_buffer;
94
 
  long rows = 0L;
 
68
  int rows = 0;
 
69
  int columns = 0;
 
70
  int offset = loc.column ();
 
71
  Mode old_mode = mode ();
95
72
  
96
73
  // dynamically allocate a huge buffer
97
 
  if (len >= (int)sizeof (short_buffer))
 
74
  if (len >= (int)sizeof (short_buffer) - 1)
98
75
    tok_buffer = new char[len + 1];
99
76
 
100
 
  char *src  = buffer ().token ();
101
 
  char *dest = tok_buffer;
102
 
  char *end  = src + len;
103
 
 
104
 
  int last = 0;
105
 
  Array<int> *cl = 0;
106
 
  while (src < end) {
107
 
    if (*src == '\\' && src + 1 < end && *(src + 1) == '\n') {
108
 
      src += 2;
109
 
      rows++;
110
 
      if (!cl) cl = new Array<int>(10,10);
111
 
      cl->append (last);
112
 
      last = 0;
113
 
    }
114
 
    else if (*src == '\\' && src + 2 < end && *(src + 1) == '\x0d' &&
115
 
             *(src + 2) == '\n') {
116
 
      src += 3;
117
 
      rows++;
118
 
      if (!cl) cl = new Array<int>(10,10);
119
 
      cl->append (last);
120
 
      last = 0;
121
 
    }
122
 
    else {
123
 
      if (*src == '\n')
124
 
        rows++;
125
 
      *dest = *src;
126
 
      dest++;
127
 
      src++;
128
 
      last++;
129
 
    }
130
 
  }
131
 
  *dest = '\0';
132
 
 
133
 
  buffer ().accept (len);
134
 
 
 
77
  // select the correct language id
135
78
  if (lang == CRecognizer::UNKNOWN)
136
79
    expr = Token::ID_UNKNOWN;
137
 
 
138
 
  // select the correct language id
139
 
 
140
80
  LanguageID lid;
141
 
 
142
 
  if (mode () == CRecognizer::IN_COMP_DIR && lang != CRecognizer::COMMENT)
 
81
  if (old_mode == CRecognizer::IN_COMP_DIR && lang != CRecognizer::COMMENT)
143
82
    lid = Token::dir_id;
144
83
  else {
145
84
    if (lang == CRecognizer::CORE && expr == TOK_OPEN_ROUND)
152
91
      lid = map_lang (lang);
153
92
  }
154
93
 
155
 
  result = new Token (expr, lid, tok_buffer);
156
 
  result->location (loc);
157
 
 
158
 
  // set the next token location
159
 
  if (rows > 0L)
160
 
    loc.setup (loc.filename (), loc.line () + rows);
161
 
 
162
 
  // attach the continuation line marks
163
 
  if (cl) result->cont_lines (cl);
 
94
//  volatile int z, s = 0;
 
95
//  for (z = 0; z < 100; z++)
 
96
//    s += z;
 
97
  char *src  = buffer ().token ();
 
98
  if (!buffer ().new_line (len)) {
 
99
    // a token without newline => can be copied directly
 
100
    memcpy (tok_buffer, src, len);
 
101
    tok_buffer[len] = '\0';
 
102
    result = new Token (expr, lid, tok_buffer);
 
103
    result->location (loc);
 
104
    loc.setup (loc.filename (), loc.line (), loc.column () + len);
 
105
  }
 
106
  else {
 
107
    // special token with newline => more complicated
 
108
    char *dest = tok_buffer;
 
109
    char *end  = src + len;
 
110
  
 
111
    int last = 0;
 
112
    Array<int> *cl = 0;
 
113
    while (src < end) {
 
114
      if (*src == '\\' && src + 1 < end && *(src + 1) == '\n') {
 
115
        src += 2;
 
116
        rows++;
 
117
        columns = 1;
 
118
        offset = 0;
 
119
        if (!cl) cl = new Array<int>(10,10);
 
120
        cl->append (last);
 
121
        last = 0;
 
122
      }
 
123
      else if (*src == '\\' && src + 2 < end && *(src + 1) == '\x0d' &&
 
124
               *(src + 2) == '\n') {
 
125
        src += 3;
 
126
        rows++;
 
127
        columns = 1;
 
128
        offset = 0;
 
129
        if (!cl) cl = new Array<int>(10,10);
 
130
        cl->append (last);
 
131
        last = 0;
 
132
      }
 
133
      else {
 
134
        if (*src == '\n') {
 
135
          rows++;
 
136
          columns = 1;
 
137
          offset = 0;
 
138
          mode (NORMAL);
 
139
          allow_directives ();
 
140
        } else {
 
141
          columns++;
 
142
        }
 
143
        *dest = *src;
 
144
        dest++;
 
145
        src++;
 
146
        last++;
 
147
      }
 
148
    }
 
149
    *dest = '\0';
 
150
  
 
151
    result = new Token (expr, lid, tok_buffer);
 
152
    result->location (loc);
 
153
  
 
154
    // set the next token location
 
155
    if (rows > 0 || columns > 0)
 
156
      loc.setup (loc.filename (), loc.line () + rows, offset + columns);
 
157
  
 
158
    // attach the continuation line marks
 
159
    if (cl) result->cont_lines (cl);
 
160
  }
 
161
  // eventually accept the token
 
162
  buffer ().accept (len);
164
163
 
165
164
  // free a dynamically allocated huge buffer
166
165
  if (len >= (int)sizeof (short_buffer))
171
170
 
172
171
 
173
172
void CScanner::scan_all (Unit &unit) {
174
 
  Token* token;
175
 
 
176
 
  loc.setup (unit.name () ? unit.name () : "<anonymous unit>", 1L);
177
 
 
178
 
  while ((token = scan ())->type () != Token::ID_END_OF_FILE) {
179
 
    if (token->type () == Token::ID_UNKNOWN)
180
 
      err << sev_error << token->location () 
181
 
          << "Unknown token" << endMessage;
182
 
    else if (token->type () == Token::ID_ERROR) {
183
 
      err << sev_error << token->location () 
184
 
          << "Error while scanning tokens" << endMessage;
 
173
 
 
174
  loc.setup (unit.name () ? unit.name () : "<anonymous unit>", 1, 1);
 
175
 
 
176
  while (buffer ().state () == CScanBuffer::STATE_OK ||
 
177
      buffer ().state () == CScanBuffer::STATE_NEW) {
 
178
 
 
179
    CRecognizer::Lang lang;
 
180
    int expr, len;
 
181
    int result = recognize (lang, expr, len); 
 
182
 
 
183
//    if (lang == WHITE && mode () == NORMAL && !buffer ().new_line (len)) {
 
184
//      // eventually accept the token
 
185
//      buffer ().accept (len);
 
186
//      continue;
 
187
//    }
 
188
//    if (lang == COMMENT) {
 
189
//      // eventually accept the token
 
190
//      buffer ().accept (len);
 
191
//      continue;
 
192
//    }
 
193
    
 
194
    if (result >= 0) {
 
195
      Token *new_token = make_token (lang, expr, len); 
 
196
      unit.append (*new_token);
 
197
//      cout << loc << " token " << new_token->type () << ": " << new_token->text () << endl;
 
198
    }
 
199
    else {
 
200
      err << sev_error << loc 
 
201
        << "Error while scanning tokens" << endMessage;
185
202
      break;
186
 
    } else if (token->type () == Token::ID_WARNING)
187
 
      err << sev_warning << token->location () 
188
 
          << "Warning while scanning tokens" << endMessage;
189
 
      
190
 
    unit.append (*token);
 
203
    }
191
204
  }
192
 
  if (token->type () == Token::ID_END_OF_FILE)
193
 
    delete token;
194
205
}
195
206
 
196
207
 
197
208
void CScanner::fill_unit (Source &in, Unit &unit) {
198
 
  if (in.size () > 0) {
199
 
    SB_WholeFile whole_file_buffer;
200
 
 
201
 
    whole_file_buffer.init (err, in);
202
 
    setup (whole_file_buffer);
203
 
    scan_all (unit);
204
 
  } else {
205
 
    SB_Sequential seq_buffer;
206
 
 
207
 
    seq_buffer.init (in);
208
 
    setup (seq_buffer);
209
 
    scan_all (unit);
 
209
 
 
210
  int size = in.size ();
 
211
  if (size == 0)
 
212
    return;
 
213
  if (size == -1) {
 
214
    err << sev_error << "can't scan file of unknown size" << endMessage;
 
215
    return;
210
216
  }
 
217
  char *buf = new char[size];
 
218
        if (in.read (buf, size) != size) {
 
219
    err << sev_error << "can't load input file" << endMessage;
 
220
          return;
 
221
        }
 
222
  setup ();
 
223
  buffer ().init (buf, size);
 
224
  scan_all (unit);
 
225
  delete[] buf;
211
226
}
212
227
 
213
228
 
214
229
void CScanner::fill_unit (const char *in, Unit &unit) {
215
 
  SB_String string_buffer;
216
 
 
217
 
  string_buffer.init (in);
218
 
  setup (string_buffer);
 
230
  setup ();
 
231
  buffer ().init (in);
219
232
  scan_all (unit);
220
233
}
221
234