public bool Parse(ExpressionCompiler compiler) { int symbolPos = compiler.Pos; if (!symbolParser.Parse(compiler)) { return(false); } // Lenient by default if (compiler.Parent.Children.Count == 0) { // Reset the compiler position compiler.Pos = symbolPos; return(false); } TokenImpl lhs = compiler.Parent.PopChild(); TokenImpl token = CreateToken(compiler, symbolPos, lhs); if (token != null) { compiler.Parent.AddChild(token); } return(true); }
public bool Parse(ExpressionCompiler compiler) { int symbolPos = compiler.Pos; if (!symbolParser.Parse(compiler)) { return(false); } // Parse the right hand side if (!compiler.ParseNextToken()) { // Reset the compiler position compiler.Pos = symbolPos; throw new ParserException(Name, symbolPos, "Missing right-hand operand"); } TokenImpl rhs = compiler.Parent.PopChild(); TokenImpl token = CreateToken(compiler, symbolPos, rhs); if (token != null) { compiler.Parent.AddChild(token); } return(true); }
public bool Parse(ExpressionCompiler compiler) { int startPos = compiler.Pos; if (!testSymbol.Parse(compiler)) { return(false); } // Must have a left-hand side... if (compiler.Parent.Children.Count == 0) { // Reset the compiler position compiler.Pos = startPos; throw new ParserException(Name, startPos, "Missing operand for the test"); } TokenImpl test = compiler.Parent.PopChild(); // Parse the result if true token if (!compiler.ParseNextToken()) { // Reset the compiler position and restore the removed child compiler.Pos = startPos; compiler.Parent.AddChild(test); throw new ParserException(Name, startPos, "Missing operand for result if true"); } TokenImpl resultIfTrue = compiler.Parent.PopChild(); // Must have the 'else' token next if (!elseSymbol.Parse(compiler)) { // Reset the compiler position and restore the (first) removed child compiler.Pos = startPos; compiler.Parent.AddChild(test); throw new ParserException(Name, startPos, "Missing ':' symbol"); } // Parse the result if false token if (!compiler.ParseNextToken()) { // Reset the compiler position and restore the (first) removed child compiler.Pos = startPos; compiler.Parent.AddChild(test); throw new ParserException(Name, startPos, "Missing operand for result if false"); } TokenImpl resultIfFalse = compiler.Parent.PopChild(); TokenImpl token = new ConditionalOperatorToken(startPos, test, resultIfTrue, resultIfFalse); compiler.Parent.AddChild(token); return(true); }
/** * Convenience method to remove the last child and return it */ public TokenImpl PopChild() { if (Children.Count == 0) { throw new System.InvalidOperationException("No children on this token"); } TokenImpl child = Children[Children.Count - 1]; Children.RemoveAt(Children.Count - 1); return(child); }
protected virtual TokenImpl GetRhs(T token) { TokenImpl impl = token; if (token is BinaryToken) { return(((BinaryToken)impl).Rhs); } else { return(null); } }
protected virtual TokenImpl GetLhs(T token) { TokenImpl impl = token; if (token is LeftHandUnaryToken) { return(((LeftHandUnaryToken)impl).Lhs); } else { return(null); } }
public static object CoerceToType(System.Type type, TokenImpl context, object value) { object defaultValue; if (type.IsValueType) { defaultValue = Activator.CreateInstance(type); } else { defaultValue = null; } return(DoCoerce(type, context, value, defaultValue)); }
public bool Parse(ExpressionCompiler compiler) { int symbolPos = compiler.Pos; if (!symbolParser.Parse(compiler)) { return(false); } // Must have a left-hand side... if (compiler.Parent.Children.Count == 0) { // Reset the compiler position compiler.Pos = symbolPos; // If we are lenient to the left-hand side missing, just return false if (lhsLenient) { return(false); } else { throw new ParserException(Name, symbolPos, "Missing left-hand operand"); } } TokenImpl lhs = compiler.Parent.PopChild(); // Parse the right hand side if (!compiler.ParseNextToken()) { // Reset the compiler position and restore the removed child compiler.Pos = symbolPos; compiler.Parent.AddChild(lhs); throw new ParserException(Name, symbolPos, "Missing right-hand operand"); } TokenImpl rhs = compiler.Parent.PopChild(); TokenImpl token = CreateToken(compiler, symbolPos, lhs, rhs); if (token != null) { compiler.Parent.AddChild(token); } return(true); }
protected override TokenImpl CreateToken(ExpressionCompiler compiler, int symbolPos, TokenImpl lhs, TokenImpl rhs) { if (!(rhs is IdentifierToken)) { throw new ParserException(Name, symbolPos, "Right-hand side of a property access token must be an identifier"); } // The lhs must be a token which supports Hosting. If it's not see if we can do anything if (lhs is HostSupport) { return(new PropertyAccessToken(symbolPos, lhs, (IdentifierToken)rhs)); } else if (lhs is BinaryToken) { // If the RHS of the binary token is a token which allows hosting we can join to it BinaryToken binaryLhs = (BinaryToken)lhs; TokenImpl lhsRhs = binaryLhs.Rhs; if (lhsRhs is HostSupport) { // We can join... binaryLhs.Rhs = new PropertyAccessToken(symbolPos, lhsRhs, (IdentifierToken)rhs); return(binaryLhs); } } else if (lhs is UnaryToken) { // If the RHS of the unary token is a token which allows hosting we can join to and replace it UnaryToken unaryLhs = (UnaryToken)lhs; TokenImpl lhsRhs = unaryLhs.Rhs; if (lhsRhs is HostSupport) { // We can join it unaryLhs.Rhs = new PropertyAccessToken(symbolPos, lhsRhs, (IdentifierToken)rhs); return(unaryLhs); } } // Can't work with the lhs throw new ParserException(Name, symbolPos, $"Left-hand side of a property access token cannot be: {lhs.Name}"); }
public virtual bool Equals(object obj, bool includeChildren) { if (obj == null) { return(false); } if (obj.GetType() != GetType()) { return(false); } TokenImpl other = (TokenImpl)obj; if (other.Position != Position) { return(false); } if (includeChildren) { if (other.Children.Count != Children.Count) { return(false); } for (int i = 0; i < other.Children.Count; ++i) { if (!other.Children[i].Equals(Children[i])) { return(false); } } } return(true); }
public DecrementAndReturnToken(int position, TokenImpl rhs) : base(position, rhs) { }
public ConditionalOperatorToken(int position, TokenImpl test, TokenImpl resultIfTrue, TokenImpl resultIfFalse) : base(position) { this.Test = test; this.ResultIfTrue = resultIfTrue; this.ResultIfFalse = resultIfFalse; }
public NotToken(int position, TokenImpl rhs) : base(position, rhs) { }
public override object Evaluate(UnityELEvaluator context) { TokenImpl child = Children[0]; return(child.Evaluate(context)); }
public NoSuchFunctionException(TokenImpl source, string message, Exception cause) : base(source, message, cause) { }
public KeyedAccessToken(int position, TokenImpl hostToken) : base(position) { this.Host = hostToken; }
public FunctionToken(int position, TokenImpl functionName) : base(position) { this.FunctionName = functionName; }
protected override TokenImpl CreateToken(ExpressionCompiler compiler, int symbolPos, TokenImpl lhs, TokenImpl rhs) { if (lhs is BinaryToken) { return(ApplyPrecedence(compiler, (BinaryToken)lhs, new AsToken(symbolPos, lhs, rhs))); } else { return(new AsToken(symbolPos, lhs, rhs)); } }
public AndToken(int position, TokenImpl lhs, TokenImpl rhs) : base(position, lhs, rhs) { }
public IsNotEmptyToken(int position, TokenImpl rhs) : base(position, rhs) { }
public EqualsToken(int position, TokenImpl lhs, TokenImpl rhs) : base(position, lhs, rhs) { }
protected abstract TokenImpl CreateToken(ExpressionCompiler compiler, int symbolPos, TokenImpl lhs);
public SubtractAndAssignToken(int position, TokenImpl lhs, TokenImpl rhs) : base(position, lhs, rhs) { }
public UnaryMinusToken(int position, TokenImpl rhs) : base(position, rhs) { }
public AddAndAssignToken(int position, TokenImpl lhs, TokenImpl rhs) : base(position, lhs, rhs) { }
public ModulusToken(int position, TokenImpl lhs, TokenImpl rhs) : base(position, lhs, rhs) { }
protected override TokenImpl CreateToken(ExpressionCompiler compiler, int symbolPos, TokenImpl rhs) { return(new ComplementToken(symbolPos, rhs)); }
public ExponentAndAssignToken(int position, TokenImpl lhs, TokenImpl rhs) : base(position, lhs, rhs) { }
public LeftHandUnaryToken(int position, TokenImpl Lhs) : base(position) { this.Lhs = Lhs; }
public ParserException(TokenImpl source, string message, Exception cause) : base($"{source.DebugName}{message}", cause) { }