private ITreeContextPart[] Flatten(ScriptFile script, DeltinScriptParser.E_expr_treeContext exprContext)
        {
            var exprList = new List <ITreeContextPart>();

            Flatten(script, exprContext, exprList);
            return(exprList.ToArray());

            // Recursive flatten function.
            void Flatten(ScriptFile script, DeltinScriptParser.E_expr_treeContext exprContext, List <ITreeContextPart> exprList)
            {
                // If the expression is a Tree, recursively flatten.
                if (exprContext.expr() is DeltinScriptParser.E_expr_treeContext)
                {
                    Flatten(script, (DeltinScriptParser.E_expr_treeContext)exprContext.expr(), exprList);
                }
                // Otherwise, add the expression to the list.
                else
                {
                    // Get the function.
                    if (exprContext.expr() is DeltinScriptParser.E_methodContext method)
                    {
                        exprList.Add(new FunctionPart(method.method()));
                    }
                    // Get the variable.
                    else if (exprContext.expr() is DeltinScriptParser.E_variableContext variable)
                    {
                        exprList.Add(new VariableOrTypePart(variable.variable()));
                    }
                    // Get the expression.
                    else
                    {
                        exprList.Add(new ExpressionPart(exprContext.expr()));
                    }
                }

                // Syntax error if there is no method or variable.
                if (exprContext.method() == null && exprContext.variable() == null)
                {
                    script.Diagnostics.Error("Expected expression.", DocRange.GetRange(exprContext.SEPERATOR()));
                    _trailingSeperator = exprContext.SEPERATOR();
                }
                else
                {
                    // Get the method.
                    if (exprContext.method() != null)
                    {
                        exprList.Add(new FunctionPart(exprContext.method()));
                    }
                    // Get the variable.
                    if (exprContext.variable() != null)
                    {
                        exprList.Add(new VariableOrTypePart(exprContext.variable()));
                    }
                }
            }
        }
        private TreeContextPart[] Flatten(ScriptFile script, DeltinScriptParser.E_expr_treeContext exprContext)
        {
            var exprList = new List <TreeContextPart>();

            Flatten(script, exprContext, exprList);
            return(exprList.ToArray());

            void Flatten(ScriptFile script, DeltinScriptParser.E_expr_treeContext exprContext, List <TreeContextPart> exprList)
            {
                if (exprContext.expr() is DeltinScriptParser.E_expr_treeContext)
                {
                    Flatten(script, (DeltinScriptParser.E_expr_treeContext)exprContext.expr(), exprList);
                }
                else
                {
                    exprList.Add(new TreeContextPart(exprContext.expr()));
                }

                if (exprContext.method() == null && exprContext.variable() == null)
                {
                    script.Diagnostics.Error("Expected expression.", DocRange.GetRange(exprContext.SEPERATOR()));
                    _trailingSeperator = exprContext.SEPERATOR();
                }
                else
                {
                    if (exprContext.method() != null)
                    {
                        exprList.Add(new TreeContextPart(exprContext.method()));
                    }
                    if (exprContext.variable() != null)
                    {
                        exprList.Add(new TreeContextPart(exprContext.variable()));
                    }
                }
            }
        }