Created
July 11, 2016 16:06
-
-
Save DankRank/587200ef8df915e55f58457722eca610 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ParserCalculator | |
{ | |
enum TokenType | |
{ | |
UNDEFINED, | |
ALPHA, | |
NUMERIC, | |
SPECIAL, | |
BRACKET, | |
QUOTE, | |
QSTR | |
} | |
class Token | |
{ | |
public TokenType type{get;set;} | |
public string data{get;set;} | |
} | |
class Parser{ | |
public List<Token> toks = new List<Token>(); | |
Token t; | |
char[] alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_".ToCharArray(); | |
char[] numerics = "0123456789.".ToCharArray(); | |
char[] specials = "!#%^&*(-=,/\\;++".ToCharArray(); //@$ | |
char[] brackets = "()[]<>{}".ToCharArray(); | |
char[] quot="\"'".ToCharArray(); | |
char curquot = '\0'; | |
void BeginToken(char c) | |
{ | |
if(quot.Contains(c)){ | |
t = new Token(); | |
t.type=TokenType.QUOTE; | |
t.data=c.ToString(); | |
EndToken(); | |
t = new Token(); | |
t.type=TokenType.QSTR; | |
t.data=""; | |
curquot = c; | |
}else if (specials.Contains(c)){ | |
t = new Token(); | |
t.type = TokenType.SPECIAL; | |
t.data = c.ToString(); | |
}else if (brackets.Contains(c)){ | |
t = new Token(); | |
t.type = TokenType.BRACKET; | |
t.data = c.ToString(); | |
EndToken(); | |
t = null; | |
} | |
else if (numerics.Contains(c)) | |
{ | |
t = new Token(); | |
t.type = TokenType.NUMERIC; | |
t.data = c.ToString(); | |
} | |
else if(alpha.Contains(c)) | |
{ | |
t = new Token(); | |
t.type = TokenType.ALPHA; | |
t.data = c.ToString(); | |
} | |
} | |
void AppendToToken(char c){ | |
t.data += c.ToString(); | |
} | |
void EndToken() | |
{ | |
if(t!=null) toks.Add(t); | |
if (t.type == TokenType.QSTR) | |
{ | |
t = new Token(); | |
t.type = TokenType.QUOTE; | |
t.data = curquot.ToString(); | |
EndToken(); | |
} | |
t = null; | |
} | |
bool tokenbroken = true; | |
public Parser(string s) | |
{ | |
for (int i = 0; i < s.Length; i++) | |
{ | |
if (Char.IsWhiteSpace(s[i]) && t!=null && t.type!=TokenType.QSTR) | |
{ | |
EndToken(); | |
tokenbroken = true; | |
continue; | |
} | |
if (tokenbroken) | |
{ | |
BeginToken(s[i]); | |
tokenbroken = false; | |
} | |
else | |
{ | |
if (t == null) | |
{ | |
BeginToken(s[i]); | |
} | |
else switch (t.type) | |
{ | |
case TokenType.ALPHA: | |
if (alpha.Contains(s[i])) | |
{ | |
AppendToToken(s[i]); | |
} | |
else | |
{ | |
EndToken(); | |
BeginToken(s[i]); | |
} | |
break; | |
case TokenType.NUMERIC: | |
if (numerics.Contains(s[i])) | |
{ | |
AppendToToken(s[i]); | |
} | |
else | |
{ | |
EndToken(); | |
BeginToken(s[i]); | |
} | |
break; | |
case TokenType.SPECIAL: | |
if (specials.Contains(s[i])) | |
{ | |
AppendToToken(s[i]); | |
} | |
else | |
{ | |
EndToken(); | |
BeginToken(s[i]); | |
} | |
break; | |
case TokenType.QSTR: | |
if (s[i] == curquot) | |
EndToken(); | |
else AppendToToken(s[i]); | |
break; | |
} | |
} | |
} | |
if (t!=null) EndToken(); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string line = Console.ReadLine(); | |
Parser p = new Parser(line); | |
foreach(var tok in p.toks){ | |
Console.WriteLine("{0}:'{1}'", tok.type.ToString(), tok.data); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment