/// <summary> /// Tries to parse a batch of similar syntactic items with specified delimiter. /// </summary> public bool ParseMany(SyntacticState state, SyntacticItem part, SyntacticItem delimiter) { return Parse( (syntacticState, args) => ParseManyInternal(syntacticState, args[0], args[1]), state, new[] { part, delimiter }); }
/// <summary> /// Tries to parse a batch of similar syntactic items with specified delimiter. /// </summary> private bool ParseManyInternal(SyntacticState state, SyntacticItem part, SyntacticItem delimiter) { int innerIndex = state.InnerPosition; int outerIndex = state.OuterPosition; if (!part.Parse(state)) { return(false); } while (true) { int lastInnerIndex = state.InnerPosition; int lastOuterIndex = state.OuterPosition; if (delimiter != null) { if (!delimiter.Parse(state)) { break; } } if (!part.Parse(state)) { state.Reset(lastInnerIndex, lastOuterIndex); break; } } state.AddBack(Key, innerIndex, outerIndex); return(true); }
/// <summary> /// Tries to parse a batch of similar syntactic items with specified delimiter. /// </summary> public bool ParseMany(SyntacticState state, SyntacticItem part, SyntacticItem delimiter) { return(Parse( (syntacticState, args) => ParseManyInternal(syntacticState, args[0], args[1]), state, new[] { part, delimiter })); }
/// <summary> /// Checks whether specified data string can be fully parsed to the syntactic item. /// </summary> public void Check(bool expected, SyntacticItem item, string data) { bool result = Parse(item, data); if (expected != result) { throw new AssertFailedException( String.Format( "Checking for [{0}] failed. Expected:<{1}>. Actual:<{2}>.", data.ToDisplay(), expected, !expected)); } }
/// <summary> /// Tries to parse specified data as a syntactic item. /// </summary> public static bool TryParse(SyntacticItem item, string data) { LexicalState lexicalState = new LexicalState(data); if (!Input.S.ParseFull(lexicalState)) return false; SyntacticState syntacticState = new SyntacticState( lexicalState.ExtractTokens(), data); if (!item.ParseFull(syntacticState)) return false; return true; }
/// <summary> /// Execute parsing method checking research deep. /// </summary> private static bool Parse(ParseDelegate action, SyntacticState state, SyntacticItem[] args) { state.Deep++; #if DEBUG Debug.WriteLine(String.Empty); Debug.WriteLine(state.GetOuterDebug()); SyntacticItem debugItem = (SyntacticItem)action.Target; Debug.WriteLine("[{0}] ({1})", debugItem.Key, state.Deep); string debugMethod = action.Method.Name; debugMethod = debugMethod .Split( new[] { "Parse", "Internal", "<", ">" }, StringSplitOptions.RemoveEmptyEntries) .First() .ToUpperInvariant(); Debug.Write(debugMethod); Debug.Write(" ="); foreach (var arg in args.Where(arg => arg != null)) { Debug.Write(" | "); Debug.Write(arg.Key); } Debug.WriteLine(String.Empty); #endif if (state.Deep >= c_maxResearchDeep) { throw new InvalidOperationException( String.Format("Maximum research deep {0} has been reached.", c_maxResearchDeep)); } bool parsed = action.Invoke(state, args); state.Deep--; return(parsed); }
/// <summary> /// Tries to parses specified data string into syntactic item. /// </summary> protected override bool Parse(SyntacticItem item, string data) { return CSharpParser.TryParse(item, data); }
/// <summary> /// Ensures that specified data could be parsed as a syntactic item. /// </summary> public static void Ensure(SyntacticItem item, string data) { if (!TryParse(item, data)) throw new InvalidOperationException(); }
/// <summary> /// Tries to parses specified data string into syntactic item. /// </summary> protected abstract bool Parse(SyntacticItem item, string data);
/// <summary> /// Execute parsing method checking research deep. /// </summary> private static bool Parse(ParseDelegate action, SyntacticState state, SyntacticItem[] args) { state.Deep++; #if DEBUG Debug.WriteLine(String.Empty); Debug.WriteLine(state.GetOuterDebug()); SyntacticItem debugItem = (SyntacticItem)action.Target; Debug.WriteLine("[{0}] ({1})", debugItem.Key, state.Deep); string debugMethod = action.Method.Name; debugMethod = debugMethod .Split( new[] { "Parse", "Internal", "<", ">" }, StringSplitOptions.RemoveEmptyEntries) .First() .ToUpperInvariant(); Debug.Write(debugMethod); Debug.Write(" ="); foreach (var arg in args.Where(arg => arg != null)) { Debug.Write(" | "); Debug.Write(arg.Key); } Debug.WriteLine(String.Empty); #endif if (state.Deep >= c_maxResearchDeep) throw new InvalidOperationException( String.Format("Maximum research deep {0} has been reached.", c_maxResearchDeep)); bool parsed = action.Invoke(state, args); state.Deep--; return parsed; }
/// <summary> /// Tries to parse a batch of similar syntactic items with specified delimiter. /// </summary> private bool ParseManyInternal(SyntacticState state, SyntacticItem part, SyntacticItem delimiter) { int innerIndex = state.InnerPosition; int outerIndex = state.OuterPosition; if (!part.Parse(state)) return false; while (true) { int lastInnerIndex = state.InnerPosition; int lastOuterIndex = state.OuterPosition; if (delimiter != null) { if (!delimiter.Parse(state)) break; } if (!part.Parse(state)) { state.Reset(lastInnerIndex, lastOuterIndex); break; } } state.AddBack(Key, innerIndex, outerIndex); return true; }