本文整理汇总了C#中System.Language.DefaultIfEmpty方法的典型用法代码示例。如果您正苦于以下问题:C# Language.DefaultIfEmpty方法的具体用法?C# Language.DefaultIfEmpty怎么用?C# Language.DefaultIfEmpty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Language
的用法示例。
在下文中一共展示了Language.DefaultIfEmpty方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParseExpression
/// <summary>
/// Parses a given expression into a graph.
/// </summary>
/// <returns>The built graph.</returns>
/// <param name="expression">The expression to create the graph from.</param>
/// <param name="initGraph">The graph to append too. Used for recursion.</param>
public Graph ParseExpression(string expression, Graph initGraph = null)
{
var lang = new Language ();
var ops = new Operations ();
expression = expression.Trim ();
var opStack = new Stack<Word>();
foreach (char letter in expression) {
//find the language mapping
var inLanguage = lang
.DefaultIfEmpty(null)
.FirstOrDefault (w => w.Letter == letter);
//find the operation mapping
var inOps = ops
.DefaultIfEmpty(null)
.FirstOrDefault(w => w.Letter == letter);
//not in the language or operators
if(inLanguage == null && inOps == null){
throw new InvalidLanguageException("Invalid input: " + letter);
}
//it is valid language input, so add it to the list.
if(inLanguage != null){
opStack.Push(inLanguage);
continue;
}
else{
//not a letter, so must be an operation
//verify that it has the correct arg count for that type of operation.
if (opStack.Count != inOps.MinArgCount && initGraph == null) {
throw new InvalidLanguageException (
string.Format(
"Unbalanced arguement count for operation (expected {0}, but got {1}.",
inOps.MinArgCount, opStack.Count
));
}
//operation, so stop, and process graph
initGraph = ProcessOperation(initGraph, inOps, opStack);
}
}
//expression was given with no operation, so error out.
if (opStack.Count > 0) {
throw new InvalidLanguageException ("No operation was specified in the given expression.");
}
//once done with graph, renumber the states.
initGraph.RenumberStates ();
//return the completed graph.
return initGraph;
}