aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Scripting/ScriptParser.cs
blob: ff8e7ff2b024909775789945f92ebec52dad9edf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.Scripting
{
    public class ScriptParser
    {
        public List<ScriptVariable> ParseScript(String code)
        {
            List<ScriptVariable> vars = new List<ScriptVariable>();

            SyntaxTree tree = CSharpSyntaxTree.ParseText(code);
            var root = (CompilationUnitSyntax)tree.GetRoot();

            List<VariableDeclaratorSyntax> variables = new List<VariableDeclaratorSyntax>();
            FillVariables(variables, root.Members[0]);
            variables = variables.Distinct().ToList();

            foreach (var item in variables)
            {
                ScriptVariable v = new ScriptVariable();
                v.Name = item.Identifier.ToString();

                if (item.Initializer.Value.GetType() == typeof(ObjectCreationExpressionSyntax))
                {
                    v.Type = (item.Initializer.Value as ObjectCreationExpressionSyntax).Type.ToString();
                }
                else if (item.Initializer.Value.GetType() == typeof(InvocationExpressionSyntax))
                {
                    v.Type = (((item.Initializer.Value as InvocationExpressionSyntax).Expression as MemberAccessExpressionSyntax).Name as GenericNameSyntax).TypeArgumentList.Arguments[0].ToString();
                }

                vars.Add(v);
            }

            return vars;
        }

        private void FillVariables(List<VariableDeclaratorSyntax> variables, SyntaxNode node)
        {
            foreach (var item in node.DescendantNodes(x => true))
            {
                if (item.GetType() == typeof(VariableDeclaratorSyntax))
                {
                    variables.Add(item as VariableDeclaratorSyntax);
                }
                else
                {
                    FillVariables(variables, item);
                }
            }
        }
    }

    public class ScriptVariable
    {
        public String Name { get; set; }
        public String Type { get; set; }

        public override string ToString()
        {
            return String.Format("{0} : {1}", Type, Name);
        }
    }
}
span>get { return _selectedEvent; } set { _selectedEvent = value; RaisePropertyChangedAuto(); OnSelectedEventChanged(); } } private ObservableCollection<DateTime> _dates; public ObservableCollection<DateTime> Dates { get { return _dates; } set { _dates = value; RaisePropertyChangedAuto(); } } private DateTime _selectedDate; public DateTime SelectedDate { get { return _selectedDate; } set { _selectedDate = value; RaisePropertyChangedAuto(); OnSelectedDateChanged(); } } private DateTime _minDate; public DateTime MinDate { get { return _minDate; } set { _minDate = value; RaisePropertyChangedAuto(); } } private DateTime _maxDate; public DateTime MaxDate { get { return _maxDate; } set { _maxDate = value; RaisePropertyChangedAuto(); } } private bool _isRealTime; public bool IsRealTime { get { return _isRealTime; } set { _isRealTime = value; RaisePropertyChangedAuto(); OnSelectedDateChanged(); } } private TimelineViewVM _timelineViewVM; public TimelineViewVM TimelineViewVM { get { return _timelineViewVM; } set { _timelineViewVM = value; RaisePropertyChangedAuto(); } } public RelayCommand<MachinesEvent> DisplayTimelineCommand { get; set; } public RelayCommand NavigateToEventsCommand { get; set; } public RelayCommand NavigateToHomeCommand { get; set; } public EventsViewVM(INotificationProvider notification, IEventLogger eventLogger, IStudioApplicationManager application, LoggingNavigationManager navigation) { TimelineViewVM = new TimelineViewVM(notification); _navigation = navigation; _application = application; _notification = notification; _eventLogger = eventLogger; _realTimeEvents = new ObservableCollection<MachinesEvent>(); _eventLogger.NewLog += _eventLogger_NewLog; DisplayTimelineCommand = new RelayCommand<MachinesEvent>(DisplayTimeline); NavigateToEventsCommand = new RelayCommand(() => _navigation.NavigateTo(LoggingNavigationView.EventsView)); NavigateToHomeCommand = new RelayCommand(() => _navigation.NavigateTo(LoggingNavigationView.HomeView)); } private void _eventLogger_NewLog(object sender, MachinesEvent machineEvent) { InvokeUI(() => { _realTimeEvents.Insert(0, machineEvent); }); } private async void OnSelectedMachineChanged() { if (SelectedMachine != null) { using (_notification.PushTaskItem("Loading machine events...")) { await Task.Factory.StartNew(() => { _db = ObservablesContext.CreateDefault(); _db.EventTypes.Load(); DateTime now = DateTime.UtcNow.AddMonths(-1); _history_events = _db.MachinesEvents.Where(x => x.MachineGuid == SelectedMachine.Guid && x.DateTime > now).Include(x => x.User).Include(x => x.User.Contact).Include(x => x.Machine).ToList(); Dates = new ObservableCollection<DateTime>(); foreach (var day in _history_events.GroupBy(x => x.DateTime.DayOfYear).Select(x => x.First().DateTime).OrderByDescending(x => x)) { Dates.Add(day); } if (Dates.Count > 0) { MinDate = Dates.Min(); MaxDate = Dates.Max(); } SelectedDate = Dates.FirstOrDefault(); }); } } } private void OnSelectedDateChanged() { if (SelectedDate != null && SelectedMachine != null) { if (IsRealTime) { Events = _realTimeEvents; } else if (_history_events != null) { Events = _history_events.Where(x => x.DateTime.DayOfYear == SelectedDate.Date.DayOfYear).OrderByDescending(x => x.DateTime).ToObservableCollection(); } } } private void OnSelectedEventChanged() { if (SelectedEvent != null && SelectedEvent.Type != BL.Enumerations.EventTypes.APPLICATION_STARTED && !_dialog_shown) { _dialog_shown = true; _notification.ShowModalDialog<EventDetailsViewVM, EventDetailsView>(new EventDetailsViewVM(SelectedEvent), (x) => { }, () => { _dialog_shown = false; }); } } private void DisplayTimeline(MachinesEvent ev) { var events = Events.OrderBy(x => x.DateTime).SkipWhile(x => x != ev).Skip(1).TakeWhile(x => x.DateTime > ev.DateTime && x.Type != BL.Enumerations.EventTypes.APPLICATION_STARTED).ToObservableCollection(); events.Insert(0, ev); TimelineViewVM.Initialize(events.ToList()); _navigation.NavigateTo(LoggingNavigationView.TimelineWrapperView); } } }