/jEdit/trunk/org/gjt/sp/jedit/bsh/bsh.jjt

# · Unknown · 1380 lines · 1265 code · 115 blank · 0 comment · 0 complexity · 7ed2ad8865bc061031a5554945411af9 MD5 · raw file

  1. /*****************************************************************************
  2. * *
  3. * This file is part of the BeanShell Java Scripting distribution. *
  4. * Documentation and updates may be found at http://www.beanshell.org/ *
  5. * *
  6. * Sun Public License Notice: *
  7. * *
  8. * The contents of this file are subject to the Sun Public License Version *
  9. * 1.0 (the "License"); you may not use this file except in compliance with *
  10. * the License. A copy of the License is available at http://www.sun.com *
  11. * *
  12. * The Original Code is BeanShell. The Initial Developer of the Original *
  13. * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
  14. * (C) 2000. All Rights Reserved. *
  15. * *
  16. * GNU Public License Notice: *
  17. * *
  18. * Alternatively, the contents of this file may be used under the terms of *
  19. * the GNU Lesser General Public License (the "LGPL"), in which case the *
  20. * provisions of LGPL are applicable instead of those above. If you wish to *
  21. * allow use of your version of this file only under the terms of the LGPL *
  22. * and not to allow others to use your version of this file under the SPL, *
  23. * indicate your decision by deleting the provisions above and replace *
  24. * them with the notice and other provisions required by the LGPL. If you *
  25. * do not delete the provisions above, a recipient may use your version of *
  26. * this file under either the SPL or the LGPL. *
  27. * *
  28. * Patrick Niemeyer (pat@pat.net) *
  29. * Author of Learning Java, O'Reilly & Associates *
  30. * http://www.pat.net/~pat/ *
  31. * *
  32. *****************************************************************************/
  33. /*
  34. Notes:
  35. There is probably a lot of room for improvement in here.
  36. All of the syntactic lookaheads have been commented with:
  37. SYNTACTIC_LOOKAHEAD
  38. These are probably expensive and we may want to start weeding them out
  39. where possible.
  40. */
  41. options {
  42. JAVA_UNICODE_ESCAPE=true;
  43. STATIC=false;
  44. MULTI=true;
  45. NODE_DEFAULT_VOID=true;
  46. NODE_SCOPE_HOOK=true;
  47. NODE_PREFIX="BSH";
  48. /* Print grammar debugging info as we parse
  49. DEBUG_PARSER=true;
  50. */
  51. /* Print detailed lookahead debugging info
  52. DEBUG_LOOKAHEAD=true;
  53. */
  54. // Not sure exactly what this does
  55. ERROR_REPORTING=false;
  56. // This breaks something for interactive use on the command line,
  57. // but may be useful in non-interactive use.
  58. //CACHE_TOKENS=true;
  59. }
  60. PARSER_BEGIN(Parser)
  61. package org.gjt.sp.jedit.bsh;
  62. import java.io.*;
  63. import java.util.Vector;
  64. /**
  65. This is the BeanShell parser. It is used internally by the Interpreter
  66. class (which is probably what you are looking for). The parser knows
  67. only how to parse the structure of the language, it does not understand
  68. names, commands, etc.
  69. <p>
  70. You can use the Parser from the command line to do basic structural
  71. validation of BeanShell files without actually executing them. e.g.
  72. <code><pre>
  73. java bsh.Parser [ -p ] file [ file ] [ ... ]
  74. </pre></code>
  75. <p>
  76. The -p option causes the abstract syntax to be printed.
  77. <p>
  78. From code you'd use the Parser like this:
  79. <p
  80. <code><pre>
  81. Parser parser = new Parser(in);
  82. while( !(eof=parser.Line()) ) {
  83. SimpleNode node = parser.popNode();
  84. // use the node, etc. (See bsh.BSH* classes)
  85. }
  86. </pre></code>
  87. */
  88. public class Parser
  89. {
  90. boolean retainComments = false;
  91. public void setRetainComments( boolean b ) {
  92. retainComments = b;
  93. }
  94. void jjtreeOpenNodeScope(Node n) {
  95. ((SimpleNode)n).firstToken = getToken(1);
  96. }
  97. void jjtreeCloseNodeScope(Node n) {
  98. ((SimpleNode)n).lastToken = getToken(0);
  99. }
  100. /**
  101. Re-initialize the input stream and token source.
  102. */
  103. void reInitInput( Reader in ) {
  104. ReInit(in);
  105. }
  106. public SimpleNode popNode()
  107. {
  108. if ( jjtree.nodeArity() > 0) // number of child nodes
  109. return (SimpleNode)jjtree.popNode();
  110. else
  111. return null;
  112. }
  113. /**
  114. Explicitly re-initialize just the token reader.
  115. This seems to be necessary to avoid certain looping errors when
  116. reading bogus input. See Interpreter.
  117. */
  118. void reInitTokenInput( Reader in ) {
  119. jj_input_stream.ReInit( in,
  120. jj_input_stream.getEndLine(),
  121. jj_input_stream.getEndColumn() );
  122. }
  123. public static void main( String [] args )
  124. throws IOException, ParseException
  125. {
  126. boolean print = false;
  127. int i=0;
  128. if ( args[0].equals("-p") ) {
  129. i++;
  130. print=true;
  131. }
  132. for(; i< args.length; i++) {
  133. Reader in = new FileReader(args[i]);
  134. Parser parser = new Parser(in);
  135. parser.setRetainComments(true);
  136. while( !parser.Line()/*eof*/ )
  137. if ( print )
  138. System.out.println( parser.popNode() );
  139. }
  140. }
  141. /**
  142. Lookahead for the enhanced for statement.
  143. Expect "for" "(" and then see whether we hit ":" or a ";" first.
  144. */
  145. boolean isRegularForStatement()
  146. {
  147. int curTok = 1;
  148. Token tok;
  149. tok = getToken(curTok++);
  150. if ( tok.kind != FOR ) return false;
  151. tok = getToken(curTok++);
  152. if ( tok.kind != LPAREN ) return false;
  153. while (true)
  154. {
  155. tok = getToken(curTok++);
  156. switch (tok.kind) {
  157. case COLON:
  158. return false;
  159. case SEMICOLON:
  160. return true;
  161. case EOF:
  162. return false;
  163. }
  164. }
  165. }
  166. /**
  167. Generate a ParseException with the specified message, pointing to the
  168. current token.
  169. The auto-generated Parser.generateParseException() method does not
  170. provide line number info, therefore we do this.
  171. */
  172. ParseException createParseException( String message )
  173. {
  174. Token errortok = token;
  175. int line = errortok.beginLine, column = errortok.beginColumn;
  176. String mess = (errortok.kind == 0) ? tokenImage[0] : errortok.image;
  177. return new ParseException( "Parse error at line " + line
  178. + ", column " + column + " : " + message );
  179. }
  180. }
  181. PARSER_END(Parser)
  182. SKIP : /* WHITE SPACE */
  183. {
  184. " " | "\t" | "\r" | "\f"
  185. | "\n"
  186. | < NONPRINTABLE: (["\u0000"-"\u0020", "\u0080"-"\u00ff"])+ >
  187. }
  188. SPECIAL_TOKEN : /* COMMENTS */
  189. {
  190. /*
  191. SINGLE_LINE_COMMENT includes a hack to accept SLC at the end of a file
  192. with no terminanting linefeed. This is actually illegal according to
  193. spec, but comes up often enough to warrant it... (especially in eval()).
  194. */
  195. <SINGLE_LINE_COMMENT: "//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")? >
  196. | <HASH_BANG_COMMENT: "#!" (~["\n","\r"])* ("\n"|"\r"|"\r\n")>
  197. /* Moved FORMAL_COMMENT to a real token. Modified MULTI_LINE_COMMENT to not
  198. catch formal comments (require no star after star) */
  199. | <MULTI_LINE_COMMENT:
  200. "/*" (~["*"])+ "*" ("*" | (~["*","/"] (~["*"])* "*"))* "/">
  201. }
  202. TOKEN : /* RESERVED WORDS AND LITERALS */
  203. {
  204. < ABSTRACT : "abstract" >
  205. | < BOOLEAN: "boolean" >
  206. | < BREAK: "break" >
  207. | < CLASS: "class" >
  208. | < BYTE: "byte" >
  209. | < CASE: "case" >
  210. | < CATCH: "catch" >
  211. | < CHAR: "char" >
  212. | < CONST: "const" >
  213. | < CONTINUE: "continue" >
  214. | < _DEFAULT: "default" >
  215. | < DO: "do" >
  216. | < DOUBLE: "double" >
  217. | < ELSE: "else" >
  218. | < ENUM: "enum" >
  219. | < EXTENDS: "extends" >
  220. | < FALSE: "false" >
  221. | < FINAL: "final" >
  222. | < FINALLY: "finally" >
  223. | < FLOAT: "float" >
  224. | < FOR: "for" >
  225. | < GOTO: "goto" >
  226. | < IF: "if" >
  227. | < IMPLEMENTS: "implements" >
  228. | < IMPORT: "import" >
  229. | < INSTANCEOF: "instanceof" >
  230. | < INT: "int" >
  231. | < INTERFACE: "interface" >
  232. | < LONG: "long" >
  233. | < NATIVE: "native" >
  234. | < NEW: "new" >
  235. | < NULL: "null" >
  236. | < PACKAGE: "package" >
  237. | < PRIVATE: "private" >
  238. | < PROTECTED: "protected" >
  239. | < PUBLIC: "public" >
  240. | < RETURN: "return" >
  241. | < SHORT: "short" >
  242. | < STATIC: "static" >
  243. | < STRICTFP : "strictfp" >
  244. | < SWITCH: "switch" >
  245. | < SYNCHRONIZED: "synchronized" >
  246. | < TRANSIENT: "transient" >
  247. | < THROW: "throw" >
  248. | < THROWS: "throws" >
  249. | < TRUE: "true" >
  250. | < TRY: "try" >
  251. | < VOID: "void" >
  252. | < VOLATILE: "volatile" >
  253. | < WHILE: "while" >
  254. }
  255. TOKEN : /* LITERALS */
  256. {
  257. < INTEGER_LITERAL:
  258. <DECIMAL_LITERAL> (["l","L"])?
  259. | <HEX_LITERAL> (["l","L"])?
  260. | <OCTAL_LITERAL> (["l","L"])?
  261. >
  262. |
  263. < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
  264. |
  265. < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
  266. |
  267. < #OCTAL_LITERAL: "0" (["0"-"7"])* >
  268. |
  269. < FLOATING_POINT_LITERAL:
  270. (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
  271. | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
  272. | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
  273. | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
  274. >
  275. |
  276. < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
  277. |
  278. < CHARACTER_LITERAL:
  279. "'"
  280. ( (~["'","\\","\n","\r"])
  281. | ("\\"
  282. ( ["n","t","b","r","f","\\","'","\""]
  283. | ["0"-"7"] ( ["0"-"7"] )?
  284. | ["0"-"3"] ["0"-"7"] ["0"-"7"]
  285. )
  286. )
  287. )
  288. "'"
  289. >
  290. |
  291. < STRING_LITERAL:
  292. "\""
  293. ( (~["\"","\\","\n","\r"])
  294. | ("\\"
  295. ( ["n","t","b","r","f","\\","'","\""]
  296. | ["0"-"7"] ( ["0"-"7"] )?
  297. | ["0"-"3"] ["0"-"7"] ["0"-"7"]
  298. )
  299. )
  300. )*
  301. "\""
  302. >
  303. |
  304. < FORMAL_COMMENT:
  305. "/**" (~["*"])* "*" ("*" | (~["*","/"] (~["*"])* "*"))* "/"
  306. >
  307. }
  308. TOKEN : /* IDENTIFIERS */
  309. {
  310. < IDENTIFIER: <LETTER> (<LETTER>|<DIGIT>)* >
  311. |
  312. < #LETTER:
  313. [
  314. "\u0024",
  315. "\u0041"-"\u005a",
  316. "\u005f",
  317. "\u0061"-"\u007a",
  318. "\u00c0"-"\u00d6",
  319. "\u00d8"-"\u00f6",
  320. "\u00f8"-"\u00ff",
  321. "\u0100"-"\u1fff",
  322. "\u3040"-"\u318f",
  323. "\u3300"-"\u337f",
  324. "\u3400"-"\u3d2d",
  325. "\u4e00"-"\u9fff",
  326. "\uf900"-"\ufaff"
  327. ]
  328. >
  329. |
  330. < #DIGIT:
  331. [
  332. "\u0030"-"\u0039",
  333. "\u0660"-"\u0669",
  334. "\u06f0"-"\u06f9",
  335. "\u0966"-"\u096f",
  336. "\u09e6"-"\u09ef",
  337. "\u0a66"-"\u0a6f",
  338. "\u0ae6"-"\u0aef",
  339. "\u0b66"-"\u0b6f",
  340. "\u0be7"-"\u0bef",
  341. "\u0c66"-"\u0c6f",
  342. "\u0ce6"-"\u0cef",
  343. "\u0d66"-"\u0d6f",
  344. "\u0e50"-"\u0e59",
  345. "\u0ed0"-"\u0ed9",
  346. "\u1040"-"\u1049"
  347. ]
  348. >
  349. }
  350. TOKEN : /* SEPARATORS */
  351. {
  352. < LPAREN: "(" >
  353. | < RPAREN: ")" >
  354. | < LBRACE: "{" >
  355. | < RBRACE: "}" >
  356. | < LBRACKET: "[" >
  357. | < RBRACKET: "]" >
  358. | < SEMICOLON: ";" >
  359. | < COMMA: "," >
  360. | < DOT: "." >
  361. }
  362. TOKEN : /* OPERATORS */
  363. {
  364. < ASSIGN: "=" >
  365. | < GT: ">" >
  366. | < GTX: "@gt" >
  367. | < LT: "<" >
  368. | < LTX: "@lt" >
  369. | < BANG: "!" >
  370. | < TILDE: "~" >
  371. | < HOOK: "?" >
  372. | < COLON: ":" >
  373. | < EQ: "==" >
  374. | < LE: "<=" >
  375. | < LEX: "@lteq" >
  376. | < GE: ">=" >
  377. | < GEX: "@gteq" >
  378. | < NE: "!=" >
  379. | < BOOL_OR: "||" >
  380. | < BOOL_ORX: "@or" >
  381. | < BOOL_AND: "&&" >
  382. | < BOOL_ANDX: "@and" >
  383. | < INCR: "++" >
  384. | < DECR: "--" >
  385. | < PLUS: "+" >
  386. | < MINUS: "-" >
  387. | < STAR: "*" >
  388. | < SLASH: "/" >
  389. | < BIT_AND: "&" >
  390. | < BIT_ANDX: "@bitwise_and" >
  391. | < BIT_OR: "|" >
  392. | < BIT_ORX: "@bitwise_or" >
  393. | < XOR: "^" >
  394. | < MOD: "%" >
  395. | < LSHIFT: "<<" >
  396. | < LSHIFTX: "@left_shift" >
  397. | < RSIGNEDSHIFT: ">>" >
  398. | < RSIGNEDSHIFTX: "@right_shift" >
  399. | < RUNSIGNEDSHIFT: ">>>" >
  400. | < RUNSIGNEDSHIFTX: "@right_unsigned_shift" >
  401. | < PLUSASSIGN: "+=" >
  402. | < MINUSASSIGN: "-=" >
  403. | < STARASSIGN: "*=" >
  404. | < SLASHASSIGN: "/=" >
  405. | < ANDASSIGN: "&=" >
  406. | < ANDASSIGNX: "@and_assign" >
  407. | < ORASSIGN: "|=" >
  408. | < ORASSIGNX: "@or_assign" >
  409. | < XORASSIGN: "^=" >
  410. | < MODASSIGN: "%=" >
  411. | < LSHIFTASSIGN: "<<=" >
  412. | < LSHIFTASSIGNX: "@left_shift_assign" >
  413. | < RSIGNEDSHIFTASSIGN: ">>=" >
  414. | < RSIGNEDSHIFTASSIGNX: "@right_shift_assign" >
  415. | < RUNSIGNEDSHIFTASSIGN: ">>>=" >
  416. | < RUNSIGNEDSHIFTASSIGNX: "@right_unsigned_shift_assign" >
  417. }
  418. /*
  419. Thanks to Sreenivasa Viswanadha for suggesting how to get rid of expensive
  420. lookahead here.
  421. */
  422. boolean Line() :
  423. {}
  424. {
  425. <EOF> {
  426. Interpreter.debug("End of File!");
  427. return true;
  428. }
  429. |
  430. BlockStatement() {
  431. return false;
  432. }
  433. }
  434. /*****************************************
  435. * THE JAVA LANGUAGE GRAMMAR STARTS HERE *
  436. *****************************************/
  437. /*
  438. Gather modifiers for a class, method, or field.
  439. I lookahead is true then we are being called as part of a lookahead and we
  440. should not enforce any rules. Otherwise we validate based on context
  441. (field, method, class)
  442. */
  443. Modifiers Modifiers( int context, boolean lookahead ) :
  444. {
  445. Modifiers mods = null;
  446. }
  447. {
  448. (
  449. (
  450. "private" | "protected" | "public" | "synchronized" | "final"
  451. | "native" | "transient" | "volatile" | "abstract" | "static"
  452. | "strictfp"
  453. ) {
  454. if ( !lookahead )
  455. try {
  456. if ( mods == null ) mods = new Modifiers();
  457. mods.addModifier( context, getToken(0).image );
  458. } catch ( IllegalStateException e ) {
  459. throw createParseException( e.getMessage() );
  460. }
  461. }
  462. )* {
  463. return mods;
  464. }
  465. }
  466. /**
  467. */
  468. void ClassDeclaration() #ClassDeclaration :
  469. {
  470. Modifiers mods;
  471. Token name;
  472. int numInterfaces;
  473. }
  474. {
  475. mods = Modifiers( Modifiers.CLASS, false )
  476. ( "class" | "interface" { jjtThis.isInterface=true; } )
  477. name=<IDENTIFIER>
  478. [ "extends" AmbiguousName() { jjtThis.extend = true; } ]
  479. [ "implements" numInterfaces=NameList()
  480. { jjtThis.numInterfaces=numInterfaces; } ]
  481. Block()
  482. {
  483. jjtThis.modifiers = mods;
  484. jjtThis.name = name.image;
  485. }
  486. }
  487. void MethodDeclaration() #MethodDeclaration :
  488. {
  489. Token t = null;
  490. Modifiers mods;
  491. int count;
  492. }
  493. {
  494. mods = Modifiers( Modifiers.METHOD, false ) { jjtThis.modifiers = mods; }
  495. (
  496. LOOKAHEAD( <IDENTIFIER> "(" )
  497. t = <IDENTIFIER> { jjtThis.name = t.image; }
  498. |
  499. ReturnType()
  500. t = <IDENTIFIER> { jjtThis.name = t.image; }
  501. )
  502. FormalParameters()
  503. [ "throws" count=NameList() { jjtThis.numThrows=count; } ]
  504. ( Block() | ";" )
  505. }
  506. void PackageDeclaration () #PackageDeclaration:
  507. { }
  508. {
  509. "package" AmbiguousName()
  510. }
  511. void ImportDeclaration() #ImportDeclaration :
  512. {
  513. Token s = null;
  514. Token t = null;
  515. }
  516. {
  517. LOOKAHEAD( 3 )
  518. [ s = "static" ] "import" AmbiguousName() [ t = "." "*" ] ";"
  519. {
  520. if ( s != null ) jjtThis.staticImport = true;
  521. if ( t != null ) jjtThis.importPackage = true;
  522. }
  523. |
  524. // bsh super import statement
  525. "import" "*" ";" {
  526. jjtThis.superImport = true;
  527. }
  528. }
  529. void VariableDeclarator() #VariableDeclarator :
  530. {
  531. Token t;
  532. }
  533. {
  534. t=<IDENTIFIER> [ "=" VariableInitializer() ]
  535. {
  536. jjtThis.name = t.image;
  537. }
  538. }
  539. /*
  540. this originally handled postfix array dimensions...
  541. void VariableDeclaratorId() #VariableDeclaratorId :
  542. { Token t; }
  543. {
  544. t=<IDENTIFIER> { jjtThis.name = t.image; }
  545. ( "[" "]" { jjtThis.addUndefinedDimension(); } )*
  546. }
  547. */
  548. void VariableInitializer() :
  549. {}
  550. {
  551. ArrayInitializer()
  552. |
  553. Expression()
  554. }
  555. void ArrayInitializer() #ArrayInitializer :
  556. {}
  557. {
  558. "{" [ VariableInitializer()
  559. ( LOOKAHEAD(2) "," VariableInitializer() )* ] [ "," ] "}"
  560. }
  561. void FormalParameters() #FormalParameters :
  562. {}
  563. {
  564. "(" [ FormalParameter() ( "," FormalParameter() )* ] ")"
  565. }
  566. void FormalParameter() #FormalParameter :
  567. { Token t; }
  568. {
  569. // added [] to Type for bsh. Removed [ final ] - is that legal?
  570. LOOKAHEAD(2) Type() t=<IDENTIFIER> { jjtThis.name = t.image; }
  571. |
  572. t=<IDENTIFIER> { jjtThis.name = t.image; }
  573. }
  574. /*
  575. Type, name and expression syntax follows.
  576. */
  577. void Type() #Type :
  578. { }
  579. {
  580. /*
  581. The embedded lookahead is (was?) necessary to disambiguate for
  582. PrimaryPrefix. ( )* is a choice point. It took me a while to
  583. figure out where to put that. This stuff is annoying.
  584. */
  585. ( PrimitiveType() | AmbiguousName() )
  586. ( LOOKAHEAD(2) "[" "]" { jjtThis.addArrayDimension(); } )*
  587. }
  588. /*
  589. Originally called ResultType in the grammar
  590. */
  591. void ReturnType() #ReturnType :
  592. { }
  593. {
  594. "void" { jjtThis.isVoid = true; }
  595. |
  596. Type()
  597. }
  598. void PrimitiveType() #PrimitiveType :
  599. { } {
  600. "boolean" { jjtThis.type = Boolean.TYPE; }
  601. | "char" { jjtThis.type = Character.TYPE; }
  602. | "byte" { jjtThis.type = Byte.TYPE; }
  603. | "short" { jjtThis.type = Short.TYPE; }
  604. | "int" { jjtThis.type = Integer.TYPE; }
  605. | "long" { jjtThis.type = Long.TYPE; }
  606. | "float" { jjtThis.type = Float.TYPE; }
  607. | "double" { jjtThis.type = Double.TYPE; }
  608. }
  609. void AmbiguousName() #AmbiguousName :
  610. /*
  611. A lookahead of 2 is required below since "Name" can be followed by a ".*"
  612. when used in the context of an "ImportDeclaration".
  613. */
  614. {
  615. Token t;
  616. StringBuffer s;
  617. }
  618. {
  619. t = <IDENTIFIER> {
  620. s = new StringBuffer(t.image);
  621. }
  622. ( LOOKAHEAD(2) "." t = <IDENTIFIER> { s.append("."+t.image); } )* {
  623. jjtThis.text = s.toString();
  624. }
  625. }
  626. int NameList() :
  627. { int count = 0; }
  628. {
  629. AmbiguousName() { ++count; } ( "," AmbiguousName() { ++count; } )*
  630. { return count; }
  631. }
  632. /*
  633. * Expression syntax follows.
  634. */
  635. void Expression() :
  636. { }
  637. {
  638. /**
  639. SYNTACTIC_LOOKAHEAD
  640. Note: the original grammar was cheating here and we've fixed that,
  641. but at the expense of another syntactic lookahead.
  642. */
  643. LOOKAHEAD( PrimaryExpression() AssignmentOperator() )
  644. Assignment()
  645. |
  646. ConditionalExpression()
  647. }
  648. void Assignment() #Assignment :
  649. { int op ; }
  650. {
  651. PrimaryExpression()
  652. op = AssignmentOperator() { jjtThis.operator = op; }
  653. // Add this for blocks, e.g. foo = { };
  654. //( Expression() | Block() )
  655. Expression()
  656. }
  657. int AssignmentOperator() :
  658. { Token t; }
  659. {
  660. ( "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "&=" | "^=" | "|=" |
  661. "<<=" | "@left_shift_assign" | ">>=" | "@right_shift_assign" |
  662. ">>>=" | "@right_unsigned_shift_assign" )
  663. {
  664. t = getToken(0);
  665. return t.kind;
  666. }
  667. }
  668. void ConditionalExpression() :
  669. { }
  670. {
  671. ConditionalOrExpression() [ "?" Expression() ":" ConditionalExpression()
  672. #TernaryExpression(3) ]
  673. }
  674. void ConditionalOrExpression() :
  675. { Token t=null; }
  676. {
  677. ConditionalAndExpression()
  678. ( ( t = "||" | t = "@or" )
  679. ConditionalAndExpression()
  680. { jjtThis.kind = t.kind; } #BinaryExpression(2) )*
  681. }
  682. void ConditionalAndExpression() :
  683. { Token t=null; }
  684. {
  685. InclusiveOrExpression()
  686. ( ( t = "&&" | t = "@and" )
  687. InclusiveOrExpression()
  688. { jjtThis.kind = t.kind; } #BinaryExpression(2) )*
  689. }
  690. void InclusiveOrExpression() :
  691. { Token t=null; }
  692. {
  693. ExclusiveOrExpression()
  694. ( ( t = "|" | t = "@bitwise_or" )
  695. ExclusiveOrExpression()
  696. { jjtThis.kind = t.kind; } #BinaryExpression(2) )*
  697. }
  698. void ExclusiveOrExpression() :
  699. { Token t=null; }
  700. {
  701. AndExpression() ( t="^" AndExpression()
  702. { jjtThis.kind = t.kind; } #BinaryExpression(2) )*
  703. }
  704. void AndExpression() :
  705. { Token t=null; }
  706. {
  707. EqualityExpression()
  708. ( ( t = "&" | t = "@bitwise_and" )
  709. EqualityExpression()
  710. { jjtThis.kind = t.kind; } #BinaryExpression(2) )*
  711. }
  712. void EqualityExpression() :
  713. { Token t = null; }
  714. {
  715. InstanceOfExpression() ( ( t= "==" | t= "!=" ) InstanceOfExpression()
  716. { jjtThis.kind = t.kind; } #BinaryExpression(2)
  717. )*
  718. }
  719. void InstanceOfExpression() :
  720. { Token t = null; }
  721. {
  722. RelationalExpression()
  723. [ t = "instanceof" Type() { jjtThis.kind = t.kind; } #BinaryExpression(2) ]
  724. }
  725. void RelationalExpression() :
  726. { Token t = null; }
  727. {
  728. ShiftExpression()
  729. ( ( t = "<" | t = "@lt" | t = ">" | t = "@gt" |
  730. t = "<=" | t = "@lteq" | t = ">=" | t = "@gteq" )
  731. ShiftExpression()
  732. { jjtThis.kind = t.kind; } #BinaryExpression(2) )*
  733. }
  734. void ShiftExpression() :
  735. { Token t = null; }
  736. {
  737. AdditiveExpression()
  738. ( ( t = "<<" | t = "@left_shift" | t = ">>" | t = "@right_shift" |
  739. t = ">>>" | t = "@right_unsigned_shift" )
  740. AdditiveExpression()
  741. { jjtThis.kind = t.kind; } #BinaryExpression(2) )*
  742. }
  743. void AdditiveExpression() :
  744. { Token t = null; }
  745. {
  746. MultiplicativeExpression()
  747. ( ( t= "+" | t= "-" ) MultiplicativeExpression() { jjtThis.kind = t.kind; }
  748. #BinaryExpression(2)
  749. )*
  750. }
  751. void MultiplicativeExpression() :
  752. { Token t = null; }
  753. {
  754. UnaryExpression() ( ( t= "*" | t= "/" | t= "%" )
  755. UnaryExpression() { jjtThis.kind = t.kind; } #BinaryExpression(2) )*
  756. }
  757. void UnaryExpression() :
  758. { Token t = null; }
  759. {
  760. ( t="+" | t="-" ) UnaryExpression()
  761. { jjtThis.kind = t.kind; } #UnaryExpression(1)
  762. |
  763. PreIncrementExpression()
  764. |
  765. PreDecrementExpression()
  766. |
  767. UnaryExpressionNotPlusMinus()
  768. }
  769. void PreIncrementExpression() :
  770. { Token t = null; }
  771. {
  772. t="++" PrimaryExpression()
  773. { jjtThis.kind = t.kind; } #UnaryExpression(1)
  774. }
  775. void PreDecrementExpression() :
  776. { Token t = null; }
  777. {
  778. t="--" PrimaryExpression()
  779. { jjtThis.kind = t.kind; } #UnaryExpression(1)
  780. }
  781. void UnaryExpressionNotPlusMinus() :
  782. { Token t = null; }
  783. {
  784. ( t="~" | t="!" ) UnaryExpression()
  785. { jjtThis.kind = t.kind; } #UnaryExpression(1)
  786. |
  787. // SYNTACTIC_LOOKAHEAD
  788. LOOKAHEAD( CastLookahead() ) CastExpression()
  789. |
  790. PostfixExpression()
  791. }
  792. // This production is to determine lookahead only.
  793. void CastLookahead() : { }
  794. {
  795. LOOKAHEAD(2) "(" PrimitiveType()
  796. |
  797. // SYNTACTIC_LOOKAHEAD
  798. LOOKAHEAD( "(" AmbiguousName() "[" ) "(" AmbiguousName() "[" "]"
  799. |
  800. "(" AmbiguousName() ")" ( "~" | "!" | "(" | <IDENTIFIER> | /* "this" | "super" | */ "new" | Literal() )
  801. }
  802. void PostfixExpression() :
  803. { Token t = null; }
  804. {
  805. // SYNTACTIC_LOOKAHEAD
  806. LOOKAHEAD( PrimaryExpression() ("++"|"--") )
  807. PrimaryExpression()
  808. ( t="++" | t="--" ) {
  809. jjtThis.kind = t.kind; jjtThis.postfix = true; } #UnaryExpression(1)
  810. |
  811. PrimaryExpression()
  812. }
  813. void CastExpression() #CastExpression :
  814. { }
  815. {
  816. // SYNTACTIC_LOOKAHEAD
  817. LOOKAHEAD( "(" PrimitiveType() ) "(" Type() ")" UnaryExpression()
  818. |
  819. "(" Type() ")" UnaryExpressionNotPlusMinus()
  820. }
  821. void PrimaryExpression() #PrimaryExpression : { }
  822. {
  823. PrimaryPrefix() ( PrimarySuffix() )*
  824. }
  825. void MethodInvocation() #MethodInvocation : { }
  826. {
  827. AmbiguousName() Arguments()
  828. }
  829. void PrimaryPrefix() : { }
  830. {
  831. Literal()
  832. |
  833. "(" Expression() ")"
  834. |
  835. AllocationExpression()
  836. |
  837. // SYNTACTIC_LOOKAHEAD
  838. LOOKAHEAD( MethodInvocation() )
  839. MethodInvocation()
  840. |
  841. LOOKAHEAD( Type() "." "class" )
  842. Type()
  843. |
  844. AmbiguousName()
  845. /*
  846. |
  847. LOOKAHEAD( "void" "." "class" )
  848. */
  849. }
  850. void PrimarySuffix() #PrimarySuffix :
  851. {
  852. Token t = null;
  853. }
  854. {
  855. LOOKAHEAD(2)
  856. "." "class" {
  857. jjtThis.operation = BSHPrimarySuffix.CLASS;
  858. }
  859. |
  860. "[" Expression() "]" {
  861. jjtThis.operation = BSHPrimarySuffix.INDEX;
  862. }
  863. |
  864. // Field access or method invocation
  865. "." t = <IDENTIFIER> [ Arguments() ] {
  866. jjtThis.operation = BSHPrimarySuffix.NAME;
  867. jjtThis.field = t.image;
  868. }
  869. |
  870. "{" Expression() "}" {
  871. jjtThis.operation = BSHPrimarySuffix.PROPERTY;
  872. }
  873. /*
  874. For inner classes
  875. |
  876. LOOKAHEAD(2)
  877. "." AllocationExpression()
  878. */
  879. }
  880. void Literal() #Literal :
  881. {
  882. Token x;
  883. boolean b;
  884. String literal;
  885. char ch;
  886. }
  887. {
  888. x = <INTEGER_LITERAL>
  889. {
  890. literal = x.image;
  891. ch = literal.charAt(literal.length()-1);
  892. if(ch == 'l' || ch == 'L')
  893. {
  894. literal = literal.substring(0,literal.length()-1);
  895. // This really should be Long.decode, but there isn't one. As a result,
  896. // hex and octal literals ending in 'l' or 'L' don't work.
  897. jjtThis.value = new Primitive( new Long( literal ).longValue() );
  898. }
  899. else
  900. try {
  901. jjtThis.value = new Primitive(
  902. Integer.decode( literal ).intValue() );
  903. } catch ( NumberFormatException e ) {
  904. throw createParseException(
  905. "Error or number too big for integer type: "+ literal );
  906. }
  907. }
  908. |
  909. x = <FLOATING_POINT_LITERAL>
  910. {
  911. literal = x.image;
  912. ch = literal.charAt(literal.length()-1);
  913. if(ch == 'f' || ch == 'F')
  914. {
  915. literal = literal.substring(0,literal.length()-1);
  916. jjtThis.value = new Primitive( new Float( literal ).floatValue() );
  917. }
  918. else
  919. {
  920. if(ch == 'd' || ch == 'D')
  921. literal = literal.substring(0,literal.length()-1);
  922. jjtThis.value = new Primitive( new Double( literal ).doubleValue() );
  923. }
  924. }
  925. |
  926. x = <CHARACTER_LITERAL> {
  927. try {
  928. jjtThis.charSetup( x.image.substring(1, x.image.length() - 1) );
  929. } catch ( Exception e ) {
  930. throw createParseException("Error parsing character: "+x.image);
  931. }
  932. }
  933. |
  934. x = <STRING_LITERAL> {
  935. try {
  936. jjtThis.stringSetup( x.image.substring(1, x.image.length() - 1) );
  937. } catch ( Exception e ) {
  938. throw createParseException("Error parsing string: "+x.image);
  939. }
  940. }
  941. |
  942. b = BooleanLiteral() {
  943. jjtThis.value = new Primitive( b ); }
  944. |
  945. NullLiteral() {
  946. jjtThis.value = Primitive.NULL;
  947. }
  948. |
  949. VoidLiteral() {
  950. jjtThis.value = Primitive.VOID; }
  951. }
  952. boolean BooleanLiteral() :
  953. {}
  954. {
  955. "true" { return true; }
  956. |
  957. "false" { return false; }
  958. }
  959. void NullLiteral() :
  960. {}
  961. {
  962. "null"
  963. }
  964. void VoidLiteral() :
  965. {}
  966. {
  967. "void"
  968. }
  969. void Arguments() #Arguments :
  970. { }
  971. {
  972. "(" [ ArgumentList() ] ")"
  973. }
  974. // leave these on the stack for Arguments() to handle
  975. void ArgumentList() :
  976. { }
  977. {
  978. Expression()
  979. ( "," Expression() )*
  980. }
  981. void AllocationExpression() #AllocationExpression :
  982. { }
  983. {
  984. LOOKAHEAD(2)
  985. "new" PrimitiveType() ArrayDimensions()
  986. |
  987. "new" AmbiguousName()
  988. (
  989. ArrayDimensions()
  990. |
  991. // SYNTACTIC_LOOKAHEAD
  992. Arguments() [ LOOKAHEAD(2) Block() ]
  993. )
  994. }
  995. void ArrayDimensions() #ArrayDimensions :
  996. {}
  997. {
  998. // e.g. int [4][3][][];
  999. LOOKAHEAD(2)
  1000. ( LOOKAHEAD(2) "[" Expression() "]" { jjtThis.addDefinedDimension(); } )+
  1001. ( LOOKAHEAD(2) "[" "]" { jjtThis.addUndefinedDimension(); } )*
  1002. |
  1003. // e.g. int [][] { {1,2}, {3,4} };
  1004. ( "[" "]" { jjtThis.addUndefinedDimension(); } )+ ArrayInitializer()
  1005. }
  1006. /*
  1007. * Statement syntax follows.
  1008. */
  1009. void Statement() : { }
  1010. {
  1011. LOOKAHEAD(2)
  1012. LabeledStatement()
  1013. |
  1014. Block()
  1015. |
  1016. EmptyStatement()
  1017. |
  1018. StatementExpression() ";"
  1019. |
  1020. SwitchStatement()
  1021. |
  1022. IfStatement()
  1023. |
  1024. WhileStatement()
  1025. |
  1026. DoStatement()
  1027. |
  1028. LOOKAHEAD ( { isRegularForStatement() } )
  1029. ForStatement()
  1030. |
  1031. EnhancedForStatement()
  1032. |
  1033. BreakStatement()
  1034. |
  1035. ContinueStatement()
  1036. |
  1037. ReturnStatement()
  1038. |
  1039. SynchronizedStatement()
  1040. |
  1041. ThrowStatement()
  1042. |
  1043. TryStatement()
  1044. }
  1045. void LabeledStatement() :
  1046. {}
  1047. {
  1048. <IDENTIFIER> ":" Statement()
  1049. }
  1050. void Block() #Block :
  1051. {}
  1052. {
  1053. "{" ( BlockStatement() )* "}"
  1054. }
  1055. void BlockStatement() :
  1056. {
  1057. }
  1058. {
  1059. LOOKAHEAD( Modifiers( Modifiers.FIELD, true ) ( "class" | "interface" ) )
  1060. ClassDeclaration()
  1061. |
  1062. LOOKAHEAD ( Modifiers( Modifiers.METHOD, true )
  1063. ReturnType() <IDENTIFIER> "("
  1064. )
  1065. MethodDeclaration()
  1066. |
  1067. LOOKAHEAD ( Modifiers( Modifiers.METHOD, true )
  1068. <IDENTIFIER> FormalParameters() [ "throws" NameList() ] "{"
  1069. )
  1070. MethodDeclaration()
  1071. |
  1072. // SYNTACTIC_LOOKAHEAD
  1073. LOOKAHEAD( Modifiers( Modifiers.FIELD, true ) Type() <IDENTIFIER> )
  1074. TypedVariableDeclaration() ";"
  1075. |
  1076. Statement()
  1077. |
  1078. // Allow BeanShell imports in any block
  1079. ImportDeclaration()
  1080. |
  1081. // Allow BeanShell package declarations in any block
  1082. PackageDeclaration()
  1083. |
  1084. FormalComment()
  1085. }
  1086. void FormalComment() #FormalComment( retainComments ) :
  1087. {
  1088. Token t;
  1089. }
  1090. {
  1091. t=<FORMAL_COMMENT> {
  1092. jjtThis.text=t.image;
  1093. }
  1094. }
  1095. void EmptyStatement() :
  1096. {}
  1097. {
  1098. ";"
  1099. }
  1100. void StatementExpression() :
  1101. { }
  1102. {
  1103. /*
  1104. This is looser than normal Java to simplify the grammar. This allows
  1105. us to type arbitrary expressions on the command line, e.g. "1+1;"
  1106. We should turn this off in the implementation in strict java mode.
  1107. */
  1108. Expression()
  1109. /*
  1110. // This was the original Java grammar.
  1111. // Original comment:
  1112. // The last expansion of this production accepts more than the legal
  1113. // Java expansions for StatementExpression.
  1114. PreIncrementExpression()
  1115. |
  1116. PreDecrementExpression()
  1117. |
  1118. // SYNTACTIC_LOOKAHEAD
  1119. LOOKAHEAD( PrimaryExpression() AssignmentOperator() )
  1120. Assignment() { }
  1121. |
  1122. PostfixExpression()
  1123. */
  1124. }
  1125. void SwitchStatement() #SwitchStatement :
  1126. {}
  1127. {
  1128. "switch" "(" Expression() ")" "{"
  1129. ( SwitchLabel() ( BlockStatement() )* )*
  1130. "}"
  1131. }
  1132. void SwitchLabel() #SwitchLabel :
  1133. {}
  1134. {
  1135. "case" Expression() ":"
  1136. |
  1137. "default" ":" { jjtThis.isDefault = true; }
  1138. }
  1139. void IfStatement() #IfStatement :
  1140. /*
  1141. * The disambiguating algorithm of JavaCC automatically binds dangling
  1142. * else's to the innermost if statement. The LOOKAHEAD specification
  1143. * is to tell JavaCC that we know what we are doing.
  1144. */
  1145. {}
  1146. {
  1147. "if" "(" Expression() ")" Statement() [ LOOKAHEAD(1) "else" Statement() ]
  1148. }
  1149. void WhileStatement() #WhileStatement :
  1150. {}
  1151. {
  1152. "while" "(" Expression() ")" Statement()
  1153. }
  1154. /*
  1155. Do statement is just a While statement with a special hook to execute
  1156. at least once.
  1157. */
  1158. void DoStatement() #WhileStatement :
  1159. {}
  1160. {
  1161. "do" Statement() "while" "(" Expression() ")" ";"
  1162. { jjtThis.isDoStatement=true; }
  1163. }
  1164. void ForStatement() #ForStatement :
  1165. { Token t = null; }
  1166. {
  1167. "for" "(" [ ForInit() { jjtThis.hasForInit=true; } ]
  1168. ";" [ Expression() { jjtThis.hasExpression=true; } ]
  1169. ";" [ ForUpdate() { jjtThis.hasForUpdate=true; } ] ")"
  1170. Statement()
  1171. }
  1172. /*
  1173. The new JDK1.5 enhanced for statement.
  1174. e.g. for( int a : arrayOfInts ) { }
  1175. We also support loose typing of the iterator var for BeanShell
  1176. e.g. for( a : arrayOfInts ) { }
  1177. */
  1178. void EnhancedForStatement() #EnhancedForStatement :
  1179. { Token t = null; }
  1180. {
  1181. LOOKAHEAD( 4 ) // look ahead for the ":" before deciding
  1182. "for" "(" t=<IDENTIFIER> ":" Expression() ")"
  1183. Statement() { jjtThis.varName = t.image; }
  1184. |
  1185. "for" "(" Type() t=<IDENTIFIER> ":" Expression() ")"
  1186. Statement() { jjtThis.varName = t.image; }
  1187. }
  1188. void ForInit() :
  1189. { Token t = null; }
  1190. {
  1191. // SYNTACTIC_LOOKAHEAD
  1192. LOOKAHEAD( Modifiers( Modifiers.FIELD, true ) Type() <IDENTIFIER> )
  1193. TypedVariableDeclaration()
  1194. |
  1195. StatementExpressionList()
  1196. }
  1197. /**
  1198. Declared a typed variable.
  1199. Untyped variables are not declared per-se but are handled by the part
  1200. of the grammar that deals with assignments.
  1201. */
  1202. void TypedVariableDeclaration() #TypedVariableDeclaration :
  1203. {
  1204. Token t = null;
  1205. Modifiers mods;
  1206. }
  1207. {
  1208. mods = Modifiers( Modifiers.FIELD, false )
  1209. Type() VariableDeclarator() ( "," VariableDeclarator() )*
  1210. {
  1211. jjtThis.modifiers = mods;
  1212. }
  1213. }
  1214. void StatementExpressionList() #StatementExpressionList :
  1215. {}
  1216. {
  1217. StatementExpression() ( "," StatementExpression() )*
  1218. }
  1219. void ForUpdate() :
  1220. {}
  1221. {
  1222. StatementExpressionList()
  1223. }
  1224. void BreakStatement() #ReturnStatement :
  1225. {}
  1226. {
  1227. "break" [ <IDENTIFIER> ] ";" { jjtThis.kind = BREAK; }
  1228. }
  1229. void ContinueStatement() #ReturnStatement :
  1230. {}
  1231. {
  1232. "continue" [ <IDENTIFIER> ] ";" { jjtThis.kind = CONTINUE; }
  1233. }
  1234. void ReturnStatement() #ReturnStatement :
  1235. {}
  1236. {
  1237. "return" [ Expression() ] ";" { jjtThis.kind = RETURN; }
  1238. }
  1239. void SynchronizedStatement() #Block :
  1240. {
  1241. }
  1242. {
  1243. "synchronized" "(" Expression() ")" Block() {
  1244. jjtThis.isSynchronized=true;
  1245. }
  1246. }
  1247. void ThrowStatement() #ThrowStatement :
  1248. {}
  1249. {
  1250. "throw" Expression() ";"
  1251. }
  1252. void TryStatement() #TryStatement:
  1253. /*
  1254. Semantic check required here to make sure that at least one
  1255. finally/catch is present. (You can have a try with finally and no catch).
  1256. */
  1257. { boolean closed = false; }
  1258. {
  1259. "try" Block()
  1260. ( "catch" "(" FormalParameter() ")" Block() { closed = true; } )*
  1261. [ "finally" Block() { closed = true; } ]
  1262. {
  1263. if ( !closed ) throw generateParseException();
  1264. }
  1265. }