1 using Mercury.Scanning;
2 using Mercury.Interpreting;
5 using System.Collections.Generic;
6 using System.Diagnostics;
18 public class Analyser<T, TResult>
22 private Func<T, TResult> rselect;
36 langinfo.RewriteRules == null ? null : new Interpreter<T>(langinfo.RewriteRules, contextFactory),
55 if (tokenizer == null)
throw new ArgumentNullException(
"tokenizer");
56 if (parser == null)
throw new ArgumentNullException(
"parser");
58 if (interpreter != null && (resultSelector == null))
59 throw new ArgumentNullException(
"resultSelector");
64 Interpreter = interpreter;
66 this.rselect = resultSelector;
78 public IParser Parser {
get;
private set; }
93 throw new ArgumentNullException(
"Null input? What am I supposed to do with that?");
97 IList<Token> tokens = null;
98 var rules =
new List<Rule>();
99 var watch =
new Stopwatch();
106 { tokens = Tokenizer.Tokenize(input); }
110 result.SetStatus(watch.Elapsed, AnalyserStatus.TokenizerFail, ex: ex);
117 { rules.AddRange(Scanner.Scan(tokens)); }
121 result.SetStatus(watch.Elapsed, AnalyserStatus.ScannerFail, ex: ex);
128 result.ParserTime = watch.Elapsed;
129 result.ParserResult = Parser.Parse(tokens, rules);
130 result.ParserTime = watch.Elapsed - result.ParserTime;
135 result.SetStatus(watch.Elapsed, AnalyserStatus.ParserFail, ex: ex);
139 if (!result.ParserResult.Accepted)
142 result.SetStatus(watch.Elapsed, AnalyserStatus.ParserFail, FailReason.NoResult);
147 if (Interpreter != null)
150 var sw =
new Stopwatch();
154 result.InterpreterResults = Interpreter.Interpret(result.ParserResult.Trees);
157 var rvals =
new TResult[result.InterpreterResults.Count];
159 for (
int ix = 0; ix < result.InterpreterResults.Count; ++ix )
161 var b = result.InterpreterResults[ix].Success;
163 rvals[ix] = b ? rselect(result.InterpreterResults[ix].Result) :
default(TResult);
166 result.Interpretations = rvals;
167 result.InterpreterTime = sw.Elapsed;
172 result.SetStatus(watch.Elapsed, AnalyserStatus.InterpreterFail, FailReason.NoResult);
180 result.SetStatus(watch.Elapsed, AnalyserStatus.InterpreterFail, ex: ex);
187 result.SetStatus(watch.Elapsed, AnalyserStatus.Success, FailReason.None);
195 public class Analyser<T> : Analyser<T,T> where T : class
203 : base(langinfo, x => x, contextFactory)
215 : base(tokenizer, scanner, parser, interpreter, x => x)
Thread-safe wrapper for ChartParserInternal class.
Represents a collection of information required to analyze a language with Mercury Mercury...
Creates a derivation tree from the list of input tokens
A default implementation of ITokenizer interface using Microsoft Regular Expressions. Splits tokens in the following fashion:
AnalyserResult< T, TResult > Analyze(string input)
Analyzes the specified input and returns the Mercury.AnalyserResult{T,TResult}.
Analyser(LanguageInfo< T > langinfo, IInterpreterContextFactory contextFactory=null)
Initializes a new instance of the Analyser{T} class.
Analyser(ITokenizer tokenizer, IScanner scanner, IParser parser, IInterpreter< T > interpreter, Func< T, TResult > resultSelector)
Initializes a new instance of analyzer from the class. Scanner and Interpreter can be null (if so...
An exception has been thrown
Analyser(ITokenizer tokenizer, IScanner scanner, IParser parser, IInterpreter< T > interpreter)
Initializes a new instance of the Analyser{T} class. See Mercury.Analyser{T,TResult} for more informa...
Holds the result of analysis. The fields hold information according to the following table: ...
Interface for interpreters that create objects from derivation trees according to RewriteRules...
Scanner scans input tokens and creates additional rules for parsing.
Represents a Context-Free Grammar. This is an immutable class, to create a grammar use Mercury...
Analyser(LanguageInfo< T > langinfo, Func< T, TResult > resultSelector, IInterpreterContextFactory contextFactory=null)
Initializes a new instance of the Analyser{T,TResult} class from the provided language information...