Mercury library 1.0 Translation of CFG grammars into an object model (Bachelor's thesis).

- Main Page
- Packages
- Classes
- Files

- File List

All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
InterpreterContext.cs
Go to the documentation of this file.
1 using Mercury.Nucleus.Trees;
2 using Mercury.Syntax;
3 using System;
4 using System.Collections.Concurrent;
5 using System.Collections.Generic;
6 using System.Runtime.Caching;
7
8 namespace Mercury.Interpreting
9 {
10 //=============================================================================
11 // InterpreterContext
12 //=============================================================================
13
18 public abstract class InterpreterContext<T>
19 where T : class
20 {
21 //--[ Internal ]-----------------------------------------------------
22
23 internal TreeEntry<T> hroot;
24 internal RuleEntry<T> cnode;
25
26 internal Dictionary<string, Argument> bindings;
27 internal MemoryCache cache = null; // results cache
28
29 internal string action;
30 internal ArgumentType reqtp;
31
32 internal InterpreterContext<T> InternalClone(RuleEntry<T> current)
33 {
34 var cx = DeepClone();
35 cx.hroot = hroot;
36 cx.cnode = current;
37 cx.cache = cache;
38 return cx;
39 }
40
41 internal void InternalCopy(InterpreterContext<T> cxtx)
42 { CopyFrom(cxtx); }
43
44 internal void LogEntryEx(string hdr, string msg, params object[] args)
45 {
46 if (cnode == null) throw new InvalidOperationException();
47 cnode.Entries.Add(new TextEntry(hdr, msg, args));
48 }
49
50 //--[ Constructors ]-------------------------------------------------
51
55 public InterpreterContext()
56 { bindings = new Dictionary<string, Argument>(); }
57
58 //--[ Properties ]---------------------------------------------------
59
66 public TreeEntry<T> RootEntry { get { return hroot; } }
67
68 //--[ Methods ]------------------------------------------------------
69
72 public abstract InterpreterContext<T> DeepClone();
73
75 public abstract void CopyFrom(InterpreterContext<T> other);
76
80 public void LogEntry(string text, params object[] args)
81 { LogEntryEx(action, text, args); }
82 }
83
84 //=============================================================================
85 // IInterpreterContextFactory
86 //=============================================================================
87
89 public interface IInterpreterContextFactory
90 {
94 InterpreterContext<T> CreateContext<T>()
95 where T : class;
96 }
97
98 //=============================================================================
99 // InternalContext
100 //=============================================================================
101
102 //==<< InternalContext >>====================================================
103
104 internal class InternalContext<T> : InterpreterContext<T>
105 where T : class
106 {
107 public override InterpreterContext<T> DeepClone()
108 { return new InternalContext<T>(); }
109
110 public override void CopyFrom(InterpreterContext<T> other)
111 { }
112 }
113
114 //==<< InternalContextFactory >>=============================================
115
117 public class EmptyContextFactory : IInterpreterContextFactory
118 {
119 //--[ Interface Implementation ]-------------------------------------
120
124 public InterpreterContext<T> CreateContext<T>()
125 where T : class
126 { return new InternalContext<T>(); }
127
128 }
129
130 }
Mercury.Interpreting.IInterpreterContextFactory
Factory of context.
Definition: InterpreterContext.cs:89
Mercury.Interpreting.RuleEntry< T >
Describes a rule that has been used to interpret a tree.
Definition: Entries.cs:92
Mercury.Interpreting.TreeEntry< T >
Represents an entry for tree interpretation.
Definition: Entries.cs:52
Mercury.Interpreting.TextEntry
Stores informations logged by actions or internal Evaluator.
Definition: Entries.cs:25
Mercury.Interpreting.ArgumentType
ArgumentType
Defines argument types using in the Mercury library
Definition: Argument.cs:11
Mercury.Interpreting.InterpreterContext< T >.InterpreterContext
InterpreterContext()
Initializes a new instance of the InterpreterContext{T} class.
Definition: InterpreterContext.cs:55
Mercury.Interpreting.InterpreterContext< T >.LogEntry
void LogEntry(string text, params object[] args)
Logs an event into the history.
Definition: InterpreterContext.cs:80
Mercury.Interpreting.InterpreterContext< T >
Interpreter Context class. Some languages may use it to override it and remember data when parse tree...
Definition: InterpreterContext.cs:18
Mercury.Interpreting.EmptyContextFactory
Produces contexts with no custom data
Definition: InterpreterContext.cs:117