Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,111 @@
package jfkbits;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.util.Iterator;
public class LispTokenizer implements Iterator<Token>
{
// Instance variables have default access to allow unit tests access.
StreamTokenizer m_tokenizer;
IOException m_ioexn;
/** Constructs a tokenizer that scans input from the given string.
* @param src A string containing S-expressions.
*/
public LispTokenizer(String src)
{
this(new StringReader(src));
}
/** Constructs a tokenizer that scans input from the given Reader.
* @param r Reader for the character input source
*/
public LispTokenizer(Reader r)
{
if(r == null)
r = new StringReader("");
BufferedReader buffrdr = new BufferedReader(r);
m_tokenizer = new StreamTokenizer(buffrdr);
m_tokenizer.resetSyntax(); // We don't like the default settings
m_tokenizer.whitespaceChars(0, ' ');
m_tokenizer.wordChars(' '+1,255);
m_tokenizer.ordinaryChar('(');
m_tokenizer.ordinaryChar(')');
m_tokenizer.ordinaryChar('\'');
m_tokenizer.commentChar(';');
m_tokenizer.quoteChar('"');
}
public Token peekToken()
{
if(m_ioexn != null)
return null;
try
{
m_tokenizer.nextToken();
}
catch(IOException e)
{
m_ioexn = e;
return null;
}
if(m_tokenizer.ttype == StreamTokenizer.TT_EOF)
return null;
Token token = new Token(m_tokenizer);
m_tokenizer.pushBack();
return token;
}
public boolean hasNext()
{
if(m_ioexn != null)
return false;
try
{
m_tokenizer.nextToken();
}
catch(IOException e)
{
m_ioexn = e;
return false;
}
if(m_tokenizer.ttype == StreamTokenizer.TT_EOF)
return false;
m_tokenizer.pushBack();
return true;
}
/** Return the most recently caught IOException, if any,
*
* @return
*/
public IOException getIOException()
{
return m_ioexn;
}
public Token next()
{
try
{
m_tokenizer.nextToken();
}
catch(IOException e)
{
m_ioexn = e;
return null;
}
Token token = new Token(m_tokenizer);
return token;
}
public void remove()
{
}
}

View file

@ -0,0 +1,29 @@
package jfkbits;
import java.io.StreamTokenizer;
public class Token
{
public static final int SYMBOL = StreamTokenizer.TT_WORD;
public int type;
public String text;
public int line;
public Token(StreamTokenizer tzr)
{
this.type = tzr.ttype;
this.text = tzr.sval;
this.line = tzr.lineno();
}
public String toString()
{
switch(this.type)
{
case SYMBOL:
case '"':
return this.text;
default:
return String.valueOf((char)this.type);
}
}
}

View file

@ -0,0 +1,17 @@
package jfkbits;
import jfkbits.LispParser.Expr;
public class Atom implements Expr
{
String name;
public String toString()
{
return name;
}
public Atom(String text)
{
name = text;
}
}

View file

@ -0,0 +1,20 @@
package jfkbits;
public class StringAtom extends Atom
{
public String toString()
{
// StreamTokenizer hardcodes escaping with \, and doesn't allow \n inside words
String escaped = name.replace("\\", "\\\\").replace("\n", "\\n").replace("\r", "\\r").replace("\"", "\\\"");
return "\""+escaped+"\"";
}
public StringAtom(String text)
{
super(text);
}
public String getValue()
{
return name;
}
}

View file

@ -0,0 +1,71 @@
package jfkbits;
import java.util.AbstractCollection;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ArrayList;
import jfkbits.LispParser.Expr;
public class ExprList extends ArrayList<Expr> implements Expr
{
ExprList parent = null;
int indent =1;
public int getIndent()
{
if (parent != null)
{
return parent.getIndent()+indent;
}
else return 0;
}
public void setIndent(int indent)
{
this.indent = indent;
}
public void setParent(ExprList parent)
{
this.parent = parent;
}
public String toString()
{
String indent = "";
if (parent != null && parent.get(0) != this)
{
indent = "\n";
char[] chars = new char[getIndent()];
Arrays.fill(chars, ' ');
indent += new String(chars);
}
String output = indent+"(";
for(Iterator<Expr> it=this.iterator(); it.hasNext(); )
{
Expr expr = it.next();
output += expr.toString();
if (it.hasNext())
output += " ";
}
output += ")";
return output;
}
@Override
public synchronized boolean add(Expr e)
{
if (e instanceof ExprList)
{
((ExprList) e).setParent(this);
if (size() != 0 && get(0) instanceof Atom)
((ExprList) e).setIndent(2);
}
return super.add(e);
}
}

View file

@ -0,0 +1,47 @@
package jfkbits;
public class LispParser
{
LispTokenizer tokenizer;
public LispParser(LispTokenizer input)
{
tokenizer=input;
}
public class ParseException extends Exception
{
}
public interface Expr
{
// abstract parent for Atom and ExprList
}
public Expr parseExpr() throws ParseException
{
Token token = tokenizer.next();
switch(token.type)
{
case '(': return parseExprList(token);
case '"': return new StringAtom(token.text);
default: return new Atom(token.text);
}
}
protected ExprList parseExprList(Token openParen) throws ParseException
{
ExprList acc = new ExprList();
while(tokenizer.peekToken().type != ')')
{
Expr element = parseExpr();
acc.add(element);
}
Token closeParen = tokenizer.next();
return acc;
}
}

View file

@ -0,0 +1,26 @@
import jfkbits.ExprList;
import jfkbits.LispParser;
import jfkbits.LispParser.ParseException;
import jfkbits.LispTokenizer;
public class LispParserDemo
{
public static void main(String args[])
{
LispTokenizer tzr = new LispTokenizer(
"((data \"quoted data\" 123 4.5)\n (data (!@# (4.5) \"(more\" \"data)\")))");
LispParser parser = new LispParser(tzr);
try
{
Expr result = parser.parseExpr();
System.out.println(result);
}
catch (ParseException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}