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]

Square brackets in Kawa


The NEWS file for Kawa  1.6.58 states:
Square brackets [ ... ] are allowed as a synonym of parentheses ( ... ).

If option 1 is chosen for simplicity, I would humbly suggest removing
this functionality, which only partially works.

However, if option 3 is chosen, it can work.  Try this in a kawa.repl
that isn't patched for option 3 and see what happens:

#|kawa:1|# [eq? 'foo 'foo]
#t
#|kawa:2|# <char[]>
Type char[]
#|kawa:3|# [procedure? <]
#t

Currently, only the second expression will work.  With my previous
patch, the first will also work.  With the patch below, all three
work.  In addition, integration with Kawa will be easier for BRL, FX
and .pfn files.

Note that the tiny bit of additional complexity is localized.  It only
applies when a token starts with <.  While such a token is being read,
there is exactly one additional allowed constituent character at any
given time (either '[' or ']').  In all other cases it works just as
it does now.

2001-11-01  Bruce Lewis  <brlewis@users.sourceforge.net>

	* ReadTable.java:  Treat '[' and ']' like parens.
	Handle '<' with ReaderTypespec
	* ReaderTypespec.java:	New class, extends ReadTableEntry
	* Makefile.am (java_sources):  Added ReaderTypespec.java

Index: gnu/kawa/lispexpr/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/01 16:40:14
@@ -17,6 +17,7 @@
   ReaderParens.java \
   ReaderQuote.java \
   ReaderString.java \
+  ReaderTypespec.java \
   ReaderVector.java \
   ReadTable.java \
   ReadTableEntry.java \
Index: gnu/kawa/lispexpr/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/01 16:40:14
@@ -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);
@@ -69,9 +66,9 @@
     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('[',  ReaderParens.getInstance('[', ']'));
+    // We want '[' to be non-terminating when reading say '<char[]>'.
+    tab.set('<',  new ReaderTypespec());
 
     tab.set('\'', new ReaderQuote(Interpreter.quote_sym));
     tab.set('`',  new ReaderQuote(Interpreter.quasiquote_sym));
Index: gnu/kawa/lispexpr/ReaderTypespec.java
===================================================================
RCS file: ReaderTypespec.java
diff -N ReaderTypespec.java
--- /dev/null	Tue May  5 13:32:27 1998
+++ ReaderTypespec.java	Thu Nov  1 08:40:14 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]