本文整理汇总了C#中LNode.AttrNamed方法的典型用法代码示例。如果您正苦于以下问题:C# LNode.AttrNamed方法的具体用法?C# LNode.AttrNamed怎么用?C# LNode.AttrNamed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LNode
的用法示例。
在下文中一共展示了LNode.AttrNamed方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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);
}
示例3: VisitInput
public virtual LNode VisitInput(LNode stmt, IMessageSink sink)
{
LNode aliasSet;
if ((stmt.Calls(_alias, 1) || stmt.CallsMin(S.Alias, 1)) &&
(aliasSet = stmt.Args[0]).Calls(S.Assign, 2))
{
IEnumerable<KeyValuePair<LNode, LNode>> q;
LNode alias = aliasSet.Args[0], replacement = aliasSet.Args[1], old;
if (_definedAliases.TryGetValue(alias, out old)) {
if (stmt.AttrNamed(S.Partial) == null || !old.Equals(replacement))
sink.Write(Severity.Warning, alias, "Redefinition of alias '{0}'", alias);
} else if ((q = _definedAliases.Where(pair => replacement.Equals(pair.Value))).Any())
sink.Write(Severity.Warning, replacement, "Aliases '{0}' and '{1}' have the same replacement value", q.First().Key, alias);
_definedAliases[alias] = replacement;
return LNode.Call(S.Splice, RVList<LNode>.Empty); // erase alias from output
}
return null;
}
示例4: StaticIf
public static LNode StaticIf(LNode @if, IMessageSink sink)
{
LNode @static;
if ((@static = @if.AttrNamed(S.Static)) == null || [email protected])
return null;
return static_if(@if, sink);
}
示例5: BranchToPred
Pred BranchToPred(LNode expr, out BranchMode mode, Context ctx)
{
if (expr.Calls(_Default, 1) || expr.Calls(_Default2, 1)) {
expr = expr.Args[0];
mode = BranchMode.Default;
} else if (expr.Calls(_Error, 1) || expr.IsIdNamed(_DefaultError)) {
mode = (expr.AttrNamed(S.Continue) != null || expr.AttrNamed(GSymbol.Get("continue")) != null)
? BranchMode.ErrorContinue : BranchMode.ErrorExit;
if (expr.Calls(_Error, 1))
expr = expr.Args[0];
else
return DefaultErrorBranch.Value;
} else
mode = BranchMode.None;
return NodeToPred(expr, ctx);
}
示例6: IsDefaultNewlineSuppressed
private bool IsDefaultNewlineSuppressed(LNode node)
{
return node.AttrNamed(S.TriviaAppendStatement) != null || (_flags & Ambiguity.OneLiner) != 0;
}
示例7: GetPatternComponents
void GetPatternComponents(LNode pattern, out LNode varBinding, out bool refExistingVar, out LNode cmpExpr, out LNode isType, out LNode inRange, out VList<LNode> subPatterns, out VList<LNode> conditions)
{
bool haveSubPatterns = false;
subPatterns = VList<LNode>.Empty;
refExistingVar = pattern.AttrNamed(S.Ref) != null;
conditions = VList<LNode>.Empty;
while (pattern.Calls(S.And, 2)) {
conditions.Add(pattern.Args.Last);
pattern = pattern.Args[0];
}
LNode cmpExprOrBinding = null;
varBinding = cmpExpr = isType = inRange = null;
for (int pass = 1; pass <= 3; pass++) {
LNode inRange2 = inRange, isType2 = isType;
{
LNode patternL;
if (pattern.Calls(CodeSymbols.In, 2) && (patternL = pattern.Args[0]) != null && (inRange = pattern.Args[1]) != null || pattern.Calls((Symbol) "in", 2) && (patternL = pattern.Args[0]) != null && (inRange = pattern.Args[1]) != null) {
pattern = patternL;
if (inRange2 != null)
_context.Write(Severity.Error, inRange2, "match-case does not support multiple 'in' operators");
} else if (pattern.Calls(CodeSymbols.Is, 2) && (cmpExprOrBinding = pattern.Args[0]) != null && (isType = pattern.Args[1]) != null || pattern.Calls((Symbol) "is", 2) && (cmpExprOrBinding = pattern.Args[0]) != null && (isType = pattern.Args[1]) != null) {
pattern = cmpExprOrBinding;
if (isType2 != null)
_context.Write(Severity.Error, isType2, "match-case does not support multiple 'is' operators");
} else if (pattern.Calls(CodeSymbols.Is, 1) && (isType = pattern.Args[0]) != null || pattern.Calls((Symbol) "is", 1) && (isType = pattern.Args[0]) != null) {
if (isType2 != null)
_context.Write(Severity.Error, isType2, "match-case does not support multiple 'is' operators");
goto doneAnalysis;
} else if (pattern.Calls(CodeSymbols.DotDotDot, 2) || pattern.Calls(CodeSymbols.DotDot, 2) || pattern.Calls(CodeSymbols.DotDotDot, 1) || pattern.Calls(CodeSymbols.DotDot, 1)) {
inRange = pattern;
goto doneAnalysis;
} else if (pattern.Calls(CodeSymbols.Tuple)) {
subPatterns = pattern.Args;
cmpExprOrBinding = null;
} else {
LNode target = pattern.Target;
if (!haveSubPatterns && pattern.IsCall && (!target.IsId || target.AttrNamed(S.TriviaInParens) != null || (!target.HasSpecialName && LesNodePrinter.IsNormalIdentifier(target.Name)))) {
haveSubPatterns = true;
subPatterns = pattern.Args;
pattern = pattern.Target;
} else
cmpExprOrBinding = pattern;
}
}
}
doneAnalysis:
if (cmpExprOrBinding != null) {
if (cmpExprOrBinding.Calls(S.Substitute, 1))
varBinding = cmpExprOrBinding[0];
else if (refExistingVar)
varBinding = cmpExprOrBinding;
else if ((varBinding ?? cmpExprOrBinding).IsIdNamed(__))
cmpExprOrBinding = varBinding = null;
if (varBinding != null) {
if (varBinding.AttrNamed(S.Ref) != null) {
refExistingVar = true;
varBinding = varBinding.WithoutAttrs();
}
if (!varBinding.IsId) {
_context.Write(Severity.Error, varBinding, "Invalid variable name in match-case: {0}", varBinding);
varBinding = null;
}
}
if (varBinding == null)
cmpExpr = cmpExprOrBinding;
}
if (refExistingVar && varBinding == null) {
refExistingVar = false;
var got = cmpExprOrBinding ?? pattern;
_context.Write(Severity.Warning, got, "'ref' expected a variable name (got `{0}`)", got);
}
}
示例8: StaticIf
public static LNode StaticIf(LNode @if, IMacroContext context)
{
LNode @static;
if ((@static = @if.AttrNamed(S.Static)) == null || [email protected])
return null;
return static_if(@if, context);
}
示例9: matchCode
public static LNode matchCode(LNode node, IMacroContext context)
{
if (node.AttrNamed(S.Static) != null)
return null; // this case is handled by static_matchCode macro
var args_body = context.GetArgsAndBody(false);
VList<LNode> args = args_body.Item1, body = args_body.Item2;
if (args.Count != 1 || body.Count < 1)
return null;
var cases = GetCases(body, context.Sink);
if (cases.IsEmpty)
return null;
var output = new WList<LNode>();
var @var = MaybeAddTempVarDecl(context, args[0], output);
var ifClauses = new List<Pair<LNode, LNode>>();
var cmc = new CodeMatchContext {
Context = context
};
foreach (var @case in cases)
{
cmc.ThenClause.Clear();
// e.g. case [$(..._)] Foo($x + 1, $y) =>
// LNode x, y, tmp9;
// if (var.Calls((Symbol) "Foo", 2) && (tmp9 = var.Args[0]).Calls(CodeSymbols.Plus, 2)
// && (x = tmp9.Args[0]) != null // this will never be null, but we want to put it the assignment in the 'if' statement
// && 1.Equals(tmp9.Args[1].Value) && (y = var.Args[1]) != null) { ... }
LNode testExpr = null;
if (@case.Key.Count > 0) {
if (cmc.IsMultiCase = @case.Key.Count > 1) {
cmc.UsageCounters.Clear();
testExpr = @case.Key.Aggregate((LNode) null, (test, pattern) => {
test = LNode.MergeBinary(test, cmc.MakeTopTestExpr(pattern, @var), S.Or);
return test;
});
foreach (var pair in cmc.UsageCounters.Where(p => p.Value < @case.Key.Count)) {
if (cmc.NodeVars.ContainsKey(pair.Key))
cmc.NodeVars[pair.Key] = true;
if (cmc.ListVars.ContainsKey(pair.Key))
cmc.ListVars[pair.Key] = true;
}
} else
testExpr = cmc.MakeTopTestExpr(@case.Key[0], @var);
}
var handler = @case.Value.AsLNode(S.Braces);
if (cmc.ThenClause.Count > 0)
handler = LNode.MergeLists(F.Braces(cmc.ThenClause), handler, S.Braces);
ifClauses.Add(Pair.Create(testExpr, handler));
}
LNode ifStmt = null;
for (int i = ifClauses.Count - 1; i >= 0; i--)
{
if (ifClauses[i].Item1 == null) {
if (ifStmt == null)
ifStmt = ifClauses[i].Item2;
else
context.Sink.Error(node, "The default case must appear last, and there can be only one.");
} else {
if (ifStmt == null)
ifStmt = F.Call(S.If, ifClauses[i].Item1, ifClauses[i].Item2);
else
ifStmt = F.Call(S.If, ifClauses[i].Item1, ifClauses[i].Item2, ifStmt);
}
}
if (cmc.NodeVars.Count > 0)
output.Add(F.Call(S.Var, ListExt.Single(F.Id("LNode")).Concat(
cmc.NodeVars.OrderBy(v => v.Key.Name).Select(kvp => kvp.Value ? F.Call(S.Assign, F.Id(kvp.Key), F.Null) : F.Id(kvp.Key)))));
if (cmc.ListVars.Count > 0) {
LNode type = LNode.Call(CodeSymbols.Of, LNode.List(LNode.Id((Symbol) "VList"), LNode.Id((Symbol) "LNode"))).SetStyle(NodeStyle.Operator);
output.Add(F.Call(S.Var, ListExt.Single(type).Concat(
cmc.ListVars.OrderBy(v => v.Key.Name).Select(kvp => kvp.Value ? LNode.Call(CodeSymbols.Assign, LNode.List(F.Id(kvp.Key), LNode.Call(CodeSymbols.Default, LNode.List(type)))).SetStyle(NodeStyle.Operator) : F.Id(kvp.Key)))));
}
if (output.Count == 0)
return ifStmt;
else {
output.Add(ifStmt);
return F.Braces(output.ToVList());
}
}
示例10: GetPatternComponents
void GetPatternComponents(LNode pattern, out LNode varBinding, out bool refExistingVar, out LNode cmpExpr, out LNode isType, out LNode inRange, out VList<LNode> subPatterns, out VList<LNode> conditions)
{
// Here's a typical pattern (case expr):
// is Shape(ShapeType.Circle, ref size, Location: p is Point<int>(x, y)):
// When there is an arg list, we decode its Target and return the args.
//
// The caller is in charge of stripping out "Property:" prefix, if any,
// so the most complex pattern that this method considers is something
// like `(expr is Type in Range)(subPatterns) && conds` where `expr` is
// a varName or $varName to deconstruct, or some expression to test for
// equality. Assuming it's an equality test, the output will be
//
// varBinding = null
// refExistingVar = false
// cmpExpr = quote(expr);
// isType = quote(Type);
// inRange = quote(Range);
// conds will have "conds" pushed to the front.
//
bool haveSubPatterns = false;
subPatterns = VList<LNode>.Empty;
refExistingVar = pattern.AttrNamed(S.Ref) != null;
// First, look for "pattern && condition"
conditions = VList<LNode>.Empty;
while (pattern.Calls(S.And, 2)) {
conditions.Add(pattern.Args.Last);
pattern = pattern.Args[0];
}
LNode cmpExprOrBinding = null;
varBinding = cmpExpr = isType = inRange = null;
// Now decode the expression. Use three passes, each of which decodes
// an "outer" layer such as A is B, A in B, or expr(args). Since you
// can combine these operators, we may need multiple passes (e.g.
// "X is T in R" and "X in R is T" are equivalent), and keep in mind
// that operator trees like "A in B" are nearly identical to prefix-
// calls like "foo(A, B)" except for the call target and the `BaseStyle`.
for (int pass = 1; pass <= 3; pass++) {
LNode inRange2 = inRange, isType2 = isType;
{
LNode patternL;
if (pattern.Calls(CodeSymbols.In, 2) && (patternL = pattern.Args[0]) != null && (inRange = pattern.Args[1]) != null || pattern.Calls((Symbol) "in", 2) && (patternL = pattern.Args[0]) != null && (inRange = pattern.Args[1]) != null) {
pattern = patternL;
if (inRange2 != null)
_context.Sink.Error(inRange2, "match-case does not support multiple 'in' operators");
} else if (pattern.Calls(CodeSymbols.Is, 2) && (cmpExprOrBinding = pattern.Args[0]) != null && (isType = pattern.Args[1]) != null || pattern.Calls((Symbol) "is", 2) && (cmpExprOrBinding = pattern.Args[0]) != null && (isType = pattern.Args[1]) != null) {
pattern = cmpExprOrBinding;
if (isType2 != null)
_context.Sink.Error(isType2, "match-case does not support multiple 'is' operators");
} else if (pattern.Calls(CodeSymbols.Is, 1) && (isType = pattern.Args[0]) != null || pattern.Calls((Symbol) "is", 1) && (isType = pattern.Args[0]) != null) {
if (isType2 != null)
_context.Sink.Error(isType2, "match-case does not support multiple 'is' operators");
goto doneAnalysis;
} else if (pattern.Calls(CodeSymbols.DotDotDot, 2) || pattern.Calls(CodeSymbols.DotDot, 2) || pattern.Calls(CodeSymbols.DotDotDot, 1) || pattern.Calls(CodeSymbols.DotDot, 1)) {
inRange = pattern;
goto doneAnalysis;
} else if (pattern.Calls(CodeSymbols.Tuple)) {
subPatterns = pattern.Args;
cmpExprOrBinding = null;
} else {
// It's very tempting to detect NodeStyle.PrefixNotation to distinguish,
// say, A.B<C> from id(A, B, C), but I'm reluctant to do so. BaseStyle
// is by convention "unsemantic" and not guaranteed to be preserved
// across serializations or supported the same way by different parsers.
// So instead of asking "is this in PrefixNotation?" I ask "does the
// target appear to be a normal identifier?"
LNode target = pattern.Target;
if (!haveSubPatterns && pattern.IsCall && (!target.IsId || target.AttrNamed(S.TriviaInParens) != null || (!target.HasSpecialName && Les2Printer.IsNormalIdentifier(target.Name)))
)
{
haveSubPatterns = true;
subPatterns = pattern.Args;
pattern = pattern.Target;
} else
cmpExprOrBinding = pattern;
}
}
}
doneAnalysis:
if (cmpExprOrBinding != null) {
if (cmpExprOrBinding.Calls(S.Substitute, 1))
varBinding = cmpExprOrBinding[0];
else if (refExistingVar)
varBinding = cmpExprOrBinding;
else if ((varBinding ?? cmpExprOrBinding).IsIdNamed(__))
cmpExprOrBinding = varBinding = null;
// Originally a plain identifier would be a binding, like $identifier
//if (cmpExprOrBinding.IsId && cmpExprOrBinding.AttrNamed(S.TriviaInParens) == null)
// varBinding = cmpExprOrBinding;
if (varBinding != null) {
if (varBinding.AttrNamed(S.Ref) != null) {
refExistingVar = true;
varBinding = varBinding.WithoutAttrs();
}
if (!varBinding.IsId) {
_context.Sink.Error(varBinding, "Invalid variable name in match-case: {0}", varBinding);
//.........这里部分代码省略.........