[SCM] master: Re-implement expression completion using ExpressionFactory.complete(...).

cagney@sourceware.org cagney@sourceware.org
Sat Nov 17 19:48:00 GMT 2007


The branch, master has been updated
       via  5c771ea53ce8b3c98d3c47d6cc591746fb6d80e4 (commit)
       via  6f7d47da1af6ab5643be544dff512580aef96314 (commit)
       via  fe41b1056315bbf0c87fbea81895c89784a01c36 (commit)
      from  3693a862c0268989040840a9ce338cf4a761f5d5 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email.

- Log -----------------------------------------------------------------
commit 5c771ea53ce8b3c98d3c47d6cc591746fb6d80e4
Author: Andrew Cagney <cagney@redhat.com>
Date:   Sat Nov 17 14:41:52 2007 -0500

    Re-implement expression completion using ExpressionFactory.complete(...).
    
    frysk-core/frysk/debuginfo/ChangeLog
    2007-11-17  Andrew Cagney  <cagney@redhat.com>
    
    	* DebugInfo.java (complete(DebugInfoFrame,String,int,List)): Delete.
    
    frysk-core/frysk/hpd/ChangeLog
    2007-11-17  Andrew Cagney  <cagney@redhat.com>
    
    	* CompletionFactory.java (completeExpression(CLI,Input,int,List)):
    	Re-implement using
    	ExpressionFactory.complete(ExprSymTab,String,List) and
    	ObjectDeclarationSearchEngine.

commit 6f7d47da1af6ab5643be544dff512580aef96314
Author: Andrew Cagney <cagney@redhat.com>
Date:   Sat Nov 17 14:06:09 2007 -0500

    Add ExprSymTab.complete(String,List); test.
    
    frysk-core/frysk/debuginfo/ChangeLog
    2007-11-17  Andrew Cagney  <cagney@redhat.com>
    
    	* ObjectDeclarationSearchEngine.java (complete(String,List)): New.
    
    frysk-core/frysk/expr/ChangeLog
    2007-11-17  Andrew Cagney  <cagney@redhat.com>
    
    	* CompletionException.java (getMessage()): New.
    	* TestCompletion.java (testOneCompletion()): New.
    	(testTwoCompletions()): New.
    	(testNoCompletions()): Replace testNothingToDo().
    	(complete(String,String[],int)): New.
    	* DetailedAST.java: New.
    	* ExprSymTab.java (complete(String,List)): New.
    	* ScratchSymTab.java (complete(String,List)): New.

commit fe41b1056315bbf0c87fbea81895c89784a01c36
Author: Andrew Cagney <cagney@redhat.com>
Date:   Fri Nov 16 13:16:53 2007 -0500

    Add ExpressionFactory and TestCompletion to frysk.expr.
    
    2007-11-16  Andrew Cagney  <cagney@redhat.com>
    
    	* TestCompletion.java: New.
    	* ExpressionFactory.java: New.

-----------------------------------------------------------------------

Summary of changes:
 frysk-core/frysk/debuginfo/ChangeLog               |    6 ++
 frysk-core/frysk/debuginfo/DebugInfo.java          |   46 ---------------
 .../debuginfo/ObjectDeclarationSearchEngine.java   |   24 +++++++-
 frysk-core/frysk/expr/ChangeLog                    |   14 +++++
 frysk-core/frysk/expr/CompletionException.java     |   10 +++-
 .../{CompletionException.java => DetailedAST.java} |   39 ++++++-------
 frysk-core/frysk/expr/ExprSymTab.java              |   20 ++++++-
 ...TestArithmetics.java => ExpressionFactory.java} |   51 ++++++++++-------
 frysk-core/frysk/expr/ScratchSymTab.java           |    6 ++-
 .../TestCompletion.java}                           |   62 ++++++++++----------
 frysk-core/frysk/expr/TestbedSymTab.java           |   14 ++++-
 frysk-core/frysk/hpd/ChangeLog                     |    5 ++
 frysk-core/frysk/hpd/CompletionFactory.java        |   11 ++--
 13 files changed, 177 insertions(+), 131 deletions(-)
 copy frysk-core/frysk/expr/{CompletionException.java => DetailedAST.java} (80%)
 copy frysk-core/frysk/expr/{TestArithmetics.java => ExpressionFactory.java} (72%)
 copy frysk-core/frysk/{isa/TestRegisters.java => expr/TestCompletion.java} (61%)

First 500 lines of diff:
diff --git a/frysk-core/frysk/debuginfo/ChangeLog b/frysk-core/frysk/debuginfo/ChangeLog
index ebf74ae..b307901 100644
--- a/frysk-core/frysk/debuginfo/ChangeLog
+++ b/frysk-core/frysk/debuginfo/ChangeLog
@@ -1,3 +1,9 @@
+2007-11-17  Andrew Cagney  <cagney@redhat.com>
+
+	* DebugInfo.java (complete(DebugInfoFrame,String,int,List)): Delete.
+
+	* ObjectDeclarationSearchEngine.java (complete(String,List)): New.
+
 2007-11-16  Andrew Cagney  <cagney@redhat.com>
 
 	* DebugInfo.java (complete(DebugInfoFrame,String,int,List)): Catch
diff --git a/frysk-core/frysk/debuginfo/DebugInfo.java b/frysk-core/frysk/debuginfo/DebugInfo.java
index cd17386..9bc2dab 100644
--- a/frysk-core/frysk/debuginfo/DebugInfo.java
+++ b/frysk-core/frysk/debuginfo/DebugInfo.java
@@ -48,8 +48,6 @@ import frysk.proc.Proc;
 import frysk.value.Type;
 import frysk.value.Value;
 import java.io.StringReader;
-import java.util.Iterator;
-import java.util.List;
 import lib.dwfl.Dwarf;
 import lib.dwfl.DwarfCommand;
 import lib.dwfl.DwarfDie;
@@ -81,50 +79,6 @@ public class DebugInfo {
 	}
     }
 
-
-    /**
-     * Handle ConsoleReader Completor
-     * 
-     * @param buffer Input buffer.
-     * @param cursor Position of TAB in buffer.
-     * @param candidates List that may complete token.
-     * @return cursor position in buffer
-     */
-    public int complete (DebugInfoFrame frame, String buffer, int cursor, List candidates) {
-	long pc = frame.getAdjustedAddress();
-	Dwfl dwfl = DwflCache.getDwfl(frame.getTask());
-	DwflDieBias bias = dwfl.getCompilationUnit(pc);
-	DwarfDie die = bias.die;
-	String token = "";
-
-	String sInput = buffer.substring(0, cursor) + '\t' + (cursor < buffer.length() 
-							      ? buffer.substring(cursor) : "");
-
-	sInput += (char)3;
-	CExprLexer lexer = new CExprLexer(new StringReader(sInput));
-	CExprParser parser = new CExprParser(lexer);
-	try {
-	    parser.start();
-	} catch (antlr.RecognitionException ignore) {
-	    // FIXME: Why is this ignored?
-	} catch (antlr.TokenStreamException ignore) {
-	    // FIXME: Why is this ignored?
-	} catch (frysk.expr.CompletionException t) {
-	    token = t.getText();
-	}
-
-	DwarfDie[] allDies = die.getScopes(pc - bias.bias);
-	List candidates_p = die.getScopeVarNames(allDies, token);
-	
-	for (Iterator i = candidates_p.iterator(); i.hasNext();) {
-            String sNext = (String) i.next();
-            candidates.add(sNext);
-        }
-	// XXX: This is a big kludge (but less of a kludge than
-	// .indexOf.
-	return buffer.lastIndexOf(token);
-    }
-
     /**
      * Get the DwarfDie for a function symbol
      */
diff --git a/frysk-core/frysk/debuginfo/ObjectDeclarationSearchEngine.java b/frysk-core/frysk/debuginfo/ObjectDeclarationSearchEngine.java
index 9434a7f..62864b0 100644
--- a/frysk-core/frysk/debuginfo/ObjectDeclarationSearchEngine.java
+++ b/frysk-core/frysk/debuginfo/ObjectDeclarationSearchEngine.java
@@ -39,12 +39,15 @@
 
 package frysk.debuginfo;
 
+import frysk.dwfl.DwflCache;
+import java.util.Iterator;
 import inua.eio.ByteBuffer;
 import inua.eio.ByteOrder;
-
+import lib.dwfl.Dwfl;
+import lib.dwfl.DwflDieBias;
+import lib.dwfl.DwarfDie;
 import java.util.LinkedList;
 import java.util.List;
-
 import frysk.expr.ExprSymTab;
 import frysk.isa.ISA;
 import frysk.isa.Register;
@@ -132,4 +135,21 @@ public class ObjectDeclarationSearchEngine implements ExprSymTab{
     {
 	return task.getISA().wordSize();
     }
+
+    /**
+     * XXX: Who knows if this works; it is certainly not implemented
+     * correctly as it should use the ObjectDeclaration.
+     */
+    public void complete(String incomplete, List candidates) {
+	long pc = frame.getAdjustedAddress();
+	Dwfl dwfl = DwflCache.getDwfl(frame.getTask());
+	DwflDieBias bias = dwfl.getCompilationUnit(pc);
+	DwarfDie die = bias.die;
+	DwarfDie[] allDies = die.getScopes(pc - bias.bias);
+	List candidates_p = die.getScopeVarNames(allDies, incomplete);
+	for (Iterator i = candidates_p.iterator(); i.hasNext();) {
+            String sNext = (String) i.next();
+            candidates.add(sNext);
+        }
+    }
 }
diff --git a/frysk-core/frysk/expr/ChangeLog b/frysk-core/frysk/expr/ChangeLog
index 1b9f728..784f33d 100644
--- a/frysk-core/frysk/expr/ChangeLog
+++ b/frysk-core/frysk/expr/ChangeLog
@@ -1,3 +1,17 @@
+2007-11-17  Andrew Cagney  <cagney@redhat.com>
+
+	* CompletionException.java (getMessage()): New.
+	* TestCompletion.java (testOneCompletion()): New.
+	(testTwoCompletions()): New.
+	(testNoCompletions()): Replace testNothingToDo().
+	(complete(String,String[],int)): New.
+	* DetailedAST.java: New.
+	* ExprSymTab.java (complete(String,List)): New.
+	* ScratchSymTab.java (complete(String,List)): New.
+
+	* TestCompletion.java: New.
+	* ExpressionFactory.java: New.
+
 2007-11-16  Andrew Cagney  <cagney@redhat.com>
 
 	* TabException.java: Delete.
diff --git a/frysk-core/frysk/expr/CompletionException.java b/frysk-core/frysk/expr/CompletionException.java
index 19aa7a4..56f2ede 100644
--- a/frysk-core/frysk/expr/CompletionException.java
+++ b/frysk-core/frysk/expr/CompletionException.java
@@ -49,9 +49,14 @@ public class CompletionException extends RuntimeException {
     static final long serialVersionUID = 1;
     private final AST ast;
     CompletionException(AST ast) {
-	super("completing " + ast);
 	this.ast = ast;
     }
+    public String getMessage() {
+	return ("complete <<"
+		+ getText()
+		+ ">> at "
+		+ getColumn());
+    }
     /**
      * Return the incomplete string (minus the tab).
      */
@@ -67,6 +72,7 @@ public class CompletionException extends RuntimeException {
 	return ast.getLine();
     }
     public int getColumn() {
-	return ast.getColumn();
+	// Antlr counts columns starting at 1.
+	return ast.getColumn() - 1;
     }
 }
diff --git a/frysk-core/frysk/expr/CompletionException.java b/frysk-core/frysk/expr/DetailedAST.java
similarity index 80%
copy from frysk-core/frysk/expr/CompletionException.java
copy to frysk-core/frysk/expr/DetailedAST.java
index 19aa7a4..bfe069a 100644
--- a/frysk-core/frysk/expr/CompletionException.java
+++ b/frysk-core/frysk/expr/DetailedAST.java
@@ -39,34 +39,31 @@
 
 package frysk.expr;
 
-import antlr.collections.AST;
+import antlr.CommonAST;
+import antlr.Token;
 
 /** 
- * An incomplete blah.
+ * An AST that also retains the location of its token.
+ *
+ * For what ever reason the default ast doesn't remember the location
+ * (line/col) of a token.
  */
 
-public class CompletionException extends RuntimeException {
+public class DetailedAST extends CommonAST {
     static final long serialVersionUID = 1;
-    private final AST ast;
-    CompletionException(AST ast) {
-	super("completing " + ast);
-	this.ast = ast;
+    public DetailedAST() {
     }
-    /**
-     * Return the incomplete string (minus the tab).
-     */
-    public String getText() {
-	String text = ast.getText();
-	int tab = text.indexOf("\t");
-	if (tab < 0)
-	    throw new RuntimeException("No tab found in " + ast);
-	return text.substring(0, tab);
-    }
-    // Useful?
-    public int getLine() {
-	return ast.getLine();
+    private int column;
+    private int line;
+    public void initialize(Token t) {
+	super.initialize(t);
+	this.column = t.getColumn();
+	this.line = t.getLine();
     }
     public int getColumn() {
-	return ast.getColumn();
+	return column;
+    }
+    public int getLine() {
+	return line;
     }
 }
diff --git a/frysk-core/frysk/expr/ExprSymTab.java b/frysk-core/frysk/expr/ExprSymTab.java
index 5f7da6b..eb8e5c2 100644
--- a/frysk-core/frysk/expr/ExprSymTab.java
+++ b/frysk-core/frysk/expr/ExprSymTab.java
@@ -41,7 +41,7 @@ package frysk.expr;
 
 import inua.eio.ByteBuffer;
 import inua.eio.ByteOrder;
-
+import java.util.List;
 import frysk.value.ObjectDeclaration;
 import frysk.value.Type;
 import frysk.value.Value;
@@ -77,4 +77,22 @@ public interface ExprSymTab
      * Return the wordsize.
      */    
     int getWordSize();
+    /**
+     * Complete the INCOMPLETE named object adding the completed names
+     * to CANDIDATES.
+     *
+     * Assuming a simple symtab containing: "argv", "argc", "main":
+     * complete "a" returns "argv" + "argc"; complete "main" returns
+     * "main"; and complete "bogus" returns <empty>.
+     *
+     * NB: The caller will append a space when there's only one
+     * completion so that "1+arg\t" expands to "1+argv ".
+     *
+     * NB: The caller will sort the list.
+     *
+     * XXX: This is subject to change without notice; for instance:
+     * s/List/Set/ (why jline used List I don't know); addition of a
+     * scope parameter; ...
+     */
+    void complete(String incomplete, List candidates);
 }
diff --git a/frysk-core/frysk/expr/TestArithmetics.java b/frysk-core/frysk/expr/ExpressionFactory.java
similarity index 72%
copy from frysk-core/frysk/expr/TestArithmetics.java
copy to frysk-core/frysk/expr/ExpressionFactory.java
index 5db7d88..65d01bf 100644
--- a/frysk-core/frysk/expr/TestArithmetics.java
+++ b/frysk-core/frysk/expr/ExpressionFactory.java
@@ -1,6 +1,6 @@
 // This file is part of the program FRYSK.
 //
-// Copyright 2007 Red Hat Inc.
+// Copyright 2007, Red Hat Inc.
 //
 // FRYSK is free software; you can redistribute it and/or modify it
 // under the terms of the GNU General Public License as published by
@@ -39,38 +39,47 @@
 
 package frysk.expr;
 
-import frysk.junit.TestCase;
-import frysk.value.Value;
 import java.io.StringReader;
-import frysk.expr.ScratchSymTab;
+import java.util.List;
+import java.util.Collections;
 
-/**
- * Scratch is for the case where there isn't any symbols, basic ops
- * should still work.
+/** 
+ * Create expressions and related stuff.
  */
 
-public class TestArithmetics extends TestCase {
-    private Value eval(String input) {
+public class ExpressionFactory {
+    /**
+     * Perform TAB completion on the expression.
+     */
+    public static int complete(ExprSymTab symTab, String incomplete,
+			       int offset, List candidates) {
 	try {
-	    input += (char) 3;
+	    String input = (incomplete.substring(0, offset)
+			    + '\t'
+			    + incomplete.substring(offset)
+			    +(char) 3);
 	    CExprLexer lexer = new CExprLexer(new StringReader(input));
 	    CExprParser parser = new CExprParser(lexer);
+	    parser.setASTNodeClass(DetailedAST.class.getName());
 	    parser.start();
-	    ExprSymTab symTab = new ScratchSymTab();
-	    CExprEvaluator exprEvaluator = new CExprEvaluator(symTab);
-	    return exprEvaluator.expr(parser.getAST());
 	} catch (antlr.RecognitionException e) {
 	    throw new RuntimeException(e);
 	} catch (antlr.TokenStreamException e) {
 	    throw new RuntimeException(e);
+	} catch (IncompleteIdentifierException ident) {
+	    symTab.complete(ident.getText(), candidates);
+	    switch (candidates.size()) {
+	    case 0:
+		return -1;
+	    case 1: 
+		// Append a space.
+		candidates.add(0, candidates.remove(0) + " ");
+		return ident.getColumn();
+	    default:
+		Collections.sort(candidates);
+		return ident.getColumn();
+	    }
 	}
-    }
-
-    private void checkExpr(long value, String expr) {
-	assertEquals(expr, value, eval(expr).asLong());
-    }
-
-    public void testAdd() {
-	checkExpr(3, "1 + 2");
+	return -1; // nothing completed.
     }
 }
diff --git a/frysk-core/frysk/expr/ScratchSymTab.java b/frysk-core/frysk/expr/ScratchSymTab.java
index 4df6397..05a3a07 100644
--- a/frysk-core/frysk/expr/ScratchSymTab.java
+++ b/frysk-core/frysk/expr/ScratchSymTab.java
@@ -41,7 +41,7 @@ package frysk.expr;
 
 import inua.eio.ByteBuffer;
 import inua.eio.ByteOrder;
-
+import java.util.List;
 import frysk.Config;
 import frysk.value.ObjectDeclaration;
 import frysk.value.Type;
@@ -93,4 +93,8 @@ public class ScratchSymTab implements ExprSymTab {
 	// debugger. Required for some expression evaluation.
 	return Config.getWordSize ();
     }
+
+    public void complete(String incomplete, List candidates) {
+	return;
+    }
 }
diff --git a/frysk-core/frysk/isa/TestRegisters.java b/frysk-core/frysk/expr/TestCompletion.java
similarity index 61%
copy from frysk-core/frysk/isa/TestRegisters.java
copy to frysk-core/frysk/expr/TestCompletion.java
index 7f6244f..47fbf2a 100644
--- a/frysk-core/frysk/isa/TestRegisters.java
+++ b/frysk-core/frysk/expr/TestCompletion.java
@@ -37,46 +37,48 @@
 // version and license this file solely under the GPL without
 // exception.
 
-package frysk.isa;
+package frysk.expr;
 
 import frysk.junit.TestCase;
+import java.util.List;
+import java.util.LinkedList;
 
-/**
- * The set of registers belonging to an ISA.
+/** 
+ * Create expressions and related stuff.
  */
-public class TestRegisters extends TestCase {
 
-    private Registers regs;
-    public void setUp() {
-	regs = new IA32Registers();
+public class TestCompletion extends TestCase {
+    private void complete(String incomplete, int offset,
+			  String[] expectedCandidates, int expectedOffset) {
+	ExprSymTab symTab = new TestbedSymTab();
+	List testCandidates = new LinkedList();
+	int testOffset = ExpressionFactory.complete(symTab, incomplete, offset,
+						    testCandidates);
+	assertEquals("candidates", expectedCandidates,
+		     (String[]) testCandidates.toArray(new String[0]));
+	assertEquals("offset", expectedOffset, testOffset);
     }
-    public void tearDown() {
-	regs = null;
+    private void complete(String incomplete, String[] expectedCandidates,
+			  int expectedOffset) {
+	complete(incomplete, incomplete.length(), expectedCandidates,
+		 expectedOffset);
     }
-
-    public void testGetGroup() {
-	assertEquals("getGroup", IA32Registers.MMX,
-		     regs.getGroup("mmx"));
+    public void testNoCompletions() {
+	complete("bogus", new String[0], -1);
     }
-
-    public void testGetRegister() {
-	assertEquals("getGroup", IA32Registers.FS,
-		     regs.getRegister("fs"));
+    public void testOneCompletion() {
+	complete("a", new String[] { "a " }, 0);
     }
-
-    public void testGetGroupNames() {
-	assertEquals("getGroupNames",
-		     new String[] { "general", "mmx", "sse", "segment", "all" },
-		     regs.getGroupNames());
+    public void testOneLongerCompletion() {
+	complete("c1", new String[] { "c123 "}, 0);
     }
-
-    public void testDefaultRegisterGroup() {
-	assertEquals("getDefaultRegisterGroup", IA32Registers.GENERAL,
-		     regs.getDefaultRegisterGroup());
+    public void testTwoCompletions() {
+	complete("b", new String[] { "b1", "b2" }, 0);
     }
-
-    public void testAllRegistersGroup() {
-	assertEquals("getAllRegistersGroup", IA32Registers.ALL,
-		     regs.getAllRegistersGroup());
+    public void testCompletionAfterOp() {
+	complete("a + b", new String[] { "b1", "b2" }, 4);
+    }
+    public void testCompletionBeforeOp() {
+	complete("b + a", 1, new String[] { "b1", "b2" }, 0);
     }
 }
diff --git a/frysk-core/frysk/expr/TestbedSymTab.java b/frysk-core/frysk/expr/TestbedSymTab.java
index 63664c5..a39b2b1 100644
--- a/frysk-core/frysk/expr/TestbedSymTab.java
+++ b/frysk-core/frysk/expr/TestbedSymTab.java
@@ -39,9 +39,10 @@
 
 package frysk.expr;
 
+import java.util.Iterator;
 import inua.eio.ByteBuffer;
 import inua.eio.ByteOrder;
-
+import java.util.List;
 import frysk.value.ObjectDeclaration;
 import frysk.value.Type;
 import frysk.value.Value;
@@ -70,6 +71,9 @@ class TestbedSymTab implements ExprSymTab {
     TestbedSymTab () {
 	symtab = new HashMap();
 	symtab.put("a", c1);
+	symtab.put("b1", c1);
+	symtab.put("b2", c1);
+	symtab.put("c123", c1);
     }
 
     /**
@@ -117,4 +121,12 @@ class TestbedSymTab implements ExprSymTab {
     public int getWordSize() {
 	throw new RuntimeException("no word size");
     }
+
+    public void complete(String incomplete, List candidates) {
+	for (Iterator i = symtab.keySet().iterator(); i.hasNext(); ) {
+	    String sym = (String)i.next();
+	    if (sym.startsWith(incomplete))
+		candidates.add(sym);
+	}
+    }
 }
diff --git a/frysk-core/frysk/hpd/ChangeLog b/frysk-core/frysk/hpd/ChangeLog


hooks/post-receive
--
frysk system monitor/debugger



More information about the Frysk-cvs mailing list