This is the mail archive of the kawa@sources.redhat.com mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Square brackets in Kawa


What I think I will do is make the default be that '[' and ']' are
token constituents.  Most people who have spoken up seem to
be in favor of that, as is my leaning.

However, I want to make it easy it easy to change the syntax.
I thought about it a bit, and came up with the appended patch.
The ReaderTypeSpec.java class is directly from Bruce's patch,
but the changes to ReadTable.java are mine.  The new
setBracketMode should make it easy to change the syntax to
match your preference.  It's trivial to define a Scheme
procedure that calls setBracketMode on any given readtable.

Bruce, could you let me know how this works for you?

    --Per


Index: ReadTable.java
===================================================================
RCS file: /cvs/kawa/kawa/gnu/kawa/lispexpr/ReadTable.java,v
retrieving revision 1.1
diff -u -r1.1 ReadTable.java
--- ReadTable.java	2001/04/23 02:19:08	1.1
+++ ReadTable.java	2001/11/20 00:46:27
@@ -49,13 +49,10 @@
     tab.set('.',  entry);
     tab.set('/',  entry);
     tab.set(':',  entry);
-    tab.set('<',  entry);
     tab.set('=',  entry);
     tab.set('>',  entry);
     tab.set('?',  entry);
     tab.set('@',  entry);
-    tab.set('[',  entry);
-    tab.set(']',  entry);
     tab.set('^',  entry);
     tab.set('_',  entry);
     tab.set('{',  entry);
@@ -68,17 +65,54 @@
     tab.set(';',  ReaderIgnoreRestOfLine.getInstance());
     tab.set('(',  ReaderParens.getInstance('(', ')'));
 
-    // Scheme, CommonLisp only - Elisp has new ReaderVector(']')
-    // We want '[' to be non-terminating for the sake of say '<char[]>'.
-    tab.set('[',  ReaderParens.getInstance('[', ']',
-					   ReadTable.NON_TERMINATING_MACRO));
-
     tab.set('\'', new ReaderQuote(Interpreter.quote_sym));
     tab.set('`',  new ReaderQuote(Interpreter.quasiquote_sym));
     tab.set(',',  new ReaderQuote(Interpreter.unquote_sym,
 				 '@', Interpreter.unquotesplicing_sym));
+
+    tab.setBracketMode();  // Sets the entries for '[', ']', and '<'.
+
     return tab;
   }
+
+  /** Default value to pass to setBracketMode() unless overridden. */
+  public static int defaultBracketMode = -1;
+
+  /** Specify how '[' and ']' (and '<') are handled.
+   * The value - means that '[' and ']' are plain token constituents.
+   * The value 0 means that '[' and ']' are equivalent to '(' and ')'.
+   * The value 1 means that '[' and ']' are equivalent to '(' and ')', except
+   * within a token starting with '<', in which case they are constituents.
+   * This is so '[' is non-terminating when reading say '<char[]>'
+   */
+  public void setBracketMode(int mode)
+  {
+    if (mode <= 0)
+      {
+	ReadTableEntry token = ReadTableEntry.getConstituentInstance();
+	set('<', token);
+	if (mode < 0)
+	  {
+	    set('[', token);
+	    set(']', token);
+	  }
+      }
+    else
+      set('<', new ReaderTypespec());
+    if (mode >= 0)
+      {
+	set('[', ReaderParens.getInstance('[', ']'));
+	remove(']');
+      }
+  }
+
+  /** Specify how '[' and ']' are handled.
+   * Overless overridden, uses defaultBracketMode. */
+  public void setBracketMode()
+  {
+    setBracketMode(defaultBracketMode);
+  }
+  
 
   public static ReadTable getCurrent() { return current; }
 
Index: Makefile.am
===================================================================
RCS file: /cvs/kawa/kawa/gnu/kawa/lispexpr/Makefile.am,v
retrieving revision 1.7
diff -u -r1.7 Makefile.am
--- Makefile.am	2001/07/18 20:02:12	1.7
+++ Makefile.am	2001/11/20 00:46:27
@@ -17,6 +17,7 @@
   ReaderParens.java \
   ReaderQuote.java \
   ReaderString.java \
+  ReaderTypespec.java \
   ReaderVector.java \
   ReadTable.java \
   ReadTableEntry.java \
Index: ReaderTypespec.java
===================================================================
RCS file: ReaderTypespec.java
diff -N ReaderTypespec.java
--- /dev/null	Tue May  5 13:32:27 1998
+++ ReaderTypespec.java	Mon Nov 19 16:46:27 2001
@@ -0,0 +1,83 @@
+// Copyright (c) 2001  Per M.A. Bothner
+// This is free software;  for terms and warranty disclaimer see ./COPYING.
+
+package gnu.kawa.lispexpr;
+import gnu.text.*;
+import gnu.mapping.InPort;
+
+public class ReaderTypespec extends ReadTableEntry
+{
+  public int getKind()
+  {
+    return ReadTable.NON_TERMINATING_MACRO;
+  }
+
+  public Object read (Lexer in, int ch, int count)
+    throws java.io.IOException, SyntaxException
+  {
+    int startPos = in.tokenBufferLength;
+    LineBufferedReader port = in.getPort();
+    ReadTable rtable =
+	(in instanceof LispReader) ?
+	((LispReader) in).getReadTable()
+	: ReadTable.getCurrent();
+    ReadTableEntry entry;
+    char saveReadState = '\0';
+    in.tokenBufferAppend(ch);
+    int c = ch;
+    int prev;
+    if (port instanceof InPort)
+      {
+	saveReadState = ((InPort) port).readState;
+	((InPort) port).readState = (char) ch;
+      }
+    try
+      {
+	boolean got_open_square = false;
+	for (;;)
+	  {
+	    int next;
+
+	    prev = c;
+
+	    if (port.pos < port.limit && prev != '\n')
+	      c = port.buffer[port.pos++];
+	    else
+	      c = port.read();
+	    if (c == '\\')
+	      {
+		if (in instanceof LispReader)
+		  c = ((LispReader) in).readEscape();
+		else
+		  c = port.read();
+	      }
+	    else
+	      {
+		if ( (!got_open_square && c == '['
+		      && true == (got_open_square = true))
+		     || (got_open_square && c == ']'
+			 && false == (got_open_square = false))
+		     || (null != (entry = rtable.lookup(c))
+			 && entry.getKind() == ReadTable.CONSTITUENT))
+		  {
+		      in.tokenBufferAppend(c);
+		      continue;
+		  }
+		else
+		  {
+		    in.unread(c);
+		    break;
+		  }
+	      }
+	    }
+	return (new java.lang.String (in.tokenBuffer, startPos,
+				      in.tokenBufferLength - startPos)).intern();
+      }
+    finally
+      {
+	in.tokenBufferLength = startPos;
+	if (port instanceof InPort)
+	  ((InPort) port).readState = saveReadState;
+      }
+  }
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]