Browse files

Let Resharper do the work. Added external version info

  • Loading branch information...
1 parent bf292ea commit 936e249d7bfe74c40ea5fcf43c10dfb37c810faf J. Preiss committed Oct 12, 2012
View
0 codeplex/Copyright.txt → Copyright.txt
File renamed without changes.
View
0 codeplex/License.txt → License.txt
File renamed without changes.
View
10 Lingua/GeneratorRuleItem.cs
@@ -122,12 +122,12 @@ public override int GetHashCode()
/// </summary>
/// <param name="lhs">A <see cref="GeneratorRuleItem"/> to compare.</param>
/// <param name="rhs">A <see cref="GeneratorRuleItem"/> to compare.</param>
- /// <returns><value>true</value> if the <see cref="Rule"/> and <see cref="Dot"/> values of <paramref name="left"/> and <paramref name="right"/> are equal; otherwise, <value>false</value>.</returns>
+ /// <returns><value>true</value> if the <see cref="Rule"/> and <see cref="Dot"/> values of <paramref name="lhs"/> and <paramref name="rhs"/> are equal; otherwise, <value>false</value>.</returns>
public static bool operator ==(GeneratorRuleItem lhs, GeneratorRuleItem rhs)
{
- if (object.ReferenceEquals(lhs, rhs)) return true;
+ if (ReferenceEquals(lhs, rhs)) return true;
- if (object.ReferenceEquals(lhs, null) || object.ReferenceEquals(rhs, null)) return false;
+ if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null)) return false;
return (lhs.Rule == rhs.Rule)
&& (lhs.Dot == rhs.Dot);
@@ -138,7 +138,7 @@ public override int GetHashCode()
/// </summary>
/// <param name="lhs">A <see cref="GeneratorRuleItem"/> to compare.</param>
/// <param name="rhs">A <see cref="GeneratorRuleItem"/> to compare.</param>
- /// <returns><value>true</value> if the <see cref="Rule"/> and <see cref="Dot"/> values of <paramref name="left"/> and <paramref name="right"/> differ; otherwise, <value>false</value>.</returns>
+ /// <returns><value>true</value> if the <see cref="Rule"/> and <see cref="Dot"/> values of <paramref name="lhs"/> and <paramref name="rhs"/> differ; otherwise, <value>false</value>.</returns>
public static bool operator !=(GeneratorRuleItem lhs, GeneratorRuleItem rhs)
{
return !(lhs == rhs);
@@ -151,7 +151,7 @@ public override int GetHashCode()
/// <returns><value>true</value> if the value of <paramref name="other"/> is the same as this instance; otherwise, <value>false</value>.</returns>
public bool Equals(GeneratorRuleItem other)
{
- if (object.ReferenceEquals(other, null)) return false;
+ if (ReferenceEquals(other, null)) return false;
return (Rule == other.Rule)
&& (Dot == other.Dot);
View
16 Lingua/GeneratorState.cs
@@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
+using System.Linq;
using System.Text;
namespace Lingua
@@ -14,8 +15,8 @@ namespace Lingua
/// </summary>
public class GeneratorState
{
- private readonly HashSet<GeneratorStateItem> _items = new HashSet<GeneratorStateItem>();
- private readonly Dictionary<LanguageElementType, GeneratorState> _transitions = new Dictionary<LanguageElementType, GeneratorState>();
+ readonly HashSet<GeneratorStateItem> _items = new HashSet<GeneratorStateItem>();
+ readonly Dictionary<LanguageElementType, GeneratorState> _transitions = new Dictionary<LanguageElementType, GeneratorState>();
/// <summary>
/// Initializes a new instance of the <see cref="GeneratorState"/> class.
@@ -93,16 +94,13 @@ public HashSet<GeneratorStateItem> Apply(LanguageElementType languageElementType
{
HashSet<GeneratorStateItem> result = null;
- foreach (GeneratorStateItem item in Items)
+ foreach (var item in Items.Where(item => item.RuleItem.DotElement == languageElementType))
{
- if (item.RuleItem.DotElement == languageElementType)
+ if (result == null)
{
- if (result == null)
- {
- result = new HashSet<GeneratorStateItem>();
- }
- result.Add(new GeneratorStateItem(new GeneratorRuleItem(item.RuleItem.Rule, item.RuleItem.Dot + 1)));
+ result = new HashSet<GeneratorStateItem>();
}
+ result.Add(new GeneratorStateItem(new GeneratorRuleItem(item.RuleItem.Rule, item.RuleItem.Dot + 1)));
}
return result;
View
14 Lingua/GeneratorStateItem.cs
@@ -17,7 +17,7 @@ namespace Lingua
/// </remarks>
public class GeneratorStateItem : IEquatable<GeneratorStateItem>, IComparable<GeneratorStateItem>
{
- private readonly HashSet<TerminalType> _lookaheads = new HashSet<TerminalType>();
+ readonly HashSet<TerminalType> _lookaheads = new HashSet<TerminalType>();
/// <summary>
/// Initializes a new instance of the <see cref="GeneratorStateItem"/> class.
@@ -63,7 +63,7 @@ public override string ToString()
{
var sb = new StringBuilder();
- sb.Append(RuleItem.ToString());
+ sb.Append(RuleItem);
if (Lookaheads.Count > 0)
{
@@ -113,12 +113,12 @@ public override int GetHashCode()
/// </summary>
/// <param name="lhs">A <see cref="GeneratorStateItem"/> to compare.</param>
/// <param name="rhs">A <see cref="GeneratorStateItem"/> to compare.</param>
- /// <returns><value>true</value> if the <see cref="RuleItem"/> and <see cref="Lookaheads"/> values of <paramref name="left"/> and <paramref name="right"/> are equal; otherwise, <value>false</value>.</returns>
+ /// <returns><value>true</value> if the <see cref="RuleItem"/> and <see cref="Lookaheads"/> values of <paramref name="lhs"/> and <paramref name="rhs"/> are equal; otherwise, <value>false</value>.</returns>
public static bool operator ==(GeneratorStateItem lhs, GeneratorStateItem rhs)
{
- if (object.ReferenceEquals(lhs, rhs)) return true;
+ if (ReferenceEquals(lhs, rhs)) return true;
- if (object.ReferenceEquals(lhs, null) || object.ReferenceEquals(rhs, null)) return false;
+ if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null)) return false;
return (lhs.RuleItem == rhs.RuleItem)
&& lhs.Lookaheads.SetEquals(rhs.Lookaheads);
@@ -129,7 +129,7 @@ public override int GetHashCode()
/// </summary>
/// <param name="lhs">A <see cref="GeneratorStateItem"/> to compare.</param>
/// <param name="rhs">A <see cref="GeneratorStateItem"/> to compare.</param>
- /// <returns><value>true</value> if the <see cref="RuleItem"/> and <see cref="Lookaheads"/> values of <paramref name="left"/> and <paramref name="right"/> differ; otherwise, <value>false</value>.</returns>
+ /// <returns><value>true</value> if the <see cref="RuleItem"/> and <see cref="Lookaheads"/> values of <paramref name="lhs"/> and <paramref name="rhs"/> differ; otherwise, <value>false</value>.</returns>
public static bool operator !=(GeneratorStateItem lhs, GeneratorStateItem rhs)
{
return !(lhs == rhs);
@@ -142,7 +142,7 @@ public override int GetHashCode()
/// <returns><value>true</value> if the value of <paramref name="other"/> is the same as this instance; otherwise, <value>false</value>.</returns>
public bool Equals(GeneratorStateItem other)
{
- if (object.ReferenceEquals(other, null)) return false;
+ if (ReferenceEquals(other, null)) return false;
return (RuleItem == other.RuleItem)
&& Lookaheads.SetEquals(other.Lookaheads);
View
157 Lingua/Grammar.cs
@@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using System.Reflection;
namespace Lingua
@@ -24,8 +25,8 @@ namespace Lingua
/// </remarks>
public class Grammar : IGrammar
{
- private readonly Dictionary<Type, TerminalType> _terminals = new Dictionary<Type, TerminalType>();
- private readonly Dictionary<Type, NonterminalType> _nonterminals = new Dictionary<Type, NonterminalType>();
+ readonly Dictionary<Type, TerminalType> _terminals = new Dictionary<Type, TerminalType>();
+ readonly Dictionary<Type, NonterminalType> _nonterminals = new Dictionary<Type, NonterminalType>();
/// <summary>
/// Retrieves all terminal and nonterminal definitions from the specified <see cref="Assembly"/> and adds them to
@@ -76,7 +77,7 @@ public LanguageElementType Load(Type type, string name)
/// <param name="name">The name of the grammar being loaded as defined by <see cref="GrammarAttribute"/>. Specify <value>null</value> to load <paramref name="assembly"/> regardless of grammar name.</param>
public void LoadRules(Assembly assembly, string name)
{
- foreach (Type type in assembly.GetTypes())
+ foreach (var type in assembly.GetTypes())
{
LoadRules(type, name);
}
@@ -102,81 +103,81 @@ public void LoadRules(Type type, string name)
{
var parameters = method.GetParameters();
- if (MethodIsRule(method, parameters))
+ if (!MethodIsRule(method, parameters)) continue;
+
+ // Determine priority associated with rule.
+ //
+ var priority = 0;
+ var attributes = method.GetCustomAttributes(typeof(RuleAttribute), false);
+ if (attributes.Length > 0)
+ {
+ var attribute = attributes[0] as RuleAttribute;
+ if (attribute != null)
+ {
+ priority = attribute.Priority;
+ }
+ }
+
+ // Find the grammer language elements associated with each parameter member.
+ //
+ NonterminalType lhs = null;
+ var rhs = new List<LanguageElementType>();
+ for (var idx = 0; idx < parameters.Length; ++idx)
{
- // Determine priority associated with rule.
- //
- int priority = 0;
- object[] attributes = method.GetCustomAttributes(typeof(RuleAttribute), false);
- if (attributes.Length > 0)
+ var parameterType = parameters[idx].ParameterType;
+
+ if (idx == 0)
{
- var attribute = attributes[0] as RuleAttribute;
- if (attribute != null)
+ NonterminalType nonterminal;
+ if (_nonterminals.TryGetValue(parameterType, out nonterminal))
{
- priority = attribute.Priority;
+ lhs = nonterminal;
+ }
+ else
+ {
+ throw new InvalidOperationException();
}
}
-
- // Find the grammer language elements associated with each parameter member.
- //
- NonterminalType lhs = null;
- var rhs = new List<LanguageElementType>();
- for (var idx = 0; idx < parameters.Length; ++idx)
+ else
{
- Type parameterType = parameters[idx].ParameterType;
-
- if (idx == 0)
+ NonterminalType nonterminal;
+ if (_nonterminals.TryGetValue(parameterType, out nonterminal))
{
- NonterminalType nonterminal;
- if (_nonterminals.TryGetValue(parameterType, out nonterminal))
- {
- lhs = nonterminal;
- }
- else
- {
- throw new InvalidOperationException();
- }
+ rhs.Add(nonterminal);
}
else
{
- NonterminalType nonterminal;
- if (_nonterminals.TryGetValue(parameterType, out nonterminal))
+ TerminalType terminal;
+ if (_terminals.TryGetValue(parameterType, out terminal))
{
- rhs.Add(nonterminal);
+ rhs.Add(terminal);
}
else
{
- TerminalType terminal;
- if (_terminals.TryGetValue(parameterType, out terminal))
+ // If parameterType is currently not defined by the grammar, attempt to process it
+ // now. This occurs when OptionalTerminal<> and OptionalNonterminal<> types
+ // appear in the RHS side of a rule.
+ //
+ var languageElementType = Load(parameterType, name);
+ if (languageElementType != null)
{
- rhs.Add(terminal);
+ rhs.Add(languageElementType);
+ LoadRules(parameterType, name);
}
else
{
- // If parameterType is currently not defined by the grammar, attempt to process it
- // now. This occurs when OptionalTerminal<> and OptionalNonterminal<> types
- // appear in the RHS side of a rule.
- //
- LanguageElementType languageElementType = Load(parameterType, name);
- if (languageElementType != null)
- {
- rhs.Add(languageElementType);
- LoadRules(parameterType, name);
- }
- else
- {
- throw new InvalidOperationException("Unrecognized rule argument.");
- }
+ throw new InvalidOperationException("Unrecognized rule argument.");
}
}
}
}
-
- // Create the rule.
- //
- var rule = new RuleType(method, priority, lhs, rhs.ToArray());
- lhs.Rules.Add(rule);
}
+
+ // Create the rule.
+ //
+ if (lhs == null) continue;
+ var rule = new RuleType(method, priority, lhs, rhs.ToArray());
+ lhs.Rules.Add(rule);
}
}
@@ -210,15 +211,7 @@ public void Resolve()
/// </summary>
public TerminalType StopTerminal
{
- get
- {
- foreach (TerminalType terminal in _terminals.Values)
- {
- if (terminal.IsStop) return terminal;
- }
-
- return null;
- }
+ get { return _terminals.Values.FirstOrDefault(terminal => terminal.IsStop); }
}
/// <summary>
@@ -228,9 +221,9 @@ public NonterminalType StartNonterminal
{
get
{
- foreach (NonterminalType nonterminal in _nonterminals.Values)
+ foreach (var nonterminal in _nonterminals.Values.Where(nonterminal => nonterminal.IsStart))
{
- if (nonterminal.IsStart) return nonterminal;
+ return nonterminal;
}
throw new InvalidOperationException("Starting nonterminal not defined by grammar.");
@@ -259,7 +252,7 @@ public NonterminalType[] GetNonterminals()
return nonterminals;
}
- private void ComputeFirst()
+ void ComputeFirst()
{
// All terminals contains themselves in FIRST.
//
@@ -277,9 +270,9 @@ private void ComputeFirst()
// Iterate over all nonterminals and their rules.
//
- foreach (NonterminalType x in _nonterminals.Values)
+ foreach (var x in _nonterminals.Values)
{
- foreach (RuleType rule in x.Rules)
+ foreach (var rule in x.Rules)
{
if (rule.Rhs.Length == 0)
{
@@ -297,11 +290,11 @@ private void ComputeFirst()
// when e is in FIRST(Yj) for j < i. Add e to FIRST(X) if e is in
// FIRST(Yi) for all i.
//
- bool epsilonInAllY = true;
+ var epsilonInAllY = true;
- foreach (LanguageElementType y in rule.Rhs)
+ foreach (var y in rule.Rhs)
{
- HashSet<TerminalType> yFirst = y.First.GetSetExcludingEpsilon();
+ var yFirst = y.First.GetSetExcludingEpsilon();
if (yFirst.Count > 0
&& !yFirst.IsSubsetOf(x.First))
{
@@ -328,7 +321,7 @@ private void ComputeFirst()
}
}
- private void ComputeFollow()
+ void ComputeFollow()
{
StartNonterminal.Follow.Add(StopTerminal);
@@ -391,7 +384,7 @@ private void ComputeFollow()
}
}
- private bool TypeIsTerminal(Type type, string name)
+ bool TypeIsTerminal(Type type, string name)
{
if (!type.IsSubclassOf(typeof(Terminal)))
{
@@ -402,7 +395,7 @@ private bool TypeIsTerminal(Type type, string name)
}
- private bool TypeIsNonterminal(Type type, string name)
+ bool TypeIsNonterminal(Type type, string name)
{
if (!type.IsSubclassOf(typeof(Nonterminal)))
{
@@ -412,25 +405,17 @@ private bool TypeIsNonterminal(Type type, string name)
return IsGrammarType(type, name);
}
- private static bool IsGrammarType(Type type, string name)
+ static bool IsGrammarType(Type type, string name)
{
if (string.IsNullOrEmpty(name))
{
return true;
}
- foreach (GrammarAttribute grammarAttribute in type.GetCustomAttributes(typeof(GrammarAttribute), true))
- {
- if (grammarAttribute.Name == name)
- {
- return true;
- }
- }
-
- return false;
+ return type.GetCustomAttributes(typeof (GrammarAttribute), true).Cast<GrammarAttribute>().Any(grammarAttribute => grammarAttribute.Name == name);
}
- private bool MethodIsRule(MethodInfo method, ParameterInfo[] parameters)
+ bool MethodIsRule(MethodInfo method, ParameterInfo[] parameters)
{
// A rule must be static.
//
View
2 Lingua/LanguageElement.cs
@@ -16,7 +16,7 @@ public abstract class LanguageElement
/// <summary>
/// Initializes a new instance of the <see cref="LanguageElement"/> class.
/// </summary>
- public LanguageElement()
+ protected LanguageElement()
{ }
/// <summary>
View
4 Lingua/LanguageElementType.cs
@@ -17,8 +17,8 @@ namespace Lingua
/// </remarks>
public abstract class LanguageElementType
{
- private readonly LanguageElementTypes _elementType;
- private readonly FirstSet _first = new FirstSet();
+ readonly LanguageElementTypes _elementType;
+ readonly FirstSet _first = new FirstSet();
/// <summary>
/// Initializes a new instance of the <see cref="LanguageElementType"/> class.
View
3 Lingua/Lingua.csproj
@@ -36,6 +36,9 @@
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="..\VersionInfo.cs">
+ <Link>VersionInfo.cs</Link>
+ </Compile>
<Compile Include="FirstSet.cs" />
<Compile Include="FollowSet.cs" />
<Compile Include="GeneratorRuleItem.cs" />
View
4 Lingua/LinguaTrace.cs
@@ -19,13 +19,13 @@ public enum LinguaTraceId
public static class LinguaTrace
{
- private static TraceSource s_traceSource = new TraceSource("Lingua", SourceLevels.All);
+ static readonly TraceSource _traceSource = new TraceSource("Lingua", SourceLevels.All);
public static TraceSource TraceSource
{
get
{
- return s_traceSource;
+ return _traceSource;
}
}
View
23 Lingua/NonterminalType.cs
@@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using System.Reflection;
namespace Lingua
@@ -19,13 +20,13 @@ public class NonterminalType : LanguageElementType
{
delegate Nonterminal Constructor();
- private readonly string _fullName;
- private readonly string _name;
- private readonly Constructor _constructor;
- private readonly bool _isStart;
+ readonly string _fullName;
+ readonly string _name;
+ readonly Constructor _constructor;
+ readonly bool _isStart;
- private readonly List<RuleType> _rules = new List<RuleType>();
- private readonly FollowSet _follow = new FollowSet();
+ readonly List<RuleType> _rules = new List<RuleType>();
+ readonly FollowSet _follow = new FollowSet();
/// <summary>
/// Initializes a new instance of the <see cref="NonterminalType"/> class.
@@ -41,16 +42,12 @@ public NonterminalType(Type type)
_fullName = type.AssemblyQualifiedName;
_name = GetName(type);
- _constructor = delegate() { return (Nonterminal)Activator.CreateInstance(type); };
+ _constructor = () => (Nonterminal) Activator.CreateInstance(type);
var attributes = type.GetCustomAttributes(typeof(NonterminalAttribute), false);
- foreach (var attribute in attributes)
+ foreach (var nonterminalAttribute in attributes.Cast<NonterminalAttribute>().Where(nonterminalAttribute => nonterminalAttribute.IsStart))
{
- var nonterminalAttribute = (NonterminalAttribute)attribute;
- if (nonterminalAttribute.IsStart)
- {
- _isStart = true;
- }
+ _isStart = true;
}
}
View
28 Lingua/Parser.cs
@@ -12,15 +12,13 @@ namespace Lingua
/// </summary>
public class Parser : IParser
{
- private ParserState m_initialState;
-
/// <summary>
/// Initializes a new instance of the <see cref="Parser"/> class.
/// </summary>
/// <param name="initialState">The initial <see cref="ParserState"/>.</param>
public Parser(ParserState initialState)
{
- m_initialState = initialState;
+ InitialState = initialState;
}
/// <summary>
@@ -42,7 +40,7 @@ public Nonterminal Parse(ITerminalReader terminalReader)
}
else
{
- ParserAction action = stack.Peek().State.GetAction(terminal.ElementType);
+ var action = stack.Peek().State.GetAction(terminal.ElementType);
LinguaTrace.TraceEvent(TraceEventType.Information, LinguaTraceId.ID_PARSE_ACTION, "{0}", action);
@@ -58,16 +56,13 @@ public Nonterminal Parse(ITerminalReader terminalReader)
var reduce = (ParserActionAccept)action;
var rule = reduce.Rule;
var lhs = Reduce(stack, rule);
-
return lhs;
}
case ParserActionTypes.Shift:
{
var shift = (ParserActionShift)action;
-
stack.Push(terminal, shift.State);
-
terminal = terminalReader.ReadTerminal();
}
break;
@@ -76,7 +71,6 @@ public Nonterminal Parse(ITerminalReader terminalReader)
{
var reduce = (ParserActionReduce)action;
var rule = reduce.Rule;
-
var lhs = Reduce(stack, rule);
// Push the LHS nonterminal on the stack.
@@ -89,27 +83,26 @@ public Nonterminal Parse(ITerminalReader terminalReader)
throw new InvalidOperationException(string.Format("Unrecognized action type {0}.", action.ActionType));
}
}
- };
-
+ }
return null;
}
- private static Nonterminal Reduce(ParserStack stack, RuleType rule)
+ static Nonterminal Reduce(ParserStack stack, RuleType rule)
{
// Create a language element array big enough to hold the LHS and RHS
// arguments.
//
- int parameterCount = 1 + rule.Rhs.Length;
- LanguageElement[] parameters = new LanguageElement[parameterCount];
+ var parameterCount = 1 + rule.Rhs.Length;
+ var parameters = new LanguageElement[parameterCount];
// Create the LHS nonterminal.
//
- Nonterminal lhs = rule.Lhs.CreateNonterminal();
+ var lhs = rule.Lhs.CreateNonterminal();
parameters[0] = lhs;
// Pop the RHS language elements off the stack.
//
- for (int idx = 0; idx < rule.Rhs.Length; ++idx)
+ for (var idx = 0; idx < rule.Rhs.Length; ++idx)
{
parameters[parameterCount - idx - 1] = stack.Pop().LanguageElement;
}
@@ -120,9 +113,6 @@ private static Nonterminal Reduce(ParserStack stack, RuleType rule)
return lhs;
}
- private ParserState InitialState
- {
- get { return m_initialState; }
- }
+ ParserState InitialState { get; set; }
}
}
View
82 Lingua/ParserGenerator.cs
@@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
+using System.Linq;
namespace Lingua
{
@@ -29,11 +30,7 @@ public ParserGeneratorResult GenerateParser(IGrammar grammar)
// Create a parser state for each generator state.
//
- var parserStates = new Dictionary<GeneratorState, ParserState>();
- foreach (var state in states)
- {
- parserStates.Add(state, new ParserState(state.Id));
- }
+ var parserStates = states.ToDictionary(state => state, state => new ParserState(state.Id));
foreach (var state in states)
{
@@ -46,28 +43,27 @@ public ParserGeneratorResult GenerateParser(IGrammar grammar)
//
var actions = new Dictionary<TerminalType, ParserAction>();
var actionRules = new Dictionary<ParserAction, GeneratorRuleItem>();
- foreach (GeneratorStateItem item in items)
+ foreach (var item in items)
{
LinguaTrace.TraceEvent(TraceEventType.Verbose, LinguaTraceId.ID_GENERATE_PROCESS_ITEM, "{0}", item);
if (item.RuleItem.DotElement == null)
{
- foreach (TerminalType terminal in item.RuleItem.Rule.Lhs.Follow)
+ foreach (var terminal in item.RuleItem.Rule.Lhs.Follow)
{
LinguaTrace.TraceEvent(TraceEventType.Verbose, LinguaTraceId.ID_GENERATE_PROCESS_TERMINAL, "{0}", terminal);
if (actions.ContainsKey(terminal))
{
- ParserGeneratorParserConflict conflict = new ParserGeneratorParserConflict(
+ var conflict = new ParserGeneratorParserConflict(
actionRules[actions[terminal]].ToString(),
item.RuleItem.ToString());
LinguaTrace.TraceEvent(TraceEventType.Information, LinguaTraceId.ID_GENERATE_PROCESS_CONFLICT, "{0}", conflict);
conflicts.Add(conflict);
}
- else if (item.RuleItem.Rule.Lhs.IsStart
- && terminal.IsStop)
+ else if (item.RuleItem.Rule.Lhs.IsStart && terminal.IsStop)
{
ParserAction action = new ParserActionAccept(item.RuleItem.Rule);
@@ -89,11 +85,11 @@ public ParserGeneratorResult GenerateParser(IGrammar grammar)
}
else if (item.RuleItem.DotElement.ElementType == LanguageElementTypes.Terminal)
{
- TerminalType terminal = (TerminalType)item.RuleItem.DotElement;
+ var terminal = (TerminalType)item.RuleItem.DotElement;
if (actions.ContainsKey(terminal))
{
- ParserGeneratorParserConflict conflict = new ParserGeneratorParserConflict(
+ var conflict = new ParserGeneratorParserConflict(
actionRules[actions[terminal]].ToString(),
item.RuleItem.ToString());
@@ -118,11 +114,10 @@ public ParserGeneratorResult GenerateParser(IGrammar grammar)
var gotos = new Dictionary<NonterminalType, ParserState>();
foreach (var transition in state.Transitions)
{
- if (transition.Key.ElementType == LanguageElementTypes.Nonterminal)
- {
- var nonterminal = (NonterminalType)transition.Key;
- gotos.Add(nonterminal, parserStates[transition.Value]);
- }
+ if (transition.Key.ElementType != LanguageElementTypes.Nonterminal) continue;
+
+ var nonterminal = (NonterminalType)transition.Key;
+ gotos.Add(nonterminal, parserStates[transition.Value]);
}
// Update the parser state.
@@ -139,13 +134,13 @@ public ParserGeneratorResult GenerateParser(IGrammar grammar)
}
}
- Parser parser = new Parser(parserStates[states[0]]);
+ var parser = new Parser(parserStates[states[0]]);
var result = new ParserGeneratorResult(parser, conflicts);
return result;
}
- private List<GeneratorState> CreateStates(IGrammar grammar)
+ List<GeneratorState> CreateStates(IGrammar grammar)
{
var states = new List<GeneratorState>();
var unevaluatedStates = new List<GeneratorState>();
@@ -180,34 +175,25 @@ private List<GeneratorState> CreateStates(IGrammar grammar)
foreach (var languageElement in languageElements)
{
var items = state.Apply(languageElement);
- if (items != null)
- {
- ComputeClosure(grammar, items);
+ if (items == null) continue;
- GeneratorState toState = null;
- foreach (GeneratorState existingState in states)
- {
- if (existingState.Items.SetEquals(items))
- {
- toState = existingState;
- break;
- }
- }
- if (toState == null)
- {
- toState = new GeneratorState(stateId++, items);
- states.Add(toState);
- unevaluatedStates.Add(toState);
- }
+ ComputeClosure(grammar, items);
- state.Transitions.Add(languageElement, toState);
+ var toState = states.FirstOrDefault(existingState => existingState.Items.SetEquals(items));
+ if (toState == null)
+ {
+ toState = new GeneratorState(stateId++, items);
+ states.Add(toState);
+ unevaluatedStates.Add(toState);
}
+
+ state.Transitions.Add(languageElement, toState);
}
}
if (LinguaTrace.TraceSource.Switch.ShouldTrace(TraceEventType.Information))
{
- foreach (GeneratorState state in states)
+ foreach (var state in states)
{
LinguaTrace.TraceEvent(TraceEventType.Information, LinguaTraceId.ID_GENERATE_STATE, "{0}", state);
}
@@ -216,7 +202,7 @@ private List<GeneratorState> CreateStates(IGrammar grammar)
return states;
}
- private void ComputeClosure(IGrammar grammar, HashSet<GeneratorStateItem> items)
+ void ComputeClosure(IGrammar grammar, HashSet<GeneratorStateItem> items)
{
// Continue to loop until new more elements are added to the state.
//
@@ -231,16 +217,14 @@ private void ComputeClosure(IGrammar grammar, HashSet<GeneratorStateItem> items)
foreach (var item in items)
{
var languageElement = item.RuleItem.DotElement;
- if (languageElement != null
- && languageElement.ElementType == LanguageElementTypes.Nonterminal)
+ if (languageElement == null || languageElement.ElementType != LanguageElementTypes.Nonterminal)
+ continue;
+
+ var nonterminal = (NonterminalType)languageElement;
+ foreach (var rule in nonterminal.Rules)
{
- var nonterminal = (NonterminalType)languageElement;
-
- foreach (var rule in nonterminal.Rules)
- {
- var newItem = new GeneratorStateItem(new GeneratorRuleItem(rule, 0));
- newItems.Add(newItem);
- }
+ var newItem = new GeneratorStateItem(new GeneratorRuleItem(rule, 0));
+ newItems.Add(newItem);
}
}
View
2 Lingua/ParserGeneratorResult.cs
@@ -11,7 +11,7 @@ namespace Lingua
/// </summary>
public class ParserGeneratorResult
{
- private readonly List<ParserGeneratorParserConflict> _conflicts = new List<ParserGeneratorParserConflict>();
+ readonly List<ParserGeneratorParserConflict> _conflicts = new List<ParserGeneratorParserConflict>();
/// <summary>
/// Initializes a new instance of the <see cref="ParserGeneratorResult"/> class.
View
16 Lingua/ParserState.cs
@@ -16,8 +16,8 @@ namespace Lingua
/// </remarks>
public class ParserState
{
- private readonly Dictionary<TerminalType, ParserAction> _actions = new Dictionary<TerminalType, ParserAction>();
- private readonly Dictionary<NonterminalType, ParserState> _gotos = new Dictionary<NonterminalType, ParserState>();
+ readonly Dictionary<TerminalType, ParserAction> _actions = new Dictionary<TerminalType, ParserAction>();
+ readonly Dictionary<NonterminalType, ParserState> _gotos = new Dictionary<NonterminalType, ParserState>();
/// <summary>
/// Initializes a new instance of the <see cref="ParserState"/> class.
@@ -63,11 +63,7 @@ public override string ToString()
public ParserAction GetAction(TerminalType terminalType)
{
ParserAction action;
- if (_actions.TryGetValue(terminalType, out action))
- {
- return action;
- }
- return null;
+ return _actions.TryGetValue(terminalType, out action) ? action : null;
}
/// <summary>
@@ -79,11 +75,7 @@ public ParserAction GetAction(TerminalType terminalType)
public ParserState GetGoto(NonterminalType nonterminalType)
{
ParserState state;
- if (_gotos.TryGetValue(nonterminalType, out state))
- {
- return state;
- }
- return null;
+ return _gotos.TryGetValue(nonterminalType, out state) ? state : null;
}
}
}
View
13 Lingua/Properties/AssemblyInfo.cs
@@ -21,16 +21,3 @@
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("ce7b8658-8286-4582-94ab-05b15aa2a698")]
-
-// Version information for an assembly consists of the following four values:
-//
-// Major Version
-// Minor Version
-// Build Number
-// Revision
-//
-// You can specify all the values or you can default the Build and Revision Numbers
-// by using the '*' as shown below:
-// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
View
9 Lingua/RuleType.cs
@@ -15,9 +15,8 @@ public class RuleType : IComparable<RuleType>
{
delegate void Invoker(LanguageElement[] parameters);
- private readonly Invoker _delegate;
-
- private readonly NonterminalType _lhs;
+ readonly Invoker _delegate;
+ readonly NonterminalType _lhs;
/// <summary>
/// Initializes a new instance of the <see cref="RuleType"/> class.
@@ -42,7 +41,7 @@ public RuleType(MethodInfo methodInfo, int priority, NonterminalType lhs, Langua
FullName = string.Format("{0}::{1}", methodInfo.DeclaringType.FullName, name);
Name = name;
- _delegate = delegate(LanguageElement[] parameters) { methodInfo.Invoke(null, parameters); };
+ _delegate = parameters => methodInfo.Invoke(null, parameters);
Priority = priority;
_lhs = lhs;
@@ -122,7 +121,7 @@ public void Invoke(LanguageElement[] parameters)
/// </remarks>
public int CompareTo(RuleType other)
{
- return this.Priority.CompareTo(other.Priority);
+ return Priority.CompareTo(other.Priority);
}
}
}
View
73 Lingua/TerminalReader.cs
@@ -15,14 +15,14 @@ namespace Lingua
/// </summary>
public class TerminalReader : ITerminalReader
{
- private readonly List<TerminalType> _terminalTypes = new List<TerminalType>();
- private readonly TerminalType _stopTerminal;
- private readonly Regex _regex;
+ readonly List<TerminalType> _terminalTypes = new List<TerminalType>();
+ readonly TerminalType _stopTerminal;
+ readonly Regex _regex;
- private string _text;
- private Match _match;
- private Terminal _queuedTerminal;
- private bool _stopTerminalRead;
+ string _text;
+ Match _match;
+ Terminal _queuedTerminal;
+ bool _stopTerminalRead;
/// <summary>
/// Initializes a new instance of the <see cref="TerminalReader"/> class.
@@ -77,12 +77,7 @@ public Terminal ReadTerminal()
/// <returns>The next <see cref="Terminal"/> in the input text. If no <see cref="TerminalType"/> recognizes the input text, <value>null</value> is returned.</returns>
public Terminal PeekTerminal()
{
- if (_queuedTerminal == null)
- {
- _queuedTerminal = GetNextTerminal();
- }
-
- return _queuedTerminal;
+ return _queuedTerminal ?? (_queuedTerminal = GetNextTerminal());
}
/// <summary>
@@ -99,63 +94,53 @@ public void PushTerminal(Terminal terminal)
_queuedTerminal = terminal;
}
- private string CreatePattern()
+ string CreatePattern()
{
- StringBuilder sb = new StringBuilder();
+ var sb = new StringBuilder();
sb.Append(@"\G(");
string prefix = null;
for (int idx = 0; idx < _terminalTypes.Count; ++idx)
{
- TerminalType terminal = _terminalTypes[idx];
- if (!string.IsNullOrEmpty(terminal.Pattern))
- {
- sb.Append(prefix); prefix = "|";
-
- sb.AppendFormat(
- @"(?<t{0}>{1})",
- idx,
- terminal.Pattern);
- }
+ var terminal = _terminalTypes[idx];
+ if (string.IsNullOrEmpty(terminal.Pattern)) continue;
+
+ sb.Append(prefix); prefix = "|";
+ sb.AppendFormat(
+ @"(?<t{0}>{1})",
+ idx,
+ terminal.Pattern);
}
sb.Append(")");
return sb.ToString();
}
- private Terminal GetNextTerminal()
+ Terminal GetNextTerminal()
{
- if (_match == null)
- {
- _match = _regex.Match(_text);
- }
- else
- {
- _match = _match.NextMatch();
- }
+ _match = _match == null ? _regex.Match(_text) : _match.NextMatch();
if (_match.Success)
{
- for (int idx = 0; idx < _terminalTypes.Count; ++idx)
+ for (var idx = 0; idx < _terminalTypes.Count; ++idx)
{
- Group group = _match.Groups[string.Format("t{0}", idx)];
- if (group.Success)
- {
- Terminal terminal = _terminalTypes[idx].CreateTerminal();
- terminal.ElementType = _terminalTypes[idx];
- terminal.Text = group.Value;
- return terminal;
- }
+ var group = _match.Groups[string.Format("t{0}", idx)];
+ if (!@group.Success) continue;
+
+ var terminal = _terminalTypes[idx].CreateTerminal();
+ terminal.ElementType = _terminalTypes[idx];
+ terminal.Text = @group.Value;
+ return terminal;
}
}
if (!_stopTerminalRead)
{
_stopTerminalRead = true;
- Terminal terminal = _stopTerminal.CreateTerminal();
+ var terminal = _stopTerminal.CreateTerminal();
terminal.ElementType = _stopTerminal;
return terminal;
}
View
37 Lingua/TerminalType.cs
@@ -3,6 +3,7 @@
*/
using System;
+using System.Linq;
using System.Reflection;
namespace Lingua
@@ -18,13 +19,9 @@ public class TerminalType : LanguageElementType
{
delegate Terminal Constructor();
- private readonly string _fullName;
- private readonly string _name;
- private readonly Constructor _constructor;
- private readonly string _pattern;
- private readonly bool _isStop;
- private readonly bool _ignore;
-
+ readonly string _fullName;
+ readonly string _name;
+ readonly Constructor _constructor;
/// <summary>
/// Initializes a new instance of the <see cref="TerminalType"/> class.
@@ -40,21 +37,20 @@ public TerminalType(Type type)
_fullName = type.AssemblyQualifiedName;
_name = GetName(type);
- _constructor = delegate() { return (Terminal)Activator.CreateInstance(type); };
+ _constructor = () => (Terminal) Activator.CreateInstance(type);
var attributes = type.GetCustomAttributes(typeof(TerminalAttribute), false);
- foreach (var attribute in attributes)
+ foreach (var terminalAttribute in attributes.Cast<TerminalAttribute>())
{
- var terminalAttribute = (TerminalAttribute)attribute;
if (terminalAttribute.IsStop)
{
- _isStop = true;
+ IsStop = true;
}
if (terminalAttribute.Ignore)
{
- _ignore = true;
+ Ignore = true;
}
- _pattern = terminalAttribute.Pattern;
+ Pattern = terminalAttribute.Pattern;
}
}
@@ -79,28 +75,19 @@ public override string Name
/// <summary>
/// Gets or sets the regular expression pattern of this <see cref="TerminalType"/>.
/// </summary>
- public string Pattern
- {
- get { return _pattern; }
- }
+ public string Pattern { get; private set; }
/// <summary>
/// Gets or sets a value that indicates if this <see cref="TerminalType"/> is the stopping terminal
/// of the <see cref="IGrammar"/>.
/// </summary>
- public bool IsStop
- {
- get { return _isStop; }
- }
+ public bool IsStop { get; private set; }
/// <summary>
/// Gets or sets a value that indicates if <see cref="Terminal"/> objects associated with this <see cref="TerminalType"/> should be ignored by any
/// <see cref="IParser"/> that encounters them.
/// </summary>
- public bool Ignore
- {
- get { return _ignore; }
- }
+ public bool Ignore { get; private set; }
public override string ToString()
View
4 README.md
@@ -1,4 +0,0 @@
-Lingua
-======
-
-Forking Lingua by R. Todd from CodePlex
View
0 codeplex/Readme.txt → Readme.txt
File renamed without changes.
View
36 VersionInfo.cs
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("Lingua")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("Lingua")]
+[assembly: AssemblyCopyright("Copyright © 2012")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("ce7b8658-8286-4582-94ab-05b15aa2a698")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
View
BIN tools/NuGet.exe
Binary file not shown.

0 comments on commit 936e249

Please sign in to comment.