本文整理汇总了C#中LNode类的典型用法代码示例。如果您正苦于以下问题:C# LNode类的具体用法?C# LNode怎么用?C# LNode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
LNode类属于命名空间,在下文中一共展示了LNode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Print
public void Print(LNode node, StringBuilder target, IMessageSink sink = null, ParsingMode mode = null, ILNodePrinterOptions options = null)
{
if (_usePlainCsPrinter)
EcsNodePrinter.PrintPlainCSharp(node, target, sink, mode, options);
else
EcsNodePrinter.PrintECSharp(node, target, sink, mode, options);
}
示例2: 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;
}
示例3: WithSpliced
public static RVList<LNode> WithSpliced(this RVList<LNode> list, LNode node, Symbol listName)
{
if (node.Calls(listName))
return list.AddRange(node.Args);
else
return list.Add(node);
}
示例4: 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;
}
示例5: SpliceAdd
public static void SpliceAdd(this RWList<LNode> list, LNode node, Symbol listName)
{
if (node.Calls(listName))
list.AddRange(node.Args);
else
list.Add(node);
}
示例6: 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));
}
示例7: AutoRemoveParens
LNode AutoRemoveParens(LNode node)
{
int i = node.Attrs.IndexWithName(S.TriviaInParens);
if ((i > -1))
return node.WithAttrs(node.Attrs.RemoveAt(i));
return node;
}
示例8:
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);
}
示例9: 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));
}
示例10: Stmt
protected override void Stmt(string text, LNode expected, Action<EcsPrinterOptions> configure = null, Mode mode = Mode.Both)
{
bool exprMode = (mode & Mode.Expression) != 0;
if ((mode & Mode.ParserTest) == 0)
return;
var sink = (mode & Mode.ExpectAndDropParserError) != 0 ? new MessageHolder() : (IMessageSink)ConsoleMessageSink.Value;
// This is the easy way:
//LNode result = EcsLanguageService.Value.ParseSingle(text, sink, exprMode ? ParsingMode.Expressions : ParsingMode.Statements, preserveComments: true);
// But to make debugging easier, I'll do it the long way:
ILexer<Token> lexer = EcsLanguageService.Value.Tokenize(new UString(text), "", ConsoleMessageSink.Value);
var preprocessed = new EcsPreprocessor(lexer, true);
var treeified = new TokensToTree(preprocessed, false);
var parser = new EcsParser(treeified.Buffered(), lexer.SourceFile, sink);
VList<LNode> results = exprMode ? LNode.List(parser.ExprStart(false)) : LNode.List(parser.ParseStmtsGreedy());
//if (!preprocessed.TriviaList.IsEmpty)
{
// Inject comments
var injector = new EcsTriviaInjector(preprocessed.TriviaList, preprocessed.SourceFile, (int)TokenType.Newline, "/*", "*"+"/", "//");
results = LNode.List(injector.Run(results.GetEnumerator()).ToList());
}
LNode result = results.AsLNode(S.Splice);
AreEqual(TokenType.EOF, parser.LT0.Type(), string.Format("Parser stopped before EOF at [{0}] in {1}", parser.LT0.StartIndex, text));
if ((mode & Mode.IgnoreTrivia) != 0)
result = result.ReplaceRecursive(n => n.IsTrivia ? Maybe<LNode>.NoValue : n, LNode.ReplaceOpt.ProcessAttrs).Value;
AreEqual(expected, result);
if (sink is MessageHolder)
GreaterOrEqual(((MessageHolder)sink).List.Count, 1, "Expected an error but got none for "+text);
}
示例11: MayBeImplicitChildStatement
/// <summary>Returns true if the specified child of the specified node
/// can be an implicit child statement, i.e. a child statement that is
/// not necessarily a braced block, e.g. the second child of a while
/// loop.</summary>
/// <remarks>
/// This method helps the printer decide when a newline should be added
/// before an unbraced child statement when there are no attributes
/// dictating whether to add a newline or not.
/// <para/>
/// This method only cares about executable parent nodes. It returns
/// false for class/space and function/property bodies, which are always
/// braced blocks and therefore get a newline before every child statement
/// automatically.
/// </remarks>
public static bool MayBeImplicitChildStatement(LNode node, int childIndex)
{
CheckParam.IsNotNull("node", node);
if (childIndex < 0) // target or attributes
return false;
var n = node.Name;
if (!LNode.IsSpecialName(n.Name))
return false;
if (n == S.Braces)
return true;
if (n == S.Try)
return childIndex == 0;
switch (node.ArgCount) {
case 1:
if (n == S.Finally)
return true;
break;
case 2:
if (childIndex == 0 ? n == S.DoWhile :
n == S.If || n == S.While || n == S.UsingStmt || n == S.Lock || n == S.Switch || n == S.Fixed)
return true;
break;
case 3:
if (childIndex != 0 && n == S.If)
return true;
if (childIndex == 2 && n == S.ForEach)
return true;
break;
case 4:
if (childIndex == 3 && (n == S.For || n == S.Catch))
return true;
break;
}
return false;
}
示例12: 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
}
示例13: 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;
}
示例14: 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());
}
}
示例15: CodeToTerminalPred
public override Pred CodeToTerminalPred(LNode expr, ref string errorMsg)
{
bool isInt = false;
PGIntSet set;
if (expr.IsIdNamed(_underscore)) {
set = PGIntSet.AllExceptEOF;
} else if (expr.IsIdNamed(_EOF)) {
set = PGIntSet.EOF;
} else if (expr.Calls(S.DotDot, 2)) {
int? from = ConstValue(expr.Args[0], ref isInt);
int? to = ConstValue(expr.Args[1], ref isInt);
if (from == null || to == null) {
errorMsg = "Expected int32 or character literal on each side of «..»";
return null;
}
set = PGIntSet.WithRanges(from.Value, to.Value);
} else if (expr.Value is string) {
return Pred.Seq((string)expr.Value);
} else {
int? num = ConstValue(expr, ref isInt);
if (num == null) {
errorMsg = "Unrecognized expression. Expected int32 or character literal instead of: " + expr.ToString(); // warning
return null;
}
set = PGIntSet.With(num.Value);
}
set.IsCharSet = !isInt;
return new TerminalPred(expr, set, true);
}