private static S4JState GetStateEnd(char[] code, Int32 index, S4JStateBag StateBag, S4JToken prevToken) { if (prevToken == null) { return(null); } if (prevToken?.State?.FoundGates == null) { return(null); } foreach (var gate in prevToken.State.FoundGates) { // TODO GATE char[] end = gate.End; if (S4JParserHelper.Is(code, index, end)) { prevToken.State.FoundGates.RemoveAll(g => g != gate); return(prevToken?.State); } } return(null); }
//////////////////////////////////// public S4JStateBag Clone() { S4JStateBag item = new S4JStateBag(); item.all_states = this.all_states.Select(i => i.Clone()).ToList(); item.all_states_dict = item.all_states.ToDictionary(i => i.ID, i => i); item.RootState = item.all_states_dict[this.RootState.ID]; item.ValueState = item.all_states_dict[this.ValueState.ID]; item.stateType_states = this.stateType_states.ToDictionary(i => i.Key, i => i.Value.Select(v => item.all_states_dict[v.ID]).ToList()); item.stateID_allowed_states = this.stateID_allowed_states.ToDictionary(i => i.Key, i => i.Value.Select(v => item.all_states_dict[v.ID]).ToArray()); return(item); }
public static S4JStateBag Get() { if (i == null) { lock (lck) if (i == null) { i = new S4JStateBag(); i.AddStatesToBag( new CSharpFunction("c#"), new DynLanFunction("dynlan"), new TSqlFunction("sql")); } } return(i);//.Clone(); }
private static S4JState GetStateBegin(char[] code, Int32 index, S4JStateBag StateBag, S4JToken prevToken) { if (prevToken == null) { return(null); } //foreach (S4JState state in StateBag.GetStates(prevToken.State.AllowedStatesNames)) { // sprawdzamy rozpoczecie stanu // if (prevToken == null /*|| // prevToken.State.IsAllowed(state)*///) { Boolean isAllowed = false; List <S4JStateGate> matchedGates = new List <S4JStateGate>(); S4JState foundState = null; // pobszukiwanie rozpoczecia stanu var allowedStates = StateBag.GetAllowedStates(prevToken?.State); //foreach (S4JState state in ) for (var i = 0; i < allowedStates.Length; i++) { var state = allowedStates[i]; Int32 gateStartCount = 0; foreach (S4JStateGate gate in state.Gates) { if (S4JParserHelper.Is(code, index, gate.Start)) { // jesli znaleziony ciag jest wiekszy od ostatniego if (gateStartCount < gate.Start.Length && matchedGates.Count > 0) { matchedGates.Clear(); } matchedGates.Add(gate.Clone()); isAllowed = true; foundState = state; gateStartCount = gate.Start.Length; //break; } } //if (isAllowed) // break; } if (isAllowed) { S4JState newState = foundState.Clone(); newState.FoundGates = matchedGates; return(newState); } } } /*foreach (S4JState state in StateBag) * { * // sprawdzamy rozpoczecie stanu * if (prevToken == null || * prevToken.State.IsAllowed(state)) * { * Boolean isAllowed = false; * List<S4JStateGate> matchedGates = new List<S4JStateGate>(); * * // pobszukiwanie rozpoczecia stanu * foreach (S4JStateGate gate in state.Gates) * { * if (S4JParserHelper.Is(code, index, gate.Start)) * { * matchedGates.Add(gate.Clone()); * isAllowed = true; * } * } * * if (isAllowed) * { * S4JState newState = state.Clone(); * newState.FoundGates = matchedGates; * return newState; * } * } * }*/ return(null); }
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); }
public S4JTokenRoot Parse(String Text) { S4JStateBag stateBag = S4JDefaultState return Parse(Text, ) }
public S4JTokenRoot Parse(String Text) { S4JStateBag stateBag = S4JDefaultStateBag.Get(); return(Parse(Text, stateBag)); }