Parser removed

This commit is contained in:
HojouFotytu 2020-08-08 12:08:36 +09:00
parent 992ba2a713
commit e7b4b6ba7d
2 changed files with 3 additions and 204 deletions

View File

@ -9,205 +9,6 @@ using System.Text.RegularExpressions;
namespace Core.ProfitTrailer namespace Core.ProfitTrailer
{ {
public class OperandToken : Token
{
}
public class OrToken : OperandToken
{
}
public class AndToken : OperandToken
{
}
public class BooleanValueToken : Token
{
}
public class FalseToken : BooleanValueToken
{
}
public class TrueToken : BooleanValueToken
{
}
public class ParenthesisToken : Token
{
}
public class ClosedParenthesisToken : ParenthesisToken
{
}
public class OpenParenthesisToken : ParenthesisToken
{
}
public class NegationToken : Token
{
}
public abstract class Token
{
}
public class Tokenizer
{
private readonly StringReader _reader;
private string _text;
public Tokenizer(string text)
{
_text = text;
_reader = new StringReader(text);
}
public IEnumerable<Token> Tokenize()
{
var tokens = new List<Token>();
while (_reader.Peek() != -1)
{
while (Char.IsWhiteSpace((char)_reader.Peek()))
{
_reader.Read();
}
if (_reader.Peek() == -1)
break;
var c = (char)_reader.Peek();
switch (c)
{
case '!':
tokens.Add(new NegationToken());
_reader.Read();
break;
case '(':
tokens.Add(new OpenParenthesisToken());
_reader.Read();
break;
case ')':
tokens.Add(new ClosedParenthesisToken());
_reader.Read();
break;
default:
if (Char.IsLetter(c))
{
var token = ParseKeyword();
tokens.Add(token);
}
else
{
var remainingText = _reader.ReadToEnd() ?? string.Empty;
throw new Exception(string.Format("Unknown grammar found at position {0} : '{1}'", _text.Length - remainingText.Length, remainingText));
}
break;
}
}
return tokens;
}
private Token ParseKeyword()
{
var text = new StringBuilder();
while (Char.IsLetter((char)_reader.Peek()))
{
text.Append((char)_reader.Read());
}
var potentialKeyword = text.ToString().ToLower();
switch (potentialKeyword)
{
case "true":
return new TrueToken();
case "false":
return new FalseToken();
case "and":
return new AndToken();
case "or":
return new OrToken();
default:
throw new Exception("Expected keyword (True, False, and, or) but found " + potentialKeyword);
}
}
}
public class Parser
{
private readonly IEnumerator<Token> _tokens;
public Parser(IEnumerable<Token> tokens)
{
_tokens = tokens.GetEnumerator();
_tokens.MoveNext();
}
public bool Parse()
{
while (_tokens.Current != null)
{
var isNegated = _tokens.Current is NegationToken;
if (isNegated)
_tokens.MoveNext();
var boolean = ParseBoolean();
if (isNegated)
boolean = !boolean;
while (_tokens.Current is OperandToken)
{
var operand = _tokens.Current;
if (!_tokens.MoveNext())
{
throw new Exception("Missing expression after operand");
}
var nextBoolean = ParseBoolean();
if (operand is AndToken)
boolean = boolean && nextBoolean;
else
boolean = boolean || nextBoolean;
}
return boolean;
}
throw new Exception("Empty expression");
}
private bool ParseBoolean()
{
if (_tokens.Current is BooleanValueToken)
{
var current = _tokens.Current;
_tokens.MoveNext();
if (current is TrueToken)
return true;
return false;
}
if (_tokens.Current is OpenParenthesisToken)
{
_tokens.MoveNext();
var expInPars = Parse();
if (!(_tokens.Current is ClosedParenthesisToken))
throw new Exception("Expecting Closing Parenthesis");
_tokens.MoveNext();
return expInPars;
}
if (_tokens.Current is ClosedParenthesisToken)
throw new Exception("Unexpected Closed Parenthesis");
// since its not a BooleanConstant or Expression in parenthesis, it must be a expression again
var val = Parse();
return val;
}
}
public static class StrategyHelper public static class StrategyHelper
{ {
public static string GetStrategyShortcut(string strategyName, bool onlyValidStrategies) public static string GetStrategyShortcut(string strategyName, bool onlyValidStrategies)
@ -597,21 +398,19 @@ namespace Core.ProfitTrailer
strategyText += ""; strategyText += "";
} }
else if (strategy.Name.Contains("STATS")) else if (strategy.Name.Contains("STATS"))
// avoid parsing advanced buy stats // Avoid displaying advanced buy stats and completed level formulas
{ {
strategy.Name = ""; strategy.Name = "";
} }
else if (strategy.Name.Contains("FORMULA")) else if (strategy.Name.Contains("FORMULA"))
// Parse Various PT Formulas // Avoid displaying formula details
{ {
if (strategy.Name.Contains("LEVEL")) if (strategy.Name.Contains("LEVEL"))
// level X
{ {
string level = strategy.Name.Substring(5, 1); string level = strategy.Name.Substring(5, 1);
strategyText += "<span class=\"label label-warning\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"LEVEL FORMULA\">L " + level + "</span> "; strategyText += "<span class=\"label label-warning\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"LEVEL FORMULA\">L " + level + "</span> ";
} }
else else
// standard formula
{ {
strategyText += "<span class=\"label label-warning\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"CONDITIONAL FORMULA\">FORM</span> "; strategyText += "<span class=\"label label-warning\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"CONDITIONAL FORMULA\">FORM</span> ";
} }

View File

@ -6,7 +6,7 @@ using Core.Helper;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
[assembly: AssemblyVersion("2.4.4")] [assembly: AssemblyVersion("2.4.5")]
[assembly: AssemblyProduct("PT Magic")] [assembly: AssemblyProduct("PT Magic")]
namespace PTMagic namespace PTMagic