PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/javacc-5.0/www/doc/apiroutines.html

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