Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
EditorAdapter.cs
Go to the documentation of this file.
1 #region License
2 /* **********************************************************************************
3  * Copyright (c) Roman Ivantsov
4  * This source code is subject to terms and conditions of the MIT License
5  * for Irony. A copy of the license can be found in the License.txt file
6  * at the root of this distribution.
7  * By using this source code in any fashion, you are agreeing to be bound by the terms of the
8  * MIT License.
9  * You must not remove this notice from this software.
10  * **********************************************************************************/
11 #endregion
12 
13 using System;
14 using System.Collections.Generic;
15 using System.Linq;
16 using System.Text;
17 using System.Threading;
18 using System.Runtime.InteropServices;
19 using System.Diagnostics;
20 
21 using Irony.Parsing;
22 
23 namespace Irony.GrammarExplorer {
24 
25  public class EditorAdapter {
26  Parsing.Parser _parser;
27  Scanner _scanner;
28  ParseTree _parseTree;
29  string _newText;
31  EditorViewAdapterList _viewsCopy; //copy used in refresh loop; set to null when views are added/removed
32  Thread _parserThread;
33  Thread _colorizerThread;
34  bool _stopped;
35 
36  public EditorAdapter(LanguageData language) {
37  _parser = new Parsing.Parser(language);
38  _scanner = _parser.Scanner;
39  _colorizerThread = new Thread(ColorizerLoop);
40  _colorizerThread.IsBackground = true;
41  _parserThread = new Thread(ParserLoop);
42  _parserThread.IsBackground = true;
43  }
44  public void Activate() {
45  if ((_colorizerThread.ThreadState & System.Threading.ThreadState.Running) == 0) {
46  _parserThread.Start();
47  _colorizerThread.Start();
48  }
49  }
50 
51  public void Stop() {
52  try {
53  _stopped = true;
54  _parserThread.Join(500);
55  if(_parserThread.IsAlive)
56  _parserThread.Abort();
57  _colorizerThread.Join(500);
58  if(_colorizerThread.IsAlive)
59  _colorizerThread.Abort();
60  } catch (Exception ex) {
61  System.Diagnostics.Debug.WriteLine("Error when stopping EditorAdapter: " + ex.Message);
62  }
63  }
64 
65  public void SetNewText(string text) {
66  text = text ?? string.Empty; //force it to become not null; null is special value meaning "no changes"
67  _newText = text;
68  }
69 
71  get { return _parseTree; }
72  }
73 
74  //Note: we don't actually parse in current version, only scan. Will implement full parsing in the future,
75  // to support all intellisense operations
76  private void ParseSource(string newText) {
77  //Explicitly catch the case when new text is empty
78  if (newText != string.Empty) {
79  _parseTree = _parser.Parse(newText);// .ScanOnly(newText, "Source");
80  }
81  //notify views
82  var views = GetViews();
83  foreach (var view in views)
84  view.UpdateParsedSource(_parseTree);
85  }
86 
87 
88  #region Views manipulation: AddView, RemoveView, GetViews
89  public void AddView(EditorViewAdapter view) {
90  lock (this) {
91  _views.Add(view);
92  _viewsCopy = null;
93  }
94  }
95  public void RemoveView(EditorViewAdapter view) {
96  lock (this) {
97  _views.Remove(view);
98  _viewsCopy = null;
99  }
100  }
101  private EditorViewAdapterList GetViews() {
102  EditorViewAdapterList result = _viewsCopy;
103  if (result == null) {
104  lock (this) {
105  _viewsCopy = new EditorViewAdapterList();
106  _viewsCopy.AddRange(_views);
107  result = _viewsCopy;
108  }//lock
109  }
110  return result;
111  }
112  #endregion
113 
114  private void ParserLoop() {
115  while (!_stopped) {
116  try {
117  string newtext = Interlocked.Exchange(ref _newText, null);
118  if(newtext != null) {
119  ParseSource(newtext);
120  }
121  Thread.Sleep(10);
122  } catch(Exception ex) {
123  fmShowException.ShowException(ex);
124  System.Windows.Forms.MessageBox.Show("Fatal error in code colorizer. Colorizing had been disabled.");
125  _stopped = true;
126  }
127  }//while
128  }
129 
130  private void ColorizerLoop() {
131  while (!_stopped) {
132  EditorViewAdapterList views = GetViews();
133  //Go through views and invoke refresh
134  foreach (EditorViewAdapter view in views) {
135  if (_stopped) break;
136  if (view.WantsColorize)
137  view.TryInvokeColorize();
138  }//foreach
139  Thread.Sleep(10);
140  }// while !_stopped
141  }//method
142 
143  }//class
144 }//namespace
EditorAdapter(LanguageData language)
Scanner base class. The Scanner's function is to transform a stream of characters into aggregates/wor...
Definition: Scanner.cs:22
Describes a language.
Definition: LanguageData.cs:23
void AddView(EditorViewAdapter view)
void RemoveView(EditorViewAdapter view)