当前位置: 首页>>代码示例>>C#>>正文


C# IMacroContext.PreProcess方法代码示例

本文整理汇总了C#中IMacroContext.PreProcess方法的典型用法代码示例。如果您正苦于以下问题:C# IMacroContext.PreProcess方法的具体用法?C# IMacroContext.PreProcess怎么用?C# IMacroContext.PreProcess使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IMacroContext的用法示例。


在下文中一共展示了IMacroContext.PreProcess方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: replace

		public static LNode replace(LNode node, IMacroContext context)
		{
			var args_body = context.GetArgsAndBody(true);
			var args = args_body.A;
			var body = args_body.B;
			if (args.Count == 1 && args[0].Calls(S.Tuple)) args = args[0].Args; // LESv2
			if (args.Count >= 1)
			{
				bool preprocess = node.Calls("replacePP");

				var patterns = new Pair<LNode, LNode>[args.Count];
				for (int i = 0; i < patterns.Length; i++)
				{
					var pair = args[i];
					if (pair.Calls(S.Lambda, 2)) {
						LNode pattern = pair[0], repl = pair[1];
						if (preprocess)
						{
							pattern = context.PreProcess(pattern);
							repl = context.PreProcess(repl);
						}
						if (pattern.Calls(S.Braces)) {
							if (pattern.ArgCount == 1)
								pattern = pattern.Args[0];
							else
								context.Write(Severity.Error, pattern, "The braces must contain only a single statement. To search for braces literally, use `{{ ... }}`");
						}
						if (repl.Calls(S.Braces))
							repl = repl.Args.AsLNode(S.Splice);
						
						// Avoid StackOverflowException when pattern is $Id (sadly, it
						// is uncatchable so it can crash LeMP.exe and even Visual Studio)
						if (LNodeExt.GetCaptureIdentifier(pattern) != null)
 							return Reject(context, pattern, "The left side of `=>` cannot be a capture. Remove the `$`.");

						patterns[i] = Pair.Create(pattern, repl);
					} else {
						string msg = "Expected 'pattern => replacement'.";
						if (pair.Descendants().Any(n => n.Calls(S.Lambda, 2)))
							msg += " " + "(Using '=>' already? Put the pattern on the left-hand side in parentheses.)";
						return Reject(context, pair, msg);
					}
				}

				int replacementCount;
				var output = Replace(body, patterns, out replacementCount);
				if (replacementCount == 0)
					context.Sink.Warning(node, "No patterns recognized; no replacements were made.");
				return output.AsLNode(S.Splice);
			}
			return null;
		}
开发者ID:qwertie,项目名称:ecsharp,代码行数:52,代码来源:ReplaceMacro.cs

示例2: on_return

		public static LNode on_return(LNode node, IMacroContext context)
		{
			VList<LNode> rest;
			LNode varDecl, bracedHandler = ValidateOnStmt(node, context, out rest, out varDecl);
			if (bracedHandler == null)
				return null;
			rest = context.PreProcess(rest);
			bracedHandler = context.PreProcess(bracedHandler);
			LNode varName;
			if (varDecl == null) {
				varName = Id__result__;
				varDecl = F.Var(F.Missing, varName);
			} else {
				{
					LNode tmp_0;
					if (varDecl.Calls(CodeSymbols.Var, 2) && (tmp_0 = varDecl.Args[1]) != null && tmp_0.Calls(CodeSymbols.Assign, 2) && (varName = tmp_0.Args[0]) != null)
						context.Write(Severity.Error, varName, "The return value cannot be assigned here. The value of this variable must be placed on the return statement(s).");
					else if (varDecl.Calls(CodeSymbols.Var, 2) && (varName = varDecl.Args[1]) != null) {
					} else if ((varName = varDecl).IsId)
						varDecl = varName.With(S.Var, F.Missing, varName);
					else
						return Reject(context, varDecl, "The first parameter to on_return must be a simple identifier (the name of a variable to return) or a variable declaration (for a variable to be returned).");
				}
			}
			bool foundReturn = false;
			rest = rest.SmartSelect(arg => arg.ReplaceRecursive(rnode => {
				{
					LNode retVal;
					if (rnode.Calls(CodeSymbols.Lambda, 2))
						return rnode;
					else if (rnode.Calls(CodeSymbols.Return, 0)) {
						foundReturn = true;
						return LNode.Call(CodeSymbols.Braces, LNode.List().AddRange(bracedHandler.Args).Add(rnode)).SetStyle(NodeStyle.Statement);
					} else if (rnode.Calls(CodeSymbols.Return, 1) && (retVal = rnode.Args[0]) != null) {
						foundReturn = true;
						var retValDecl = varDecl.WithArgChanged(1, LNode.Call(CodeSymbols.Assign, LNode.List(varName, retVal)).SetStyle(NodeStyle.Operator));
						rnode = rnode.WithArgs(varName);
						return LNode.Call(CodeSymbols.Braces, LNode.List().Add(retValDecl).AddRange(bracedHandler.Args).Add(rnode)).SetStyle(NodeStyle.Statement);
					} else
						return null;
				}
			}));
			if (DetectMissingVoidReturn(context, rest[rest.Count - 1, LNode.Missing]))
				rest.Add(bracedHandler.Args.AsLNode(S.Braces));
			else if (!foundReturn)
				context.Write(Severity.Warning, node, "'on_return': no 'return' statements were found in this context, so this macro had no effect.");
			return LNode.Call((Symbol) "#noLexicalMacros", LNode.List(rest));
		}
开发者ID:jonathanvdc,项目名称:Loyc,代码行数:48,代码来源:OnReturn.out.cs

示例3: static_matchCode

		public static LNode static_matchCode(LNode node, IMacroContext context)
		{
			if (node.AttrNamed(S.Static) == null && !node.HasSpecialName)
				return null; // handled by normal matchCode macro

			var args_body = context.GetArgsAndBody(false);
			VList<LNode> args = args_body.Item1, body = args_body.Item2;
			if (args.Count != 1)
				return Reject(context, args[1], "Expected only one expression to match");

			var expression = context.PreProcess(AutoStripBraces(args[0]));

			var cases = GetCases(body, context.Sink);
			// The `default:` case is represented by an empty list of patterns.
			if (cases.WithoutLast(1).Any(pair => pair.Key.IsEmpty))
				context.Write(Severity.Error, node, "The `default:` case must be the last one, because the cases are tested in the order they appear, so no case after `default:` can be matched.");

			MMap<Symbol, LNode> captures = new MMap<Symbol, LNode>();
			foreach (Pair<VList<LNode>, VList<LNode>> pair in cases)
			{
				var patterns = pair.Key.IsEmpty ? new VList<LNode>((LNode)null) : pair.Key;
				foreach (var pattern in patterns)
				{
					captures.Clear();
					VList<LNode> _;
					if (pattern == null || LNodeExt.MatchesPattern(expression, pattern, ref captures, out _)) {
						captures[_hash] = expression; // define $#
						captures.Remove(__);
						return ReplaceCaptures(pair.Value.AsLNode(S.Splice), captures);
					}
				}
			}
			return F.Call(S.Splice); // none of the cases matched
		}
开发者ID:qwertie,项目名称:ecsharp,代码行数:34,代码来源:StaticMatchCodeMacro.cs

示例4: useSymbols

		public static LNode useSymbols(LNode input, IMacroContext context)
		{
			bool inType = context.Ancestors.Any(parent => {
				var kind = EcsValidators.SpaceDefinitionKind(parent);
				return kind != null && kind != S.Namespace;
			});
			var args_body = context.GetArgsAndBody(true);
			args_body.B = context.PreProcess(args_body.B);
			return UseSymbolsCore(input.Attrs, args_body.A, args_body.B, context, inType);
		}
开发者ID:qwertie,项目名称:ecsharp,代码行数:10,代码来源:UseSymbolsMacro.cs

示例5: replace

		public static LNode replace(LNode node, IMacroContext context)
		{
			var args_body = context.GetArgsAndBody(true);
			var args = args_body.A;
			var body = args_body.B;
			if (args.Count == 1 && args[0].Calls(S.Tuple)) args = args[0].Args; // LESv2
			if (args.Count >= 1)
			{
				bool preprocess = node.Calls("replacePP");

				var patterns = new Pair<LNode, LNode>[args.Count];
				for (int i = 0; i < patterns.Length; i++)
				{
					var pair = args[i];
					if (pair.Calls(S.Lambda, 2)) {
						LNode pattern = pair[0], repl = pair[1];
						if (preprocess)
						{
							pattern = context.PreProcess(pattern);
							repl = context.PreProcess(repl);
						}
						if (pattern.Calls(S.Braces, 1) && repl.Calls(S.Braces)) {
							pattern = pattern.Args[0];
							repl = repl.WithTarget(S.Splice);
						}
						patterns[i] = Pair.Create(pattern, repl);
					} else {
						string msg = "Expected 'pattern => replacement'.";
						if (pair.Descendants().Any(n => n.Calls(S.Lambda, 2)))
							msg += " " + "(Using '=>' already? Put the pattern on the left-hand side in parentheses.)";
						return Reject(context, pair, msg);
					}
				}

				int replacementCount;
				var output = Replace(body, patterns, out replacementCount);
				if (replacementCount == 0)
					context.Write(Severity.Warning, node, "No patterns recognized; no replacements were made.");
				return output.AsLNode(S.Splice);
			}
			return null;
		}
开发者ID:jonathanvdc,项目名称:Loyc,代码行数:42,代码来源:ReplaceMacro.cs

示例6: AddCsLineDirectives

		public static LNode AddCsLineDirectives(LNode node, IMacroContext context)
		{
			if (node.ArgCount != 0)
				return null;
			int sourceLine = -1;
			var list0 = new VList<LNode>(context.RemainingNodes);
			var list1 = context.PreProcess(list0);
			var list2 = AddLineDirectives(list1, true, ref sourceLine);
			context.DropRemainingNodes = true;
			return F.Call(S.Splice, list2);
		}
开发者ID:qwertie,项目名称:ecsharp,代码行数:11,代码来源:AddLineDirectivesMacro.cs

示例7: staticMatches

		public static LNode staticMatches(LNode node, IMacroContext context)
		{
			if (node.ArgCount != 2)
				return null;

			LNode candidate = context.PreProcess(AutoStripBraces(node[0]));
			LNode pattern = AutoStripBraces(node[1]);
			MMap<Symbol, LNode> captures = new MMap<Symbol, LNode>();
			VList<LNode> _;
			if (LNodeExt.MatchesPattern(candidate, pattern, ref captures, out _)) {
				SetSyntaxVariables(captures, context);
				return F.True;
			}
			return F.False;
		}
开发者ID:qwertie,项目名称:ecsharp,代码行数:15,代码来源:StaticMatchCodeMacro.cs

示例8: useSequenceExpressions

		public static LNode useSequenceExpressions(LNode node, IMacroContext context)
		{
			var tmp_10 = context.GetArgsAndBody(true);
			var args = tmp_10.Item1;
			var body = tmp_10.Item2;
			if (args.Count > 0)
				context.Sink.Error(node[1], "#useSequenceExpressions does not support arguments.");
		
			{
				context.ScopedProperties[_useSequenceExpressionsIsRunning] = G.BoxedTrue;
				try {
					body = context.PreProcess(body);
				} finally {
					context.ScopedProperties.Remove(_useSequenceExpressionsIsRunning);
				}
			}
			var ers = new EliminateRunSequences(context);
			return ers.EliminateSequenceExpressions(body, true).AsLNode(S.Splice);
		}
开发者ID:qwertie,项目名称:ecsharp,代码行数:19,代码来源:UseSequenceExpressions.out.cs

示例9: unroll

		public static LNode unroll(LNode node, IMacroContext context)
		{
			LNode clause;
			// unroll (X, Y) \in ((X, Y), (Y, X)) {...}
			// unroll ((X, Y) in ((X, Y), (Y, X))) {...}
			if (node.ArgCount == 2 && ((clause = node.Args[0]).Calls(@in, 2) || clause.Calls(S.In, 2)))
			{
				LNode identifiers = clause.Args[0], cases = clause.Args[1];
				if (!cases.Calls(S.Tuple) && !cases.Calls(S.Braces) && !cases.Calls(S.Splice)) {
					cases = context.PreProcess(cases);
					if (!cases.Calls(S.Tuple) && !cases.Calls(S.Braces) && !cases.Calls(S.Splice))
						return Reject(context, cases, "The right-hand side of 'in' should be a tuple or braced block.");
				}
				var result = unroll(identifiers, cases.Args, node.Args[1], context.Sink);
				if (result != null && node.HasPAttrs())
					context.Sink.Warning(result.Attrs[0], "'unroll' does not support attributes.");
				return result;
			}
			return null;
		}
开发者ID:qwertie,项目名称:ecsharp,代码行数:20,代码来源:UnrollMacro.cs

示例10: DoDeconstruct

		private static void DoDeconstruct(LNode arg, IMacroContext context, bool printErrorOnFailure)
		{
			LNode patternSpec = arg.Args[0, LNode.Missing], input = arg.Args[1, LNode.Missing];
			if (!arg.Calls(S.Assign, 2))
			{
				if (arg.Calls(S.Lambda, 2))
					G.Swap(ref patternSpec, ref input);
				else {
					context.Sink.Error(arg, "expected an assignment (`patterns = input`)");
					return;
				}
			}

			// Build list of patterns out of the binary operator series p1 | p2 | p3
			var patterns = new FVList<LNode>();
			while (patternSpec.Calls(S.OrBits, 2) && !patternSpec.IsParenthesizedExpr()) {
				patterns.Add(patternSpec[1]);
				patternSpec = patternSpec[0];
			}
			patterns.Add(patternSpec);

			// Remove outer braces, then run macros
			patterns = patterns.SmartSelect(p => p.Calls(S.Braces, 1) ? p[0] : p);
			input = input.Calls(S.Braces, 1) ? input[0] : input;
			input = context.PreProcess(input);

			// Perform matching & capturing
			foreach (var pattern in patterns) {
				IDictionary<Symbol, LNode> captures;
				if (LNodeExt.MatchesPattern(input, pattern, out captures)) {
					if (captures.Count == 0)
						context.Write(printErrorOnFailure ? Severity.Warning : Severity.Error, pattern,
							"This pattern has no effect, since it does not use `$` to capture any variables.");
					SetSyntaxVariables(captures, context);
					return;
				}
			}

			if (printErrorOnFailure)
				context.Sink.Error(arg, "Deconstruction failed.");
		}
开发者ID:qwertie,项目名称:ecsharp,代码行数:41,代码来源:DeconstructMacro.cs

示例11: includeFileText

		public static LNode includeFileText(LNode node, IMacroContext context)
		{
			string filename;
			if (node.ArgCount == 1 && (filename = context.PreProcess(node[0]).Value as string) != null) {
				var inputFolder = context.ScopedProperties.TryGetValue((Symbol)"#inputFolder", "").ToString();
				var path = System.IO.Path.Combine(inputFolder, filename);
				var text = File.ReadAllText(path, Encoding.UTF8);
				return F.Literal(text).SetBaseStyle(NodeStyle.TDQStringLiteral);
			}
			return null;
		}
开发者ID:qwertie,项目名称:ecsharp,代码行数:11,代码来源:StandardMacros.cs

示例12: includeFileBinary

		public static LNode includeFileBinary(LNode node, IMacroContext context)
		{
			string filename;
			if (node.ArgCount == 1 && (filename = context.PreProcess(node[0]).Value as string) != null) {
				var inputFolder = context.ScopedProperties.TryGetValue((Symbol)"#inputFolder", "").ToString();
				var path = System.IO.Path.Combine(inputFolder, filename);
				var bytes = File.ReadAllBytes(path);
				var literal = F.Literal(bytes);
				// hex is typically more readable but decimal takes up fewer characters
				if (bytes.Length <= 1024)
					literal.SetBaseStyle(NodeStyle.HexLiteral);
				return literal;
			}
			return null;
		}
开发者ID:qwertie,项目名称:ecsharp,代码行数:15,代码来源:StandardMacros.cs

示例13: includeFile

		public static LNode includeFile(LNode node, IMacroContext context)
		{
			string filename;
			if (node.ArgCount == 1 && (filename = context.PreProcess(node[0]).Value as string) != null) {
				var parser = ParsingService.GetServiceForFileName(filename) ?? ParsingService.Default;
				var inputFolder = context.ScopedProperties.TryGetValue((Symbol)"#inputFolder", "").ToString();
				var path = System.IO.Path.Combine(inputFolder, filename);
				var contents = LNode.List(parser.ParseFile(path, context.Sink));
				return LNode.Call(S.Splice, contents, node);
			}
			return null;
		}
开发者ID:qwertie,项目名称:ecsharp,代码行数:12,代码来源:StandardMacros.cs

示例14: static_if

		public static LNode static_if(LNode @if, IMacroContext context)
		{
			if ([email protected](2, 3))
				return null;
			LNode cond = context.PreProcess(@if.Args[0]);
			cond = ReduceBooleanExpr(cond);
			object @bool;
			if ((@bool = cond.Value) is bool)
			{
				LNode output = (bool)@bool ? @if.Args[1] : @if.Args.TryGet(2, null) ?? F.Call(S.Splice);
				if (output.Calls(S.Braces))
					return output.WithTarget(S.Splice);
				else
					return output;
			}
			else
				return Reject(context, @if.Args[0], "Only boolean expressions can be evaluated.");
		}
开发者ID:qwertie,项目名称:ecsharp,代码行数:18,代码来源:StandardMacros.cs

示例15: static_if

		public static LNode static_if(LNode @if, IMacroContext context)
		{
			if (!Range.IsInRange(@if.ArgCount, 2, 3))
				return null;
			LNode cond = context.PreProcess(@if.Args[0]);
			object @bool;
			if ((@bool = cond.Value) is bool)
			{
				LNode output = (bool)@bool ? @if.Args[1] : @if.Args.TryGet(2, null) ?? F.Call(S.Splice);
				if (output.Calls(S.Braces))
					return output.WithTarget(S.Splice);
				else
					return output;
			}
			else
				return Reject(context, @if.Args[0], "'static if' is incredibly limited right now. Currently it only supports a literal boolean or (x `tree==` y)");
		}
开发者ID:jonathanvdc,项目名称:Loyc,代码行数:17,代码来源:StandardMacros.cs


注:本文中的IMacroContext.PreProcess方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。