Add more structure constructor tests.
[piglit/hramrach.git] / tests / glean / lex.cpp
blob84891f141f3ccc8aec5a2c50af020743911f212a
1 // BEGIN_COPYRIGHT
2 //
3 // Copyright (C) 1999 Allen Akin All Rights Reserved.
4 //
5 // Permission is hereby granted, free of charge, to any person
6 // obtaining a copy of this software and associated documentation
7 // files (the "Software"), to deal in the Software without
8 // restriction, including without limitation the rights to use,
9 // copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following
12 // conditions:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the
16 // Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ALLEN AKIN BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 // AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 // OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 // DEALINGS IN THE SOFTWARE.
26 //
27 // END_COPYRIGHT
32 // lex.cpp: Implementation of simple lexical analyzer
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include "lex.h"
38 namespace GLEAN {
41 ///////////////////////////////////////////////////////////////////////////////
42 // Constructor:
43 ///////////////////////////////////////////////////////////////////////////////
44 Lex::Lex(const char* s, bool ignoreCase/* = false */) {
45 input = s;
46 p = input;
47 ignoringCase = ignoreCase;
48 } // Lex::Lex
50 ///////////////////////////////////////////////////////////////////////////////
51 // next - Fetch next token from the input string
52 ///////////////////////////////////////////////////////////////////////////////
53 void
54 Lex::next() {
55 while (isspace(*p))
56 ++p;
58 if (isalpha(*p) || *p == '_') {
59 id = "";
60 if (ignoringCase)
61 while (isalnum(*p) || *p == '_')
62 id += tolower(*p++);
63 else
64 while (isalnum(*p) || *p == '_')
65 id += *p++;
66 token = ID;
67 return;
70 if (isdigit(*p)) {
71 iValue = strtol(p, const_cast<char**>(&p), 0);
72 token = ICONST;
73 return;
76 char nextC = 0;
77 char c = *p++;
78 if (c)
79 nextC = *p;
80 switch (c) {
81 case '|':
82 if (nextC == '|') {
83 ++p;
84 token = OR_OR;
86 else
87 token = OR;
88 break;
89 case '&':
90 if (nextC == '&') {
91 ++p;
92 token = AND_AND;
94 else
95 token = AND;
96 break;
97 case '<':
98 if (nextC == '=') {
99 ++p;
100 token = LE;
102 else
103 token = LT;
104 break;
105 case '>':
106 if (nextC == '=') {
107 ++p;
108 token = GE;
110 else
111 token = GT;
112 break;
113 case '=':
114 if (nextC == '=') {
115 ++p;
116 token = EQ;
118 else
119 token = ASSIGN;
120 break;
121 case '!':
122 if (nextC == '=') {
123 ++p;
124 token = NE;
126 else
127 token = BANG;
128 break;
129 case '+':
130 token = PLUS;
131 break;
132 case '-':
133 token = MINUS;
134 break;
135 case '*':
136 token = STAR;
137 break;
138 case '/':
139 token = SLASH;
140 break;
141 case '%':
142 token = PERCENT;
143 break;
144 case ',':
145 token = COMMA;
146 break;
147 case '(':
148 token = LPAREN;
149 break;
150 case ')':
151 token = RPAREN;
152 break;
153 case '.':
154 token = DOT;
155 break;
156 case '\0':
157 token = END;
158 --p; // push back '\0' so that token will always be END
159 break;
160 default:
161 throw Lexical("unrecognized symbol", p - input);
164 return;
165 } // Lex::next
167 } // namespace GLEAN