public IExpression Parse(string value) { ParserContext ctx = new ParserContext(value, 0); return(GetExpression(ctx)); }
private bool skipASymbol(ParserContext context) { return(skipIf((c) => true, context)); }
private bool skipWhiteSpace(ParserContext context) { return(skipWhile((c) => char.IsWhiteSpace(c), context)); }
private bool skipCharacter(char toSkip, ParserContext context) { return(skipIf((c) => c == toSkip, context)); }
private IExpression GetUnary(ParserContext ctx) { skipWhiteSpace(ctx); if (ctx.CurrentPosition == ctx.Text.Length) { //throw exception } if (skipCharacter('!', ctx)) { skipWhiteSpace(ctx); return(new Negation(GetUnary(ctx))); } if (skipCharacter('(', ctx)) { var saveCtx = ctx.Clone(); try { var expr = GetExpression(ctx); if (expr is IMath) { ctx = saveCtx; ctx.CurrentPosition--; return(GetPredicate(ctx)); } else { skipASymbol(ctx); return(expr); } } catch { } ctx.CurrentPosition = saveCtx.CurrentPosition - 1; return(GetPredicate(ctx)); } var prevChar = ctx.Text[ctx.CurrentPosition]; if (skipCharacter('@', ctx) || skipCharacter('?', ctx)) { var value = ""; while (ctx.CurrentPosition != ctx.Text.Length && (char.IsLower(ctx.Text[ctx.CurrentPosition]) || char.IsDigit(ctx.Text[ctx.CurrentPosition]))) { value += ctx.Text[ctx.CurrentPosition]; skipASymbol(ctx); } var constant = new Variable(value); if (prevChar == '@') { skipWhiteSpace(ctx); return(new Universal(constant, GetUnary(ctx))); } else { skipWhiteSpace(ctx); return(new Existence(constant, GetUnary(ctx))); } } skipWhiteSpace(ctx); return(GetPredicate(ctx)); }