Commits

Tim Vernum committed c9914eb

More AST improvements

Comments (0)

Files changed (2)

parser/source/grammar/java.jjt

 void Expression():
 /*
  * This expansion has been written this way instead of:
- *   Assignment() | ConditionalExpression()
+ *   Assignment() | TernaryExpression()
  * for performance reasons.
  * However, it is a weakening of the grammar for it allows the LHS of
  * assignments to be any conditional expression whereas it can only be
  */
 {}
 {
-  ConditionalExpression()
+  TernaryExpression()
   [
     LOOKAHEAD(2)
     AssignmentOperator() Expression()
   "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | ">>>=" | "&=" | "^=" | "|="
 }
 
-void ConditionalExpression() #void :
+void TernaryExpression() #void :
 {}
 {
-  ( LogicalOrExpression() [ "?" Expression() ":" Expression() ] ) #ConditionalExpression(>1)
+  ( LogicalOrExpression() [ "?" Expression() ":" Expression() ] ) #TernaryExpression(>1)
 }
 
 void LogicalOrExpression() #void :
 void EqualityExpression() #void:
 {}
 {
-  ( InstanceOfExpression() ( ( "==" | "!=" ) InstanceOfExpression() )* ) #EqualityExpression(>1)
+  ( InstanceOfExpression() ( EqualityOperator() InstanceOfExpression() )* ) #EqualityExpression(>1)
+}
+
+void EqualityOperator() #BinaryOperator :
+{}
+{
+  "==" | "!=" 
 }
 
 void InstanceOfExpression() #void :
 void RelationalExpression() #void :
 {}
 {
-( ShiftExpression() ( ( "<" | ">" | "<=" | ">=" ) ShiftExpression() )*  ) #RelationalExpression(>1)
+( ShiftExpression() ( RelationalOperator() ShiftExpression() )*  ) #RelationalExpression(>1)
+}
+
+void RelationalOperator() #BinaryOperator :
+{}
+{
+    "<" | ">" | "<=" | ">=" 
 }
 
 void ShiftExpression() #void :
 {}
 {
-( AdditiveExpression() ( ( "<<" | RSIGNEDSHIFT() | RUNSIGNEDSHIFT() ) AdditiveExpression() )*  ) #ShiftExpression(>1)
+    ( AdditiveExpression() ( ShiftOperator() AdditiveExpression() )*  ) #ShiftExpression(>1)
+}
+
+void ShiftOperator() #BinaryOperator :
+{}
+{
+    "<<" | RSIGNEDSHIFT() | RUNSIGNEDSHIFT()
 }
 
 void AdditiveExpression() #void :
 {}
 {
-( MultiplicativeExpression() ( ( "+" | "-" ) MultiplicativeExpression() )*  ) #AdditiveExpression(>1)
+    ( MultiplicativeExpression() ( AdditiveOperator() MultiplicativeExpression() )*  ) #AdditiveExpression(>1)
+}
+
+void AdditiveOperator() #BinaryOperator :
+{}
+{
+    "+" | "-"
 }
 
 void MultiplicativeExpression() #void :
 {}
 {
-( UnaryExpression() ( ( "*" | "/" | "%" ) UnaryExpression() )*  ) #MultiplicativeExpression(>1)
+( UnaryExpression() ( MultiplicativeOperator() UnaryExpression() )*  ) #MultiplicativeExpression(>1)
+}
+
+void MultiplicativeOperator() #BinaryOperator :
+{}
+{
+    "*" | "/" | "%"
 }
 
 void UnaryExpression() #void :
 void Literal():
 {}
 {
-  <INTEGER_LITERAL>
+  IntegerLiteral()
 |
-  <FLOATING_POINT_LITERAL>
+  FloatingPointLiteral()
 |
-  <CHARACTER_LITERAL>
+  CharacterLiteral()
 |
-  <STRING_LITERAL>
+  StringLiteral()
 |
   BooleanLiteral()
 |
   NullLiteral()
 }
 
+void IntegerLiteral():
+{}
+{
+  <INTEGER_LITERAL>
+}
+
+void FloatingPointLiteral():
+{}
+{
+  <FLOATING_POINT_LITERAL>
+}
+
+void CharacterLiteral():
+{}
+{
+  <CHARACTER_LITERAL>
+}
+
+void StringLiteral():
+{}
+{
+  <STRING_LITERAL>
+}
+
 void This():
 {}
 {
  |
    MemberValueArrayInitializer()
  |
-   ConditionalExpression()
+   TernaryExpression()
 }
 
 void  MemberValueArrayInitializer():

parser/source/java/parser/org/adjective/syntactic/parser/ASTPrinter.java

 
     private void print(SimpleNode node, String prefix)
     {
-        _stream.println(node.toString(prefix));
+        _stream.print(node.toString(prefix));
+        if (node.jjtGetNumChildren() == 0 || getChild(node, 0).jjtGetFirstToken() != node.jjtGetFirstToken())
+        {
+            _stream.print(' ');
+            _stream.print(node.jjtGetFirstToken().image);
+        }
+        _stream.print('\n');
         for (int i=0; i<  node.jjtGetNumChildren(); i++)
         {
-            Node child = node.jjtGetChild(i);
-            print((SimpleNode) child, prefix + "  ");
+            SimpleNode child = getChild(node, i);
+            print(child, prefix + "  ");
         }
     }
 
+    private BaseNode getChild(SimpleNode node, int i)
+    {
+        return (BaseNode) node.jjtGetChild(i);
+    }
+
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.