Commits

Tim Vernum committed 5160cac

Update AST

  • Participants
  • Parent commits 7c42db3

Comments (0)

Files changed (4)

File parser/source/grammar/java.jjt

     VISITOR = true;
     TRACK_TOKENS = true;
     TOKEN_EXTENDS = "org.adjective.syntactic.parser.BaseToken";
+    NODE_CLASS = "org.adjective.syntactic.parser.BaseNode" ;
     NODE_SCOPE_HOOK = false;
     COMMON_TOKEN_ACTION = false;
     ERROR_REPORTING = false;
 }
 
 PARSER_BEGIN(JavaParser)
-package org.adjective.syntactic.parser;
+package org.adjective.syntactic.parser.jj;
 
 import java.io.*;
 
         {
             return modifiers & ~mod;
         }
-
-        static int withModifier(int modifier, int mod) 
-        {
-            return modifier & mod;
-        }
     }
 
     public JavaParser(String fileName)
 {}
 {
   [ LOOKAHEAD( ( Annotation() )* "package" ) PackageDeclaration() ]
-  ( ImportDeclaration() )*
-  ( TypeDeclaration() )*
+  Imports() 
+  Types() 
   ( < "\u001a" > )?
   ( <STUFF_TO_IGNORE: ~[]> )?
   <EOF> { return jjtThis; }
 void PackageDeclaration():
 {}
 {
-  Modifiers() "package" QualifiedIdentifier() ";"
+  Annotations() "package" QualifiedIdentifier() ";"
+}
+
+void Annotations() :
+{}
+{
+    ( Annotation() )*
+}
+
+void Imports() :
+{}
+{
+    ( ImportDeclaration() )*
 }
 
 void ImportDeclaration():
 {}
 {
-  "import" [ "static" ] QualifiedIdentifier() [ "." "*" ] ";"
+  "import" [ StaticImport() ] QualifiedIdentifier() [ "." "*" ] ";"
+}
+
+void StaticImport():
+{}
+{
+    "static"
+}
+
+void Types():
+{}
+{
+    ( TypeDeclaration() )*
 }
 
 /*
 }
 {
   ( "class" | "interface" { isInterface = true; } )
-  <IDENTIFIER>
+  Identifier()
   [ TypeParameters() ]
   [ ExtendsList(isInterface) ]
   [ ImplementsList(isInterface) ]
   ClassOrInterfaceBody(isInterface)
 }
 
+void Identifier():
+{}
+{
+    <IDENTIFIER>
+}
+
 void ExtendsList(boolean isInterface):
 {
    boolean extendsMoreThanOne = false;
 void EnumDeclaration(int modifiers):
 {}
 {
-  "enum" <IDENTIFIER>
+  "enum" Identifier()
   [ ImplementsList(false) ]
   EnumBody()
 }
    "{"
    [ EnumConstant() ( LOOKAHEAD(2) "," EnumConstant() )* ]
     [ "," ]
-   [ ";" ( ClassOrInterfaceBodyDeclaration(false) )* ]
+   [ ";" ( ClassOrInterfaceBodyElement(false) )* ]
    "}"
 }
 
 void EnumConstant():
 {}
 {
-  Modifiers() <IDENTIFIER> [ Arguments() ] [ ClassOrInterfaceBody(false) ]
+  Modifiers() Identifier() [ Arguments() ] [ ClassOrInterfaceBody(false) ]
 }
 
 void TypeParameters():
 void ClassOrInterfaceBody(boolean isInterface):
 {}
 {
-  "{" ( ClassOrInterfaceBodyDeclaration(isInterface) )* "}"
+  "{" ( ClassOrInterfaceBodyElement(isInterface) )* "}"
 }
 
-void ClassOrInterfaceBodyDeclaration(boolean isInterface):
+void ClassOrInterfaceBodyElement(boolean isInterface) :
 {
    boolean isNestedInterface = false;
    int modifiers;
   VariableDeclaratorId() [ "=" VariableInitializer() ]
 }
 
-void VariableDeclaratorId():
+void VariableDeclaratorId() #void :
 {}
 {
-  <IDENTIFIER> ( "[" "]" )*
+  Identifier() ArraySuffixList()
 }
 
-void VariableInitializer():
+void VariableInitializer() #void :
 {}
 {
   ArrayInitializer()
   ( Block() | ";" )
 }
 
-void MethodDeclarator():
+void MethodDeclarator() #void :
 {}
 {
-  <IDENTIFIER> FormalParameters() ( "[" "]" )*
+  Identifier() FormalParameters() ArraySuffixList()
+}
+
+void ArraySuffixList() #void :
+{}
+{
+    ( ArraySuffix() )* 
+}
+
+void ArraySuffix() :
+{}
+{
+    "[" "]"
 }
 
 void FormalParameters():
  * Type, name and expression syntax follows.
  */
 
-void Type():
+void Type() #void :
 {}
 {
    LOOKAHEAD(2) ReferenceType()
    PrimitiveType()
 }
 
-void ReferenceType():
+void ReferenceType() #void :
 {}
 {
-   PrimitiveType() ( LOOKAHEAD(2) "[" "]" )+
+   PrimitiveType() ( LOOKAHEAD(2) ArraySuffix() )+
   |
-   ( ClassOrInterfaceType() ) ( LOOKAHEAD(2) "[" "]" )*
+   ( ClassOrInterfaceType() ) ( LOOKAHEAD(2) ArraySuffix() )*
 }
 
 void ClassOrInterfaceType():
 {}
 {
-  <IDENTIFIER> [ LOOKAHEAD(2) TypeArguments() ]
-  ( LOOKAHEAD(2) "." <IDENTIFIER> [ LOOKAHEAD(2) TypeArguments() ] )*
+  Identifier() [ LOOKAHEAD(2) TypeArguments() ]
+  ( LOOKAHEAD(2) "." Identifier() [ LOOKAHEAD(2) TypeArguments() ] )*
 }
 
 void TypeArguments():
   "double"
 }
 
-void ResultType():
+void ResultType() #void:
 {}
 {
-  "void"
+  Void()
 |
   Type()
 }
 
+void Void():
+{}
+{ "void" }
+
 void QualifiedIdentifier():
 /*
  * A lookahead of 2 is required below since "QualifiedIdentifier" can be followed
   "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | ">>>=" | "&=" | "^=" | "|="
 }
 
-void ConditionalExpression():
+void ConditionalExpression() #void :
 {}
 {
-  ConditionalOrExpression() [ "?" Expression() ":" Expression() ]
+  ( LogicalOrExpression() [ "?" Expression() ":" Expression() ] ) #ConditionalExpression(>1)
 }
 
-void ConditionalOrExpression():
+void LogicalOrExpression() #void :
 {}
 {
-  ConditionalAndExpression() ( "||" ConditionalAndExpression() )*
+( LogicalAndExpression() ( "||" LogicalAndExpression() )*  ) #LogicalOrExpression(>1)
 }
 
-void ConditionalAndExpression():
+void LogicalAndExpression() #void :
 {}
 {
-  InclusiveOrExpression() ( "&&" InclusiveOrExpression() )*
+( InclusiveOrExpression() ( "&&" InclusiveOrExpression() )*  ) #LogicalAndExpression(>1)
 }
 
-void InclusiveOrExpression():
+void InclusiveOrExpression() #void :
 {}
 {
-  ExclusiveOrExpression() ( "|" ExclusiveOrExpression() )*
+( ExclusiveOrExpression() ( "|" ExclusiveOrExpression() )*  ) #InclusiveOrExpression(>1)
 }
 
-void ExclusiveOrExpression():
+void ExclusiveOrExpression() #void :
 {}
 {
-  AndExpression() ( "^" AndExpression() )*
+( AndExpression() ( "^" AndExpression() )*  ) #ExclusiveOrExpression(>1)
 }
 
-void AndExpression():
+void AndExpression() #void:
 {}
 {
-  EqualityExpression() ( "&" EqualityExpression() )*
+( EqualityExpression() ( "&" EqualityExpression() )*  ) #AndExpression(>1)
 }
 
-void EqualityExpression():
+void EqualityExpression() #void:
 {}
 {
-  InstanceOfExpression() ( ( "==" | "!=" ) InstanceOfExpression() )*
+  ( InstanceOfExpression() ( ( "==" | "!=" ) InstanceOfExpression() )* ) #EqualityExpression(>1)
 }
 
-void InstanceOfExpression():
+void InstanceOfExpression() #void :
 {}
 {
-  RelationalExpression() [ "instanceof" Type() ]
+( RelationalExpression() [ "instanceof" Type() ]  ) #InstanceOfExpression(>1)
 }
 
-void RelationalExpression():
+void RelationalExpression() #void :
 {}
 {
-  ShiftExpression() ( ( "<" | ">" | "<=" | ">=" ) ShiftExpression() )*
+( ShiftExpression() ( ( "<" | ">" | "<=" | ">=" ) ShiftExpression() )*  ) #RelationalExpression(>1)
 }
 
-void ShiftExpression():
+void ShiftExpression() #void :
 {}
 {
-  AdditiveExpression() ( ( "<<" | RSIGNEDSHIFT() | RUNSIGNEDSHIFT() ) AdditiveExpression() )*
+( AdditiveExpression() ( ( "<<" | RSIGNEDSHIFT() | RUNSIGNEDSHIFT() ) AdditiveExpression() )*  ) #ShiftExpression(>1)
 }
 
-void AdditiveExpression():
+void AdditiveExpression() #void :
 {}
 {
-  MultiplicativeExpression() ( ( "+" | "-" ) MultiplicativeExpression() )*
+( MultiplicativeExpression() ( ( "+" | "-" ) MultiplicativeExpression() )*  ) #AdditiveExpression(>1)
 }
 
-void MultiplicativeExpression():
+void MultiplicativeExpression() #void :
 {}
 {
-  UnaryExpression() ( ( "*" | "/" | "%" ) UnaryExpression() )*
+( UnaryExpression() ( ( "*" | "/" | "%" ) UnaryExpression() )*  ) #MultiplicativeExpression(>1)
 }
 
-void UnaryExpression():
+void UnaryExpression() #void :
 {}
 {
-  ( "+" | "-" ) UnaryExpression()
+  ( "+" | "-" ) UnaryExpression() #UnaryExpression(true)
 |
   PreIncrementExpression()
 |
   UnaryExpressionNotPlusMinus()
 }
 
-void PreIncrementExpression():
+void PreIncrementExpression() #void :
 {}
 {
-  "++" PrimaryExpression()
+  "++" PrimaryExpression() #PrefixExpression(true)
 }
 
 void PreDecrementExpression():
 {}
 {
-  "--" PrimaryExpression()
+  "--" PrimaryExpression() #PrefixExpression(true)
 }
 
-void UnaryExpressionNotPlusMinus():
+void UnaryExpressionNotPlusMinus() #void :
 {}
 {
-  ( "~" | "!" ) UnaryExpression()
+  ( "~" | "!" ) UnaryExpression() #UnaryExpression(true)
 |
   LOOKAHEAD( CastLookahead() )
   CastExpression()
 // This production is to determine lookahead only.  The LOOKAHEAD specifications
 // below are not used, but they are there just to indicate that we know about
 // this.
-void CastLookahead():
+void CastLookahead() #void:
 {}
 {
   LOOKAHEAD(2)
   "(" Type() ")" ( "~" | "!" | "(" | <IDENTIFIER> | "this" | "super" | "new" | Literal() )
 }
 
-void PostfixExpression():
+void PostfixExpression() #void:
 {}
 {
-  PrimaryExpression() [ "++" | "--" ]
+    PrimaryExpression() [ ( "++" | "--" ) #PostfixExpression() ]
 }
 
 void CastExpression():
   "(" Type() ")" UnaryExpressionNotPlusMinus()
 }
 
-void PrimaryExpression():
+void PrimaryExpression() #void :
 {}
 {
-  PrimaryPrefix() ( LOOKAHEAD(2) PrimarySuffix() )*
+    ( PrimaryPrefix() ( LOOKAHEAD(2) PrimarySuffix() )*  ) #PrimaryExpression(>1)
 }
 
-void MemberSelector():
-{}
-{
-  "." TypeArguments() <IDENTIFIER>
-}
-
-void PrimaryPrefix():
+void PrimaryPrefix() #void:
 {}
 {
   Literal()
 |
-  LOOKAHEAD( ( <IDENTIFIER> "." )* "this" )
-  ( <IDENTIFIER> "." )*
-  "this"
+  This()
 |
-  "super" "." <IDENTIFIER>
-|
-   // danson, added this part to support a construct like:
-   //   Buffer.super.setDirty(true);
-   // See Java Language Specification, 3rd edition, section 15.11.2.
-   LOOKAHEAD( ClassOrInterfaceType() "." "super" "." <IDENTIFIER> )
-   ClassOrInterfaceType() "." "super" "." <IDENTIFIER>
+  Super()
 |
   "(" Expression() ")"
 |
   AllocationExpression()
 |
-  LOOKAHEAD( ResultType() "." "class" )
-  ResultType() "." "class"
+  LOOKAHEAD( ClassReference() )
+  ClassReference()
 |
   QualifiedIdentifier()
 }
 
-void PrimarySuffix():
+void PrimarySuffix() #void:
 {}
 {
-  LOOKAHEAD("." "super" ".")
-  "." "super"
+  "." (
+        Super()
+      | This()
+      | AllocationExpression() 
+      | [ TypeArguments() ] Identifier()
+      )
 |
-  LOOKAHEAD("." "this")
-  "." "this"
-|
-  LOOKAHEAD(2)
-  "." AllocationExpression()
-|
-  LOOKAHEAD(3)
-  MemberSelector()
-|
-  "[" Expression() "]"
-|
-  "." <IDENTIFIER>
+  ArrayIndex()
 |
   Arguments()
 }
   NullLiteral()
 }
 
+void This():
+{}
+{
+  "this"
+}
+
+void Super():
+{}
+{
+  "super" 
+}
+
+void ClassReference():
+{}
+{
+  ResultType() "." "class"
+}
+
+void ArrayIndex():
+{}
+{
+    "[" Expression() "]"
+}
+
 void BooleanLiteral():
 {}
 {
   "{" ( BlockStatement() )* "}"
 }
 
-void BlockStatement():
+void BlockStatement() #void :
 {}
 {
   LOOKAHEAD( Modifiers() Type() <IDENTIFIER> )

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

+/* ------------------------------------------------------------------------
+ * Copyright 2012 Tim Vernum
+ * ------------------------------------------------------------------------
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ------------------------------------------------------------------------
+ */
+
+package org.adjective.syntactic.parser;
+
+import java.io.PrintStream;
+
+import org.adjective.syntactic.parser.ast.Node;
+import org.adjective.syntactic.parser.ast.SimpleNode;
+
+/**
+ * @author <a href="http://blog.adjective.org/">Tim Vernum</a>
+ */
+public class ASTPrinter
+{
+    private final PrintStream _stream;
+
+    public ASTPrinter()
+    {
+        this(System.out);
+    }
+    
+    public ASTPrinter(PrintStream stream)
+    {
+        _stream = stream;
+    }
+
+    public void print(Node node)
+    {
+        String prefix ="";
+        print((SimpleNode) node, prefix);
+    }
+
+    private void print(SimpleNode node, String prefix)
+    {
+        _stream.println(node.toString(prefix));
+        for (int i=0; i<  node.jjtGetNumChildren(); i++)
+        {
+            Node child = node.jjtGetChild(i);
+            print((SimpleNode) child, prefix + "  ");
+        }
+    }
+
+}

File parser/source/java/parser/org/adjective/syntactic/parser/BaseNode.java

+/* ------------------------------------------------------------------------
+ * Copyright 2012 Tim Vernum
+ * ------------------------------------------------------------------------
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ------------------------------------------------------------------------
+ */
+
+package org.adjective.syntactic.parser;
+
+import org.adjective.syntactic.parser.ast.SimpleNode;
+import org.adjective.syntactic.parser.jj.JavaParser;
+
+/**
+ * @author <a href="http://blog.adjective.org/">Tim Vernum</a>
+ */
+public class BaseNode extends SimpleNode
+{
+    public BaseNode(int i)
+    {
+        super(i);
+    }
+
+    public BaseNode(JavaParser p, int i)
+    {
+        super(p, i);
+    }
+}

File parser/source/java/parser/org/adjective/syntactic/parser/Main.java

 package org.adjective.syntactic.parser;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.adjective.syntactic.parser.ast.Node;
+import org.adjective.syntactic.parser.jj.JavaParser;
+
 public class Main {
 
     public static void main(String[] args)
     {
-       org.adjective.syntactic.parser.ast.Node node = JavaParser.execute(args); 
+        ASTPrinter printer = new ASTPrinter(); 
+        if(args.length == 0)    
+        {
+            printer.print( JavaParser.processStdin() );
+            return;
+        }
+            
+        List<String> errors = new ArrayList<String>();
+        for( String arg : args )
+        {
+            Node node = JavaParser.process(arg);
+            if(node == null) {
+                errors.add(arg);
+            } else {
+                printer.print( node );
+            }
+        }
+
+        for( String error : errors )
+        {
+            System.err.println("Error: Failed to parse '" + error + "'");
+        }
+
+        if( errors.isEmpty() ) 
+        {
+            System.out.println(" -- No errors --");
+        }
+
     }
 
 }