public void TestCombinationWithSlice() { var l = new SlidingList <int>(new IList <int>[] { new int[] { 1, 2, 3 }, new int[] { 10, 20, 30 }, new int[] { 111, 222, 333, 444 } }); var l2 = new ListSlice <int>(l, 2, 7); var a = new int[8]; l2.CopyTo(a, 1); CollectionAssert.AreEqual(new int[] { 0, 3, 10, 20, 30, 111, 222, 333 }, a); }
private LNode ApplyMacrosFound2(CurNodeState s, ListSlice <MacroInfo> foundMacros) { s.StartApplyMacros(); if (!s.Input.Calls(S.Braces)) { return(ApplyMacrosFound3(s, foundMacros)); } else { _scopes.Add(null); try { return(ApplyMacrosFound3(s, foundMacros)); } finally { PopScope(); } } }
public void AddNode(TagNodeData inNode) #endif // EXPANDED_REFS { if (m_Nodes == null) { m_Nodes = new TagNodeData[InitialNodeCount]; } else if (m_NodeCount >= m_Nodes.Length) { Array.Resize(ref m_Nodes, m_Nodes.Length * 2); } m_Nodes[m_NodeCount++] = inNode; m_NodeList = new ListSlice <TagNodeData>(m_Nodes, 0, m_NodeCount); if (inNode.Type == TagNodeType.Event) { ++m_EventCount; } }
public static void Compile(ByteCodeCompiler bcc, ParserContext parser, ByteBuffer buffer, ListSlice listSlice, bool outputUsed) { ByteCodeCompiler.EnsureUsed(listSlice, outputUsed); bcc.CompileExpression(parser, buffer, listSlice.Root, true); Expression step = listSlice.Items[2]; bool isStep1 = step is IntegerConstant && ((IntegerConstant)step).Value == 1; int serializeThese = isStep1 ? 2 : 3; for (int i = 0; i < serializeThese; ++i) { Expression item = listSlice.Items[i]; if (item != null) { bcc.CompileExpression(parser, buffer, item, true); } } bool firstIsPresent = listSlice.Items[0] != null; bool secondIsPresent = listSlice.Items[1] != null; buffer.Add(listSlice.BracketToken, OpCode.LIST_SLICE, new int[] { firstIsPresent ? 1 : 0, secondIsPresent ? 1 : 0, isStep1 ? 0 : 1 }); }
public MacroResult(MacroInfo macro, LNode newNode, ListSlice <MessageHolder.Message> msgs, bool dropRemaining) { Macro = macro; NewNode = newNode; Msgs = msgs; DropRemainingNodes = dropRemaining; }
private LNode ApplyMacrosFound3(CurNodeState s, ListSlice <MacroInfo> foundMacros) { LNode input = s.Input; Debug.Assert(s.Results.IsEmpty && s.MessageHolder.List.Count == 0); IList <MessageHolder.Message> messageList = s.MessageHolder.List; int accepted = 0, acceptedIndex = -1; for (int i = 0; i < foundMacros.Count; i++) { var macro = foundMacros[i]; var macroInput = input; if ((macro.Mode & MacroMode.ProcessChildrenBefore) != 0) { macroInput = ProcessChildrenOfCurrentNode(); } LNode output = null; s.DropRemainingNodes = (macro.Mode & MacroMode.DropRemainingListItems) != 0; int mhi = messageList.Count; try { Scope scope = AutoInitScope(); MacrosInvoked++; output = macro.Macro(macroInput, scope); if (output != null) { accepted++; acceptedIndex = i; } } catch (ThreadAbortException e) { _sink.Write(Severity.Error, input, "Macro-processing thread aborted in {0}", QualifiedName(macro.Macro.Method)); _sink.Write(Severity.Detail, input, e.StackTrace); s.Results.Add(new MacroResult(macro, output, messageList.Slice(mhi, messageList.Count - mhi), s.DropRemainingNodes)); PrintMessages(s.Results, input, accepted, Severity.Error); throw; } catch (Exception e) { s.MessageHolder.Write(Severity.Error, input, "{0}: {1}", e.GetType().Name, e.Message); s.MessageHolder.Write(Severity.Detail, input, e.StackTrace); } s.Results.Add(new MacroResult(macro, output, messageList.Slice(mhi, messageList.Count - mhi), s.DropRemainingNodes)); } s.DropRemainingNodes = false; PrintMessages(s.Results, input, accepted, s.MessageHolder.List.MaxOrDefault(msg => (int)msg.Severity).Severity); if (accepted >= 1) { var result = s.Results[acceptedIndex]; NodesReplaced++; Debug.Assert(result.NewNode != null); if ((result.Macro.Mode & MacroMode.ProcessChildrenBefore) != 0) { s.MaxExpansions--; } LNode result2; if ((result.Macro.Mode & MacroMode.Normal) != 0) { if (result.NewNode == input) { result2 = ApplyMacrosToChildrenOf(result.NewNode, s.MaxExpansions - 1); } else { result2 = ApplyMacros(result.NewNode, s.MaxExpansions - 1, s.IsTarget); if (result2 != null) { result.DropRemainingNodes |= _s.DropRemainingNodes; } } } else if ((result.Macro.Mode & MacroMode.ProcessChildrenAfter) != 0) { result2 = ApplyMacrosToChildrenOf(result.NewNode, s.MaxExpansions - 1); } else { return(result.NewNode); } s.DropRemainingNodes = result.DropRemainingNodes; return(result2 ?? result.NewNode); } else { return(s.Preprocessed ?? ApplyMacrosToChildrenOf(input, s.MaxExpansions)); } }
public PrefetchingIteratorAnonymousInnerClass(ListSlice outerInstance) { this.outerInstance = outerInstance; innerIterator = outerInstance.Inner.GetEnumerator(); }
internal Enumerator(ListSlice <T> owner) => _owner = owner;
private static Expression ParseEntity(TokenStream tokens, Executable owner) { Expression root; if (tokens.PopIfPresent("(")) { root = Parse(tokens, owner); tokens.PopExpected(")"); } else { root = ParseEntityWithoutSuffixChain(tokens, owner); } bool anySuffixes = true; while (anySuffixes) { if (tokens.IsNext(".")) { Token dotToken = tokens.Pop(); Token stepToken = tokens.Pop(); Parser.VerifyIdentifier(stepToken); root = new DotStep(root, dotToken, stepToken, owner); } else if (tokens.IsNext("[")) { Token openBracket = tokens.Pop(); List <Expression> sliceComponents = new List <Expression>(); if (tokens.IsNext(":")) { sliceComponents.Add(null); } else { sliceComponents.Add(Parse(tokens, owner)); } for (int i = 0; i < 2; ++i) { if (tokens.PopIfPresent(":")) { if (tokens.IsNext(":") || tokens.IsNext("]")) { sliceComponents.Add(null); } else { sliceComponents.Add(Parse(tokens, owner)); } } } tokens.PopExpected("]"); if (sliceComponents.Count == 1) { Expression index = sliceComponents[0]; root = new BracketIndex(root, openBracket, index, owner); } else { root = new ListSlice(root, sliceComponents, openBracket, owner); } } else if (tokens.IsNext("(")) { Token openParen = tokens.Pop(); List <Expression> args = new List <Expression>(); while (!tokens.PopIfPresent(")")) { if (args.Count > 0) { tokens.PopExpected(","); } args.Add(Parse(tokens, owner)); } root = new FunctionCall(root, openParen, args, owner); } else if (tokens.IsNext("is")) { Token isToken = tokens.Pop(); Token classToken = tokens.Pop(); string className = PopClassName(tokens, classToken); root = new IsComparison(root, isToken, classToken, className, owner); } else { anySuffixes = false; } } return(root); }
private MacroResult?ApplyMacrosFound2(CurNodeState s, ListSlice <MacroInfo> foundMacros) { s.BeforeApplyMacros(); LNode input = s.Input; Debug.Assert(s.Results.IsEmpty && s.MessageHolder.List.Count == 0); IList <LogMessage> messageList = s.MessageHolder.List; int accepted = 0, acceptedIndex = -1; for (int i = 0; i < foundMacros.Count; i++) { var macro = foundMacros[i]; var macroInput = input; if ((macro.Mode & MacroMode.ProcessChildrenBefore) != 0) { macroInput = ProcessChildrenOfCurrentNode(); } LNode output = null; s.DropRemainingNodesRequested = (macro.Mode & MacroMode.DropRemainingListItems) != 0; int mhi = messageList.Count; try { Scope scope = AutoInitScope(); MacrosInvoked++; // CALL THE MACRO! output = macro.Macro(macroInput, scope); if (output != null) { accepted++; acceptedIndex = i; } } catch (ThreadAbortException e) { _sink.Write(Severity.Error, input, "Macro-processing thread aborted in {0}", QualifiedName(macro.Macro.Method)); _sink.Write(Severity.Detail, input, e.StackTrace); s.Results.Add(new MacroResult(macro, output, messageList.Slice(mhi, messageList.Count - mhi), s.DropRemainingNodesRequested)); PrintMessages(s.Results, input, accepted, Severity.Error); throw; } catch (LogException e) { e.Msg.WriteTo(s.MessageHolder); } catch (Exception e) { s.MessageHolder.Write(Severity.Error, input, "{0}: {1}", e.GetType().Name, e.Message); s.MessageHolder.Write(Severity.Detail, input, e.StackTrace); } s.Results.Add(new MacroResult(macro, output, messageList.Slice(mhi, messageList.Count - mhi), s.DropRemainingNodesRequested)); } s.DropRemainingNodesRequested = false; PrintMessages(s.Results, input, accepted, s.MessageHolder.List.MaxOrDefault(msg => (int)msg.Severity).Severity); if (accepted >= 1) { var result = s.Results[acceptedIndex]; return(result); } else { return(null); } }
private Expression ParseEntity(TokenStream tokens, Executable owner) { Expression root; if (tokens.PopIfPresent("(")) { root = this.Parse(tokens, owner); tokens.PopExpected(")"); } else { root = ParseEntityWithoutSuffixChain(tokens, owner); } bool anySuffixes = true; while (anySuffixes) { if (tokens.IsNext(".")) { Token dotToken = tokens.Pop(); Token stepToken = tokens.Pop(); // HACK alert: "class" is a valid field on a class. // ParserVerifyIdentifier is invoked downstream for non-resolved fields. if (stepToken.Value != this.parser.Keywords.CLASS) { this.parser.VerifyIdentifier(stepToken); } root = new DotStep(root, dotToken, stepToken, owner); } else if (tokens.IsNext("[")) { Token openBracket = tokens.Pop(); List <Expression> sliceComponents = new List <Expression>(); if (tokens.IsNext(":")) { sliceComponents.Add(null); } else { sliceComponents.Add(Parse(tokens, owner)); } for (int i = 0; i < 2; ++i) { if (tokens.PopIfPresent(":")) { if (tokens.IsNext(":") || tokens.IsNext("]")) { sliceComponents.Add(null); } else { sliceComponents.Add(Parse(tokens, owner)); } } } tokens.PopExpected("]"); if (sliceComponents.Count == 1) { Expression index = sliceComponents[0]; root = new BracketIndex(root, openBracket, index, owner); } else { root = new ListSlice(root, sliceComponents, openBracket, owner); } } else if (tokens.IsNext("(")) { Token openParen = tokens.Pop(); List <Expression> args = new List <Expression>(); while (!tokens.PopIfPresent(")")) { if (args.Count > 0) { tokens.PopExpected(","); } args.Add(Parse(tokens, owner)); } root = new FunctionCall(root, openParen, args, owner); } else if (tokens.IsNext(this.parser.Keywords.IS)) { Token isToken = tokens.Pop(); Token classToken = tokens.Pop(); string className = this.parser.PopClassNameWithFirstTokenAlreadyPopped(tokens, classToken); root = new IsComparison(root, isToken, classToken, className, owner); } else { anySuffixes = false; } } return(root); }
private Expression ParseEntity(TokenStream tokens, Node owner) { Expression root; Token firstToken = tokens.Peek(); AType castPrefix = firstToken.Value == "(" ? this.MaybeParseCastPrefix(tokens) : null; if (castPrefix != null) { root = this.ParseEntity(tokens, owner); return(new Cast(firstToken, castPrefix, root, owner, true)); } Token maybeOpenParen = tokens.Peek(); if (tokens.PopIfPresent("(")) { if (tokens.PopIfPresent(")")) { root = this.ParseLambda(tokens, firstToken, new AType[0], new Token[0], owner); } else { if (this.parser.CurrentScope.IsStaticallyTyped) { TokenStream.StreamState state = tokens.RecordState(); AType lambdaArgType = this.parser.TypeParser.TryParse(tokens); Token lambdaArg = null; if (lambdaArgType != null) { lambdaArg = tokens.PopIfWord(); } if (lambdaArg != null) { List <AType> lambdaArgTypes = new List <AType>() { lambdaArgType }; List <Token> lambdaArgs = new List <Token>() { lambdaArg }; while (tokens.PopIfPresent(",")) { lambdaArgTypes.Add(this.parser.TypeParser.Parse(tokens)); lambdaArgs.Add(tokens.PopWord()); } tokens.PopExpected(")"); return(this.ParseLambda(tokens, maybeOpenParen, lambdaArgTypes, lambdaArgs, owner)); } else { tokens.RestoreState(state); } } root = this.Parse(tokens, owner); if (root is Variable) { if (tokens.PopIfPresent(")")) { if (tokens.IsNext("=>")) { root = this.ParseLambda(tokens, firstToken, new AType[] { AType.Any(root.FirstToken) }, new Token[] { root.FirstToken }, owner); } } else if (tokens.IsNext(",")) { List <Token> lambdaArgs = new List <Token>() { root.FirstToken }; List <AType> lambdaArgTypes = new List <AType>() { AType.Any(root.FirstToken) }; Token comma = tokens.Peek(); while (tokens.PopIfPresent(",")) { Token nextArg = tokens.Pop(); if (nextArg.Type != TokenType.WORD) { throw new ParserException(comma, "Unexpected comma."); } lambdaArgTypes.Add(AType.Any(nextArg)); lambdaArgs.Add(nextArg); comma = tokens.Peek(); } tokens.PopExpected(")"); root = this.ParseLambda(tokens, firstToken, lambdaArgTypes, lambdaArgs, owner); } else { // This will purposely cause an unexpected token error // since it's none of the above conditions. tokens.PopExpected(")"); } } else { tokens.PopExpected(")"); } } } else { root = ParseEntityWithoutSuffixChain(tokens, owner); } bool anySuffixes = true; bool isPreviousADot = false; while (anySuffixes) { if (tokens.IsNext(".")) { isPreviousADot = true; Token dotToken = tokens.Pop(); Token fieldToken = tokens.Pop(); // HACK alert: "class" is a valid field on a class. // ParserVerifyIdentifier is invoked downstream for non-resolved fields. if (fieldToken.Value != this.parser.Keywords.CLASS) { this.parser.VerifyIdentifier(fieldToken); } root = new DotField(root, dotToken, fieldToken, owner); } else if (tokens.IsNext("[")) { Token openBracket = tokens.Pop(); List <Expression> sliceComponents = new List <Expression>(); if (tokens.IsNext(":")) { sliceComponents.Add(null); } else { sliceComponents.Add(Parse(tokens, owner)); } for (int i = 0; i < 2; ++i) { if (tokens.PopIfPresent(":")) { if (tokens.IsNext(":") || tokens.IsNext("]")) { sliceComponents.Add(null); } else { sliceComponents.Add(Parse(tokens, owner)); } } } tokens.PopExpected("]"); if (sliceComponents.Count == 1) { Expression index = sliceComponents[0]; root = new BracketIndex(root, openBracket, index, owner); } else { root = new ListSlice(root, sliceComponents, openBracket, owner); } } else if (tokens.IsNext("(")) { Token openParen = tokens.Pop(); List <Expression> args = new List <Expression>(); while (!tokens.PopIfPresent(")")) { if (args.Count > 0) { tokens.PopExpected(","); } args.Add(Parse(tokens, owner)); } root = new FunctionCall(root, openParen, args, owner); } else if (tokens.IsNext(this.parser.Keywords.IS)) { Token isToken = tokens.Pop(); Token classToken = tokens.Pop(); string className = this.parser.PopClassNameWithFirstTokenAlreadyPopped(tokens, classToken); root = new IsComparison(root, isToken, classToken, className, owner); } else if (isPreviousADot && this.parser.IsCSharpCompat && tokens.IsNext("<")) { TokenStream.StreamState s = tokens.RecordState(); Token openBracket = tokens.Pop(); AType funcType = this.parser.TypeParser.TryParse(tokens); List <AType> types = new List <AType>() { funcType }; if (funcType != null) { while (tokens.PopIfPresent(",")) { types.Add(this.parser.TypeParser.Parse(tokens)); } } if (funcType == null) { anySuffixes = false; tokens.RestoreState(s); } else if (!tokens.PopIfPresent(">") || !tokens.IsNext("(")) { anySuffixes = false; tokens.RestoreState(s); } else { // TODO(acrylic-conversion): do something with this types list. } } else { anySuffixes = false; } } return(root); }
private LNode ApplyMacrosFound2(LNode input, int maxExpansions, ListSlice <MacroInfo> foundMacros, ref ApplyMacroState s) { s.results.Clear(); s.messageHolder.List.Clear(); int accepted = 0, acceptedIndex = -1; for (int i = 0; i < foundMacros.Count; i++) { var macro = foundMacros[i]; var macroInput = input; if ((macro.Mode & MacroMode.ProcessChildrenBefore) != 0) { if (maxExpansions == 1) { continue; // avoid expanding both this macro and its children } if (s.preprocessed == null) { // _foundMacros, _results, and _messageHolder are re-used // by callee for unrelated contexts, so make copies of the s.* // variables which point to them. s.foundMacros = new List <MacroInfo>(s.foundMacros); s.results = new List <Result>(s.results); s.messageHolder = s.messageHolder.Clone(); foundMacros = new List <MacroInfo>(foundMacros).Slice(0); s.preprocessed = ApplyMacrosToChildren(input, maxExpansions) ?? input; } macroInput = s.preprocessed; } LNode output = null; int mhi = s.messageHolder.List.Count; try { output = macro.Macro(macroInput, s.messageHolder); if (output != null) { accepted++; acceptedIndex = i; } } catch (ThreadAbortException e) { _sink.Write(Severity.Error, input, "Macro-processing thread aborted in {0}", QualifiedName(macro.Macro.Method)); _sink.Write(Severity.Detail, input, e.StackTrace); s.results.Add(new Result(macro, output, s.messageHolder.List.Slice(mhi, s.messageHolder.List.Count - mhi))); PrintMessages(s.results, input, accepted, Severity.Error); throw; } catch (Exception e) { s.messageHolder.Write(Severity.Error, input, "{0}: {1}", e.GetType().Name, e.Message); s.messageHolder.Write(Severity.Detail, input, e.StackTrace); } s.results.Add(new Result(macro, output, s.messageHolder.List.Slice(mhi, s.messageHolder.List.Count - mhi))); } PrintMessages(s.results, input, accepted, s.messageHolder.List.MaxOrDefault(msg => (int)msg.Severity).Severity); if (accepted >= 1) { var result = s.results[acceptedIndex]; Debug.Assert(result.Node != null); if ((result.Macro.Mode & MacroMode.ProcessChildrenBefore) != 0) { maxExpansions--; } if ((result.Macro.Mode & MacroMode.Normal) != 0) { if (result.Node == input) { return(ApplyMacrosToChildren(result.Node, maxExpansions - 1) ?? result.Node); } else { return(ApplyMacros(result.Node, maxExpansions - 1) ?? result.Node); } } else if ((result.Macro.Mode & MacroMode.ProcessChildrenAfter) != 0) { return(ApplyMacrosToChildren(result.Node, maxExpansions - 1) ?? result.Node); } else { return(result.Node); } } else { // "{}" needs special treatment if (input.Calls(S.Braces)) { try { return(s.preprocessed ?? ApplyMacrosToChildren(input, maxExpansions)); } finally { PopScope(); } } return(s.preprocessed ?? ApplyMacrosToChildren(input, maxExpansions)); } }
public Result(MacroInfo macro, LNode node, ListSlice <MessageHolder.Message> msgs) { Macro = macro; Node = node; Msgs = msgs; }