package com.eas.script;

import java.util.Map;
import java.util.TreeMap;
import jdk.nashorn.internal.ir.FunctionNode;
import jdk.nashorn.internal.ir.LexicalContext;
import jdk.nashorn.internal.ir.Node;
import jdk.nashorn.internal.ir.visitor.NodeVisitor;
import jdk.nashorn.internal.parser.Lexer;
import jdk.nashorn.internal.parser.Token;
import jdk.nashorn.internal.parser.TokenStream;
import jdk.nashorn.internal.parser.TokenType;
import jdk.nashorn.internal.runtime.Source;

/**
 * Base class for annotations mining.
 * Important! The visitor's <code>accept</code> method must be invoked on an AST root.
 * @author mg
 */
public abstract class BaseAnnotationsMiner extends NodeVisitor<LexicalContext> {

    protected final int TOP_SCOPE_LEVEL = 1;
    protected final int TOP_CONSTRUCTORS_SCOPE_LEVEL = 2;
    protected final int AMD_CONSTRUCTORS_SCOPE_LEVEL = 3;
    protected Map<Long, Long> prevComments = new TreeMap<>();
    protected Source source;

    public BaseAnnotationsMiner(Source aSource) {
        super(new LexicalContext());
        source = aSource;
        mineComments();
    }

    private void mineComments() {
        TokenStream tokens = new TokenStream();
        Lexer lexer = new Lexer(source, tokens);
        long prevT = 0;
        long t;
        TokenType tt = TokenType.EOL;
        int i = 0;
        while (tt != TokenType.EOF) {
            // Get next token in nashorn's parser way
            while (i > tokens.last()) {
                if (tokens.isFull()) {
                    tokens.grow();
                }
                lexer.lexify();
            }
            t = tokens.get(i++);
            // next token has been obtained.
            tt = Token.descType(t);
            if (tt == TokenType.EOL) {
                continue;
            }
            TokenType prevTt = Token.descType(prevT);
            if (prevTt == TokenType.COMMENT) {
                prevComments.put(t, prevT);
            }
            prevT = t;
        }
    }

    protected int scopeLevel;

    @Override
    public boolean enterFunctionNode(FunctionNode functionNode) {
        scopeLevel++;
        long ft = functionNode.getFirstToken();
        if (prevComments.containsKey(ft)) {
            long prevComment = prevComments.get(ft);
            commentedFunction(functionNode, source.getString(prevComment));
        }
        return super.enterFunctionNode(functionNode);
    }

    @Override
    public Node leaveFunctionNode(FunctionNode functionNode) {
        scopeLevel--;
        return super.leaveFunctionNode(functionNode);
    }

    protected abstract void commentedFunction(FunctionNode aFunction, String aComment);

}

@@ -66,24 +66,91 @@
 l.TreeMap;%0D%0A
+import java.util.logging.Level;%0D%0Aimport java.util.logging.Logger;%0D%0A
 import jdk.n
@@ -500,24 +500,78 @@
 TokenType;%0D%0A
+import jdk.nashorn.internal.runtime.ParserException;%0D%0A
 import jdk.n
@@ -647,20 +647,16 @@
  mining.
-%0D%0A *
  Importa
@@ -672,16 +672,20 @@
 isitor's
+%0D%0A *
  %3Ccode%3Ea
@@ -737,16 +737,20 @@
  root.%0D%0A
+ *%0D%0A
  * @auth
@@ -1285,24 +1285,43 @@
 mments() %7B%0D%0A
+        try %7B%0D%0A    
         Toke
@@ -1353,24 +1353,28 @@
 nStream();%0D%0A
+    
         Lexe
@@ -1407,24 +1407,28 @@
 , tokens);%0D%0A
+    
         long
@@ -1444,24 +1444,28 @@
 0;%0D%0A        
+    
 long t;%0D%0A   
@@ -1457,24 +1457,28 @@
    long t;%0D%0A
+    
         Toke
@@ -1512,16 +1512,20 @@
         
+    
 int i = 
@@ -1524,24 +1524,28 @@
 int i = 0;%0D%0A
+    
         whil
@@ -1583,16 +1583,20 @@
         
+ 
+   
 // Get n
@@ -1638,24 +1638,28 @@
             
+    
 while (i %3E t
@@ -1687,24 +1687,28 @@
             
+ 
+   
 if (tokens.i
@@ -1739,16 +1739,20 @@
         
+    
 tokens.g
@@ -1767,35 +1767,43 @@
                 
+  
+  
 %7D%0D%0A
+    
                 
@@ -1823,35 +1823,43 @@
 );%0D%0A            
+  
+  
 %7D%0D%0A
+    
             t = 
@@ -1888,16 +1888,20 @@
         
+   
+ 
 // next 
@@ -1938,16 +1938,20 @@
         
+    
 tt = Tok
@@ -1963,24 +1963,28 @@
 scType(t);%0D%0A
+    
             
@@ -2027,16 +2027,20 @@
         
+    
 continue
@@ -2046,32 +2046,36 @@
 e;%0D%0A            
+    
 %7D%0D%0A            T
@@ -2065,32 +2065,36 @@
  %7D%0D%0A            
+ 
+   
 TokenType prevTt
@@ -2124,32 +2124,36 @@
 );%0D%0A            
+    
 if (prevTt == To
@@ -2184,24 +2184,28 @@
             
+ 
+   
 prevComments
@@ -2225,35 +2225,43 @@
 );%0D%0A            
-%7D%0D%0A
+    %7D%0D%0A    
             prev
@@ -2264,24 +2264,184 @@
 prevT = t;%0D%0A
+            %7D%0D%0A        %7D catch (ParserException ex) %7B%0D%0A            Logger.getLogger(BaseAnnotationsMiner.class.getName()).log(Level.WARNING, ex.getMessage());%0D%0A
         %7D%0D%0A 

fix of minecomments parseexception