Exemplo n.º 1
0
        // contextオブジェクトの直下の全ての子ノードからコメントを取得する
        private Comments GetComments(ParserRuleContext context)
        {
            var ret = new Comments();

            if (context == null)
            {
                return(ret);
            }

            var start = context.Start.TokenIndex;

            for (var i = 0; i < context.ChildCount; ++i)
            {
                IParseTree child = context.GetChild(i);

                // 終端ノードの直後のコメントだけを取得する
                if (child.ChildCount > 1)
                {
                    continue;
                }
                else if (child.ChildCount == 1)
                {
                    if (child.GetType() == typeof(MiniSqlParserParser.IdentifierContext) ||
                        child.GetType() == typeof(MiniSqlParserParser.Column_aliasContext) ||
                        child.GetType() == typeof(MiniSqlParserParser.Table_aliasContext) ||
                        child.GetType() == typeof(MiniSqlParserParser.Collation_nameContext) ||
                        child.GetType() == typeof(MiniSqlParserParser.Constraint_nameContext))
                    {
                        child = child.GetChild(0);
                    }
                    else
                    {
                        continue;
                    }
                }

                var childTokenIndex = child.SourceInterval.b;
                //IList<IToken> commentTokens = _tokens.GetHiddenTokensToRight(childTokenIndex, 1);
                IList <IToken> commentTokens = this.GetHiddenTokensToRight(childTokenIndex);
                if (commentTokens == null || commentTokens.Count == 0)
                {
                    ret.Add(null);
                }
                else
                {
                    string comment = null;
                    foreach (var commentToken in commentTokens)
                    {
                        comment += commentToken.Text;
                    }
                    ret.Add(comment);
                }
            }

            return(ret);
        }
Exemplo n.º 2
0
        private Object Interpret(IParseTree tree)
        {
            Func <IParseTree, object> func;

            if (interpreters.TryGetValue(tree.GetType(), out func))
            {
                return(func(tree));
            }
            throw new InterpreterException(tree, "Unknown node type: " + tree.GetType().Name);
        }
Exemplo n.º 3
0
        public OperatorExpression(IParseTree tree, ScriptLoadingContext lcontext)
            : base(tree, lcontext)
        {
            var child0 = tree.GetChild(0);

            if (s_OperatorTypes.Contains(child0.GetType()))
            {
                // unary op
                SyntaxAssert(tree.ChildCount == 2, "Unexpected node found");
                m_Operator = ParseUnaryOperator(child0);
                m_Exp1     = NodeFactory.CreateExpression(tree.GetChild(1), lcontext);
                m_IsUnary  = true;
            }
            else if (s_LeftAssocOperatorTypes.Contains(tree.GetType()))
            {
                // binary right associative op or simple left associative
                IParseTree child2 = tree.GetChild(2);

                if (child2.GetType() == tree.GetType())
                {
                    m_Exps = new List <Expression>();
                    m_Ops  = new List <Operator>();

                    m_Operator = ParseBinaryOperator(tree.GetChild(1));

                    while (child2.GetType() == tree.GetType())
                    {
                        m_Exps.Add(NodeFactory.CreateExpression(child2.GetChild(0), lcontext));
                        m_Ops.Add(m_Operator);
                        m_Operator = ParseBinaryOperator(child2.GetChild(1));
                        child2     = child2.GetChild(2);
                    }

                    m_Exp1 = NodeFactory.CreateExpression(child0, lcontext);
                    m_Exp2 = NodeFactory.CreateExpression(child2, lcontext);
                }
                else
                {
                    SyntaxAssert(tree.ChildCount == 3, "Unexpected node found");
                    m_Operator = ParseBinaryOperator(tree.GetChild(1));
                    m_Exp1     = NodeFactory.CreateExpression(child0, lcontext);
                    m_Exp2     = NodeFactory.CreateExpression(child2, lcontext);
                }
            }
            else
            {
                // binary right associative op or simple left associative
                SyntaxAssert(tree.ChildCount == 3, "Unexpected node found");
                m_Operator = ParseBinaryOperator(tree.GetChild(1));
                m_Exp1     = NodeFactory.CreateExpression(child0, lcontext);
                m_Exp2     = NodeFactory.CreateExpression(tree.GetChild(2), lcontext);
            }
        }
Exemplo n.º 4
0
        public static IEnumerable <IParseTree> DFS(ParserRuleContext root)
        {
            var toVisit          = new Stack <IParseTree>();
            var visitedAncestors = new Stack <IParseTree>();

            toVisit.Push(root);
            while (toVisit.Count > 0)
            {
                IParseTree node = toVisit.Peek();
                if (node.ChildCount > 0)
                {
                    if (visitedAncestors.PeekOrDefault() != node)
                    {
                        visitedAncestors.Push(node);
                        for (int i = node.ChildCount - 1; i >= 0; --i)
                        {
                            IParseTree o = node.GetChild(i);
                            Type       t = o.GetType();
                            toVisit.Push(o);
                        }
                        continue;
                    }
                    visitedAncestors.Pop();
                }
                yield return(node);

                //Console.Write(node.Id);
                toVisit.Pop();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the binder for the specified node.
        /// </summary>
        /// <param name="node">The node to get the binder for.</param>
        /// <returns>
        /// The binder, or null if no binder exists for the specified node.
        /// </returns>
        public IBinder GetBinder(IParseTree node)
        {
            if (node is null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            if (node is DocumentContext)
            {
                return(DocumentBinder);
            }
            else if (node is NamespaceStatementContext)
            {
                return(NamespaceBinder);
            }
            else if (node is StructDefinitionContext)
            {
                return(StructBinder);
            }
            else if (node is EnumDefinitionContext)
            {
                return(EnumBinder);
            }
            else if (node is EnumMemberContext)
            {
                return(EnumMemberBinder);
            }
            else if (node is FieldContext)
            {
                return(FieldBinder);
            }
            else if (node is FieldTypeContext)
            {
                return(FieldTypeBinder);
            }
            else if (node is BaseTypeContext)
            {
                return(BaseTypeBinder);
            }
            else if (node is UserTypeContext)
            {
                return(UserTypeBinder);
            }
            else if (node is ListTypeContext)
            {
                return(ListTypeBinder);
            }
            else if (node is SetTypeContext)
            {
                return(SetTypeBinder);
            }
            else if (node is MapTypeContext)
            {
                return(MapTypeBinder);
            }

            throw new ArgumentException(
                      $"No binder could be found to bind the {node.GetType().Name} node type",
                      nameof(node));
        }
Exemplo n.º 6
0
 public override Expression Visit(IParseTree node)
 {
     // Hack. There is no language construct that says "all Visit*()"
     // methods must be implemented all tree node types used.
     // So, get the node type, and use reflection on this class to find
     // a visit method overriden for the node type. If not, throw
     // "not implemented".
     if (node as TerminalNodeImpl == null)
     {
         Type   type      = node.GetType();
         Type   this_type = this.GetType();
         string type_name = type.Name;
         // node type:   Parenthesized_expressionContext
         // method name: VisitParenthesized_expression
         Regex  regex               = new Regex("Context$");
         string name                = regex.Replace(type_name, "");
         string method_name         = "Visit" + name;
         IEnumerable <MethodInfo> m = this_type.GetTypeInfo().GetDeclaredMethods(method_name);
         if (!m.GetEnumerator().MoveNext())
         {
             ParserRuleContext where = node as ParserRuleContext;
             IToken token = where.Start;
             int    index = token.StartIndex;
             // No method by the given name exists, which means it's unimplemented.
             throw new Helpers.EvaluationException("unimplemented feature (" + name + ")", index);
         }
     }
     return(node.Accept(this));
 }
Exemplo n.º 7
0
        static private void PrintSyntaxTree(IParseTree p, int level)
        {
            string s = p.GetType().ToString();

            if (p is ErrorNodeImpl)
            {
                errCount++;
            }
            else if (p is TerminalNodeImpl)
            {
                Console.WriteLine("<Terminal type=\"" + SVLexer.DefaultVocabulary.GetSymbolicName(((TerminalNodeImpl)p).Payload.Type) + "\">" + SecurityElement.Escape(p.GetText()) + "</Terminal>");
            }
            else
            {
                if (p.ChildCount > 0)
                {
                    string tag = s.Substring(s.IndexOf('+') + 1).Replace("Context", "");
                    Console.Write("<" + tag + ">");
                    for (int i = 0; i < p.ChildCount; i++)
                    {
                        PrintSyntaxTree(p.GetChild(i), level + 1);
                    }
                    Console.Write("</" + tag + ">");
                }
            }
        }
Exemplo n.º 8
0
        private string getContextType(IParseTree context)
        {
            string switchString = context.GetType().ToString();

            switch (switchString)
            {
            case "GiraphParser+VariableContext":
                IParseTree childString = context.GetChild(0).GetChild(context.GetChild(0).ChildCount - 1);
                if (childString == null)
                {
                    return("void");
                }

                switch (childString.GetText())
                {
                case "Vertices":
                    return("vertex");

                case "Edges":
                    return("edge");

                default:
                    return("void");
                }
            }
            throw new WrongExpressionPartTypeFoundException("Spørg Mads");
        }
Exemplo n.º 9
0
        public static void ParenthesizedAST(this IParseTree tree, StringBuilder sb, string file_name, CommonTokenStream stream, int level = 0)
        {
            // Antlr always names a non-terminal with first letter lowercase,
            // but renames it when creating the type in C#. So, remove the prefix,
            // lowercase the first letter, and remove the trailing "Context" part of
            // the name. Saves big time on output!
            if (tree as TerminalNodeImpl != null)
            {
                TerminalNodeImpl tok      = tree as TerminalNodeImpl;
                Interval         interval = tok.SourceInterval;

                System.Collections.Generic.IList <IToken> inter = tok.Symbol.TokenIndex >= 0 ? stream.GetHiddenTokensToLeft(tok.Symbol.TokenIndex) : null;
                if (inter != null)
                {
                    foreach (IToken t in inter)
                    {
                        StartLine(sb, file_name, tree, stream, level);
                        sb.AppendLine("( HIDDEN text=" + t.Text.PerformEscapes());
                    }
                }

                StartLine(sb, file_name, tree, stream, level);
                sb.AppendLine("( TOKEN i=" + tree.SourceInterval.a
                              + " t=" + tree.GetText().PerformEscapes());
            }
            else
            {
                string fixed_name = tree.GetType().ToString()
                                    .Replace("Antlr4.Runtime.Tree.", "");
                fixed_name = Regex.Replace(fixed_name, "^.*[+]", "");
                fixed_name = fixed_name.Substring(0, fixed_name.Length - "Context".Length);
                fixed_name = fixed_name[0].ToString().ToLower()
                             + fixed_name.Substring(1);
                StartLine(sb, file_name, tree, stream, level);
                sb.Append("( " + fixed_name);
                if (level == 0)
                {
                    sb.Append(" File=\""
                              + file_name
                              + "\"");
                }

                sb.AppendLine();
            }
            for (int i = 0; i < tree.ChildCount; ++i)
            {
                IParseTree c = tree.GetChild(i);
                c.ParenthesizedAST(sb, file_name, stream, level + 1);
            }
            if (level == 0)
            {
                for (int k = 0; k < 1 + changed - level; ++k)
                {
                    sb.Append(") ");
                }

                sb.AppendLine();
                changed = 0;
            }
        }
Exemplo n.º 10
0
 private void ValidateType <T>(IParseTree node)
 {
     if (!node.GetType().Equals(typeof(T)))
     {
         throw new InterpreterException(node, string.Format("Wrong node type, expected: {0} actual: {1}", typeof(T).Name, node.GetType().Name));
     }
 }
Exemplo n.º 11
0
        internal ParseTreeNodeViewModel(IParseTree node)
        {
            this.node = node;
            Name      = "not set";
            TextColor = Brushes.Black;

            if (node is IErrorNode)
            {
                TextColor = Brushes.Red;
            }

            var terminalNode = node as ITerminalNode;

            if (terminalNode != null)
            {
                Name = terminalNode.Symbol.Text;
                return;
            }

            Name = node.GetType().Name;
            IRuleNode ruleNode = (IRuleNode)node;

            for (int i = 0; i < ruleNode.ChildCount; i++)
            {
                Children.Add(new ParseTreeNodeViewModel(ruleNode.GetChild(i)));
            }
        }
Exemplo n.º 12
0
 private string ToText(IParseTree tree, IVocabulary names)
 {
     if (tree is TerminalNodeImpl token)
     {
         return(String.Concat("\"", token.GetText(), "\" - ", names.GetDisplayName(token.Symbol.Type)));
     }
     return(tree.GetType().Name.ToString());
 }
Exemplo n.º 13
0
        private string getContextTypeRecurcive(IParseTree context)
        {
            if (context.GetChild(0).GetType().ToString().Contains("TerminalNodeImpl"))
            {
                return(getConstTypeFromCSTNodeContext(context.GetType().ToString()));
            }

            return(getContextTypeRecurcive(context.GetChild(0)));
        }
Exemplo n.º 14
0
        public override IProgramPart Visit(IParseTree tree)
        {
            var x = base.Visit(tree);

            //Console.WriteLine(tree.GetText());
#pragma warning disable 162
            if (false)
            {
                const int col = -30;
                try {
                    Console.WriteLine($"{tree.GetType().Name,col}|{x.GetType().Name,col}|{x}");
                } catch {
                    Console.WriteLine($"{tree.GetType().Name,col}|{x?.GetType().Name,col}|{(x?.ToString() ?? "NULL")}");
                }
            }
#pragma warning restore 162
            return(x);
        }
Exemplo n.º 15
0
 private static void DumpParseTree(IParseTree tree, IndentedTextWriter writer)
 {
     writer.WriteLine("{0}: {1}", tree.GetType().Name, tree.GetText());
     writer.Indent++;
     for (var i = 0; i < tree.ChildCount; i++)
     {
         DumpParseTree(tree.GetChild(i), writer);
     }
     writer.Indent--;
 }
Exemplo n.º 16
0
        /// <summary>
        /// 해당 노드의 차일드 노드인지를 확인하고 가져옵니다.
        /// </summary>
        /// <param name="childRuleName">차일드 노드 이름.</param>
        /// <param name="tree">해당 노드 오브젝트.</param>
        /// <param name="matchedTree">차일드 노드 오브젝트.</param>
        /// <returns>차일드 노드 존재 여부.</returns>
        public static bool TryChildContext(string childRuleName, IParseTree tree, out IParseTree matchedTree)
        {
            MethodInfo info = tree.GetType().GetMethod(childRuleName);

            bool isExist = info != null;

            matchedTree = isExist ? info.Invoke(tree, null) as IParseTree : null;

            return(isExist);
        }
Exemplo n.º 17
0
        public override Statement Visit(IParseTree tree)
        {
            var result = base.Visit(tree);

            if (result == null)
            {
                throw new InvalidOperationException($"The tree {tree.GetType()} is not implemented yet.");
            }

            return(result);
        }
Exemplo n.º 18
0
 public override object Visit(IParseTree tree)
 {
     for (int i = 0; i < tree.ChildCount; i++)
     {
         IParseTree item = tree.GetChild(i);
         if (typeof(smilesParser.ChainContext).IsAssignableFrom(item.GetType()))
         {
             VisitChain((smilesParser.ChainContext)item);
         }
     }
     return(retVal.Atoms);
 }
Exemplo n.º 19
0
        public StatementNode VisitStatement(DazelParser.StatementContext context)
        {
            IParseTree child = context.GetChild(0);

            if (child.GetType() == typeof(DazelParser.IfStatementContext))
            {
                return(VisitIfStatement(context.ifStatement()));
            }

            if (child.GetType() == typeof(DazelParser.RepeatLoopContext))
            {
                return(VisitRepeatLoop(context.repeatLoop()));
            }

            if (child.GetType() == typeof(DazelParser.StatementExpressionContext))
            {
                return(VisitStatementExpression(context.statementExpression()));
            }

            throw new ArgumentException("Invalid statement");
        }
Exemplo n.º 20
0
 public static IParseTree GetNearestAncestorOfTypes(this IParseTree context, params Type[] types)
 {
     if (context == null)
     {
         return(null);
     }
     if (types.Contains(context.GetType()))
     {
         return(context);
     }
     return(context.Parent.GetNearestAncestorOfTypes(types));
 }
        public void EvaluateSyntaxErrors(IParseTree item)
        {
            if (item != null)
            {
                if (item is ErrorNodeImpl e)
                {
                    this._errors.Add(new ErrorModel()
                    {
                        Filename   = Filename,
                        Line       = e.Symbol.Line,
                        StartIndex = e.Symbol.StartIndex,
                        Column     = e.Symbol.Column,
                        Text       = e.Symbol.Text,
                        Message    = $"Failed to parse script at position {e.Symbol.StartIndex}, line {e.Symbol.Line}, col {e.Symbol.Column} '{e.Symbol.Text}'",
                        Code       = "Syntax"
                    });
                }

                else if (item is ParserRuleContext r)
                {
                    if (_hash.Contains(item.GetType()))
                    {
                        Add(r);
                        this._references.Add(new ReferenceModel()
                        {
                            Filename   = Filename,
                            Line       = r.Start.Line,
                            StartIndex = r.Start.StartIndex,
                            Column     = r.Start.Column,
                            Text       = r.Start.Text,
                        });
                    }
                }

                else if (item is ITerminalNode t)
                {
                    this._keywords.Add(new KeywordModel()
                    {
                        Filename   = Filename,
                        Line       = t.Symbol.Line,
                        StartIndex = t.Symbol.StartIndex,
                        Column     = t.Symbol.Column,
                        Text       = t.Symbol.Text,
                    });
                }

                int c = item.ChildCount;
                for (int i = 0; i < c; i++)
                {
                    EvaluateSyntaxErrors(item.GetChild(i));
                }
            }
        }
Exemplo n.º 22
0
 public override object Visit(IParseTree tree)
 {
     for (int i = 0; i < tree.ChildCount; i++)
     {
         IParseTree item = tree.GetChild(i);
         if (typeof(smilesParser.ChainContext).IsAssignableFrom(item.GetType()))
         {
             VisitChain((smilesParser.ChainContext)item);
         }
         // else throw new System.NotImplementedException("String not a chain.");
     }
     return(retVal.Atoms);
 }
Exemplo n.º 23
0
        static void OutDot(StringBuilder sb, IParseTree tree)
        {
            var hc = tree.GetHashCode();

            if (low_ints.TryGetValue(hc, out int low_hc))
            {
                hc = low_hc;
            }
            else
            {
                low_ints[hc] = gen++;
                hc           = gen - 1;
            }
            if (tree is ParserRuleContext)
            {
                var name = "\"" + hc + " " + tree.GetType().Name.Replace("Context", "") + "\"";
                for (int i = 0; i < tree.ChildCount; ++i)
                {
                    var c   = tree.GetChild(i);
                    var chc = c.GetHashCode();
                    if (low_ints.TryGetValue(chc, out int low_chc))
                    {
                        chc = low_chc;
                    }
                    else
                    {
                        low_ints[chc] = gen++;
                        chc           = gen - 1;
                    }
                    string cname;
                    if (c is TerminalNodeImpl)
                    {
                        cname = ("\"" + chc + " " + c.GetText() + "\"");
                    }
                    else
                    {
                        cname = ("\"" + chc + " " + c.GetType().Name.Replace("Context", "") + "\"");
                    }
                    sb.AppendLine(name + " -> " + cname);
                    OutDot(sb, c);
                }
            }
            else if (tree is TerminalNodeImpl)
            {
            }
            else
            {
            }
        }
Exemplo n.º 24
0
        // загрузить дерево в TreeView по дереву парсинга
        public void LoadNode(TreeNode node, IParseTree tree)
        {
            string s;

            if (node == null)
            {// если дерева в TreeView еще нет
                {
                    s = tree.GetType().ToString();
                    if (s.Contains("+"))
                    {
                        s = tree.GetType().ToString().Split('+')[1];
                    }
                    if (s.Contains("."))
                    {
                        s = tree.GetType().ToString().Split('.')[3];
                    }
                    if (s.Contains("Context"))
                    {
                        s = "Rule:" + s.Substring(0, s.IndexOf("Context"));
                    }
                    if (s.Contains("Terminal"))
                    {
                        s = s.Substring(0, 8);
                    }
                    node = tv.Nodes.Add(s + " # " + tree.GetText());
                }
            }
            else
            {
                {
                    s = tree.GetType().ToString();
                    if (s.Contains("+"))
                    {
                        s = tree.GetType().ToString().Split('+')[1];
                    }
                    if (s.Contains("."))
                    {
                        s = tree.GetType().ToString().Split('.')[3];
                    }
                    if (s.Contains("Context"))
                    {
                        s = "Rule:" + s.Substring(0, s.IndexOf("Context"));
                    }
                    if (s.Contains("Terminal"))
                    {
                        s = s.Substring(0, 8);
                    }
                    node = node.Nodes.Add(s + " # " + tree.GetText());
                }
            }
            // загрузить дочерние ноды
            for (int i = 0; i < tree.ChildCount; i++)
            {
                LoadNode(node, tree.GetChild(i));
            }
        }
Exemplo n.º 25
0
        private void DumpTree(bool dooutput, IParseTree tree, int depth = 0)
        {
            string tabs;

            if (dooutput)
            {
                tabs = new string(' ', depth * 4);
                m_TreeDump.AppendFormat("{0}{1} : {2}\n", tabs, Purify(tree.GetType()), tree.GetText());
            }

            for (int i = 0; i < tree.ChildCount; i++)
            {
                DumpTree(dooutput, tree.GetChild(i), depth + 1);
            }
        }
Exemplo n.º 26
0
        static private void CheckSyntaxTree(IParseTree p, int level)
        {
            string s = p.GetType().ToString();

            if (p is ErrorNodeImpl)
            {
                errCount++;
            }
            else if (p.ChildCount > 0)
            {
                for (int i = 0; i < p.ChildCount; i++)
                {
                    CheckSyntaxTree(p.GetChild(i), level + 1);
                }
            }
        }
Exemplo n.º 27
0
        private ExpressionPartType ExpressionPartTypeFinder(IParseTree context)
        {
            string type = context.GetType().ToString();

            switch (type)
            {
            case "GiraphParser+BoolContext":
            case "GiraphParser+SimpleBoolCompOrExpContext":
            case "GiraphParser+BoolCompOrExpContext":
                return(ExpressionPartType.BOOL);

            case "GiraphParser+FloatnumContext":
                return(ExpressionPartType.DECIMAL);

            case "GiraphParser+IntegerContext":
                return(ExpressionPartType.INT);

            case "GiraphParser+SimpleOperatorsContext":
                return(ExpressionPartType.OPERATOR);

            case "GiraphParser+StringContext":
                return(ExpressionPartType.STRING);

            case "GiraphParser+AdvancedOperatorsContext":
                return(ExpressionPartType.ADVANCED_OPERATOR);

            case "GiraphParser+VariableContext":
            case "GiraphParser+ObjectsContext":
                return(ExpressionPartType.VARIABLE);

            case "GiraphParser+SelectContext":
            case "GiraphParser+PopOPContext":
            case "GiraphParser+PushOPContext":
            case "GiraphParser+EnqueueOPContext":
            case "GiraphPArser+DequeueOPContext":
            //case "GiraphParser+ObjectsContext":
            case "GiraphParser+WhereContext":
            case "GiraphParser+ExtractMaxOPContext":
            case "GiraphParser+ExtractMinOPContext":
            case "GiraphParser+DequeueOPContext":
                return(ExpressionPartType.QUERYTYPE);

            case "GiraphParser+AttributeContext":
                return(ExpressionPartType.ATTRIBUTE);
            }
            throw new WrongExpressionPartTypeFoundException($"Typen: {type} har ikke en case i typefinder!!");
        }
Exemplo n.º 28
0
        public IExpressionBinding BuildTree(Declaration module, Declaration parent, IParseTree expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext, bool requiresLetCoercion = false, bool isLetAssignment = false)
        {
            switch (expression)
            {
            case VBAParser.LExprContext lExprContext:
                return(Visit(module, parent, lExprContext.lExpression()));

            case VBAParser.CtLExprContext ctLExprContext:
                return(Visit(module, parent, ctLExprContext.lExpression()));

            case VBAParser.BuiltInTypeExprContext builtInTypeExprContext:
                return(Visit(builtInTypeExprContext.builtInType()));

            default:
                throw new NotSupportedException($"Unexpected context type {expression.GetType()}");
            }
        }
Exemplo n.º 29
0
        public override INode Visit(IParseTree tree)
        {
            if (tree is Antlr4.Runtime.ParserRuleContext ruleContext)
            {
                AssertNoException(ruleContext);
            }

            var result = base.Visit(tree);

#if DEBUG
            if (result == null)
            {
                throw new NotImplementedException($"{tree.GetType().Name} should not have returned null, or if it is a union type, see {tree.GetChild(0).GetType().Name}");
            }
#endif
            return(result);
        }
Exemplo n.º 30
0
        /// <summary>
        /// Serializing parse tree to DOT using recursive tree lookup.
        /// </summary>
        /// <param name="node"> Current parse tree node in lookup. </param>
        private static void ToDotRecursive(IParseTree node, StringBuilder buf)
        {
            string nodeName = Regex.Replace(node.GetType().Name, "Context$", string.Empty);
            string nodeHash = node.GetHashCode().ToString();

            for (int i = 0; i < node.ChildCount; i++)
            {
                string childName = Regex.Replace(node.GetChild(i).GetType().Name, "Context$", string.Empty);
                string childHash = node.GetChild(i).GetHashCode().ToString();
                string childText = node.GetChild(i).GetText();

                buf.AppendLine(
                    $"\"{nodeHash}\n{nodeName}\" -> \"{childHash}\n{childName}" +
                    $"{((node.GetChild(i).ChildCount == 0) ? "\nToken = \\\"" + Escape(childText) + "\\\"" : string.Empty)}\"\n");
                ToDotRecursive(node.GetChild(i), buf);
            }
        }
Exemplo n.º 31
0
 // загрузить дерево в TreeView по дереву парсинга
 public void LoadNode(TreeNode node, IParseTree tree)
 {
     string s;
     if (node == null)
     {// если дерева в TreeView еще нет
         {
             s = tree.GetType().ToString();
             if (s.Contains("+")) s = tree.GetType().ToString().Split('+')[1];
             if (s.Contains(".")) s = tree.GetType().ToString().Split('.')[3];
             if (s.Contains("Context")) s = "Rule:" + s.Substring(0, s.IndexOf("Context"));
             if (s.Contains("Terminal")) s = s.Substring(0, 8);
             node = tv.Nodes.Add(s + " # " + tree.GetText());
         }
     }
     else
     {
         {
             s = tree.GetType().ToString();
             if (s.Contains("+")) s = tree.GetType().ToString().Split('+')[1];
             if (s.Contains(".")) s = tree.GetType().ToString().Split('.')[3];
             if (s.Contains("Context")) s = "Rule:" + s.Substring(0, s.IndexOf("Context"));
             if (s.Contains("Terminal")) s = s.Substring(0, 8);
             node = node.Nodes.Add(s + " # " + tree.GetText());
         }
     }
     // загрузить дочерние ноды
     for (int i = 0; i < tree.ChildCount; i++)
     {
         LoadNode(node, tree.GetChild(i));
     }
 }