1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
// $Id$
using System;
using System.IO;
namespace Lisp
{
public class Parser {
public enum LispType {
START_LIST,
END_LIST,
SYMBOL,
INTEGER,
STRING,
REAL,
BOOLEAN
};
private Lexer lexer;
private Lexer.TokenType token;
public Parser(StreamReader stream) {
lexer = new Lexer(stream);
}
public bool Parse() {
token = lexer.GetNextToken();
if(delayinc) {
depth++;
delayinc = false;
}
if(token == Lexer.TokenType.EOF) {
depth = 0;
return false;
}
/*
Console.WriteLine("Token: " + token.ToString() + " - " +
lexer.TokenString);
*/
switch(token) {
case Lexer.TokenType.CLOSE_PAREN:
if(depth == 0)
throw new Exception("Parse Error: unexpected )");
depth--;
type = LispType.END_LIST;
break;
case Lexer.TokenType.OPEN_PAREN:
type = LispType.START_LIST;
delayinc = true;
break;
case Lexer.TokenType.SYMBOL:
type = LispType.SYMBOL;
break;
case Lexer.TokenType.STRING:
type = LispType.STRING;
break;
case Lexer.TokenType.TRUE:
type = LispType.BOOLEAN;
break;
case Lexer.TokenType.INTEGER:
type = LispType.INTEGER;
break;
}
return true;
}
public static void ParseIntList(Parser parser, System.Collections.Generic.List<int> intList) {
int d = parser.Depth;
while(parser.Depth >= d) {
intList.Add(parser.IntegerValue);
parser.Parse();
}
}
private LispType type;
public LispType Type {
get { return type; }
}
private bool delayinc;
private int depth;
public int Depth {
get { return depth; }
}
//public int IntValue
public string SymbolValue {
get { return lexer.TokenString; }
}
public string StringValue {
get { return lexer.TokenString; }
}
public int IntegerValue {
get { return Int32.Parse(lexer.TokenString); }
}
public bool BoolValue {
get { return StringValue == "t"; }
}
public float FloatValue {
get { return Single.Parse(lexer.TokenString); }
}
}
}
|