Parser.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * From supertux/tools/tilemanager
  3. */
  4. using System;
  5. using System.IO;
  6. using System.Globalization;
  7. namespace Lisp
  8. {
  9. public class Parser {
  10. public enum LispType {
  11. START_LIST,
  12. END_LIST,
  13. SYMBOL,
  14. INTEGER,
  15. STRING,
  16. REAL,
  17. BOOLEAN
  18. };
  19. private Lexer lexer;
  20. private Lexer.TokenType token;
  21. public Parser(StreamReader stream) {
  22. lexer = new Lexer(stream);
  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. Console.WriteLine("Token: " + token.ToString() + " - " +
  36. lexer.TokenString);
  37. */
  38. switch(token) {
  39. case Lexer.TokenType.CLOSE_PAREN:
  40. if(depth == 0)
  41. throw new Exception("Parse Error: unexpected )");
  42. depth--;
  43. type = LispType.END_LIST;
  44. break;
  45. case Lexer.TokenType.OPEN_PAREN:
  46. type = LispType.START_LIST;
  47. delayinc = true;
  48. break;
  49. case Lexer.TokenType.SYMBOL:
  50. type = LispType.SYMBOL;
  51. break;
  52. case Lexer.TokenType.STRING:
  53. type = LispType.STRING;
  54. break;
  55. case Lexer.TokenType.TRUE:
  56. type = LispType.BOOLEAN;
  57. break;
  58. case Lexer.TokenType.INTEGER:
  59. type = LispType.INTEGER;
  60. break;
  61. }
  62. return true;
  63. }
  64. private LispType type;
  65. public LispType Type {
  66. get { return type; }
  67. }
  68. private bool delayinc;
  69. private int depth;
  70. public int Depth {
  71. get { return depth; }
  72. }
  73. //public int IntValue
  74. public string SymbolValue {
  75. get { return lexer.TokenString; }
  76. }
  77. public string StringValue {
  78. get { return lexer.TokenString; }
  79. }
  80. public int IntegerValue {
  81. get { return Int32.Parse(lexer.TokenString); }
  82. }
  83. public bool BoolValue {
  84. get { return StringValue == "t" ? true : false; }
  85. }
  86. public float FloatValue {
  87. get { return Single.Parse(lexer.TokenString, new CultureInfo("en-US")); }
  88. }
  89. }
  90. }