changeset 145:3ae677faacf1

Eclipse plugin understands ranges and secret values
author Sigurd Meldgaard <stm@daimi.au.dk>
date Mon, 23 Nov 2009 11:26:33 +0100
parents a95fc481028c
children c5779433111c
files eclipse/src/eu/cace/pysmcl/builder/PySMCLCompiler.java
diffstat 1 files changed, 56 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/eclipse/src/eu/cace/pysmcl/builder/PySMCLCompiler.java	Mon Nov 23 11:24:09 2009 +0100
+++ b/eclipse/src/eu/cace/pysmcl/builder/PySMCLCompiler.java	Mon Nov 23 11:26:33 2009 +0100
@@ -5,8 +5,10 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.regex.Matcher;
@@ -64,6 +66,7 @@
 	}
 
 	public void compile(IFile resource, CompilerMessageHandler handler) {
+		System.out.println("Compiling");
 
 		try {
 			String result = compile(resource.getRawLocation().makeAbsolute());
@@ -86,6 +89,20 @@
 
 	}
 
+	private List<String> splitGroup(String regex, String input){
+		Matcher m = Pattern.compile(regex).matcher(input);
+		ArrayList<String> a = new ArrayList<String>();
+		if(m.find()){
+			for(int i=1; i <= m.groupCount(); i++){
+				a.add(m.group(i));
+			}
+			return a;
+		} else { 
+			return null;
+		}
+		
+	}
+	
 	/**
 	 * Parses the output from the PySMCL compiler into separate SMCLProblem
 	 * instances.
@@ -96,25 +113,49 @@
 	private Set<SMCLProblem> parsePySMCLOutput(IFile resource, String output)
 			throws PySMCLException {
 		Set<SMCLProblem> res = new HashSet<SMCLProblem>();
+		try{
+			System.out.println(output);
+			for(String line : output.split("\n")){
+				if(line.startsWith("secret:")){
+					List<String> m = splitGroup("secret: (\\d+) (\\d+) (\\d+) \\((.*)\\)", line);
+					res.add(createMarker(resource,
+							"This expression may return a secret value: "
+							+ m.get(3),
+							Integer.parseInt(m.get(0)),
+							Integer.parseInt(m.get(1)),
+							Integer.parseInt(m.get(2))));				
+				} else if(line.startsWith("values:")){
+					List<String> m = splitGroup("values: (\\d+) (\\d+) (\\d+) ((\\(.*\\))|(_\\|_))", line);
+					res.add(createMarker(resource,
+							""+m.get(3),
+							Integer.parseInt(m.get(0)),
+							Integer.parseInt(m.get(1)),
+							Integer.parseInt(m.get(2))));				
+				} else {
+					throw new RuntimeException("Badly formatted output");
+				}
 
-		// TODO: Could be optimized by compiling pattern only once.
-		Pattern pattern = Pattern.compile("(\\d+) (\\d+) (\\d+)");
-		Matcher matcher = pattern.matcher(output);
-		while (matcher.find()) {
-			int line = Integer.valueOf(matcher.group(1));
-			int startChar = Integer.valueOf(matcher.group(2));
-			int length = Integer.valueOf(matcher.group(3));
-			String program = readProgram(resource);
-			int lineOffset = getLineOffset(program, line - 1);
-			String location = "Line " + line + " (" + (startChar + 1) + ";"
-					+ (startChar + length) + ")";
-			res.add(new SMCLProblem(lineOffset + startChar, lineOffset
-					+ startChar + length,
-					"This variable may contain secret information", location));
+
+			}
+		} catch(Exception e){
+			e.printStackTrace(System.out);
+			System.out.println("error");
+			throw new RuntimeException(e);
 		}
 		return res;
 	}
 
+	private SMCLProblem createMarker(IFile resource, String description, int line,
+			int startChar, int length) throws PySMCLException {
+		String program = readProgram(resource);
+		int lineOffset = getLineOffset(program, line - 1);
+		String location = "Line " + line + " (" + (startChar + 1) + ";"
+				+ (startChar + length) + ")";
+		return new SMCLProblem(lineOffset + startChar, lineOffset
+				+ startChar + length,
+				description, location);
+	}
+
 	/**
 	 * Reads the program and returns it as a string.
 	 * 
@@ -218,7 +259,6 @@
 	 *             program.
 	 */
 	private String compile(IPath resource) throws PySMCLException {
-
 		IPreferenceStore store = Activator.getDefault().getPreferenceStore();
 		String python = store.getString(PreferenceConstants.P_PYTHON);
 		String pySmclBasePath = store.getString(PreferenceConstants.P_PYSMCL);
@@ -227,7 +267,7 @@
 		if (!pySmclBasePath.endsWith(File.pathSeparator))
 			pySmclBasePath += File.separator;
 		
-		String compiler = pySmclBasePath + "pysmcl" + File.separator + "analyze_for_editor.py";
+		String compiler = pySmclBasePath + "pysmcl" + File.separator + "editor_info.py";
 
 		Map<String, String> environ = new HashMap<String, String>();
 		environ.put("PYTHONPATH", pySmclBasePath);