本文整理汇总了C#中IMacroContext类的典型用法代码示例。如果您正苦于以下问题:C# IMacroContext类的具体用法?C# IMacroContext怎么用?C# IMacroContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IMacroContext类属于命名空间,在下文中一共展示了IMacroContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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
}
示例2: _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;
}
示例3: NullDot
public static LNode NullDot(LNode node, IMacroContext context)
{
if (!node.Calls(S.NullDot, 2))
return null;
var a = node.Args;
LNode leftSide = a[0], rightSide = a[1];
// So our input will be something like a.b?.c().d<x>, which is parsed
// (a.b) ?. (c().d<x>)
// in EC# we would transform this to
// a.b::tmp != null ? tmp.c().d<x> : null
// but there's no EC# compiler yet, so instead use code that plain C#
// can support:
// a.b != null ? (a.b).c().d<x> : null
LNode condition, thenExpr;
if (StandardMacros.LooksLikeSimpleValue(leftSide))
{
condition = F.Call(S.Neq, leftSide, [email protected]);
thenExpr = ConvertToNormalDot(leftSide, rightSide);
}
else
{
LNode tempVar = F.Id(StandardMacros.NextTempName(context, leftSide));
condition = F.Call(S.Neq, F.Var(F.Missing, tempVar, leftSide), [email protected]);
thenExpr = ConvertToNormalDot(tempVar, rightSide);
}
return F.InParens(F.Call(S.QuestionMark, condition, thenExpr, F.Null));
}
示例4: ForwardProperty
public static LNode ForwardProperty(LNode prop, IMacroContext context)
{
LNode name, fwd, body;
if (prop.ArgCount != 4)
return null;
LNode target = GetForwardingTarget(name = prop.Args[1], fwd = prop.Args[3]);
if (target != null)
{
body = F.Braces(F.Call(S.get, F.Braces(F.Call(S.Return, target))).SetBaseStyle(NodeStyle.Special));
return prop.WithArgChanged(3, body);
}
else if ((body = fwd).Calls(S.Braces))
{
var body2 = body.WithArgs(stmt => {
if (stmt.Calls(S.get, 1) && (target = GetForwardingTarget(name, stmt.Args[0])) != null)
return stmt.WithArgs(new VList<LNode>(F.Braces(F.Call(S.Return, target))));
if (stmt.Calls(S.set, 1) && (target = GetForwardingTarget(name, stmt.Args[0])) != null)
return stmt.WithArgs(new VList<LNode>(F.Braces(F.Call(S.Assign, target, F.Id(S.value)))));
return stmt;
});
if (body2 != body)
return prop.WithArgChanged(3, body2);
}
return null;
}
示例5: saveAndRestore
public static LNode saveAndRestore(LNode node, IMacroContext context)
{
var tmp_0 = context.GetArgsAndBody(true);
var args = tmp_0.Item1;
var body = tmp_0.Item2;
if (args.Count == 1) {
LNode newValue = null;
{
var tmp_1 = args[0];
LNode property;
if (tmp_1.Calls(CodeSymbols.Assign, 2) && (property = tmp_1.Args[0]) != null && (newValue = tmp_1.Args[1]) != null || (property = tmp_1) != null) {
string mainProp = KeyNameComponentOf(property).Name;
string varPrefix = "old" + mainProp + "_";
LNode varName, varDecl = TempVarDecl(property, out varName, varPrefix);
LNode tryFinally = LNode.Call(CodeSymbols.Try, LNode.List(LNode.Call(CodeSymbols.Braces, LNode.List(body)).SetStyle(NodeStyle.Statement), LNode.Call(CodeSymbols.Finally, LNode.List(LNode.Call(CodeSymbols.Braces, LNode.List(LNode.Call(CodeSymbols.Assign, LNode.List(property, varName)).SetStyle(NodeStyle.Operator))).SetStyle(NodeStyle.Statement)))));
if (newValue != null) {
return LNode.Call(CodeSymbols.Splice, LNode.List(varDecl, LNode.Call(CodeSymbols.Assign, LNode.List(property, newValue)).SetStyle(NodeStyle.Operator), tryFinally));
} else {
return LNode.Call(CodeSymbols.Splice, LNode.List(varDecl, tryFinally));
}
}
}
}
return null;
}
示例6: with
Mode = MacroMode.ProcessChildrenBefore)] // post-normal-macro-expansion
public static LNode with(LNode fn, IMacroContext context)
{
LNode braces;
if (fn.ArgCount != 2 || !(braces = fn.Args[1]).Calls(S.Braces))
return null;
LNode tmp = F.Id(NextTempName(context));
WList<LNode> stmts = braces.Args.ToWList();
stmts = stmts.SmartSelect(stmt =>
stmt.ReplaceRecursive(expr => {
if (expr.Calls(S.Dot, 1))
return expr.WithArgs(new VList<LNode>(tmp, expr.Args.Last));
else if (expr.IsIdNamed("#"))
return tmp;
return null;
}));
stmts.Insert(0, F.Var(null, tmp.Name, fn.Args[0]));
if (IsExpressionContext(context)) {
stmts.Add(tmp);
return F.Call("#runSequence", stmts.ToVList());
} else {
return F.Braces(stmts.ToVList());
}
}
示例7: In
[LexicalMacro("x in lo..hi; x in lo...hi; x in ..hi; x in lo..._; x in range", "Converts an 'in' expression to a normal C# expression using the following rules " + "(keeping in mind that the EC# parser treats `..<` as an alias for `..`):\n" + "1. `x in _..hi` and `x in ..hi` become `x.IsInRangeExcl(hi)`\n" + "2. `x in _...hi` and `x in ...hi` become `x.IsInRangeIncl(hi)`\n" + "3. `x in lo.._` and `x in lo..._` become simply `x >= lo`\n" + "4. `x in lo..hi` becomes `x.IsInRangeExcludeHi(lo, hi)`\n" + "5. `x in lo...hi` becomes `x.IsInRange(lo, hi)`\n" + "6. `x in range` becomes `range.Contains(x)`\n" + "The first applicable rule is used.", "#in")] public static LNode In(LNode node, IMacroContext context)
{
{
LNode range, x;
if (node.Calls(CodeSymbols.In, 2) && (x = node.Args[0]) != null && (range = node.Args[1]) != null) {
LNode parens;
range = range.WithoutAttrNamed(S.TriviaInParens, out parens);
if (parens == null) {
{
LNode hi, lo;
if (range.Calls(CodeSymbols.DotDot, 2) && (lo = range.Args[0]) != null && (hi = range.Args[1]) != null)
if (lo.IsIdNamed(__))
return LNode.Call(CodeSymbols.LT, LNode.List(x, hi)).SetStyle(NodeStyle.Operator);
else if (hi.IsIdNamed(__))
return LNode.Call(CodeSymbols.GE, LNode.List(x, lo)).SetStyle(NodeStyle.Operator);
else
return LNode.Call(LNode.Call(CodeSymbols.Dot, LNode.List(x, LNode.Id((Symbol) "IsInRangeExcludeHi"))), LNode.List(lo, hi));
else if (range.Calls(CodeSymbols.DotDot, 1) && (hi = range.Args[0]) != null)
return LNode.Call(CodeSymbols.LT, LNode.List(x, hi)).SetStyle(NodeStyle.Operator);
else if (range.Calls(CodeSymbols.DotDotDot, 2) && (lo = range.Args[0]) != null && (hi = range.Args[1]) != null)
if (lo.IsIdNamed(__))
return LNode.Call(CodeSymbols.LE, LNode.List(x, hi)).SetStyle(NodeStyle.Operator);
else if (hi.IsIdNamed(__))
return LNode.Call(CodeSymbols.GE, LNode.List(x, lo)).SetStyle(NodeStyle.Operator);
else
return LNode.Call(LNode.Call(CodeSymbols.Dot, LNode.List(x, LNode.Id((Symbol) "IsInRange"))), LNode.List(lo, hi));
else if (range.Calls(CodeSymbols.DotDotDot, 1) && (hi = range.Args[0]) != null)
return LNode.Call(CodeSymbols.LE, LNode.List(x, hi)).SetStyle(NodeStyle.Operator);
}
}
return LNode.Call(LNode.Call(CodeSymbols.Dot, LNode.List(range, LNode.Id((Symbol) "Contains"))), LNode.List(x));
}
}
return null;
}
示例8: In
public static LNode In(LNode node, IMacroContext context)
{
{
LNode range, x;
if (node.Calls(CodeSymbols.In, 2) && (x = node.Args[0]) != null && (range = node.Args[1]) != null) {
LNode parens;
range = range.WithoutAttrNamed(S.TriviaInParens, out parens);
if (parens == null) {
{
LNode hi, lo;
if (range.Calls(CodeSymbols.DotDot, 2) && (lo = range.Args[0]) != null && (hi = range.Args[1]) != null)
if (lo.IsIdNamed(__))
return LNode.Call(CodeSymbols.LT, LNode.List(x, hi)).SetStyle(NodeStyle.Operator);
else if (hi.IsIdNamed(__))
return LNode.Call(CodeSymbols.GE, LNode.List(x, lo)).SetStyle(NodeStyle.Operator);
else
return LNode.Call(LNode.Call(CodeSymbols.Dot, LNode.List(x, LNode.Id((Symbol) "IsInRangeExcludeHi"))).SetStyle(NodeStyle.Operator), LNode.List(lo, hi));
else if (range.Calls(CodeSymbols.DotDot, 1) && (hi = range.Args[0]) != null)
return LNode.Call(CodeSymbols.LT, LNode.List(x, hi)).SetStyle(NodeStyle.Operator);
else if (range.Calls(CodeSymbols.DotDotDot, 2) && (lo = range.Args[0]) != null && (hi = range.Args[1]) != null)
if (lo.IsIdNamed(__))
return LNode.Call(CodeSymbols.LE, LNode.List(x, hi)).SetStyle(NodeStyle.Operator);
else if (hi.IsIdNamed(__))
return LNode.Call(CodeSymbols.GE, LNode.List(x, lo)).SetStyle(NodeStyle.Operator);
else
return LNode.Call(LNode.Call(CodeSymbols.Dot, LNode.List(x, LNode.Id((Symbol) "IsInRange"))).SetStyle(NodeStyle.Operator), LNode.List(lo, hi));
else if (range.Calls(CodeSymbols.DotDotDot, 1) && (hi = range.Args[0]) != null)
return LNode.Call(CodeSymbols.LE, LNode.List(x, hi)).SetStyle(NodeStyle.Operator);
}
}
return LNode.Call(LNode.Call(CodeSymbols.Dot, LNode.List(range, LNode.Id((Symbol) "Contains"))).SetStyle(NodeStyle.Operator), LNode.List(x));
}
}
return null;
}
示例9: 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;
}
示例10:
public static LNode @nameof(LNode nameof, IMacroContext context)
{
if (nameof.ArgCount != 1)
return null;
Symbol expr = EcsValidators.KeyNameComponentOf(nameof.Args[0]);
return F.Literal(expr.Name);
}
示例11: on_finally
public static LNode on_finally(LNode node, IMacroContext context)
{
LNode firstArg, rest, on_handler = ValidateOnStmt(node, context, out rest, out firstArg);
if (on_handler == null || firstArg != null)
return null;
return node.With(S.Try, rest, node.With(S.Finally, on_handler));
}
示例12: runSequence
public static LNode runSequence(LNode node, IMacroContext context)
{
if (context.Parent.Calls(S.Braces))
return node.With(S.Splice, MaybeRemoveNoOpFromRunSeq(node.Args));
if (!context.ScopedProperties.ContainsKey(_useSequenceExpressionsIsRunning))
Reject(context, node, "#useSequenceExpressions is required to make #runSequence work");
return null;
}
示例13: useDefaultTupleTypes
public static LNode useDefaultTupleTypes(LNode node, IMacroContext context)
{
if (node.ArgCount != 0)
return null;
context.ScopedProperties.Remove(TupleMakers);
context.ScopedProperties.Remove(DefaultTupleMaker);
return F.Call(S.Splice);
}
示例14: on_throw
public static LNode on_throw(LNode node, IMacroContext context)
{
LNode firstArg, rest, on_handler = ValidateOnStmt(node, context, out rest, out firstArg);
if (on_handler == null)
return null;
on_handler = on_handler.PlusArg(F.Call(S.Throw));
return TransformOnCatch(node, firstArg, rest, on_handler);
}
示例15: static_tryDeconstruct
public static LNode static_tryDeconstruct(LNode node, IMacroContext context)
{
if (node.AttrNamed(S.Static) == null && !node.HasSpecialName)
return Reject(context, node, "Expected 'static' attribute");
foreach (var arg in node.Args)
DoDeconstruct(arg, context, printErrorOnFailure: false);
return F.Call(S.Splice);
}