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

Last change on this file since 261 was 259, checked in by cinc, 18 years ago

Started work on an IDL compiler

File size: 5.5 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 setSymbolInfo(GTokenType token)
49{
50 PSYMBOLINFO psi;
51
52 psi=(PSYMBOLINFO)gScanner->user_data;
53
54 switch(token)
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(token>G_TOKEN_LAST)
64 {
65 psi->uiCurSymbolKind=psi->pSymbols[token-G_TOKEN_LAST-1].uiKind;
66 }
67}
68
69guint queryCurTokensKind(void)
70{
71 PSYMBOLINFO psi;
72
73 psi=(PSYMBOLINFO)gScanner->user_data;
74 return psi->uiCurSymbolKind;
75}
76
77static guint getKindFromTokenType(GTokenType token)
78{
79 if(token>G_TOKEN_LAST)
80 {
81 PSYMBOLINFO psi;
82
83 psi=(PSYMBOLINFO)gScanner->user_data;
84 return psi->pSymbols[token-G_TOKEN_LAST-1].uiKind;
85 }
86 switch(token)
87 {
88 case G_TOKEN_IDENTIFIER:
89 {
90 /* Compare strings here. Yes, that's slow... */
91 break;
92 }
93 default:
94 break;
95 }
96
97 return KIND_UNKNOWN;
98}
99
100guint queryNextTokensKind(void)
101{
102 return getKindFromTokenType(gScanner->next_token);
103}
104
105
106/* Well, the name says all... */
107void getNextToken(void)
108{
109 curToken = g_scanner_get_next_token(gScanner);
110 setSymbolInfo(curToken);
111}
112
113gboolean matchNextKind(guint uiKind)
114{
115 GTokenType token;
116
117 token=g_scanner_peek_next_token(gScanner);
118 if(uiKind==getKindFromTokenType(token))
119 {
120 getNextToken();
121 return TRUE;
122 }
123 return FALSE;
124}
125
126/* Well, the name says all... */
127gboolean matchCur(GTokenType token)
128{
129 if(token==curToken)
130 {
131 return TRUE;
132 }
133 return FALSE;
134}
135
136/* Well, the name says all...
137
138 Note that this function advances to the next token if the
139 tokens match.
140*/
141gboolean matchNext(GTokenType token)
142{
143 if(token==g_scanner_peek_next_token(gScanner))
144 {
145 getNextToken();
146 return TRUE;
147 }
148 return FALSE;
149}
150
151/*
152 Print current token info.
153 */
154void printToken(GTokenType token)
155{
156 GTokenValue value=gScanner->value;
157
158 switch(token)
159 {
160 case IDL_SYMBOL_INTERFACE:
161 g_message("Token: %d (IDL_SYMBOL_INTERFACE)\tParsing...", token);
162 break;
163 case G_TOKEN_IDENTIFIER:
164 g_message("Token: %d (G_TOKEN_IDENTIFIER)\t\t%s", token, value.v_identifier);
165 break;
166 case G_TOKEN_STRING:
167 g_message("Token: %d (G_TOKEN_STRING)\t\t\t%s", token, value.v_string);
168 break;
169 case G_TOKEN_LEFT_PAREN:
170 g_message("Token: %d (G_TOKEN_LEFT_PAREN)\t\t(", token);
171 break;
172 case G_TOKEN_RIGHT_PAREN:
173 g_message("Token: %d (G_TOKEN_RIGHT_PAREN)\t\t)", token);
174 break;
175 case ':':
176 g_message("Token: %d (colon)\t\t:", token);
177 break;
178 case ';':
179 g_message("Token: %d (semicolon)\t\t\t; (LINE %d)", token, g_scanner_cur_line(gScanner));
180 break;
181 case '#':
182 g_message("Token: %d (hash)\t\t\t#", token);
183 break;
184 case '/':
185 g_message("Token: %d (slash)\t\t\t/ %s", token, value.v_comment);
186 break;
187 case G_TOKEN_COMMA:
188 g_message("Token: %d (G_TOKEN_COMMA)\t\t\t,", token);
189 break;
190 case G_TOKEN_INT:
191 g_message("Token: %d (G_TOKEN_INT)\t\t\t%ld", token, value.v_int);
192 break;
193 case IDL_SYMBOL_DEFINE:
194 g_message("Token: %d (IDL_SYMBOL_DEFINE)\t\t\t", token);
195 break;
196 case IDL_SYMBOL_IFDEF:
197 g_message("Token: %d (IDL_SYMBOL_IFDEF)\t\t\t", token);
198 break;
199 case IDL_SYMBOL_ENDIF:
200 g_message("Token: %d (IDL_SYMBOL_ENDIF)\t\t\t", token);
201 break;
202 default:
203 g_message("Token: %d (---)\t\t\t (LINE %d)", token, g_scanner_cur_line(gScanner));
204 break;
205 }
206}
Note: See TracBrowser for help on using the repository browser.