File: scanner.cpp

package info (click to toggle)
madman 0.93.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,060 kB
  • ctags: 3,309
  • sloc: cpp: 14,194; ansic: 13,331; sh: 100; makefile: 62; python: 54
file content (75 lines) | stat: -rw-r--r-- 1,949 bytes parent folder | download
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
/*
madman - a music manager
Copyright (C) 2003  Andreas Kloeckner <ak@ixion.net>

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 <stdexcept>
#include "base.h"
#include "scanner.h"




void tScanner::addTokenDescriptor( int type, const QString &pattern, bool minimal, bool ignore, unsigned flags )
{
  tTokenDescriptor d;
  d.TokenType = type;
  d.RegExp = "^"+pattern;
  d.RegExp.setMinimal( minimal );
  d.Ignore = ignore;
  d.Flags = flags;
  TokenDescriptorList.push_back( d );
}




void tScanner::scan( const QString &victim, tTokenList &tokenlist )
{
  int index = 0;

  tokenlist.clear();
  while ( index < (int) victim.length() )
  {
    bool found_a_token = false;
    FOREACH_CONST( first, TokenDescriptorList, tTokenDescriptorList )
    {
      if ( first->RegExp.search( victim.mid( index ) ) != -1 )
      {
	if ( !first->Ignore )
	{
	  tToken token;
	  token.TokenType = first->TokenType;
	  token.Flags = first->Flags;
	  token.Text = victim.mid( index, first->RegExp.matchedLength() );
	  tokenlist.push_back( token );
	}
	index += first->RegExp.matchedLength();
	found_a_token = first->RegExp.matchedLength() > 0;
	if ( found_a_token )
	  break;
      }
    }

    if (!found_a_token)
      throw runtime_error( "invalid token at: " + QString2string( victim.mid( index, 20 ) ) );
  }
}