Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. package production;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.Map.Entry;
  8. import java.util.Scanner;
  9. import java.util.Set;
  10.  
  11. import org.antlr.v4.runtime.ANTLRInputStream;
  12. import org.antlr.v4.runtime.CommonTokenStream;
  13. import org.antlr.v4.runtime.RuleContext;
  14. import org.antlr.v4.runtime.tree.ParseTree;
  15. import org.json.JSONArray;
  16. import org.json.JSONException;
  17. import org.json.JSONObject;
  18.  
  19. import structure.PoLBaseVisitor;
  20. import structure.PoLClass;
  21. import structure.PoLLexer;
  22. import structure.PoLParser;
  23.  
  24. public class PoLParserTest
  25. {
  26.  
  27.     private static int numberOfErrors;
  28.     private static PoLParser parser;
  29.     private static PoLLexer lexer;
  30.     private static RuleContext tree;
  31.     private static PoLBaseVisitor visit;
  32.    
  33.     public static void main(String[] args) throws Exception
  34.     {
  35.         try
  36.         {
  37.             generateJSONFile();
  38.         }
  39.         catch(FileNotFoundException fnf)
  40.         {
  41.             fnf.printStackTrace();
  42.         }
  43.     }
  44.    
  45.     public static int generateJSONFile() throws FileNotFoundException
  46.     {
  47.        
  48.         String content = new Scanner(new File("src/polExample.txt")).useDelimiter("\\Z").next();
  49.        
  50.         System.out.println( "POL File:\n" + content + "\n\n");
  51.        
  52.         setupParser(content);
  53.    
  54.         JSONObject output = new JSONObject();
  55.        
  56.         try {
  57.            
  58.             output.put("module", visit.getModules().toArray());
  59.            
  60.             String constructValue = "";
  61.            
  62.             if(content.contains("noset"))
  63.             {
  64.                 constructValue = "noset";
  65.             }
  66.             else
  67.             {
  68.                 constructValue = "noflow";
  69.             }
  70.            
  71.             output.put("construct", constructValue);
  72.            
  73.             JSONArray JSONClasses = new JSONArray();
  74.             ArrayList<PoLClass> classes = visit.getClasses();
  75.            
  76.             for(int i = 0; i < classes.size(); i++)
  77.             {
  78.                 JSONObject currentClass = new JSONObject();
  79.                 currentClass.put("class-name", classes.get(i).getClassName());
  80.                
  81.                 JSONArray JSONFieldsArray = new JSONArray();
  82.                 ArrayList<String> fields = classes.get(i).getFields();
  83.                
  84.                 for(int j = 0; j < fields.size(); j++)
  85.                 {
  86.                     JSONFieldsArray.put(fields.get(j));
  87.                 }
  88.                
  89.                 JSONObject JSONFields = new JSONObject();
  90.                 JSONFields.put("fields", JSONFieldsArray);
  91.                
  92.                 JSONClasses.put(currentClass);
  93.             }
  94.            
  95.             output.put("classes", JSONClasses);
  96.            
  97.             System.out.println(output.toString());
  98.            
  99.         } catch (JSONException e) {
  100.             e.printStackTrace();
  101.         }
  102.        
  103.         System.out.println(tree.toStringTree(parser));
  104.        
  105.         return numberOfErrors;
  106.     }
  107.    
  108.     public static void setupParser(String content)
  109.     {
  110.         ANTLRInputStream input = new ANTLRInputStream( content );
  111.        
  112.         lexer = new PoLLexer(input);
  113.        
  114.         CommonTokenStream tokens = new CommonTokenStream(lexer);
  115.        
  116.         parser = new PoLParser(tokens);
  117.        
  118.         parser.setBuildParseTree(true);
  119.        
  120.         tree = parser.prog();
  121.        
  122.         tree.inspect(parser);
  123.        
  124.         numberOfErrors = parser.getNumberOfSyntaxErrors();
  125.        
  126.         visit = new PoLBaseVisitor();
  127.        
  128.         visit.visit(tree);
  129.        
  130.        
  131.     }
  132.    
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement