Ejemplo n.º 1
0
        public void Basics()
        {
            try{
            var parser = new BVE5RouteFileParser();
            var stmt = parser.ParseOneStatement(@"Sound.Load(sounds.txt);");
            var expected1 = new List<TypeDescriber>{
                TypeDescriber.Create(NodeType.Statement, new List<TypeDescriber>{
                    TypeDescriber.Create(NodeType.Invocation, new List<TypeDescriber>{
                        TypeDescriber.Create(NodeType.MemRef, new List<TypeDescriber>{
                            TypeDescriber.Create(NodeType.Identifier, null),
                            TypeDescriber.Create(NodeType.Identifier, null),
                            TypeDescriber.Create(NodeType.Identifier, null)
                        })
                    })
                })
            };
            Helpers.TestStructualEqual(expected1.GetEnumerator(), stmt);
            }
            catch(TypeLoadException e){
                var asms = AppDomain.CurrentDomain.GetAssemblies();
                foreach(var asm in asms)
                    Console.WriteLine(asm.FullName);

                Console.WriteLine(e.Message);
                Console.WriteLine(e.TypeName);
            }
        }
        void ShiftPositions(int amountShift)
        {
            //TODO: (保留)数式入力による距離程シフトの対応・相対インデックスによる既存距離程の参照機能の追加
            var provider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider;
            if(provider == null)
                return;

            var doc = provider.TextEditor.Document;
            int begin_line = 1;
            int end_line = doc.TotalNumberOfLines;

            if(provider.TextEditor.SelectionLength != 0){
                begin_line = doc.GetLineForOffset(provider.TextEditor.SelectionStart).LineNumber;
                end_line = doc.GetLineForOffset(provider.TextEditor.SelectionStart + provider.TextEditor.SelectionLength).LineNumber;
            }
            LoggingService.Debug(string.Format("Shifting positions started; begin: {0}, end: {1}", begin_line, end_line));

            using(provider.TextEditor.Document.OpenUndoGroup()){	//do the real work
                var content_text = provider.TextEditor.Document.Text;
                var tree = new BVE5RouteFileParser().Parse(content_text, provider.TextEditor.FileName, true);
                var pos_stmts = tree.FindNodes(n => begin_line <= n.StartLocation.Line && n.EndLocation.Line <= end_line &&
                                                   n.Type == NodeType.Statement && ((Statement)n).Expr.Type == NodeType.Literal)
                    .OfType<Statement>().ToArray();

                foreach(var stmt in pos_stmts){
                    var literal_expr = (LiteralExpression)stmt.Expr;
                    int original_value = (int)literal_expr.Value, modified_value = original_value + amountShift;
                    literal_expr.ReplaceWith(new LiteralExpression(modified_value, literal_expr.StartLocation,
                                                                   new TextLocation(literal_expr.EndLocation.Line,
                                                                                    literal_expr.StartLocation.Column + modified_value.ToString().Length)));
                    doc.SmartReplaceLine(doc.GetLine(stmt.StartLocation.Line), stmt.GetText());
                }
            }
        }
 public void Statements()
 {
     var parser = new BVE5RouteFileParser();
     var tree = parser.Parse(@"Track[1].Position(0, 0, 100, 0);
     //This is a comment
     Track[2].Position(5.4, 0, 100, 0);",
                             "<string>");
     var expected4 = new List<TypeDescriber>{
         TypeDescriber.Create(NodeType.Tree, new List<TypeDescriber>{
             TypeDescriber.Create(NodeType.Statement, new List<TypeDescriber>{
                 TypeDescriber.Create(NodeType.Invocation, new List<TypeDescriber>{
                     TypeDescriber.Create(NodeType.MemRef, new List<TypeDescriber>{
                         TypeDescriber.Create(NodeType.Indexer, new List<TypeDescriber>{
                             TypeDescriber.Create(NodeType.Identifier, null),
                             TypeDescriber.Create(NodeType.Literal, null)
                         }),
                         TypeDescriber.Create(NodeType.Identifier, null)
                     }),
                     TypeDescriber.Create(NodeType.Literal, null),	//0
                     TypeDescriber.Create(NodeType.Literal, null),	//0
                     TypeDescriber.Create(NodeType.Literal, null),	//100
                     TypeDescriber.Create(NodeType.Literal, null)	//0
                 })
             }),
             TypeDescriber.Create(NodeType.Statement, new List<TypeDescriber>{
                 TypeDescriber.Create(NodeType.Invocation, new List<TypeDescriber>{
                     TypeDescriber.Create(NodeType.MemRef, new List<TypeDescriber>{
                         TypeDescriber.Create(NodeType.Indexer, new List<TypeDescriber>{
                             TypeDescriber.Create(NodeType.Identifier, null),
                             TypeDescriber.Create(NodeType.Literal, null)
                         }),
                         TypeDescriber.Create(NodeType.Identifier, null)
                     }),
                     TypeDescriber.Create(NodeType.Literal, null),	//5.4
                     TypeDescriber.Create(NodeType.Literal, null),	//0
                     TypeDescriber.Create(NodeType.Literal, null),	//100
                     TypeDescriber.Create(NodeType.Literal, null)	//0
                 })
             })
         })
     };
     Helpers.TestStructualEqual(expected4.GetEnumerator(), tree);
 }
 public void MetaHeader()
 {
     var parser = new BVE5RouteFileParser();
     var tree = parser.Parse(@"BveTs Map 1.00
     Sound.Load(sounds\a.wav);", "<string>", true);
     Assert.IsFalse(parser.HasErrors);
 }
        public void Invalid()
        {
            var parser = new BVE5RouteFileParser();
            var tree = parser.Parse("bvets mip 1.00", "<invalid header>", true);
            Assert.IsTrue(parser.HasErrors && parser.Errors.Count() == 1);
            Assert.IsNull(tree);

            var parser2 = new BVE5RouteFileParser();
            var stmt = parser2.ParseOneStatement("Track[.Position");
            var expected1 = new List<TypeDescriber>{
                TypeDescriber.Create(NodeType.Statement, new List<TypeDescriber>{
                    TypeDescriber.Create(NodeType.MemRef, new List<TypeDescriber>{
                        TypeDescriber.Create(NodeType.Indexer, new List<TypeDescriber>{
                            TypeDescriber.Create(NodeType.Identifier, null)
                        }),
                        TypeDescriber.Create(NodeType.Identifier, null)
                    })
                })
            };
            Helpers.TestStructualEqual(expected1.GetEnumerator(), stmt);
            Assert.IsTrue(parser2.HasErrors && parser2.Errors.Count() == 3);

            var parser3 = new BVE5RouteFileParser();
            var stmt2 = parser3.ParseOneStatement("Track[0].Position(100");
            var expected2 = new List<TypeDescriber>{
                TypeDescriber.Create(NodeType.Statement, new List<TypeDescriber>{
                    TypeDescriber.Create(NodeType.Invocation, new List<TypeDescriber>{
                        TypeDescriber.Create(NodeType.MemRef, new List<TypeDescriber>{
                            TypeDescriber.Create(NodeType.Indexer, new List<TypeDescriber>{
                                TypeDescriber.Create(NodeType.Identifier, null),
                                TypeDescriber.Create(NodeType.Literal, null)
                            }),
                            TypeDescriber.Create(NodeType.Identifier, null)
                        }),
                        TypeDescriber.Create(NodeType.Literal, null)
                    })
                })
            };
            Helpers.TestStructualEqual(expected2.GetEnumerator(), stmt2);
            Assert.IsTrue(parser3.HasErrors && parser3.Errors.Count() == 2);
        }
        public void Basics()
        {
            var parser = new BVE5RouteFileParser();
            var stmt = parser.ParseOneStatement("Sound.Load(sounds.txt);");
            var expected1 = new List<TypeDescriber>{
                TypeDescriber.Create(NodeType.Statement, new List<TypeDescriber>{
                    TypeDescriber.Create(NodeType.Invocation, new List<TypeDescriber>{
                        TypeDescriber.Create(NodeType.MemRef, new List<TypeDescriber>{
                            TypeDescriber.Create(NodeType.Identifier, null),
                            TypeDescriber.Create(NodeType.Identifier, null)
                        }),
                        TypeDescriber.Create(NodeType.Identifier, null)
                    })
                })
            };
            Helpers.TestStructualEqual(expected1.GetEnumerator(), stmt);

            var stmt2 = parser.ParseOneStatement("1000;");
            var expected2 = new List<TypeDescriber>{
                TypeDescriber.Create(NodeType.Statement, new List<TypeDescriber>{
                    TypeDescriber.Create(NodeType.Literal, null)
                })
            };
            Helpers.TestStructualEqual(expected2.GetEnumerator(), stmt2);

            var stmt3 = parser.ParseOneStatement("Track[Rail1].Position(9.7, 0, 300, 0);");
            var expected3 = new List<TypeDescriber>{
                TypeDescriber.Create(NodeType.Statement, new List<TypeDescriber>{
                    TypeDescriber.Create(NodeType.Invocation, new List<TypeDescriber>{
                        TypeDescriber.Create(NodeType.MemRef, new List<TypeDescriber>{
                            TypeDescriber.Create(NodeType.Indexer, new List<TypeDescriber>{
                                TypeDescriber.Create(NodeType.Identifier, null),
                                TypeDescriber.Create(NodeType.Literal, null)
                            }),
                            TypeDescriber.Create(NodeType.Identifier, null)
                        }),
                        TypeDescriber.Create(NodeType.Literal, null),   //9.7
                        TypeDescriber.Create(NodeType.Literal, null),	//0
                        TypeDescriber.Create(NodeType.Literal, null),	//300
                        TypeDescriber.Create(NodeType.Literal, null)	//0
                    })
                })
            };
            Helpers.TestStructualEqual(expected3.GetEnumerator(), stmt3);
        }
        public void Additions()
        {
            var parser = new BVE5RouteFileParser();
            var stmt = parser.ParseOneStatement("let a = 1;");
            var expected1 = new List<TypeDescriber>{
                TypeDescriber.Create(NodeType.Statement, new List<TypeDescriber>{
                    TypeDescriber.Create(NodeType.Definition, new List<TypeDescriber>{
                        TypeDescriber.Create(NodeType.Identifier, null),
                        TypeDescriber.Create(NodeType.Literal, null)
                    })
                })
            };
            Helpers.TestStructualEqual(expected1.GetEnumerator(), stmt);

            var stmt2 = parser.ParseOneStatement("let ‰½‚© = 1;");
            var expected2 = new List<TypeDescriber>{
                TypeDescriber.Create(NodeType.Statement, new List<TypeDescriber>{
                    TypeDescriber.Create(NodeType.Definition, new List<TypeDescriber>{
                        TypeDescriber.Create(NodeType.Identifier, null),
                        TypeDescriber.Create(NodeType.Literal, null)
                    })
                })
            };
            Helpers.TestStructualEqual(expected2.GetEnumerator(), stmt2);
        }