本文整理汇总了C#中IMacroContext.Write方法的典型用法代码示例。如果您正苦于以下问题:C# IMacroContext.Write方法的具体用法?C# IMacroContext.Write怎么用?C# IMacroContext.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IMacroContext
的用法示例。
在下文中一共展示了IMacroContext.Write方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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));
}
示例2: 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)
{
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 (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;
}
示例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
}
示例4: _set
public static LNode _set(LNode node, IMacroContext context)
{
var lhs = node.Args[0, LNode.Missing];
var name = lhs.Name;
bool isSnippet = name == _hash_snippet;
if ((isSnippet || name == _hash_set) && node.ArgCount == 2 && lhs.IsId)
{
node = context.PreProcessChildren();
Symbol newTarget = isSnippet ? _hash_setScopedPropertyQuote : _hash_setScopedProperty;
var stmts = node.Args.Slice(1).Select(key =>
{
LNode value = [email protected];
if (key.Calls(S.Assign, 2))
{
value = key.Args[1];
key = key.Args[0];
if (isSnippet && value.Calls(S.Braces))
value = value.Args.AsLNode(S.Splice);
}
if (!key.IsId)
context.Write(Severity.Error, key, "Invalid key; expected an identifier.");
return node.With(newTarget, LNode.Literal(key.Name, key), value);
});
return F.Call(S.Splice, stmts);
}
return null;
}
示例5: TranslateSpaceDefinition
public static LNode @using1(LNode node, IMacroContext sink)
{
if (node.ArgCount == 1 && IsComplexId(node.Args[0])) {
// Looks like an import statement
sink.Write(Severity.Warning, node.Target, "The 'import' statement replaces the 'using' statement in LeMP.");
return node.WithTarget(S.Import);
}
var result = TranslateSpaceDefinition(node, sink, S.Alias);
if (result != null)
return result.PlusAttr(F.Id(S.FilePrivate));
return null;
}
示例6: 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;
}
示例7: ForwardMethod
public static LNode ForwardMethod(LNode fn, IMacroContext context)
{
LNode args, fwd, body;
if (fn.ArgCount != 4 || !(fwd = fn.Args[3]).Calls(S.Forward, 1) || !(args = fn.Args[2]).Calls(S.AltList))
return null;
VList<LNode> argList = GetArgNamesFromFormalArgList(args, formalArg =>
context.Write(Severity.Error, formalArg, "'==>': Expected a variable declaration here"));
LNode target = GetForwardingTarget(fn.Args[1], fwd);
LNode call = F.Call(target, argList);
bool isVoidFn = fn.Args[0].IsIdNamed(S.Void);
body = F.Braces(isVoidFn ? call : F.Call(S.Return, call));
return fn.WithArgChanged(3, body);
}
示例8: ValidateOnStmt
private static LNode ValidateOnStmt(LNode node, IMacroContext context, out VList<LNode> restOfStmts, out LNode firstArg)
{
var a = node.Args;
LNode on_handler;
restOfStmts = LNode.List();
firstArg = null;
if (a.Count == 2) {
firstArg = a[0];
} else if (a.Count != 1)
return null;
if (!(on_handler = a.Last).Calls(S.Braces))
return null;
if (context.RemainingNodes.Count == 0)
context.Write(Severity.Warning, node, "{0} should not be the final statement of a block.", node.Name);
restOfStmts = new VList<LNode>(context.RemainingNodes);
context.DropRemainingNodes = true;
return on_handler;
}
示例9: Shape
[LexicalMacro("match (var) { case ...: ... }; // In LES, use a => b instead of case a: b", "Attempts to match and deconstruct an object against a \"pattern\", such as a tuple or an algebraic data type. Example:\n" + "match (obj) { \n" + " case is Shape(ShapeType.Circle, $size, Location: $p is Point<int>($x, $y)): \n" + " Circle(size, x, y); \n" + "}\n\n" + "This is translated to the following C# code: \n" + "do { \n" + " Point<int> p; \n" + " Shape tmp1; \n" + " if (obj is Shape) { \n" + " var tmp1 = (Shape)obj; \n" + " if (tmp1.Item1 == ShapeType.Circle) { \n" + " var size = tmp1.Item2; \n" + " var tmp2 = tmp1.Location; \n" + " if (tmp2 is Point<int>) { \n" + " var p = (Point<int>)tmp2; \n" + " var x = p.Item1; \n" + " var y = p.Item2; \n" + " Circle(size, x, y); \n" + " break; \n" + " } \n" + " }\n" + " }\n" + "} while(false); \n" + "`break` is not expected at the end of each handler (`case` code block), but it can " + "be used to exit early from a `case`. You can associate multiple patterns with the same " + "handler using `case pattern1, pattern2:` in EC#, but please note that (due to a " + "limitation of plain C#) this causes code duplication since the handler will be repeated " + "for each pattern.")] public static LNode match(LNode node, IMacroContext context)
{
{
LNode input;
VList<LNode> contents;
if (node.Args.Count == 2 && (input = node.Args[0]) != null && node.Args[1].Calls(CodeSymbols.Braces)) {
contents = node.Args[1].Args;
var outputs = new WList<LNode>();
input = MaybeAddTempVarDecl(input, outputs);
int next_i = 0;
for (int case_i = 0; case_i < contents.Count; case_i = next_i) {
var @case = contents[case_i];
if (!IsCaseLabel(@case))
return Reject(context, contents[0], "In 'match': expected 'case' statement");
for (next_i = case_i + 1; next_i < contents.Count; next_i++) {
var stmt = contents[next_i];
if (IsCaseLabel(stmt))
break;
if (stmt.Calls(S.Break, 0)) {
next_i++;
break;
}
}
var handler = new VList<LNode>(contents.Slice(case_i + 1, next_i - (case_i + 1)));
if (@case.Calls(S.Case) && @case.Args.Count > 0) {
var codeGen = new CodeGeneratorForMatchCase(context, input, handler);
foreach (var pattern in @case.Args)
outputs.Add(codeGen.GenCodeForPattern(pattern));
} else {
outputs.Add(LNode.Call(CodeSymbols.Braces, LNode.List(handler)).SetStyle(NodeStyle.Statement));
if (next_i < contents.Count)
context.Write(Severity.Error, contents[next_i], "The default branch must be the final branch in a 'match' statement.");
}
}
return LNode.Call(CodeSymbols.DoWhile, LNode.List(outputs.ToVList().AsLNode(S.Braces), LNode.Literal(false)));
}
}
return null;
}
示例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.");
}
示例11: on_return
public static LNode on_return(LNode node, IMacroContext context)
{
LNode firstArg, rest, on_handler = ValidateOnStmt(node, context, out rest, out firstArg);
if (on_handler == null)
return null;
// Get/construct the declaration of the var to return, and get its name
LNode varDecl = firstArg, varName = firstArg;
bool varAssigned = false;
if (firstArg == null) {
varName = F.Id(__result__);
varDecl = F.Var(F._Missing, varName);
} else {
if (varDecl.Calls(S.Var, 2)) {
if (varAssigned = (varName = varDecl.Args[1]).Calls(S.Assign, 2))
varName = varName.Args[0];
} else if (varName.IsId) {
varDecl = node.With(S.Var, F._Missing, varName);
} else
return Reject(context, firstArg, "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).");
}
var retExpr = F.Call(S.Substitute, F.Id(__retexpr__));
Pair<LNode, LNode>[] patterns = new Pair<LNode, LNode>[2] {
// return; => { <on_handler> return; }
new Pair<LNode,LNode>(F.Call(S.Return), varAssigned
? on_handler.WithArgs(new RVList<LNode>(varDecl)
.AddRange(on_handler.Args)
.Add(F.Call(S.Return, varName)))
: on_handler.PlusArg(F.Call(S.Return))),
// return exp; => { <varDecl = $exp> <on_handler> return <varName>; }
new Pair<LNode,LNode>(F.Call(S.Return, retExpr),
on_handler.WithArgs(new RVList<LNode>(
varDecl.WithArgChanged(1, F.Call(S.Assign, varName, retExpr)))
.AddRange(on_handler.Args)
.Add(F.Call(S.Return, varName))))
};
int replacementCount = 0;
RVList<LNode> output = StandardMacros.Replace(rest.Args, patterns, out replacementCount);
if (replacementCount == 0)
context.Write(Severity.Warning, node, "'on_return': no 'return' statements were found below this line, so this macro had no effect.");
return output.AsLNode(S.Splice);
}