public static void GetHook(ParseInfo parseInfo, Scope scope, DeltinScriptParser.HookContext context) { // Get the hook variable's expression. IExpression variableExpression = parseInfo.GetExpression(scope, context.var); // Get the hook value. IExpression valueExpression = parseInfo.GetExpression(scope, context.value); // Resolve the variable. VariableResolve resolvedVariable = new VariableResolve(new VariableResolveOptions() { // Not indexable CanBeIndexed = false, // Hook variables are not settable. ShouldBeSettable = false }, variableExpression, DocRange.GetRange(context.var), parseInfo.Script.Diagnostics); if (valueExpression == null) { return; } // Check if the resolved variable is a HookVar. if (resolvedVariable.SetVariable?.Calling is HookVar hookVar) { // If it is, set the hook. hookVar.TrySet(parseInfo, valueExpression, DocRange.GetRange(context.value)); } else { // Not a hook variable. parseInfo.Script.Diagnostics.Error("Expected a hook variable.", DocRange.GetRange(context.var)); } }
public SetVariableAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.VarsetContext varsetContext) { IExpression variableExpression = parseInfo.GetExpression(scope, varsetContext.var); // Get the variable being set. VariableResolve = new VariableResolve(new VariableResolveOptions(), variableExpression, DocRange.GetRange(varsetContext), parseInfo.Script.Diagnostics); // Get the operation. if (varsetContext.statement_operation() != null) { Operation = varsetContext.statement_operation().GetText(); // If there is no value, syntax error. if (varsetContext.val == null) { parseInfo.Script.Diagnostics.Error("Expected an expression.", DocRange.GetRange(varsetContext).end.ToRange()); } // Parse the value. else { Value = parseInfo.GetExpression(scope, varsetContext.val); } } else if (varsetContext.INCREMENT() != null) { Operation = "++"; } else if (varsetContext.DECREMENT() != null) { Operation = "--"; } }
public OperatorAction(ParseInfo parseInfo, Scope scope, BinaryOperatorExpression op) { Operator = op.Operator; // Left operator. Left = parseInfo.GetExpression(scope, op.Left); // Right operator. Right = parseInfo.GetExpression(scope, op.Right); }
public SetVariableAction(ParseInfo parseInfo, Scope scope, Assignment assignmentContext) { IExpression variableExpression = parseInfo.GetExpression(scope, assignmentContext.VariableExpression); // Get the variable being set. VariableResolve = new VariableResolve(new VariableResolveOptions(), variableExpression, assignmentContext.VariableExpression.Range, parseInfo.Script.Diagnostics); // Get the operation. Operation = assignmentContext.AssignmentToken; Value = parseInfo.GetExpression(scope, assignmentContext.Value); }
public OperatorAction(ParseInfo parseInfo, Scope scope, BinaryOperatorExpression context) { Left = parseInfo.GetExpression(scope, context.Left); Right = parseInfo.GetExpression(scope, context.Right); _defaultType = parseInfo.Types.Any(); string op = context.Operator.Operator.Operator; Operation = Left.Type()?.Operations.GetOperation(TypeOperation.TypeOperatorFromString(op), Right.Type()) ?? GetDefaultOperation(op, parseInfo.TranslateInfo.Types); if (Operation == null) { parseInfo.Script.Diagnostics.Error("Operator '" + op + "' cannot be applied to the types '" + Left.Type().GetNameOrAny() + "' and '" + Right.Type().GetNameOrAny() + "'.", context.Operator.Token.Range); } }
public VariableApply(ParseInfo parseInfo, Scope scope, Scope getter, IVariableInstance variable, Identifier variableContext) { Variable = variable; _parseInfo = parseInfo; _name = variableContext.Token.Text; CallRange = variableContext.Token.Range; getter = getter ?? scope; // Get the index. if (variableContext.Index != null) { _index = new IExpression[variableContext.Index.Count]; for (int i = 0; i < _index.Length; i++) { _index[i] = parseInfo.GetExpression(scope, variableContext.Index[i].Expression, getter: getter); } } // Get the generics. if (variableContext.TypeArgs != null) { _generics = new CodeType[variableContext.TypeArgs.Count]; for (int i = 0; i < _generics.Length; i++) { _generics[i] = TypeFromContext.GetCodeTypeFromContext(parseInfo, getter, variableContext.TypeArgs[i]); } } VariableCall = Variable.GetExpression(_parseInfo, CallRange, _index, _generics); }
public DefinedEnum(ParseInfo parseInfo, EnumContext enumContext) : base(enumContext.Identifier.GetText()) { Kind = TypeKind.Enum; _translateInfo = parseInfo.TranslateInfo; Scope = new Scope("enum " + Name); if (enumContext.Identifier) { parseInfo.TranslateInfo.CheckConflict(parseInfo, new(Name), enumContext.Identifier.Range); // Set location and symbol link. DefinedAt = new Location(parseInfo.Script.Uri, enumContext.Identifier.Range); parseInfo.Script.Elements.AddDeclarationCall(this, new(enumContext.Identifier.Range, true)); } // Get the enum members. for (int i = 0; i < enumContext.Values.Count; i++) { if (enumContext.Values[i].Identifier) { var expression = enumContext.Values[i].Value != null ? new ExpressionOrWorkshopValue(parseInfo.GetExpression(Scope, enumContext.Values[i].Value)) : new ExpressionOrWorkshopValue(Element.Num(i)); var newMember = new DefinedEnumMember(parseInfo, this, enumContext.Values[i].Identifier.Text, new Location(parseInfo.Script.Uri, enumContext.Values[i].Identifier.Range), expression); Scope.AddVariable(newMember, parseInfo.Script.Diagnostics, newMember.DefinedAt.range); } } }
public IfAction(ParseInfo parseInfo, Scope scope, If ifContext) { // Get the if condition. Expression = parseInfo.GetExpression(scope, ifContext.Expression); // Contains the path info of all blocks in the if/else-if/else list. var paths = new List <PathInfo>(); // Get the if's block. Block = parseInfo.GetStatement(scope, ifContext.Statement); // Add the if block path info. paths.Add(new PathInfo(Block, ifContext.Range, false)); // Get the else-ifs. ElseIfs = new ElseIfAction[ifContext.ElseIfs.Count]; for (int i = 0; i < ElseIfs.Length; i++) { ElseIfs[i] = new ElseIfAction(parseInfo, scope, ifContext.ElseIfs[i]); paths.Add(new PathInfo(Block, ifContext.Range, false)); } // If there is an else statement, get the else block. if (ifContext.Else != null) { ElseBlock = parseInfo.GetStatement(scope, ifContext.Else.Statement); // Add the else path info. paths.Add(new PathInfo(ElseBlock, ifContext.Range, true)); } Paths = paths.ToArray(); }
public DefinedEnum(ParseInfo parseInfo, EnumContext enumContext) : base(enumContext.Identifier.Text) { CanBeExtended = false; CanBeDeleted = false; Kind = "enum"; // Check if a type with the same name already exists. if (parseInfo.TranslateInfo.Types.IsCodeType(Name)) { parseInfo.Script.Diagnostics.Error($"A type with the name '{Name}' already exists.", enumContext.Identifier.Range); } _translateInfo = parseInfo.TranslateInfo; Scope = new Scope("enum " + Name); // Set location and symbol link. DefinedAt = new Location(parseInfo.Script.Uri, enumContext.Identifier.Range); _translateInfo.GetComponent <SymbolLinkComponent>().AddSymbolLink(this, DefinedAt, true); // Get the enum members. for (int i = 0; i < enumContext.Values.Count; i++) { if (enumContext.Values[i].Identifier) { var expression = enumContext.Values[i].Value != null ? new ExpressionOrWorkshopValue(parseInfo.GetExpression(Scope, enumContext.Values[i].Value)) : new ExpressionOrWorkshopValue(new V_Number(i)); var newMember = new DefinedEnumMember(parseInfo, this, enumContext.Values[i].Identifier.Text, new Location(parseInfo.Script.Uri, enumContext.Values[i].Identifier.Range), expression); Scope.AddVariable(newMember, parseInfo.Script.Diagnostics, newMember.DefinedAt.range); } } }
public StringAction(ParseInfo parseInfo, Scope scope, StringExpression stringContext) { _parseInfo = parseInfo; _stringRange = stringContext.Token.Range; _classicFormatSyntax = stringContext.ClassicFormatSyntax; Value = stringContext.Value; Localized = stringContext.Localized; // Add completion if the string is localized. if (Localized) { _parseInfo.Script.AddCompletionRange(new CompletionRange(parseInfo.TranslateInfo, StringCompletion, _stringRange, CompletionRangeKind.ClearRest)); } // Get the format parameters. if (stringContext.Formats == null) { // No formats. FormatParameters = new IExpression[0]; } else { // Has formats. FormatParameters = new IExpression[stringContext.Formats.Count]; for (int i = 0; i < FormatParameters.Length; i++) { FormatParameters[i] = parseInfo.GetExpression(scope, stringContext.Formats[i]); } } parseInfo.CurrentUsageResolver?.OnResolve(usage => _shouldParse = usage != UsageType.StringFormat); ParseString(); }
/// <summary>Parses the function's parameter values without using them for anything.</summary> public static void DiscardParameters(ParseInfo parseInfo, Scope scope, List <ParameterValue> values) { foreach (var value in values) { parseInfo.GetExpression(scope, value.Expression); } }
public ForeachAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.ForeachContext foreachContext) { RawContinue = false; Scope varScope = scope.Child(); ForeachVar = new ForeachVariable(varScope, new ForeachContextHandler(parseInfo, foreachContext)); // Get the array that will be iterated on. Syntax error if it is missing. if (foreachContext.expr() != null) { Array = parseInfo.GetExpression(scope, foreachContext.expr()); } else { parseInfo.Script.Diagnostics.Error("Expected expression.", DocRange.GetRange(foreachContext.IN())); } // Get the foreach block. Syntax error if it is missing. if (foreachContext.block() != null) { Block = new BlockAction(parseInfo.SetLoop(this), varScope, foreachContext.block()); // Get the path info. Path = new PathInfo(Block, DocRange.GetRange(foreachContext.FOREACH()), false); } else { parseInfo.Script.Diagnostics.Error("Expected block.", DocRange.GetRange(foreachContext.RIGHT_PAREN())); } }
public IsAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.E_isContext isContext) { // Get the expression. expression = parseInfo.GetExpression(scope, isContext.expr()); // Get the type. if (isContext.type == null) { parseInfo.Script.Diagnostics.Error("Expected type name.", DocRange.GetRange(isContext.IS())); } else { CodeType type = parseInfo.TranslateInfo.Types.GetCodeType(isContext.type.Text, parseInfo.Script.Diagnostics, DocRange.GetRange(isContext.type)); // Make sure the received type is a class. if (type != null && type is ClassType == false) { parseInfo.Script.Diagnostics.Error("Expected a class type.", DocRange.GetRange(isContext.type)); } else { checkingIfType = (ClassType)type; } } }
public StringAction(ParseInfo parseInfo, Scope scope, StringExpression stringContext) { _parseInfo = parseInfo; Value = stringContext.Value; Localized = stringContext.Localized; _stringRange = stringContext.Token.Range; // Add completion if the string is localized. if (Localized) { _parseInfo.Script.AddCompletionRange(new CompletionRange(StringCompletion, _stringRange, CompletionRangeKind.ClearRest)); } // Get the format parameters. if (stringContext.Formats == null) { // No formats. FormatParameters = new IExpression[0]; } else { // Has formats. FormatParameters = new IExpression[stringContext.Formats.Count]; for (int i = 0; i < FormatParameters.Length; i++) { FormatParameters[i] = parseInfo.GetExpression(scope, stringContext.Formats[i]); } } ParseString(); }
public CreateArrayAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.CreatearrayContext createArrayContext) { Values = new IExpression[createArrayContext.expr().Length]; for (int i = 0; i < Values.Length; i++) { Values[i] = parseInfo.GetExpression(scope, createArrayContext.expr(i)); } }
public CreateArrayAction(ParseInfo parseInfo, Scope scope, CreateArray createArrayContext) { Values = new IExpression[createArrayContext.Values.Count]; for (int i = 0; i < Values.Length; i++) { Values[i] = parseInfo.GetExpression(scope, createArrayContext.Values[i]); } }
public TypeConvertAction(ParseInfo parseInfo, Scope scope, TypeCast typeConvert) { // Get the expression. Syntax error if there is none. Expression = parseInfo.GetExpression(scope, typeConvert.Expression); // Get the type. Syntax error if there is none. ConvertingTo = CodeType.GetCodeTypeFromContext(parseInfo, typeConvert.Type); }
public TernaryConditionalAction(ParseInfo parseInfo, Scope scope, TernaryExpression ternaryContext) { this.parseInfo = parseInfo; Condition = parseInfo.GetExpression(scope, ternaryContext.Condition); Consequent = parseInfo.GetExpression(scope, ternaryContext.Consequent); Alternative = parseInfo.GetExpression(scope, ternaryContext.Alternative); if (Consequent.Type() != null && Consequent.Type().IsConstant()) { parseInfo.Script.Diagnostics.Error($"Cannot use constant types in a ternary expression.", ternaryContext.Consequent.Range); } if (Alternative.Type() != null && Alternative.Type().IsConstant()) { parseInfo.Script.Diagnostics.Error($"Cannot use constant types in a ternary expression.", ternaryContext.Alternative.Range); } }
public WhileAction(ParseInfo parseInfo, Scope scope, While whileContext) { RawContinue = true; Condition = parseInfo.GetExpression(scope, whileContext.Condition); Block = parseInfo.SetLoop(this).GetStatement(scope, whileContext.Statement); Path = new PathInfo(Block, whileContext.Range, false); }
public ElseIfAction(ParseInfo parseInfo, Scope scope, ElseIf elseIfContext) { // Get the else-if's expression. Expression = parseInfo.GetExpression(scope, elseIfContext.Expression); // Get the else-if's block. Block = parseInfo.GetStatement(scope, elseIfContext.Statement); }
public ReturnAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.ReturnContext returnContext) { ErrorRange = DocRange.GetRange(returnContext.RETURN()); if (returnContext.expr() != null) { ReturningValue = parseInfo.GetExpression(scope, returnContext.expr()); } ReturningFromScope = scope; }
public IncrementAction(ParseInfo parseInfo, Scope scope, Increment increment) { _decrement = increment.Decrement; // Get the variable. IExpression variableExpr = parseInfo.GetExpression(scope, increment.VariableExpression); _resolve = new VariableResolve(new VariableResolveOptions(), variableExpr, increment.VariableExpression.Range, parseInfo.Script.Diagnostics); }
// Formatted public StringAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.Formatted_stringContext stringContext) { Init(parseInfo, stringContext.@string()); FormatParameters = new IExpression[stringContext.expr().Length]; for (int i = 0; i < FormatParameters.Length; i++) { FormatParameters[i] = parseInfo.GetExpression(scope, stringContext.expr(i)); } ParseString(); }
public ElseIfAction(ParseInfo parseInfo, Scope scope, ElseIf elseIfContext) { // Get the else-if's expression. Expression = parseInfo.GetExpression(scope, elseIfContext.Expression); TypeComparison.ExpectNonConstant(parseInfo, elseIfContext.Expression.Range, Expression.Type()); // Get the else-if's block. Block = parseInfo.GetStatement(scope, elseIfContext.Statement); }
private SwitchElement[] ResolveElements(ParseInfo parseInfo, Scope scope, Switch switchContext) { List <SwitchElement> elements = new List <SwitchElement>(); bool inSection = false; bool caseError = false; bool gotDefault = false; // Resolve paths. foreach (var statement in switchContext.Statements) { var switchCase = statement as SwitchCase; // Syntax error if there is a statement before a case. if (switchCase == null && !inSection && !caseError) { parseInfo.Script.Diagnostics.Error("Expected case or default.", statement.Range); caseError = true; } // Don't throw the syntax error multiple times in one switch. if (switchCase != null) { inSection = true; } // Default case. if (switchCase != null && switchCase.IsDefault) { if (gotDefault) { parseInfo.Script.Diagnostics.Error("Switch cannot have multiple defaults.", switchCase.Range); } gotDefault = true; } // Get the statement if (switchCase == null) { elements.Add(new SwitchElement(parseInfo.GetStatement(scope, statement))); } // Get the case else if (!switchCase.IsDefault) { elements.Add(new SwitchElement(switchCase.Token.Range, parseInfo.GetExpression(scope, switchCase.Value))); } // Get default else { elements.Add(new SwitchElement(switchCase.Token.Range)); } } return(elements.ToArray()); }
public SwitchAction(ParseInfo parseInfo, Scope scope, Switch switchContext) { // Get the expression. Expression = parseInfo.GetExpression(scope, switchContext.Expression); paths = GetSections(ResolveElements(parseInfo.SetBreakHandler(this), scope, switchContext)); pathInfo = new PathInfo[paths.Length]; for (int i = 0; i < pathInfo.Length; i++) { pathInfo[i] = new PathInfo(paths[i].Block, paths[i].ErrorRange, paths[i].IsDefault); } }
public RuleIfAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.Rule_ifContext ifContext) { // Syntax error if there is no expression. if (ifContext.expr() == null) { parseInfo.Script.Diagnostics.Error("Expected expression.", DocRange.GetRange(ifContext.RIGHT_PAREN())); } // Get the expression. else { Expression = parseInfo.GetExpression(scope, ifContext.expr()); } }
public ValueInArrayAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.E_array_indexContext exprContext) { Expression = parseInfo.GetExpression(scope, exprContext.array); this.parseInfo = parseInfo; if (exprContext.index == null) { parseInfo.Script.Diagnostics.Error("Expected an expression.", DocRange.GetRange(exprContext.INDEX_START())); } else { Index = new IExpression[] { parseInfo.GetExpression(scope, exprContext.index) } }; }
public TernaryConditionalAction(ParseInfo parseInfo, Scope scope, DeltinScriptParser.E_ternary_conditionalContext ternaryContext) { this.parseInfo = parseInfo; Condition = parseInfo.GetExpression(scope, ternaryContext.condition); if (ternaryContext.consequent == null) { parseInfo.Script.Diagnostics.Error("Expected expression.", DocRange.GetRange(ternaryContext.TERNARY())); } else { Consequent = parseInfo.GetExpression(scope, ternaryContext.consequent); } if (ternaryContext.alternative == null) { parseInfo.Script.Diagnostics.Error("Expected expression.", DocRange.GetRange(ternaryContext.TERNARY_ELSE())); } else { Alternative = parseInfo.GetExpression(scope, ternaryContext.alternative); } }
private void GetParts(ParseInfo parseInfo, Scope scope, DeltinScriptParser.ExprContext left, string op, DocRange opRange, DeltinScriptParser.ExprContext right) { // Left operator. if (left == null) { parseInfo.Script.Diagnostics.Error("Missing left operator.", opRange); } else { Left = parseInfo.GetExpression(scope, left); } // Right operator. if (right == null) { parseInfo.Script.Diagnostics.Error("Missing right operator.", opRange); } else { Right = parseInfo.GetExpression(scope, right); } Operator = op; }