Trivial Prolog in Java

001: package ca.draisey.free.tprolog;
002:
003:
004:
005:
006:
007: final class Lex {
008:         Lex( java.io.Reader rin )
009:         {
010:                 cin = new java.io.PushbackReader( rin );
011:         }
012:
013:         // the exported interface
014:         final static int EOF = -1;
015:         boolean popif( int t ) throws LexException
016:         {
017:                 int c = lead();
018:                 if( c == t ) return true;
019:                 else {
020:                         unread( c );
021:                         return false;
022:                 }
023:         }
024:         boolean peekatom() throws LexException
025:         {
026:                 int c = peek();
027:                 return ( c >= 'a' && c <= 'z' );
028:         }
029:         boolean peekvariable() throws LexException
030:         {
031:                 int c = peek();
032:                 return ( c >= 'A' && c <= 'Z' || c == '_' );
033:         }
034:         void popso( int t ) throws LexException
035:         {
036:                 if( lead() != t ) throw new LexException( "not a " + String.valueOf((char) t ) );
037:                 return; 
038:         }
039:         boolean popifqq() throws LexException
040:         {
041:                 int c = read();
042:                 if( c == '"' ) return true;
043:                 else {
044:                         unread( c );
045:                         return false;
046:                 }
047:         }
048:         String popnonqq() throws LexException
049:         {
050:                 int c = read();
051:                 if ( c == '\'' || c == '"' ) throw new LexException( "illegal close quote character" );
052:                 return "'" + String.valueOf( (char)c ) + "'";
053:         }
054:         String popatom() throws LexException
055:         {
056:                 int c = lead();
057:                 if( !( c >= 'a' && c <= 'z' ) ) throw new LexException( "not an atom" );
058:                 StringBuffer s = new StringBuffer();
059:                 do {
060:                         s.append( (char)c );
061:                         c = read();
062:                 }
063:                 while( c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_' || c >= '0' && c <= '9' );
064:                 unread( c );
065:                 return s.toString();
066:         }
067:         String popvariable() throws LexException
068:         {
069:                 int c = lead();
070:                 if( !( c >= 'A' && c <= 'Z' || c == '_' ) ) throw new LexException( "not a variable" );
071:                 StringBuffer s = new StringBuffer();
072:                 do {
073:                         s.append( (char)c );
074:                         c = read();
075:                 }
076:                 while( c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_' || c >= '0' && c <= '9' );
077:                 unread( c );
078:                 return s.toString();
079:         }
080:         
081:         // private reader interface
082:         private int read() throws LexException
083:         {
084:                 try {
085:                         return cin.read();
086:                 }
087:                 catch( java.io.IOException x ) {
088:                         throw new LexException( "read error" );
089:                 }
090:         }
091:         private int lead() throws LexException
092:         {
093:                 int c;
094:                 do c = read();
095:                 while( c >= 0 && c <= 32 );
096:                 return c;
097:         }
098:         private void unread( int c ) throws LexException
099:         {
100:                 try {
101:                         if( c >= 0 ) cin.unread( c );
102:                 }
103:                 catch( java.io.IOException x ) {
104:                         throw new LexException( "unread error" );
105:                 }
106:         }
107:         private int peek() throws LexException
108:         {
109:                 int c = lead();
110:                 unread( c );
111:                 return c;
112:         }
113:
114:         // input stream
115:         private final java.io.PushbackReader cin;
116: }
117:
118: // fin