Removed trailing whitespaces in tilemanager source files (yes I know that code needs...
[supertux.git] / tools / tilemanager / Parser.cs
1 using System;
2 using System.IO;
3
4 namespace Lisp
5 {
6
7 public class Parser {
8     public enum LispType {
9         START_LIST,
10         END_LIST,
11         SYMBOL,
12         INTEGER,
13         STRING,
14         REAL,
15         BOOLEAN
16     };
17     private Lexer lexer;
18     private Lexer.TokenType token;
19
20     public Parser(StreamReader stream) {
21         lexer = new Lexer(stream);
22     }
23
24     public bool Parse() {
25         token = lexer.GetNextToken();
26         if(delayinc) {
27             depth++;
28             delayinc = false;
29         }
30         if(token == Lexer.TokenType.EOF) {
31             depth = 0;
32             return false;
33         }
34
35         /*
36         Console.WriteLine("Token: " + token.ToString() + " - " +
37                 lexer.TokenString);
38         */
39         switch(token) {
40             case Lexer.TokenType.CLOSE_PAREN:
41                 if(depth == 0)
42                     throw new Exception("Parse Error: unexpected )");
43                 depth--;
44                 type = LispType.END_LIST;
45                 break;
46             case Lexer.TokenType.OPEN_PAREN:
47                 type = LispType.START_LIST;
48                 delayinc = true;
49                 break;
50             case Lexer.TokenType.SYMBOL:
51                 type = LispType.SYMBOL;
52                 break;
53             case Lexer.TokenType.STRING:
54                 type = LispType.STRING;
55                 break;
56             case Lexer.TokenType.TRUE:
57                 type = LispType.BOOLEAN;
58                 break;
59             case Lexer.TokenType.INTEGER:
60                 type = LispType.INTEGER;
61                 break;
62         }
63         return true;
64     }
65
66     private LispType type;
67     public LispType Type {
68         get { return type; }
69     }
70     private bool delayinc;
71     private int depth;
72     public int Depth {
73         get { return depth; }
74     }
75     //public int IntValue
76     public string SymbolValue {
77         get { return lexer.TokenString; }
78     }
79     public string StringValue {
80         get { return lexer.TokenString; }
81     }
82     public int IntegerValue {
83         get { return Int32.Parse(lexer.TokenString); }
84     }
85     public bool BoolValue {
86         get { return StringValue == "t"; }
87     }
88     public float FloatValue {
89         get { return Single.Parse(lexer.TokenString); }
90     }
91 }
92
93 }