Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text.RegularExpressions;
- namespace Lexer
- {
- public class Lexer
- {
- private string text, buf;
- private int line = 1, col = 0, tmp = 0;
- public Lexer(string text)
- {
- this.text = text;
- }
- public Token Next()
- {
- Token t = Peek();
- buf = null;
- return t;
- }
- public Token Peek()
- {
- bool eow = false, isID = true;
- char tmpChar;
- string num = null;
- int tmpCol = col, tmpLine = line;
- while (buf == null) {
- tmpCol = col;
- tmpLine = line;
- eow = false;
- while (!eow)
- {
- tmpChar = text[col];
- if (text[col] == ' ')
- {
- col++;
- eow = true;
- }
- else if (text[col] == '\n' || text[col] == '\r')
- {
- if (buf == null)
- {
- if (text[++col] == '\n') col++;
- line++;
- tmp = col;
- }
- eow = true;
- }
- else if (text[col] == '\t')
- {
- tmp -= 8;
- }
- else if (text[col] == '(' || text[col] == ')' || text[col] == ',' || text[col] == ';')
- {
- if (buf == null)
- {
- buf += text[col++];
- Token tmpTok = new Token(Token.Type.SEP, buf, tmpLine, (tmpCol - tmp)+1);
- return tmpTok;
- }
- eow = true;
- }
- else if (text[col] == '^' && text[col + 1] == 'D')
- {
- buf += text[col] + text[col + 1];
- Token tmpTok = new Token(Token.Type.EOF, null, tmpLine, (tmpCol - tmp) + 1);
- return tmpTok;
- }
- else if (Char.IsLetter(text[col]) || Char.IsDigit(text[col]))
- {
- buf += text[col++];
- }
- else if ( text[col] == '+' || text[col] == ':')
- {
- if (buf == null)
- {
- buf += text[col++];
- if (text[col] == '=')
- {
- buf += text[col++];
- }
- Token tmpTok = new Token(Token.Type.OP, buf, tmpLine, (tmpCol - tmp) + 1);
- return tmpTok;
- }
- eow = true;
- }
- else
- {
- col++;
- eow = true;
- }
- }
- }
- if (string.Equals(buf, "print"))
- {
- Token tmpTok = new Token(Token.Type.KEYW, buf, tmpLine, (tmpCol - tmp) + 1);
- return tmpTok;
- }
- else
- {
- if (Char.IsDigit(buf[0]))
- {
- num += buf[0];
- for (int i = 1; i < buf.Length; i++)
- {
- if (!Char.IsDigit(buf[i]))
- {
- col -= buf.Length - i;
- i = buf.Length;
- }
- else
- {
- num += buf[i];
- }
- }
- Token tmpTok = new Token(Token.Type.NUM, num, tmpLine, (tmpCol - tmp) + 1);
- return tmpTok;
- }
- else if (Char.IsLetter(buf[0]))
- {
- for (int i = 1; i < buf.Length; i++)
- {
- if (Char.IsLetter(buf[i]) || Char.IsDigit(buf[i]))
- isID = true;
- else
- isID = false;
- }
- if (isID)
- {
- Token tmpTok = new Token(Token.Type.ID, buf, line, (tmpCol - tmp) + 1);
- return tmpTok;
- }
- }
- }
- Token t = new Token(0, null,0,0);
- return t;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement