Debian Codegraph @

www/doc/apiroutines.html

raw
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <!--
4
5 Copyright (c) 2006, Sun Microsystems, Inc.
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 * Redistributions of source code must retain the above copyright notice,
12 this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 * Neither the name of the Sun Microsystems, Inc. nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 THE POSSIBILITY OF SUCH DAMAGE.
31
32 -->
33 <head>
34 <title>JavaCC API Documentation</title>
35 <!-- Changed by: Michael Van De Vanter, 14-Jan-2003 -->
36 </head>
37 <body bgcolor="#FFFFFF" >
38
39 <h1>JavaCC [tm]: API Routines</h1>
40
41 <p>
42 This web page is a comprehensive list of all classes, methods,
43 and variables available for use by a JavaCC [tm] user. These classes,
44 methods, and variables are typically used from the actions that
45 are embedded in a JavaCC grammar. In the sample code used below,
46 it is assumed that the name of the generated parser is "TheParser".
47 </p>
48
49 <h3>Non-Terminals in the Input Grammar</h3>
50 <p>
51 For each non-terminal NT in the input grammar file, the following method
52 is generated into the parser class:
53 </p>
54 <ul>
55 <li>
56 <strong><em>returntype</em> NT(<em>parameters</em>) throws ParseException;</strong>
57 </li>
58 </ul>
59 <p>
60 Here, <em>returntype</em> and <em>parameters</em> are what were specified
61 in the JavaCC input file in the definition of NT (where NT occurred on the
62 left-hand side).
63 </p>
64 <p>
65 When this method is called, the input stream is parsed to match this non-terminal.
66 On a successful parse, this method returns normally. On detection of a parse
67 error, an error message is displayed and the method returns by throwing an exception
68 of the type ParseException.
69 </p>
70 <p>
71 <em>Note that all non-terminals in a JavaCC input grammar have equal status;
72 it is possible to parse to any non-terminal by calling the non-terminal's method.
73 </em>
74 </p>
75
76 <h3>API for Parser Actions</h3>
77 <ul>
78 <li><strong> Token token;</strong>
79 <br />
80 This variable holds the last token consumed by the parser and can be used
81 in parser actions. This is <em>exactly</em> the same as the token returned by
82 <em>getToken(0)</em>.
83 </li>
84 </ul>
85 <p>
86 In addition, the two methods - <em><a href="#getToken">getToken(int i)</a></em> and
87 <em><a href="#getNextToken">getNextToken()</a></em> can also be used in
88 actions to traverse the token list.
89 </p>
90 <h3>The Token Manager Interface</h3>
91 <p>
92 Typically, the token manager interface is not to be used. Instead all access
93 must be made through the parser interface. However, in certain situations -
94 such as if you are not building a parser and building only the token manager -
95 the token manager interface is useful.
96 The token manager provides the following routine:
97 </p>
98 <ul>
99 <li>
100 <strong>Token getNextToken() throws ParseError;</strong>
101 </li>
102 </ul>
103 <p>
104 Each call to this method returns the next token in the input stream. This
105 method throws a ParseError exception when there is a lexical error, i.e.,
106 it could not find a match for any of the specified tokens from the input
107 stream. The type Token is described later.
108 </p>
109
110 <h3>Constructors and Other Initialization Routines</h3>
111 <ul>
112 <li>
113 <strong>TheParser.TheParser(java.io.InputStream stream)</strong>
114 <br />
115 This creates a new parser object, which in turn creates a new token manager object
116 that reads its tokens from "stream". This constructor is available only
117 when both the options USER_TOKEN_MANAGER and USER_CHAR_STREAM are false.
118 If the option STATIC is true, this constructor (along with other constructors)
119 can be called exactly once to create a single parser object.
120 </li>
121 <li>
122 <strong>TheParser.TheParser(CharStream stream)</strong>
123 <br />
124 Similar to the previous constructor, except that this one is available only
125 when the option USER_TOKEN_MANAGER is false and USER_CHAR_STREAM is true.
126 </li>
127 <li>
128 <strong>void TheParser.ReInit(java.io.InputStream stream)</strong>
129 <br />
130 This reinitializes an existing parser object. In addition, it also reinitializes
131 the existing token manager object that corresponds to this parser object. The result
132 is a parser object with the exact same functionality as one that was created
133 with the constructor above. The only difference is that new objects are not
134 created. This method is available only
135 when both the options USER_TOKEN_MANAGER and USER_CHAR_STREAM are false.
136 If the option STATIC is true, this (along with the other ReInit methods)
137 is the only way to restart a parse operation
138 for there is only one parser and all one can do is reinitialize it.
139 </li>
140 <li>
141 <strong>void TheParser.ReInit(CharStream stream)</strong>
142 <br />
143 Similar to the previous method, except that this one is available only
144 when the option USER_TOKEN_MANAGER is false and USER_CHAR_STREAM is true.
145 </li>
146 <li>
147 <strong>TheParser(TheParserTokenManager tm)</strong>
148 This creates a new parser object which uses an already created token manager object "tm" as
149 its token manager. This constructor is only available if option USER_TOKEN_MANAGER is
150 false. If the option STATIC is true, this constructor (along with other constructors)
151 can be called exactly once to create a single parser object.
152 </li>
153 <li>
154 <strong>TheParser(TokenManager tm)</strong>
155 <br />
156 Similar to the previous constructor, except that this one is available only
157 when the option USER_TOKEN_MANAGER is true.
158 </li>
159 <li>
160 <strong>void TheParser.ReInit(TheParserTokenManager tm)</strong>
161 <br />
162 This reinitializes an existing parser object with the token manager object "tm" as its
163 new token manager. This method is only available if option USER_TOKEN_MANAGER is
164 false. If the option STATIC is true, this (along with the other ReInit methods)
165 is the only way to restart a parse operation
166 for there is only one parser and all one can do is reinitialize it.
167 </li>
168 <li>
169 <strong>void TheParser.ReInit(TokenManager tm)</strong>
170 <br />
171 Similar to the previous method, except that this one is available only
172 when the option USER_TOKEN_MANAGER is true.
173 </li>
174 <li>
175 <strong>TheParserTokenManager.TheParserTokenManager(CharStream stream)</strong>
176 <br />
177 Creates a new token manager object initialized to read input from "stream". When
178 the option STATIC is true, this constructor may be called only once.
179 This is available only when USER_TOKEN_MANAGER is false and USER_CHAR_STREAM
180 is true. When USER_TOKEN_MANAGER is false and USER_CHAR_STREAM is false (the default situation),
181 a constructor similar to the one above is available with the type CharStream
182 replaced as follows:
183 <ul>
184 <li>
185 When JAVA_UNICODE_ESCAPE is false and UNICODE_INPUT is false, CharStream is
186 replaced by ASCII_CharStream.
187 </li>
188 <li>
189 When JAVA_UNICODE_ESCAPE is false and UNICODE_INPUT is true, CharStream is
190 replaced by UCode_CharStream.
191 </li>
192 <li>
193 When JAVA_UNICODE_ESCAPE is true and UNICODE_INPUT is false, CharStream is
194 replaced by ASCII_UCodeESC_CharStream.
195 </li>
196 <li>
197 When JAVA_UNICODE_ESCAPE is true and UNICODE_INPUT is true, CharStream is
198 replaced by UCode_UCodeESC_CharStream.
199 </li>
200 </ul>
201 </li>
202 <li>
203 <strong>void TheParserTokenManager.ReInit(CharStream stream)</strong>
204 <br />
205 Reinitializes the current token manager object to read input from "stream". When
206 the option STATIC is true, this is the only way to restart a token manager operation.
207 This is available only when USER_TOKEN_MANAGER is false and USER_CHAR_STREAM
208 is true. When USER_TOKEN_MANAGER is false and USER_CHAR_STREAM is false (the default situation),
209 a constructor similar to the one above is available with the type CharStream
210 replaced as follows:
211 <ul>
212 <li>
213 When JAVA_UNICODE_ESCAPE is false and UNICODE_INPUT is false, CharStream is
214 replaced by ASCII_CharStream.
215 </li>
216 <li>
217 When JAVA_UNICODE_ESCAPE is false and UNICODE_INPUT is true, CharStream is
218 replaced by UCode_CharStream.
219 </li>
220 <li>
221 When JAVA_UNICODE_ESCAPE is true and UNICODE_INPUT is false, CharStream is
222 replaced by ASCII_UCodeESC_CharStream.
223 </li>
224 <li>
225 When JAVA_UNICODE_ESCAPE is true and UNICODE_INPUT is true, CharStream is
226 replaced by UCode_UCodeESC_CharStream.
227 </li>
228 </ul>
229 </li>
230 </ul>
231
232 <h3>The Token Class</h3>
233 <p>
234 The Token class is the type of token objects that are created by the token manager
235 after a successful scanning of the token stream. These token objects are then
236 passed to the parser and are accessible to the actions in a JavaCC grammar usually
237 by grabbing the return value of a token. The methods getToken and getNextToken
238 described below also give access to objects of this type.
239 </p>
240 <p>
241 Each Token object has the following fields and methods:
242 </p>
243 <ul>
244 <li>
245 <strong>int kind;</strong>
246 <br />
247 This is the index for this kind of token in the internal representation scheme
248 of JavaCC. When tokens in the JavaCC input file are given labels, these labels
249 are used to generate "int" constants that can be used in actions.
250 The value 0 is always used to represent the predefined token &lt;EOF&gt;. A
251 constant "EOF" is generated for convenience in the ...Constants file.
252 </li>
253 <li>
254 <strong>int beginLine, beginColumn, endLine, endColumn;</strong>
255 <br />
256 These indicate the beginning and ending positions of the token as it appeared
257 in the input stream.
258 </li>
259 <li>
260 <strong>String image;</strong>
261 <br />
262 This represents the image of the token as it appeared in the input stream.
263 </li>
264 <li>
265 <strong>Token next;</strong>
266 <br />
267 A reference to the next regular (non-special) token from the input
268 stream. If this is the last token from the input stream, or if the
269 token manager has not read tokens beyond this one, this field is
270 set to null.
271 <br />
272 <br />
273 The description in the above paragraph holds only if this token is also a regular
274 token. Otherwise, see below for a description of the contents of
275 this field.
276 <br />
277 <br />
278 Note: There are two kinds of tokens - regular and special. Regular tokens are
279 the normal tokens that are fed to the parser. Special tokens are other useful
280 tokens (like comments) that are not discarded (like white space). For more
281 information on the different kinds of tokens
282 <a href="tokenmanager.html">please see the minitutorial on the token manager</a>.
283 </li>
284 <li>
285 <strong>Token specialToken;</strong>
286 <br />
287 This field is used to access special tokens that occur prior to this
288 token, but after the immediately preceding regular (non-special) token.
289 If there are no such special tokens, this field is set to null.
290 When there are more than one such special token, this field refers
291 to the last of these special tokens, which in turn refers to the next
292 previous special token through its specialToken field, and so on
293 until the first special token (whose specialToken field is null).
294 The next fields of special tokens refer to other special tokens that
295 immediately follow it (without an intervening regular token). If there
296 is no such token, this field is null.
297 </li>
298
299 <li>
300 <strong>public Object getValue();</strong>
301 <br />
302 An optional attribute value of the Token.
303 <br />
304 Tokens which are not used as syntactic sugar will often contain
305 meaningful values that will be used later on by the compiler or
306 interpreter. This attribute value is often different from the image.
307 Any subclass of Token that actually wants to return a non-null value can
308 override this method as appropriate.
309 </li>
310
311 <li>
312 <strong>static final Token newToken(int ofKind);</strong>
313 <br />
314 <strong>static final Token newToken(int ofKind, String image);</strong>
315 <br />
316 Returns a new token object as its default behavior. If you wish to
317 perform special actions when a token is constructed or create subclasses
318 of class Token and instantiate them instead, you can redefine this method
319 appropriately. The only constraint is that this method returns a <em>new</em>
320 object of type Token (or a subclass of Token).
321 </li>
322 </ul>
323
324 <h3>Reading Tokens from the Input Stream</h3>
325 <p>
326 There are two methods available for this purpose:
327 </p>
328 <ul>
329 <li>
330 <strong><a name="getNextToken">Token TheParser.getNextToken() throws ParseError</a></strong>
331 <br />
332 This method returns the next available token in the input stream and moves
333 the token pointer one step in the input stream (i.e., this changes the state
334 of the input stream). If there are no more tokens available in the input
335 stream, the exception ParseError is thrown. Care must be taken when calling
336 this method since it can interfere with the parser's knowledge of the state
337 of the input stream, current token, etc.
338 </li>
339 <li>
340 <strong><a name="getToken">Token TheParser.getToken(int index) throws ParseError</a></strong>
341 <br />
342 This method returns the index-th token from the current token ahead in the
343 token stream. If index is 0, it returns the current token (the last token
344 returned by getNextToken or consumed by the parser); if index is 1, it returns
345 the next token (the next token that will be returned by getNextToken of consumed
346 by the parser) and so on. The index parameter cannot be negative. This method
347 does not change the input stream pointer (i.e., it does not change the
348 state of the input stream). If an attempt is made to access a token beyond the
349 last available token, the exception ParseError is thrown.
350 If this method is called from a semantic lookahead specification, which in turn
351 is called during a lookahead determination process, the current token is temporarily
352 adjusted to be the token currently being inspected by the lookahead process.
353 For more details,
354 <a href="lookahead.html">please see the minitutorial on using lookahead</a>.
355 </li>
356 </ul>
357
358 <h3>Working with Debugger Tracing</h3>
359 <p>
360 When you generate parsers with the options DEBUG_PARSER or DEBUG_LOOKAHEAD, these
361 parsers produce a trace of their activity which is printed to the user console.
362 You can insert calls to the following methods to control this tracing activity:
363 </p>
364 <ul>
365 <li>
366 <strong>void TheParser.enable_tracing()</strong>
367 </li>
368 <li>
369 <strong>void TheParser.disable_tracing()</strong>
370 </li>
371 </ul>
372 <p>
373 For convenience, these methods are available even when you build parsers without
374 the debug options. In this case, these methods are no-ops. Hence you can
375 permanently leave these methods in your code and they automatically kick in when
376 you use the debug options.
377 </p>
378
379 <h3>Customizing Error Messages</h3>
380 <p>
381 To help the user in customizing error messages generated by the parser and lexer, the
382 user is offered the facilities described in this section. In the case of the
383 parser, these facilities are only available if the option ERROR_REPORTING is
384 true, while in the case of the lexer, these facilities are always available.
385 </p>
386 <p>
387 The parser contains the following method definition:
388 </p>
389 <ul>
390 <li>
391 <strong>protected void token_error() { ... }</strong>
392 </li>
393 </ul>
394 To customize error reporting by the parser, the parser class must be subclassed
395 and this method redefined in the subclass. To help with creating your
396 error reporting scheme, the following variables are available:
397 <ul>
398 <li>
399 <strong>protected int error_line, error_column;</strong>
400 <br />
401 The line and column where the error was detected.
402 </li>
403 <li>
404 <strong>protected String error_string;</strong>
405 <br />
406 The image of the offending token or set of tokens. When a lookahead of more
407 than 1 is used, more than one token may be present here.
408 </li>
409 <li>
410 <strong>protected String[] expected_tokens;</strong>
411 <br />
412 An array of images of legitimate token sequences. Here again, each legitimate
413 token sequence may be more than just one token when a lookahead of more than
414 1 is used.
415 </li>
416 </ul>
417 <p>
418 The lexer contains the following method definition:
419 </p>
420 <ul>
421 <li>
422 <strong>protected void LexicalError() { ... }</strong>
423 </li>
424 </ul>
425 <p>
426 To customize error reporting by the lexer, the lexer class must be subclassed
427 and this method redefined in the subclass. To help with creating your
428 error reporting scheme, the following variables are available:
429 </p>
430 <ul>
431 <li>
432 <strong>protected int error_line, error_column;</strong>
433 <br />
434 The line and column where the error was detected.
435 </li>
436 <li>
437 <strong>protected String error_after;</strong>
438 <br />
439 The partial string that has been read since the last successful token
440 match was performed.
441 </li>
442 <li>
443 <strong>protected char curChar;</strong>
444 <br />
445 The offending character.
446 </li>
447 </ul>
448
449 <hr />
450
451 <h1>JavaCC [tm]: JJTree</h1>
452
453 <p>JJTree has two APIs: it adds some <a href="#jjtree-state">parser
454 methods</a>; and it requires all node objects to implement the <a
455 href="#jjtree-node">Node</a> interface.</p>
456
457 <h2><a name="jjtree-state">JJTree parser methods</a></h2>
458
459 <p>JJTree maintains some state in the parser object itself. It
460 encapsulates all this state with an object that can be referred to via
461 the <code>jjtree</code> field.</p>
462
463 <p>The parser state implements an open stack where nodes are held
464 until they can be added to their parent node. The <code>jjtree</code>
465 state object provides methods for you to manipulate the contents of
466 the stack in your actions if the basic JJTree mechanisms are not
467 sufficient.
468 </p>
469
470 <dl>
471 <dt><strong><code>void reset()</code></strong></dt>
472
473 <dd>Call this to reinitialize the node stack. All nodes
474 currently on the stack are thrown away. Don't call this from
475 within a node scope, or terrible things will surely happen.</dd>
476
477 <dt><strong><code>Node rootNode();</code></strong></dt>
478
479 <dd>Returns the root node of the AST. Since JJTree operates
480 bottom-up, the root node is only defined after the parse has
481 finished.</dd>
482
483 <dt><strong><code>boolean nodeCreated();</code></strong></dt>
484
485 <dd>Determines whether the current node was actually closed and
486 pushed. Call this in the final action within a conditional node
487 scope.</dd>
488
489 <dt><strong><code>int arity();</code></strong></dt>
490
491 <dd>Returns the number of nodes currently pushed on the node
492 stack in the current node scope.</dd>
493
494 <dt><strong><code>void pushNode(Node n);</code></strong></dt>
495
496 <dd>Pushes a node on to the stack.</dd>
497
498 <dt><strong><code>Node popNode();</code></strong></dt>
499
500 <dd>Returns the node on the top of the stack, and removes it from
501 the stack.</dd>
502
503 <dt><strong><code>Node peekNode();</code></strong></dt>
504
505 <dd>Returns the node currently on the top of the stack.</dd>
506 </dl>
507
508 <hr />
509
510 <h2>The <strong><code><a name="jjtree-node">Node</a></code></strong> interface</h2>
511
512 <p>
513 All AST nodes must implement this interface. It provides basic
514 machinery for constructing the parent and child relationships between
515 nodes.
516 </p>
517
518 <dl>
519 <dt><strong><code>public void jjtOpen();</code></strong></dt>
520
521 <dd>This method is called after the node has been made the
522 current node. It indicates that child nodes can now be added to
523 it.</dd>
524
525 <dt><strong><code>public void jjtClose();</code></strong></dt>
526
527 <dd>This method is called after all the child nodes have been
528 added.</dd>
529
530 <dt><strong><code>public void jjtSetParent(Node n);</code><br /><code>public Node jjtGetParent();</code></strong></dt>
531
532 <dd>This pair of methods is used to inform the node of its
533 parent.</dd>
534
535 <dt><strong><code>public void jjtAddChild(Node n, int i);</code></strong></dt>
536
537 <dd>This method tells the node to add its argument to the node's
538 list of children.</dd>
539
540 <dt><strong><code>public Node jjtGetChild(int i);</code></strong></dt>
541
542 <dd>This method returns a child node. The children are numbered
543 from zero, left to right.</dd>
544
545 <dt><strong><code>int jjtGetNumChildren();</code></strong></dt>
546
547 <dd>Return the number of children the node has.</dd>
548
549 </dl>
550
551 </body>
552 </html>