Beispiel #1
1
        public Decoder(string astm_message)
            : this()
        {
            log.Debug("Decoder:ctor(string)");

            try
            {
                _astm_message = astm_message;
                _parser = new Parser();
                _nodes = _parser.CreateNode((int)ParseTreeLevel.Message);
                string msg = _parser.RemoveDelimiter(_astm_message);
                _parser.Parse(_nodes, msg, "\r|\\^", (int)ParseTreeLevel.Message);
                _parser.ProcessItems(_nodes);
                _message = Mapper.MapToMessage(_nodes);
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
                throw;
            }
        }
Beispiel #2
0
 /// <summary>
 /// Construct a new instance of the
 /// <see cref="ParseTreePattern"/>
 /// class.
 /// </summary>
 /// <param name="matcher">
 /// The
 /// <see cref="ParseTreePatternMatcher"/>
 /// which created this
 /// tree pattern.
 /// </param>
 /// <param name="pattern">The tree pattern in concrete syntax form.</param>
 /// <param name="patternRuleIndex">
 /// The parser rule which serves as the root of the
 /// tree pattern.
 /// </param>
 /// <param name="patternTree">
 /// The tree pattern in
 /// <see cref="Antlr4.Runtime.Tree.IParseTree"/>
 /// form.
 /// </param>
 public ParseTreePattern(ParseTreePatternMatcher matcher, string pattern, int patternRuleIndex, IParseTree patternTree)
 {
     this.matcher = matcher;
     this.patternRuleIndex = patternRuleIndex;
     this.pattern = pattern;
     this.patternTree = patternTree;
 }
        public Questionnaire BuildAST(string inputString)
        {
            MemoryStream inputStream = new MemoryStream(Encoding.UTF8.GetBytes(inputString ?? ""));

            AntlrInputStream antlrInputStream = new AntlrInputStream(inputStream);
            QLLexer lexer = new QLLexer(antlrInputStream);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            _parser = new QLParser(tokens);

            //Replaxe lexer/parser error listeners
            lexer.RemoveErrorListeners();
            _parser.RemoveErrorListeners();
            lexer.AddErrorListener(new LexerErrorListener() { OnError = LexerErrors.Add });
            _parser.AddErrorListener(new ParserErrorListener() { OnError = ParserErrors.Add });

            //set manager on partial parser class
            _parser.SetIdManager(Memory);

            //build AST
            _parseTree = _parser.questionnaire();
            AST = _parser.GetAST();

            //check for lexer/parser errors
            if (!LexerErrors.Any() && !ParserErrors.Any())
            {
                TypeChecker.Run(AST);
            }

            return AST;
        }
 public Expression(string expression)
 {
     var input = new AntlrInputStream(expression);
     var lexer = new EstimatingExpressionEvaluatorLexer(input);
     var tokens = new CommonTokenStream(lexer);
     this.parser = new EstimatingExpressionEvaluatorParser(tokens);
     this.parseTree = parser.parse();
 }
Beispiel #5
0
 private void Init()
 {
     var inputStream = new AntlrInputStream(new System.IO.StreamReader(_filename));
     var lexer = new PropositionalCalculusLexer(inputStream);
     var tokenStream = new CommonTokenStream(lexer);
     _parser = new PropositionalCalculusParser(tokenStream) { BuildParseTree = true };
     _tree = _parser.text();
 }
 public override ICollection<IParseTree> Evaluate(IParseTree t)
 {
     if (invert)
     {
         return new List<IParseTree>();
     }
     // !* is weird but valid (empty)
     return Trees.Descendants(t);
 }
Beispiel #7
0
 public static IList<IParseTree> Descendants(IParseTree t)
 {
     List<IParseTree> nodes = new List<IParseTree>();
     nodes.Add(t);
     int n = t.ChildCount;
     for (int i = 0; i < n; i++)
     {
         nodes.AddRange(Descendants(t.GetChild(i)));
     }
     return nodes;
 }
 public override ICollection<IParseTree> Evaluate(IParseTree t)
 {
     if (invert)
     {
         return new List<IParseTree>();
     }
     // !* is weird but valid (empty)
     IList<IParseTree> kids = new List<IParseTree>();
     foreach (ITree c in Trees.GetChildren(t))
     {
         kids.Add((IParseTree)c);
     }
     return kids;
 }
Beispiel #9
0
        /// <summary>
        /// Проверяет два синтаксических дерева на сходство, игнорируя вертикальные "цепочки" и скобки.
        /// </summary>
        public static bool Same(this IParseTree tree1, IParseTree tree2)
        {
            tree1 = tree1.Collapse();
            tree2 = tree2.Collapse();

            if (tree1.ChildCount == 0 || tree2.ChildCount == 0) return tree1.GetText() == tree2.GetText();
            if (tree1.ChildCount != tree2.ChildCount) return false;

            for (int i = 0; i < tree1.ChildCount; i++)
            {
                if (!tree1.GetChild(i).Same(tree2.GetChild(i))) return false;
            }
            return true;
        }
        public VBComponentParseResult(VBComponent component, IParseTree parseTree, IEnumerable<CommentNode> comments, ITokenStream tokenStream)
        {
            _component = component;
            _qualifiedName = new QualifiedModuleName(component);
            _parseTree = parseTree;
            _comments = comments;
            _tokenStream = tokenStream;

            var listener = new DeclarationSymbolsListener(_qualifiedName, Accessibility.Implicit, _component.Type);
            var walker = new ParseTreeWalker();
            walker.Walk(listener, _parseTree);

            _declarations.AddRange(listener.Declarations.Items);
        }
Beispiel #11
0
 // --- Get tokens in terminal nodes ---
 public static ITerminalNode GetFirstTerminalNode(IParseTree node)
 {
     while (!(node is ITerminalNode))
     {
         if (node.ChildCount == 0)
         {
             return null;
         }
         else
         {
             node = node.GetChild(0);
         }
     }
     return (ITerminalNode)node;
 }
Beispiel #12
0
 public static string GetAlphanumericLiteral(IParseTree node)
 {
     if (node != null)
     {
         ITerminalNode terminalNode = GetFirstTerminalNode(node);
         if (terminalNode != null)
         {
             Token alphaNumericLiteralToken = GetTokenFromTerminalNode(terminalNode);
             if (alphaNumericLiteralToken.TokenFamily == TokenFamily.AlphanumericLiteral)
             {
                 return ((AlphanumericLiteralTokenValue)alphaNumericLiteralToken.LiteralValue).Text;
             }
         }
     }
     return null;
 }
Beispiel #13
0
 public static long? GetIntegerLiteral(IParseTree node)
 {
     if (node != null)
     {
         ITerminalNode terminalNode = GetFirstTerminalNode(node);
         if (terminalNode != null)
         {
             Token integerLiteralToken = GetTokenFromTerminalNode(terminalNode);
             if (integerLiteralToken.TokenType == TokenType.IntegerLiteral)
             {
                 return ((IntegerLiteralTokenValue)integerLiteralToken.LiteralValue).Number;
             }
         }
     }
     return null;
 }
Beispiel #14
0
        public Semantic(IParseTree root)
        {
            _root = root;

            _listener = new DefinitionListener();

            ParseTreeWalker.Default.Walk(_listener, root);

            _errors.AddRange(_listener.Errors);

            var resolvingListener = new ResolvingListener(this);

            ParseTreeWalker.Default.Walk(resolvingListener, root);

            _errors.AddRange(resolvingListener.Errors);
        }
Beispiel #15
0
 public override ICollection<IParseTree> Evaluate(IParseTree t)
 {
     // return all children of t that match nodeName
     IList<IParseTree> nodes = new List<IParseTree>();
     foreach (ITree c in Trees.GetChildren(t))
     {
         if (c is ParserRuleContext)
         {
             ParserRuleContext ctx = (ParserRuleContext)c;
             if ((ctx.RuleIndex == ruleIndex && !invert) || (ctx.RuleIndex != ruleIndex && invert))
             {
                 nodes.Add(ctx);
             }
         }
     }
     return nodes;
 }
Beispiel #16
0
 public override ICollection<IParseTree> Evaluate(IParseTree t)
 {
     // return all children of t that match nodeName
     IList<IParseTree> nodes = new List<IParseTree>();
     foreach (ITree c in Trees.GetChildren(t))
     {
         if (c is ITerminalNode)
         {
             ITerminalNode tnode = (ITerminalNode)c;
             if ((tnode.Symbol.Type == tokenType && !invert) || (tnode.Symbol.Type != tokenType && invert))
             {
                 nodes.Add(tnode);
             }
         }
     }
     return nodes;
 }
Beispiel #17
0
        /// <summary>
        /// Mapira na Message
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public static Message MapToMessage(IParseTree node)
        {
            log.Debug("Mapper:MapToMessage");

            try
            {
                MessageId message_id = node.GetNodes()[0].GetNodes()[10].GetValue();
                if (message_id != "None")
                {
                    Message message = Message.Create(message_id);

                    int i = 0;
                    bool result_record_processing = false;
                    foreach (IParseTree item in node.GetNodes())
                    {
                        Record record = MapToRecord(item);
                        if (record.Type == RecordId.ResultRecord)
                        {
                            result_record_processing = true;
                            ((Results)message[i]).ResultComment.Add(new ResultCommentPair() { Result = (ResultRecord)record });
                        }
                        else if (record.Type == RecordId.CommentRecord)
                        {
                            result_record_processing = true;
                            ((Results)message[i]).ResultComment[((Results)message[i]).ResultComment.Count - 1].Comment = (CommentRecord)record;
                        }
                        else
                        {
                            if (result_record_processing == true)
                                i++;
                            message[i++] = record;
                            result_record_processing = false;
                        }
                    }

                    return message;
                }

                return null;
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
                throw;
            }
        }
Beispiel #18
0
        public static void Populate(TreeView treeview, IParseTree parsetree)
        {
            treeview.Visible = false;
            treeview.SuspendLayout();
            treeview.Nodes.Clear();
            treeview.Tag = parsetree;

            IParseNode start = parsetree.INodes[0];
            TreeNode node = new TreeNode(start.Text);
            node.Tag = start;
            node.ForeColor = Color.SteelBlue;
            treeview.Nodes.Add(node);

            PopulateNode(node, start);
            treeview.ExpandAll();
            treeview.ResumeLayout();
            treeview.Visible = true;
        }
Beispiel #19
0
 /// <summary>
 /// Constructs a new instance of
 /// <see cref="ParseTreeMatch"/>
 /// from the specified
 /// parse tree and pattern.
 /// </summary>
 /// <param name="tree">The parse tree to match against the pattern.</param>
 /// <param name="pattern">The parse tree pattern.</param>
 /// <param name="labels">
 /// A mapping from label names to collections of
 /// <see cref="Antlr4.Runtime.Tree.IParseTree"/>
 /// objects located by the tree pattern matching process.
 /// </param>
 /// <param name="mismatchedNode">
 /// The first node which failed to match the tree
 /// pattern during the matching process.
 /// </param>
 /// <exception>
 /// IllegalArgumentException
 /// if
 /// <paramref name="tree"/>
 /// is
 /// <see langword="null"/>
 /// </exception>
 /// <exception>
 /// IllegalArgumentException
 /// if
 /// <paramref name="pattern"/>
 /// is
 /// <see langword="null"/>
 /// </exception>
 /// <exception>
 /// IllegalArgumentException
 /// if
 /// <paramref name="labels"/>
 /// is
 /// <see langword="null"/>
 /// </exception>
 public ParseTreeMatch(IParseTree tree, ParseTreePattern pattern, MultiMap<string, IParseTree> labels, IParseTree mismatchedNode)
 {
     if (tree == null)
     {
         throw new ArgumentException("tree cannot be null");
     }
     if (pattern == null)
     {
         throw new ArgumentException("pattern cannot be null");
     }
     if (labels == null)
     {
         throw new ArgumentException("labels cannot be null");
     }
     this.tree = tree;
     this.pattern = pattern;
     this.labels = labels;
     this.mismatchedNode = mismatchedNode;
 }
Beispiel #20
0
 public static Token GetFirstToken(IParseTree node)
 {
     if (node != null)
     {
         ITerminalNode terminalNode = GetFirstTerminalNode(node);
         if (terminalNode != null)
         {
             return GetTokenFromTerminalNode(terminalNode);
         }
         else
         {
             return null;
         }
     }
     else
     {
         return null;
     }
 }
Beispiel #21
0
 public virtual void Walk(IParseTreeListener listener, IParseTree t)
 {
     if (t is IErrorNode)
     {
         listener.VisitErrorNode((IErrorNode)t);
         return;
     }
     else
     {
         if (t is ITerminalNode)
         {
             listener.VisitTerminal((ITerminalNode)t);
             return;
         }
     }
     IRuleNode r = (IRuleNode)t;
     EnterRule(listener, r);
     int n = r.ChildCount;
     for (int i = 0; i < n; i++)
     {
         Walk(listener, r.GetChild(i));
     }
     ExitRule(listener, r);
 }
        public WorldTranspilationResult Transpile(IParseTree parseTree)
        {
            Visit(parseTree);

            return(new WorldTranspilationResult(worldBuilder.Output, dataBuilder.Output));
        }
Beispiel #23
0
 /*********
 * Recurisve call to output the parsed game for graphviz dot
 *********/
 public static void DOTMaker(IParseTree node, string nodeName, StringBuilder builder)
 {
     for (int i = 0; i < node.ChildCount; ++i)
     {
         var dontCreate  = false;
         var newNodeName = nodeName + "_" + i;
         var contextName = node.GetChild(i).GetType().ToString().Replace("RecycleParser+", "").Replace("Context", "");
         if (node.GetChild(i).ChildCount > 0 && contextName == "Int")
         {
             var text = node.GetChild(i).GetText();
             int myi  = 0;
             while (myi < text.Length && Char.IsDigit(text[myi]))
             {
                 myi++;
             }
             if (myi != text.Length)
             {
                 builder.AppendLine(newNodeName + " [label=\"" + node.GetChild(i).GetType().ToString().Replace("RecycleParser+", "").Replace("Context", "") + "\" ]");
                 DOTMaker(node.GetChild(i), newNodeName, builder);
             }
             else
             {
                 builder.AppendLine(newNodeName + " [label=\"" + node.GetChild(i).GetText() + "\" style=filled fillcolor=\"lightblue\"]");
             }
         }
         else if (node.GetChild(i).ChildCount > 0 && contextName != "Namegr" && contextName != "Name" && contextName != "Trueany")
         {
             var extra = "";
             if (contextName == "Stage")
             {
                 extra = " style=filled fillcolor=\"red\"";
             }
             else if (contextName == "Computermoves")
             {
                 extra = " style=filled shape=box fillcolor=\"yellow\"";
             }
             else if (contextName == "Playermoves")
             {
                 extra = " style=filled shape=diamond fillcolor=\"orange\"";
             }
             builder.AppendLine(newNodeName + " [label=\"" + node.GetChild(i).GetType().ToString().Replace("RecycleParser+", "").Replace("Context", "") + "\" " + extra + "]");
             DOTMaker(node.GetChild(i), newNodeName, builder);
         }
         else if (node.GetChild(i).ChildCount > 0)
         {
             builder.AppendLine(newNodeName + " [fillcolor=\"green\" style=filled label=\"" + node.GetChild(i).GetText() + "\"]");
         }
         else if (node.GetChild(i).GetText() == "(" || node.GetChild(i).GetText() == ")" || node.GetChild(i).GetText() == "," ||
                  node.GetChild(i).GetText() == "end" || node.GetChild(i).GetText() == "stage" || node.GetChild(i).GetText() == "comp" ||
                  node.GetChild(i).GetText() == "create" || node.GetChild(i).GetText() == "sto" || node.GetChild(i).GetText() == "loc" ||
                  node.GetChild(i).GetText() == "initialize" || node.GetChild(i).GetText() == "move" || node.GetChild(i).GetText() == "copy" ||
                  node.GetChild(i).GetText() == "inc" || node.GetChild(i).GetText() == "dec" || node.GetChild(i).GetText() == "shuffle" ||
                  node.GetChild(i).GetText() == "remove" ||
                  node.GetChild(i).GetText() == "choice")
         {
             dontCreate = true;
         }
         else
         {
             builder.AppendLine(newNodeName + " [label=\"" + node.GetChild(i).GetText() + "\"]");
         }
         if (!dontCreate)
         {
             builder.AppendLine(nodeName + " -- " + newNodeName);
         }
     }
 }
Beispiel #24
0
        // Given a bunch of raw text, load all nodes that were inside it.
        // You can call this multiple times to append to the collection of nodes,
        // but note that new nodes will replace older ones with the same name.
        // Returns the number of nodes that were loaded.
        public Program Load(string text, Library library, string fileName, Program includeProgram, bool showTokens, bool showParseTree, string onlyConsiderNode, NodeFormat format, bool experimentalMode = false)
        {
            if (format == NodeFormat.Unknown)
            {
                format = GetFormatFromFileName(fileName);
            }

            // currently experimental node can only be used on yarn.txt yarn files and single nodes
            if (experimentalMode && (format == NodeFormat.Text || format == NodeFormat.SingleNodeText))
            {
                // this isn't the greatest...
                if (format == NodeFormat.SingleNodeText)
                {
                    // it is just the body
                    // need to add a dummy header and body delimiters
                    StringBuilder builder = new StringBuilder();
                    builder.Append("title:Start\n");
                    builder.Append("---\n");
                    builder.Append(text);
                    builder.Append("\n===\n");
                    text = builder.ToString();
                }

                string      inputString = preprocessor(text);
                ICharStream input       = CharStreams.fromstring(inputString);

                YarnSpinnerLexer  lexer  = new YarnSpinnerLexer(input);
                CommonTokenStream tokens = new CommonTokenStream(lexer);

                YarnSpinnerParser parser = new YarnSpinnerParser(tokens);
                // turning off the normal error listener and using ours
                parser.RemoveErrorListeners();
                parser.AddErrorListener(ErrorListener.Instance);

                IParseTree    tree          = parser.dialogue();
                AntlrCompiler antlrcompiler = new AntlrCompiler(library);
                antlrcompiler.Compile(tree);

                // merging in the other program if requested
                if (includeProgram != null)
                {
                    antlrcompiler.program.Include(includeProgram);
                }

                return(antlrcompiler.program);
            }
            else
            {
                // The final parsed nodes that were in the file we were given
                Dictionary <string, Yarn.Parser.Node> nodes = new Dictionary <string, Parser.Node>();

                // Load the raw data and get the array of node title-text pairs

                var nodeInfos = GetNodesFromText(text, format);

                int nodesLoaded = 0;

                foreach (NodeInfo nodeInfo in nodeInfos)
                {
                    if (onlyConsiderNode != null && nodeInfo.title != onlyConsiderNode)
                    {
                        continue;
                    }

                    // Attempt to parse every node; log if we encounter any errors
#if CATCH_EXCEPTIONS
                    try
                    {
#endif

                    if (nodeInfo.title == null)
                    {
                        throw new InvalidOperationException("Tried to load a node with no title.");
                    }
                    if (nodes.ContainsKey(nodeInfo.title))
                    {
                        throw new InvalidOperationException("Attempted to load a node called " +
                                                            nodeInfo.title + ", but a node with that name has already been loaded!");
                    }

                    var lexer  = new Lexer();
                    var tokens = lexer.Tokenise(nodeInfo.title, nodeInfo.body);

                    if (showTokens)
                    {
                        PrintTokenList(tokens);
                    }

                    var node = new Parser(tokens, library).Parse();

                    // If this node is tagged "rawText", then preserve its source
                    if (string.IsNullOrEmpty(nodeInfo.tags) == false &&
                        nodeInfo.tags.Contains("rawText"))
                    {
                        node.source = nodeInfo.body;
                    }

                    node.name = nodeInfo.title;

                    node.nodeTags = nodeInfo.tagsList;

                    if (showParseTree)
                    {
                        PrintParseTree(node);
                    }

                    nodes[nodeInfo.title] = node;

                    nodesLoaded++;

#if CATCH_EXCEPTIONS
                }
                catch (Yarn.TokeniserException t)
                {
                    // Add file information
                    var message = string.Format("In file {0}: Error reading node {1}: {2}", fileName, nodeInfo.title, t.Message);
                    throw new Yarn.TokeniserException(message);
                }
                catch (Yarn.ParseException p)
                {
                    var message = string.Format("In file {0}: Error parsing node {1}: {2}", fileName, nodeInfo.title, p.Message);
                    throw new Yarn.ParseException(message);
                }
                catch (InvalidOperationException e)
                {
                    var message = string.Format("In file {0}: Error reading node {1}: {2}", fileName, nodeInfo.title, e.Message);
                    throw new InvalidOperationException(message);
                }
#endif
                }

                var compiler = new Yarn.Compiler(fileName);

                foreach (var node in nodes)
                {
                    compiler.CompileNode(node.Value);
                }

                if (includeProgram != null)
                {
                    compiler.program.Include(includeProgram);
                }

                return(compiler.program);
            }
        }
Beispiel #25
0
        /// <summary>
        /// Make parse tree string representation. This method is recursive and not open to public.
        /// </summary>
        /// <param name="rules"></param>
        /// <param name="nRules"></param>
        /// <param name="leaves"></param>
        /// <param name="nLeaves"></param>
        /// <param name="tree"></param>
        /// <returns></returns>
        private StringBuilder makeParseTree(StringBuilder rules, ref int nRules, StringBuilder leaves, ref int nLeaves, IParseTree tree = null)
        {
            if (tree == null && this.cst == null)
            {
                return(new StringBuilder());
            }
            else if (tree == null)
            {
                tree = this.cst;
            }

            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < tree.ChildCount; i++)
            {
                if (i == 0)
                {
                    builder.Append("[");
                }

                IParseTree child = tree.GetChild(i);

                if (child is ITerminalNode)
                {
                    string text = child.GetText();
                    text = SanitizeJsCode(text);

                    int    tokenType = ((CommonToken)child.Payload).Type;
                    string varName   = String.Format("tokens[{0}]", nLeaves++);
                    leaves.AppendFormat("{0} = {{l:{1},t:\"{2}\"}};\n", varName, tokenType, text);

                    builder.Append(varName);
                }
                else if (child is RuleContext)
                {
                    StringBuilder childTree = makeParseTree(rules, ref nRules, leaves, ref nLeaves, child);
                    if (childTree.Length < 1)
                    {
                        childTree.Append("[]");
                    }

                    string varName = String.Format("rules[{0}]", nRules++);
                    rules.AppendFormat("{0} = {{r:{1},c:{2}}};\n", varName, ((RuleContext)child).RuleIndex, childTree.ToString());

                    builder.Append(varName);
                }

                if (i == tree.ChildCount - 1)
                {
                    builder.Append("]");
                }
                else
                {
                    builder.Append(",");
                }
            }

            return(builder);
        }
Beispiel #26
0
 public abstract List <TokenInfo> GetColumnNameTokens(IParseTree node);
Beispiel #27
0
 public TinyFunction(List <ITerminalNode> args, IParseTree block)
 {
     this.parameters    = args;
     this.functionBlock = block;
 }
 public ParseTreeWrapper(IParseTree tree)
 {
     _tree = tree;
 }
 /// <summary>
 /// Получить значение, ассоциированное с указанным узлом дерева.
 /// Возвращает <c><see langword="null"/></c>, если для указанного
 /// узла в коллекции нет значений.
 /// </summary>
 /// <param name="node">Узел дерева, для которого должно быть значение в коллекции.</param>
 /// <returns>Значение, ассоциированное с узлом дерева.</returns>
 public static T?TryGet <T>(this ParseTreeProperty <T> annotations, IParseTree node) where T : class
 {
     return(annotations.Get(node));
 }
 public static int StopLineNumber(this IParseTree tree)
 {
     return(((ParserRuleContext)tree).Stop.Line);
 }
Beispiel #31
0
 public virtual void Put(IParseTree node, V value)
 {
     annotations[node] = value;
 }
Beispiel #32
0
 public GotoStatement(IParseTree context, IExpression condition, GotoTargetStatement targetStatement) : base(context, condition.Yield())
 {
     TargetStatement = targetStatement;
 }
Beispiel #33
0
 public GotoStatement(IParseTree context, GotoTargetStatement targetStatement) : base(context)
 {
     TargetStatement = targetStatement;
 }
 /// <summary>
 /// Is
 /// <code>t</code>
 /// 
 /// <code>(expr &lt;expr&gt;)</code>
 /// subtree?
 /// </summary>
 protected internal virtual RuleTagToken GetRuleTagToken(IParseTree t)
 {
     if (t is IRuleNode)
     {
         IRuleNode r = (IRuleNode)t;
         if (r.ChildCount == 1 && r.GetChild(0) is ITerminalNode)
         {
             ITerminalNode c = (ITerminalNode)r.GetChild(0);
             if (c.Symbol is RuleTagToken)
             {
                 //					System.out.println("rule tag subtree "+t.toStringTree(parser));
                 return (RuleTagToken)c.Symbol;
             }
         }
     }
     return null;
 }
Beispiel #35
0
        public IEnumerable <Location> FindRefsAndDefs(int index, Document doc)
        {
            List <Location> result = new List <Location>();
            IParseTree      ref_pt = Util.Find(index, doc);

            if (ref_pt == null)
            {
                return(result);
            }
            var workspace = doc.Workspace;

            ParsingResults ref_pd = ParsingResultsFactory.Create(doc);

            if (ref_pd.ParseTree == null)
            {
                Compile(workspace);
            }

            ref_pd.Attributes.TryGetValue(ref_pt, out IList <Symtab.CombinedScopeSymbol> list_value);
            if (list_value == null)
            {
                return(result);
            }

            List <Symtab.ISymbol> found_defs = null;

            Symtab.ISymbol found_ref = null;
            foreach (CombinedScopeSymbol value in list_value)
            {
                if (value == null)
                {
                    continue;
                }

                Symtab.ISymbol @ref = value as Symtab.ISymbol;
                if (@ref == null)
                {
                    continue;
                }

                if (@ref.Token == null)
                {
                    continue;
                }

                found_ref = @ref;
                List <Symtab.ISymbol> defs = @ref.resolve();
                if (defs == null)
                {
                    continue;
                }
                found_defs = defs;
                break;
            }
            if (found_defs != null)
            {
                foreach (var def in found_defs)
                {
                    result.Add(
                        new Location()
                    {
                        Range = new Workspaces.Range(def.Token.First().StartIndex, def.Token.First().StopIndex),
                        Uri   = workspace.FindDocument(def.file)
                    });
                    var dd = def as BaseSymbol;
                    foreach (var r in dd.Refs)
                    {
                        result.Add(
                            new Location()
                        {
                            Range = new Workspaces.Range(r.Token.First().StartIndex, r.Token.First().StopIndex),
                            Uri   = workspace.FindDocument(r.file)
                        });
                    }
                }
            }
            return(result);
        }
Beispiel #36
0
 public static ICollection <IParseTree> FindAllRuleNodes(IParseTree t, int ruleIndex)
 {
     return(FindAllNodes(t, ruleIndex, false));
 }
Beispiel #37
0
 public abstract List <TokenInfo> GetRoutineNameTokens(IParseTree node);
Beispiel #38
0
 public static ICollection<IParseTree> FindAllRuleNodes(IParseTree t, int ruleIndex)
 {
     return FindAllNodes(t, ruleIndex, false);
 }
Beispiel #39
0
 public abstract List <TokenInfo> GetTableNameTokens(IParseTree node);
 public Command getNodeCommand(IParseTree node)
 {
     return nodeCmd.Get(node);
 }
Beispiel #41
0
 public abstract bool IsFunction(IParseTree node);
Beispiel #42
0
 public static ICollection <IParseTree> FindAllTokenNodes(IParseTree t, int ttype)
 {
     return(FindAllNodes(t, ttype, true));
 }
 /// <summary>
 /// Установить ассоциацию узла дерева с указанным значением.
 /// Если для указанного узла дерева в этой коллекции уже было
 /// установлено значение, то оно заменится новым значением.
 /// </summary>
 /// <param name="node">Узел дерева, для которого устанавливается ассоциация.</param>
 /// <param name="value">Произвольное значение, которое ассоциируется с узлом дерева.</param>
 public void Put(IParseTree node, T value)
 {
     _annotations[node] = value;
 }
Beispiel #44
0
 public MappingExecutor(IParseTree <TokenType, ParserRuleType> mappingDefinition)
 {
     this.extract = mappingDefinition.SourceExtractor();
     this.update  = mappingDefinition.TargetUpdater();
 }
        public static void WalkWith(this IParseTree parseTree, IParseTreeListener listener)
        {
            var parseTreeWalker = ParseTreeWalker.Default;

            parseTree.WalkWith(listener, parseTreeWalker);
        }
Beispiel #46
0
 private static string AsType(IParseTree tree)
 {
     return(tree.GetText() == "String" ? "string" : "int");
 }
Beispiel #47
0
 public VarPragma(IZ80AsmVisitorContext visitorContext, IParseTree context)
     : base(visitorContext, context)
 {
 }
Beispiel #48
0
 public static IEnumerable <Token <TToken> > Tokens <TToken, TRule>(
     this IParseTree <TToken, TRule> node)
 => node.TokenNodes().Select(o => o.Token);
        protected internal virtual IParseTree MatchImpl(IParseTree tree, IParseTree patternTree, MultiMap<string, IParseTree> labels)
        {
            if (tree == null)
            {
                throw new ArgumentException("tree cannot be null");
            }
            if (patternTree == null)
            {
                throw new ArgumentException("patternTree cannot be null");
            }
            // x and <ID>, x and y, or x and x; or could be mismatched types
            if (tree is ITerminalNode && patternTree is ITerminalNode)
            {
                ITerminalNode t1 = (ITerminalNode)tree;
                ITerminalNode t2 = (ITerminalNode)patternTree;
                IParseTree mismatchedNode = null;
                // both are tokens and they have same type
                if (t1.Symbol.Type == t2.Symbol.Type)
                {
                    if (t2.Symbol is TokenTagToken)
                    {
                        // x and <ID>
                        TokenTagToken tokenTagToken = (TokenTagToken)t2.Symbol;
                        // track label->list-of-nodes for both token name and label (if any)

                        labels.Map(tokenTagToken.TokenName, tree);
                        if (tokenTagToken.Label != null)
                        {
                            labels.Map(tokenTagToken.Label, tree);
                        }
                    }
                    else
                    {
                        if (t1.GetText().Equals(t2.GetText(), StringComparison.Ordinal))
                        {
                        }
                        else
                        {
                            // x and x
                            // x and y
                            if (mismatchedNode == null)
                            {
                                mismatchedNode = t1;
                            }
                        }
                    }
                }
                else
                {
                    if (mismatchedNode == null)
                    {
                        mismatchedNode = t1;
                    }
                }
                return mismatchedNode;
            }
            if (tree is ParserRuleContext && patternTree is ParserRuleContext)
            {
                ParserRuleContext r1 = (ParserRuleContext)tree;
                ParserRuleContext r2 = (ParserRuleContext)patternTree;
                IParseTree mismatchedNode = null;
                // (expr ...) and <expr>
                RuleTagToken ruleTagToken = GetRuleTagToken(r2);
                if (ruleTagToken != null)
                {
                    if (r1.RuleIndex == r2.RuleIndex)
                    {
                        // track label->list-of-nodes for both rule name and label (if any)
                        labels.Map(ruleTagToken.RuleName, tree);
                        if (ruleTagToken.Label != null)
                        {
                            labels.Map(ruleTagToken.Label, tree);
                        }
                    }
                    else
                    {
                        if (mismatchedNode == null)
                        {
                            mismatchedNode = r1;
                        }
                    }
                    return mismatchedNode;
                }
                // (expr ...) and (expr ...)
                if (r1.ChildCount != r2.ChildCount)
                {
                    if (mismatchedNode == null)
                    {
                        mismatchedNode = r1;
                    }
                    return mismatchedNode;
                }
                int n = r1.ChildCount;
                for (int i = 0; i < n; i++)
                {
                    IParseTree childMatch = MatchImpl(r1.GetChild(i), patternTree.GetChild(i), labels);
                    if (childMatch != null)
                    {
                        return childMatch;
                    }
                }
                return mismatchedNode;
            }
            // if nodes aren't both tokens or both rule nodes, can't match
            return tree;
        }
Beispiel #50
0
 public static IEnumerable <IParseTreeToken <TToken, TRule> > TokenNodes <TToken, TRule>(
     this IParseTree <TToken, TRule> node)
 => new[] { node }.TokenNodes();
Beispiel #51
0
 public EmbeddedStatementElementFinder(IParseTree tree, PythonParser parser) : base(tree, parser)
 {
 }
Beispiel #52
0
        public IList <Location> FindDefs(int index, Document doc)
        {
            List <Location> result = new List <Location>();

            if (doc == null)
            {
                return(result);
            }
            var workspace = doc.Workspace;

            IParseTree ref_pt = Util.Find(index, doc);

            if (ref_pt == null)
            {
                return(result);
            }

            ParsingResults ref_pd = ParsingResultsFactory.Create(doc);

            if (ref_pd.ParseTree == null)
            {
                Compile(workspace);
            }

            ref_pd.Attributes.TryGetValue(ref_pt, out IList <Symtab.CombinedScopeSymbol> list_values);
            if (list_values == null)
            {
                return(result);
            }

            foreach (CombinedScopeSymbol value in list_values)
            {
                if (value == null)
                {
                    continue;
                }

                Symtab.ISymbol @ref = value as Symtab.ISymbol;
                if (@ref == null)
                {
                    continue;
                }

                List <Symtab.ISymbol> defs = @ref.resolve();
                if (defs == null)
                {
                    continue;
                }

                foreach (var def in defs)
                {
                    string def_file = def.file;
                    if (def_file == null)
                    {
                        continue;
                    }

                    Document def_item = workspace.FindDocument(def_file);
                    if (def_item == null)
                    {
                        continue;
                    }

                    Location new_loc = new Location()
                    {
                        Range = new Workspaces.Range(def.Token.First().StartIndex, def.Token.First().StopIndex),
                        Uri   = def_item
                    };
                    result.Add(new_loc);
                }
            }
            return(result);
        }
Beispiel #53
0
 private static void _findAllNodes(IParseTree t, int index, bool findTokens, IList<IParseTree> nodes)
 {
     // check this node (the root) first
     if (findTokens && t is ITerminalNode)
     {
         ITerminalNode tnode = (ITerminalNode)t;
         if (tnode.Symbol.Type == index)
         {
             nodes.Add(t);
         }
     }
     else
     {
         if (!findTokens && t is ParserRuleContext)
         {
             ParserRuleContext ctx = (ParserRuleContext)t;
             if (ctx.RuleIndex == index)
             {
                 nodes.Add(t);
             }
         }
     }
     // check children
     for (int i = 0; i < t.ChildCount; i++)
     {
         _findAllNodes(t.GetChild(i), index, findTokens, nodes);
     }
 }
 public override ICollection <IParseTree> Evaluate(IParseTree t)
 {
     return(Trees.FindAllRuleNodes(t, ruleIndex));
 }
Beispiel #55
0
 public static IList<IParseTree> FindAllNodes(IParseTree t, int index, bool findTokens)
 {
     IList<IParseTree> nodes = new List<IParseTree>();
     _findAllNodes(t, index, findTokens, nodes);
     return nodes;
 }
Beispiel #56
0
        private bool TryGetAssociatedTree(out IParseTree associatedTree, out IList <IToken> tokens)
        {
            try
            {
                string sourcePath = _location.GetSourcePath();
                if (!File.Exists(sourcePath))
                {
                    associatedTree = null;
                    tokens         = null;
                    return(false);
                }

                string            text        = File.ReadAllText(sourcePath);
                AntlrInputStream  input       = new AntlrInputStream(text);
                JavaLexer         lexer       = new JavaLexer(new JavaUnicodeStreamV4(input));
                CommonTokenStream tokenStream = new CommonTokenStream(lexer);
                JavaParser        parser      = new JavaParser(tokenStream);

                parser.Interpreter.PredictionMode = PredictionMode.Sll;
                parser.BuildParseTree             = true;
                JavaParser.CompilationUnitContext result = parser.compilationUnit();

                associatedTree = null;
                tokens         = tokenStream.GetTokens();

                AssociatedTreeListener listener = new AssociatedTreeListener(_location, tokens);
                ParseTreeWalker.Default.Walk(listener, result);
                List <IParseTree> potentialTrees = listener.AssociatedTree;

                if (potentialTrees.Count == 1)
                {
                    associatedTree = potentialTrees[0];
                }
                else if (potentialTrees.Count > 1)
                {
                    byte[]             bytecode           = _location.GetMethod().GetBytecodes();
                    DisassembledMethod disassembledMethod = BytecodeDisassembler.Disassemble(bytecode);

                    var constantPool = _location.GetDeclaringType().GetConstantPool();
                    ReadOnlyCollection <ExceptionTableEntry> exceptionTable;
                    try
                    {
                        exceptionTable = _location.GetMethod().GetExceptionTable();
                    }
                    catch (DebuggerException)
                    {
                        exceptionTable = new ReadOnlyCollection <ExceptionTableEntry>(new ExceptionTableEntry[0]);
                    }

                    ImmutableList <int?>           evaluationStackDepths = BytecodeDisassembler.GetEvaluationStackDepths(disassembledMethod, constantPool, exceptionTable);
                    ReadOnlyCollection <ILocation> locations             = _location.GetMethod().GetLineLocations();

                    // find all bytecode offsets with evaluation stack depth 0 on the current line
                    List <int> relevantOffsets = new List <int>();
                    for (int i = 0; i < locations.Count; i++)
                    {
                        if (locations[i].GetLineNumber() != _location.GetLineNumber())
                        {
                            continue;
                        }

                        long offsetLimit = i < locations.Count - 1 ? locations[i + 1].GetCodeIndex() : bytecode.Length;
                        // start with the instruction for this bytecode offset
                        for (int j = GetInstructionAtOffset(disassembledMethod, locations[i].GetCodeIndex());
                             j >= 0 && j < disassembledMethod.Instructions.Count && disassembledMethod.Instructions[j].Offset < offsetLimit;
                             j++)
                        {
                            if (evaluationStackDepths[j] == 0)
                            {
                                // ignore unconditional branches
                                if (disassembledMethod.Instructions[j].OpCode.FlowControl == JavaFlowControl.Branch)
                                {
                                    continue;
                                }

                                relevantOffsets.Add(disassembledMethod.Instructions[j].Offset);
                            }
                        }
                    }

                    if (relevantOffsets.Count == potentialTrees.Count)
                    {
                        // heuristic: assume they appear in the same order as the source code on this line
                        int treeIndex = relevantOffsets.IndexOf((int)_location.GetCodeIndex());
                        if (treeIndex >= 0)
                        {
                            associatedTree = potentialTrees[treeIndex];
                        }
                    }
                }

                if (associatedTree == null)
                {
                    tokens = null;
                    return(false);
                }

                return(true);
            }
            catch (Exception e)
            {
                if (ErrorHandler.IsCriticalException(e))
                {
                    throw;
                }

                associatedTree = null;
                tokens         = null;
                return(false);
            }
        }
Beispiel #57
0
 public static ICollection<IParseTree> FindAllTokenNodes(IParseTree t, int ttype)
 {
     return FindAllNodes(t, ttype, true);
 }
Beispiel #58
0
        public List <ZhangShashaCSharp.Operation> Main(IParseTree t1, IParseTree t2)
        {
            // Convert the parse tree to a graph of rule symbols, then create a tree of uses from start rule.
            var table1 = new Transform.TableOfRules();

            table1.ReadRules(t1);
            table1.FindPartitions();
            table1.FindStartRules();

            var table2 = new Transform.TableOfRules();

            table2.ReadRules(t1);
            table2.FindPartitions();
            table2.FindStartRules();

            Digraph <string> g1 = new Digraph <string>();

            foreach (TableOfRules.Row r in table1.rules)
            {
                if (!r.is_parser_rule)
                {
                    continue;
                }
                g1.AddVertex(r.LHS);
            }
            foreach (TableOfRules.Row r in table1.rules)
            {
                if (!r.is_parser_rule)
                {
                    continue;
                }
                List <string> j = r.RHS;
                //j.Reverse();
                foreach (string rhs in j)
                {
                    TableOfRules.Row sym = table1.rules.Where(t => t.LHS == rhs).FirstOrDefault();
                    if (!sym.is_parser_rule)
                    {
                        continue;
                    }

                    DirectedEdge <string> e = new DirectedEdge <string>(r.LHS, rhs);
                    g1.AddEdge(e);
                }
            }
            List <string> starts1 = new List <string>();

            foreach (TableOfRules.Row r in table1.rules)
            {
                if (r.is_parser_rule && r.is_start)
                {
                    starts1.Add(r.LHS);
                }
            }

            Digraph <string> g2 = new Digraph <string>();

            foreach (TableOfRules.Row r in table2.rules)
            {
                if (!r.is_parser_rule)
                {
                    continue;
                }
                g2.AddVertex(r.LHS);
            }
            foreach (TableOfRules.Row r in table2.rules)
            {
                if (!r.is_parser_rule)
                {
                    continue;
                }
                List <string> j = r.RHS;
                //j.Reverse();
                foreach (string rhs in j)
                {
                    TableOfRules.Row sym = table2.rules.Where(t => t.LHS == rhs).FirstOrDefault();
                    if (!sym.is_parser_rule)
                    {
                        continue;
                    }

                    DirectedEdge <string> e = new DirectedEdge <string>(r.LHS, rhs);
                    g2.AddEdge(e);
                }
            }
            List <string> starts2 = new List <string>();

            foreach (TableOfRules.Row r in table2.rules)
            {
                if (r.is_parser_rule && r.is_start)
                {
                    starts2.Add(r.LHS);
                }
            }

            var map1 = new Dictionary <string, Node>();
            var po1  = Algorithms.Postorder.Sort(g1, starts1).ToList();

            throw new NotImplementedException();
        }
 public void setNodeCommand(IParseTree node, Command value)
 {
     nodeCmd.Put(node, value);
 }
Beispiel #60
0
 public override Expression Visit(IParseTree tree)
 {
     writer($"{System.Reflection.MethodBase.GetCurrentMethod().Name} | {tree.ToStringTree()}");
     return(base.Visit(tree));
 }