Advertisement
Lennnyo

Untitled

Dec 14th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace Lexer
  5. {
  6. public class Lexer
  7. {
  8. private string text, buf;
  9. private int line = 1, col = 0, tmp = 0;
  10.  
  11. public Lexer(string text)
  12. {
  13. this.text = text;
  14. }
  15.  
  16. public Token Next()
  17. {
  18. Token t = Peek();
  19. buf = null;
  20. return t;
  21.  
  22. }
  23.  
  24. public Token Peek()
  25. {
  26. bool eow = false, isID = true;
  27. char tmpChar;
  28. string num = null;
  29. int tmpCol = col, tmpLine = line;
  30.  
  31. while (buf == null) {
  32. tmpCol = col;
  33. tmpLine = line;
  34. eow = false;
  35. while (!eow)
  36. {
  37. tmpChar = text[col];
  38. if (text[col] == ' ')
  39. {
  40. col++;
  41. eow = true;
  42. }
  43. else if (text[col] == '\n' || text[col] == '\r')
  44. {
  45. if (buf == null)
  46. {
  47. if (text[++col] == '\n') col++;
  48. line++;
  49. tmp = col;
  50. }
  51. eow = true;
  52. }
  53. else if (text[col] == '\t')
  54. {
  55. tmp -= 8;
  56. }
  57. else if (text[col] == '(' || text[col] == ')' || text[col] == ',' || text[col] == ';')
  58. {
  59. if (buf == null)
  60. {
  61. buf += text[col++];
  62. Token tmpTok = new Token(Token.Type.SEP, buf, tmpLine, (tmpCol - tmp)+1);
  63. return tmpTok;
  64. }
  65. eow = true;
  66. }
  67. else if (text[col] == '^' && text[col + 1] == 'D')
  68. {
  69. buf += text[col] + text[col + 1];
  70. Token tmpTok = new Token(Token.Type.EOF, null, tmpLine, (tmpCol - tmp) + 1);
  71. return tmpTok;
  72. }
  73. else if (Char.IsLetter(text[col]) || Char.IsDigit(text[col]))
  74. {
  75. buf += text[col++];
  76. }
  77. else if ( text[col] == '+' || text[col] == ':')
  78. {
  79. if (buf == null)
  80. {
  81. buf += text[col++];
  82. if (text[col] == '=')
  83. {
  84. buf += text[col++];
  85. }
  86. Token tmpTok = new Token(Token.Type.OP, buf, tmpLine, (tmpCol - tmp) + 1);
  87. return tmpTok;
  88. }
  89. eow = true;
  90. }
  91. else
  92. {
  93. col++;
  94. eow = true;
  95. }
  96.  
  97. }
  98. }
  99. if (string.Equals(buf, "print"))
  100. {
  101. Token tmpTok = new Token(Token.Type.KEYW, buf, tmpLine, (tmpCol - tmp) + 1);
  102. return tmpTok;
  103. }
  104. else
  105. {
  106. if (Char.IsDigit(buf[0]))
  107. {
  108. num += buf[0];
  109. for (int i = 1; i < buf.Length; i++)
  110. {
  111. if (!Char.IsDigit(buf[i]))
  112. {
  113. col -= buf.Length - i;
  114. i = buf.Length;
  115. }
  116. else
  117. {
  118. num += buf[i];
  119. }
  120. }
  121. Token tmpTok = new Token(Token.Type.NUM, num, tmpLine, (tmpCol - tmp) + 1);
  122. return tmpTok;
  123. }
  124. else if (Char.IsLetter(buf[0]))
  125. {
  126. for (int i = 1; i < buf.Length; i++)
  127. {
  128. if (Char.IsLetter(buf[i]) || Char.IsDigit(buf[i]))
  129. isID = true;
  130. else
  131. isID = false;
  132. }
  133. if (isID)
  134. {
  135. Token tmpTok = new Token(Token.Type.ID, buf, line, (tmpCol - tmp) + 1);
  136. return tmpTok;
  137. }
  138. }
  139. }
  140.  
  141. Token t = new Token(0, null,0,0);
  142. return t;
  143. }
  144.  
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement