source: trunk/idl-compiler/c/token.c@ 270

Last change on this file since 270 was 265, checked in by cinc, 18 years ago

Rewrote symbol handling.

File size: 6.9 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2* Version: CDDL 1.0/LGPL 2.1
3*
4* The contents of this file are subject to the COMMON DEVELOPMENT AND
5* DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
6* this file except in compliance with the License. You may obtain a copy of
7* the License at http://www.sun.com/cddl/
8*
9* Software distributed under the License is distributed on an "AS IS" basis,
10* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11* for the specific language governing rights and limitations under the
12* License.
13*
14* The Original Code is "NOM" Netlabs Object Model
15*
16* The Initial Developer of the Original Code is
17* netlabs.org: Chris Wohlgemuth <cinc-ml@netlabs.org>.
18* Portions created by the Initial Developer are Copyright (C) 2005-2007
19* the Initial Developer. All Rights Reserved.
20*
21* Contributor(s):
22*
23* Alternatively, the contents of this file may be used under the terms of
24* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
25* case the provisions of the LGPL are applicable instead of those above. If
26* you wish to allow use of your version of this file only under the terms of
27* the LGPL, and not to allow others to use your version of this file under
28* the terms of the CDDL, indicate your decision by deleting the provisions
29* above and replace them with the notice and other provisions required by the
30* LGPL. If you do not delete the provisions above, a recipient may use your
31* version of this file under the terms of any one of the CDDL or the LGPL.
32*
33* ***** END LICENSE BLOCK ***** */
34
35#include <os2.h>
36#include <gtk/gtk.h>
37#include "nom.h"
38
39#include "parser.h"
40
41extern GScanner *gScanner;
42extern GTokenType curToken;
43
44/*
45 We need this information during parsing to decide if an identifier
46 is e.g. a typespec or just a var name.
47 */
48void setCurSymbolInfo(void)
49{
50 PSYMBOLINFO psi;
51
52 psi=(PSYMBOLINFO)gScanner->user_data;
53
54 switch(curToken)
55 {
56 case G_TOKEN_IDENTIFIER:
57 /* Here we have to check identifiers if they are for example types, e.g. int */
58 break;
59 default:
60 psi->uiCurSymbolKind=KIND_UNKNOWN;
61 break;
62 }
63 if(curToken==G_TOKEN_SYMBOL)
64 {
65 GTokenValue value;
66 PSYMBOL pCurSymbol;
67
68 value=gScanner->value;
69 pCurSymbol=value.v_symbol;
70
71 psi->uiCurSymbolKind=pCurSymbol->uiKind;
72 }
73}
74
75guint queryCurTokensKind(void)
76{
77 PSYMBOLINFO psi;
78
79 psi=(PSYMBOLINFO)gScanner->user_data;
80 return psi->uiCurSymbolKind;
81}
82
83/* This token is not necessarily the current token! */
84static guint getKindOfNextToken()
85{
86 /* Load info into gScanner */
87 g_scanner_peek_next_token(gScanner);
88
89 if(gScanner->next_token==G_TOKEN_SYMBOL)
90 {
91 GTokenValue value;
92 PSYMBOL pCurSymbol;
93
94 value=gScanner->next_value;
95 pCurSymbol=value.v_symbol;
96
97 return pCurSymbol->uiKind;
98 }
99 switch(gScanner->next_token)
100 {
101 case G_TOKEN_IDENTIFIER:
102 {
103 /* Compare strings here. Yes, that's slow... */
104 break;
105 }
106 default:
107 break;
108 }
109
110 return KIND_UNKNOWN;
111}
112
113#if 0
114guint queryNextTokensKind(void)
115{
116 return getKindFromTokenType(gScanner->next_token);
117}
118#endif
119
120/* Well, the name says all... */
121void getNextToken(void)
122{
123 curToken = g_scanner_get_next_token(gScanner);
124 setCurSymbolInfo();
125}
126
127gboolean matchNextKind(guint uiKind)
128{
129 if(uiKind==getKindOfNextToken())
130 {
131 getNextToken();
132 return TRUE;
133 }
134 return FALSE;
135}
136
137/* Well, the name says all... */
138gboolean matchCur(GTokenType token)
139{
140 if(token==curToken)
141 {
142 return TRUE;
143 }
144 return FALSE;
145}
146
147/* Well, the name says all...
148
149 Note that this function advances to the next token if the
150 tokens match.
151*/
152gboolean matchNext(GTokenType token)
153{
154 if(token==g_scanner_peek_next_token(gScanner))
155 {
156 getNextToken();
157 return TRUE;
158 }
159 return FALSE;
160}
161
162/*
163 Print current token info.
164 */
165void printToken(GTokenType token)
166{
167 GTokenValue value=gScanner->value;
168
169 switch(token)
170 {
171 case G_TOKEN_SYMBOL:
172 {
173 PSYMBOL pCurSymbol=value.v_symbol;
174
175 switch(pCurSymbol->uiSymbolToken)
176 {
177 case IDL_SYMBOL_INTERFACE:
178 g_message("Token: %d (IDL_SYMBOL_INTERFACE)\t\t\t (LINE %d)",
179 pCurSymbol->uiSymbolToken, g_scanner_cur_line(gScanner));
180 break;
181 case IDL_SYMBOL_DEFINE:
182 g_message("Token: %d (IDL_SYMBOL_DEFINE)\t\t\t", pCurSymbol->uiSymbolToken);
183 break;
184 case IDL_SYMBOL_IFDEF:
185 g_message("Token: %d (IDL_SYMBOL_IFDEF)\t\t\t", pCurSymbol->uiSymbolToken);
186 break;
187 case IDL_SYMBOL_ENDIF:
188 g_message("Token: %d (IDL_SYMBOL_ENDIF)\t\t\t", pCurSymbol->uiSymbolToken);
189 break;
190 default:
191 {
192 PSYMBOLINFO psi;
193 psi=(PSYMBOLINFO)gScanner->user_data;
194
195 g_message("Token: %d (%s)\t\t\t (LINE %d)", pCurSymbol->uiSymbolToken,
196 pCurSymbol->chrSymbolName, g_scanner_cur_line(gScanner));
197
198 break;
199 }
200 }/* switch */
201 break;
202 }
203 case G_TOKEN_IDENTIFIER:
204 g_message("Token: %d (G_TOKEN_IDENTIFIER)\t\t%s (LINE %d)",
205 token, value.v_identifier, g_scanner_cur_line(gScanner));
206 break;
207 case G_TOKEN_STRING:
208 g_message("Token: %d (G_TOKEN_STRING)\t\t\t%s", token, value.v_string);
209 break;
210 case G_TOKEN_LEFT_PAREN:
211 g_message("Token: %d (G_TOKEN_LEFT_PAREN)\t\t\t( (LINE %d)", token, g_scanner_cur_line(gScanner));
212 break;
213 case G_TOKEN_RIGHT_PAREN:
214 g_message("Token: %d (G_TOKEN_RIGHT_PAREN)\t\t\t) (LINE %d)", token, g_scanner_cur_line(gScanner));
215 break;
216 case G_TOKEN_LEFT_CURLY:
217 g_message("Token: %d (G_TOKEN_LEFT_CURLY)\t\t\t{ (LINE %d)", token, g_scanner_cur_line(gScanner));
218 break;
219 case G_TOKEN_RIGHT_CURLY:
220 g_message("Token: %d (G_TOKEN_RIGHT_CURLY)\t\t\t} (LINE %d)", token, g_scanner_cur_line(gScanner));
221 break;
222 case ':':
223 g_message("Token: %d (colon)\t\t:", token);
224 break;
225 case ';':
226 g_message("Token: %d (semicolon)\t\t\t; (LINE %d)", token, g_scanner_cur_line(gScanner));
227 break;
228 case '#':
229 g_message("Token: %d (hash)\t\t\t#", token);
230 break;
231 case '/':
232 g_message("Token: %d (slash)\t\t\t/ %s", token, value.v_comment);
233 break;
234 case G_TOKEN_COMMA:
235 g_message("Token: %d (G_TOKEN_COMMA)\t\t\t,", token);
236 break;
237 case G_TOKEN_INT:
238 g_message("Token: %d (G_TOKEN_INT)\t\t\t%ld", token, value.v_int);
239 break;
240 default:
241 {
242 g_message("Token: %d (---)\t\t\t (LINE %d)", token, g_scanner_cur_line(gScanner));
243 break;
244 } /* default */
245 } /* switch */
246}
Note: See TracBrowser for help on using the repository browser.