Beispiel #1
0
        private string GetErrorLine(JavaCompiler.Parser.Node node)
        {
            StringBuilder errorLine = new StringBuilder();

            node.Traverse(x =>
            {
                if (x.Left == null && x.Right == null)
                {
                    errorLine.Append(x.Value.ToString()).Append(' ');
                }
            });
            return(errorLine.ToString());
        }
Beispiel #2
0
        public IReadOnlyList <Variable> Analyse(JavaCompiler.Parser.Node tree)
        {
            tree.Traverse(node =>
            {
                switch (node.Value.ToString())
                {
                case "block":
                    if (!inFunction)
                    {
                        inFunction = true;
                    }
                    else
                    {
                        blocks.Push(currentBlock);
                        currentBlock = nextBlock;
                        nextBlock++;
                    }
                    break;

                case "r_brace":
                    if (blocks.Count > 0)
                    {
                        currentBlock = blocks.Pop();
                    }
                    else
                    {
                        if (!string.IsNullOrWhiteSpace(funcName))
                        {
                            inFunction = false;
                            funcName   = string.Empty;
                        }
                        else
                        {
                            className = string.Empty;
                        }
                        currentBlock = 0;
                        nextBlock    = 1;
                    }
                    break;

                case "field_declaration":
                    type = string.Empty;
                    if (node.Right.Value.ToString() == "field_declaration")
                    {
                        break;
                    }
                    else
                    {
                        node.Traverse(y =>
                        {
                            switch (y.Value.ToString())
                            {
                            case "declarator":
                                type = y.Left.Value.ToString();
                                break;

                            case "var_name":
                                string name = y.Left.Value.ToString();
                                if (namesTable.Exists(x => x.Name == name && x.Block == Block))
                                {
                                    var errorLine = GetErrorLine(node);
                                    errors.Add(string.Format("Redeclaration of variable {0}:\n{1}", name, errorLine));
                                    return;
                                }
                                namesTable.Add(new Variable(name, type, Block));
                                break;
                            }
                        });
                    }
                    break;

                case "var_declaration":
                    type = string.Empty;
                    node.Traverse(y =>
                    {
                        switch (y.Value.ToString())
                        {
                        case "declarator":
                            type = y.Left.Value.ToString();
                            break;

                        case "var_name":
                            string name = y.Left.Value.ToString();
                            if (namesTable.Exists(x => x.Name == name && x.Block == Block))
                            {
                                var errorLine = GetErrorLine(node);
                                errors.Add(string.Format("Redeclaration of variable {0}:\n{1}", name, errorLine));
                                return;
                            }
                            namesTable.Add(new Variable(name, type, Block));
                            break;
                        }
                    });
                    break;

                case "class_declarator":
                    className = node.Right.Left.Value.ToString();
                    namesTable.Add(new Variable(className, node.Left.Left.Value.ToString(), Block));
                    break;

                case "func_signature":
                    StringBuilder funcSignature = new StringBuilder("function ");
                    node.Traverse(x =>
                    {
                        switch (x.Value.ToString())
                        {
                        case "declarator":
                            funcSignature.Append(x.Left.Value.ToString());
                            if (!string.IsNullOrWhiteSpace(funcName))
                            {
                                funcSignature.Append(' ');
                            }
                            break;

                        case "void_token":
                            funcSignature.Append(x.Left.Value.ToString());
                            break;

                        case "func_name":
                            funcName = x.Left.Value.ToString();
                            funcSignature.Append(' ').Append(funcName).Append(' ');
                            break;
                        }
                    });
                    if (namesTable.Exists(x => x.Name == funcName && x.Block == Block && x.Type == funcSignature.ToString()))
                    {
                        var errorLine = GetErrorLine(node);
                        errors.Add(string.Format("Redeclaration of function {0}:\n{1}", funcName, errorLine));
                        return;
                    }
                    namesTable.Add(new Variable(funcName, funcSignature.ToString(), Block));
                    break;

                case "expression":
                    /*if (node.Right == null)
                     *  if ((node.Left.Value is string) && !namesTable.Exists(x => x.Name == node.Left.Value && x.Block == block))
                     *      errors.Add(string.Format("Undeclared variable {0}.", node.Left.Value));*/
                    break;
                }
            });

            return(namesTable);
        }