private static IEnumerable <S4JStateStackEvent> Analyse(char[] code, int index, S4JStateBag StateBag, S4JTokenStack stateStack) // S4JStateStack stateStack) { // sprawdzamy zakończenie stanu S4JToken prevToken = stateStack.Peek(); if (GetStateEnd(code, index, StateBag, prevToken) != null) { Int32 nextIndex = index + (prevToken.State.FoundGates.First().End == null ? 0 : (prevToken.State.FoundGates.First().End.Length - 1)) + 1; yield return(new S4JStateStackEvent() { NewIndex = S4JParserHelper.SkipWhiteSpaces(code, nextIndex), State = prevToken.State, Popped = true, // Chars = end }); yield break; } prevToken = stateStack.Peek(); S4JState state = GetStateBegin(code, index, StateBag, prevToken); if (state != null) { Int32 nextIndex = index + (state.FoundGates?.FirstOrDefault()?.Start == null ? 0 : (state.FoundGates.First().Start.Length - 1)) + 1; yield return(new S4JStateStackEvent() { // NewIndex = null, NewIndex = state.IsQuotation ? nextIndex : // state.IsCollection ? S4JParserHelper.SkipWhiteSpaces(code, nextIndex), // index + (matchedGate?.Start == null ? 0 : (matchedGate.Start.Count - 1)) + 1, State = state, Pushed = true, // Chars = matchedGate?.Start ?? new[] { code[index] } }); } else { Int32?newIndex = null; // pominiecie białych znaków do nastepnego 'stanu' // tylko jesli nie nie jestesmy w cytacie if (!prevToken.State.IsQuotation && !prevToken.State.IsComment) { newIndex = S4JParserHelper.SkipWhiteSpaces(code, index + 1); if (newIndex != null) { S4JState stateBegin = GetStateBegin(code, newIndex.Value, StateBag, prevToken); S4JState stateEnd = GetStateEnd(code, newIndex.Value, StateBag, prevToken); if (stateBegin != null || stateEnd != null) { newIndex = newIndex; } else { newIndex = null; } } else { newIndex = Int32.MaxValue; } } yield return(new S4JStateStackEvent() { NewIndex = newIndex, State = stateStack.Peek()?.State, Chars = new[] { code[index] } }); } }
public S4JTokenRoot Parse(String Text, S4JStateBag stateBag) { char[] chars = Text.Trim().ToCharArray(); S4JTokenStack valueStack = new S4JTokenStack(); S4JTokenRoot rootVal = new S4JTokenRoot() { State = stateBag.RootState }; valueStack.Push(rootVal); Int32 startIndex = S4JParserHelper. SkipWhiteSpaces(chars, 0) ?? Int32.MaxValue; for (int i = startIndex; i < chars.Length; i++) { foreach (S4JStateStackEvent stackEvent in Analyse(chars, i, stateBag, valueStack)) { if (stackEvent.NewIndex != null) { i = stackEvent.NewIndex.Value - 1; } // zdjęcie ze stosu if (stackEvent.Popped) { S4JToken currentVal = valueStack.Peek(); // PeekNonValue(); if (stackEvent.Chars != null) { currentVal.AppendCharsToToken(stackEvent.Chars); } // zatwierdzenie tokena currentVal = valueStack.Peek(); if (currentVal != null) { currentVal.Commit(); } currentVal.OnPop(); valueStack.Pop(); } else { if (stackEvent.State.StateType == EStateType.S4J_VALUE_DELIMITER) { if (valueStack.Peek() != null) { valueStack.Peek().MarkLastChildAsObjectKey(); valueStack.Peek().Commit(); } } else if (stackEvent.State.StateType == EStateType.S4J_COMA) { if (valueStack.Peek() != null) { valueStack.Peek().Commit(); } } else { if (stackEvent.Pushed && stackEvent.State.IsSimpleValue == false) { S4JToken prevVal = valueStack.Peek(); S4JToken newToken = new S4JTokenFactory().To_token(stackEvent.State); valueStack.Push(newToken); newToken.Parent = prevVal; if (!stackEvent.State.IsComment) { prevVal.AddChildToToken(newToken); } } if (stackEvent.Chars != null) { if (stackEvent.State.IsSimpleValue) { } S4JToken currentVal = valueStack.Peek(); currentVal.AppendCharsToToken(stackEvent.Chars); } } } } } while (valueStack.Count > 0) { S4JToken currentVal = valueStack.Peek(); if (currentVal != null) { currentVal.Commit(); } currentVal.OnPop(); valueStack.Pop(); } /*if (String.IsNullOrEmpty(rootVal.Name)) * { * return rootVal.Children.Single() as S4JToken; * }*/ return(rootVal); }