File: token.c

package info (click to toggle)
swish++ 1.1b3-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 416 kB
  • ctags: 409
  • sloc: ansic: 2,842; makefile: 247; sh: 48
file content (143 lines) | stat: -rw-r--r-- 3,182 bytes parent folder | download | duplicates (2)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
**	SWISH++
**	token.c
**
**	Copyright (C) 1998  Paul J. Lucas
**
**	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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

// standard
#include <cctype>
#include <cstring>
#include <iostream.h>

// local
#include "config.h"
#include "fake_ansi.h"
#include "token.h"
#include "util.h"

#ifndef	PJL_NO_NAMESPACES
using namespace std;
#endif

bool token::on_hold_;
//
// We needed somplace to store a token that was put back but obviously can't
// declare a token static data member inside token directly.
//
token& token::hold() {
	static token t;
	return t;
}

//*****************************************************************************
//
// SNYOPSIS
//
	istream& operator>>( istream &in, token &t )
//
// DESCRIPTION
//
//	Extract a token from the given input stream.
//
// PARAMETERS
//
//	in	The input stream to extract the token from.
//
//	t	The newly extracted token.
//
// RETURN VALUE
//
//	Returns the given input stream as is standard practice.
//
// SEE ALSO
//
//	Stroustrup, Bjarne.  "The C++ Programming Language, 3rd ed."
//	Addison-Wesley, Reading, MA.  p. 621.
//
//*****************************************************************************
{
	if ( token::on_hold_ ) {
		//
		// If there was a token previously put back, use it.
		//
		t = token::hold();
		token::on_hold_ = false;
		return in;
	}
	t.type_ = token::no_token;
	bool in_word = false;
	char c;

	while ( in.get( c ) ) {

		if ( is_word_char( c ) ) {
			if ( !in_word ) {
				// start a new word
				t.buf_[ 0 ] = tolower( c );
				t.len_ = 1;
				in_word = true;
				continue;
			}
			if ( t.len_ < Word_Hard_Max_Size ) {
				// continue same word
				t.buf_[ t.len_++ ] = tolower( c );
				continue;
			}
			in_word = false;		// too big: skip chars
			while ( in.get( c ) && is_word_char( c ) ) ;
			continue;
		}

		if ( in_word ) {
			if ( c == '*' )
				t.type_ = token::word_star_token;
			else
				in.putback( c );
			break;
		}

		switch ( c ) {
			case '(':
				t.type_ = token::lparen_token;
				return in;
			case ')':
				t.type_ = token::rparen_token;
				return in;
		}
	}

	if ( in_word ) {
		t.buf_[ t.len_ ] = '\0';
		if ( t.type_ )
			return in;
		//
		// Check to see if the word is an operator.  For only 3
		// comparisons, linear search is good enough.
		//
		if ( !::strcmp( t.buf_, "and" ) )
			t.type_ = token::and_token;
		else if ( !::strcmp( t.buf_, "or"  ) )
			t.type_ = token::or_token;
		else if ( !::strcmp( t.buf_, "not" ) )
			t.type_ = token::not_token;
		else
			t.type_ = token::word_token;
	}

	return in;
}