Ejemplo n.º 1
0
    def _test_single_input_path(cls, input_path):
        print('Start creating AST for ' + input_path + ' ...'),
        input_file = FileStream(input_path)
        lexer = PyNestMLLexer(input_file)
        lexer._errHandler = BailErrorStrategy()
        lexer._errHandler.reset(lexer)

        # create a token stream
        stream = CommonTokenStream(lexer)
        stream.fill()

        # parse the file
        parser = PyNestMLParser(stream)
        parser._errHandler = BailErrorStrategy()
        parser._errHandler.reset(parser)

        # process the comments
        compilation_unit = parser.nestMLCompilationUnit()

        # now build the meta_model
        ast_builder_visitor = ASTBuilderVisitor(stream.tokens)
        ast = ast_builder_visitor.visit(compilation_unit)
        assert isinstance(ast, ASTNestMLCompilationUnit)

        # now, do the actual test for clone()
        ast_copy = ast.clone()
        assert str(ast) == str(ast_copy)
        ast.get_neuron_list()[0].name = "foo"
        ast_copy.get_neuron_list()[0].name = "bar"
        assert str(ast) != str(ast_copy)
        ast_copy.get_neuron_list()[0].name = "foo"
        assert str(ast) == str(ast_copy)
Ejemplo n.º 2
0
    def test(self):
        for filename in os.listdir(
                os.path.realpath(
                    os.path.join(os.path.dirname(__file__),
                                 os.path.join('..', 'models')))):
            if filename.endswith(".nestml"):
                print('Start creating AST for ' + filename + ' ...'),
                input_file = FileStream(
                    os.path.join(
                        os.path.dirname(__file__),
                        os.path.join(os.path.join('..', 'models'), filename)))
                lexer = PyNestMLLexer(input_file)
                lexer._errHandler = BailErrorStrategy()
                lexer._errHandler.reset(lexer)

                # create a token stream
                stream = CommonTokenStream(lexer)
                stream.fill()

                # parse the file
                parser = PyNestMLParser(stream)
                parser._errHandler = BailErrorStrategy()
                parser._errHandler.reset(parser)

                compilation_unit = parser.nestMLCompilationUnit()

                # now build the meta_model
                ast_builder_visitor = ASTBuilderVisitor(stream.tokens)
                ast = ast_builder_visitor.visit(compilation_unit)
                assert isinstance(ast, ASTNestMLCompilationUnit)
Ejemplo n.º 3
0
    def test(self):
        # print('Start creating AST for ' + filename + ' ...'),
        input_file = FileStream(
            os.path.join(
                os.path.realpath(
                    os.path.join(os.path.dirname(__file__), 'resources')),
                'CommentTest.nestml'))
        lexer = PyNestMLLexer(input_file)
        lexer._errHandler = BailErrorStrategy()
        lexer._errHandler.reset(lexer)

        # create a token stream
        stream = CommonTokenStream(lexer)
        stream.fill()

        # parse the file
        parser = PyNestMLParser(stream)
        parser._errHandler = BailErrorStrategy()
        parser._errHandler.reset(parser)

        # process the comments
        compilation_unit = parser.nestMLCompilationUnit()

        # now build the meta_model
        ast_builder_visitor = ASTBuilderVisitor(stream.tokens)
        ast = ast_builder_visitor.visit(compilation_unit)
        neuron_body_elements = ast.get_neuron_list()[0].get_body(
        ).get_body_elements()

        # check if init values comment is correctly detected
        assert (neuron_body_elements[0].get_comment()[0] == 'state comment ok')

        # check that all declaration comments are detected
        comments = neuron_body_elements[0].get_declarations()[0].get_comment()
        assert (comments[0] == 'pre comment 1 ok')
        assert (comments[1] == 'pre comment 2 ok')
        assert (comments[2] == 'inline comment ok')
        assert (comments[3] == 'post comment 1 ok')
        assert (comments[4] == 'post comment 2 ok')
        assert ('pre comment not ok' not in comments)
        assert ('post comment not ok' not in comments)

        # check that equation block comment is detected
        self.assertEqual(neuron_body_elements[1].get_comment()[0],
                         'equations comment ok')
        # check that parameters block comment is detected
        self.assertEqual(neuron_body_elements[2].get_comment()[0],
                         'parameters comment ok')
        # check that internals block comment is detected
        self.assertEqual(neuron_body_elements[3].get_comment()[0],
                         'internals comment ok')
        # check that input comment is detected
        self.assertEqual(neuron_body_elements[4].get_comment()[0],
                         'input comment ok')
        # check that output comment is detected
        self.assertEqual(neuron_body_elements[5].get_comment()[0],
                         'output comment ok')
        # check that update comment is detected
        self.assertEqual(neuron_body_elements[6].get_comment()[0],
                         'update comment ok')
Ejemplo n.º 4
0
    def test(self):
        input_file = FileStream(
            os.path.join(os.path.realpath(os.path.join(os.path.dirname(__file__), 'resources')),
                         'ExpressionCollection.nestml'))
        lexer = PyNestMLLexer(input_file)
        lexer._errHandler = BailErrorStrategy()
        lexer._errHandler.reset(lexer)

        # create a token stream
        stream = CommonTokenStream(lexer)
        stream.fill()

        # parse the file
        parser = PyNestMLParser(stream)
        parser._errHandler = BailErrorStrategy()
        parser._errHandler.reset(parser)
        compilation_unit = parser.nestMLCompilationUnit()
        assert compilation_unit is not None

        ast_builder_visitor = ASTBuilderVisitor(stream.tokens)
        ast = ast_builder_visitor.visit(compilation_unit)
        self.assertTrue(isinstance(ast, ASTNestMLCompilationUnit))
Ejemplo n.º 5
0
    def test(self):
        for filename in os.listdir(
                os.path.realpath(
                    os.path.join(os.path.dirname(__file__),
                                 os.path.join('..', 'models')))):
            if filename.endswith(".nestml"):
                input_file = FileStream(
                    os.path.join(
                        os.path.dirname(__file__),
                        os.path.join(os.path.join('..', 'models'), filename)))
                lexer = PyNestMLLexer(input_file)
                lexer._errHandler = BailErrorStrategy()
                lexer._errHandler.reset(lexer)

                # create a token stream
                stream = CommonTokenStream(lexer)
                stream.fill()

                # parse the file
                parser = PyNestMLParser(stream)
                parser._errHandler = BailErrorStrategy()
                parser._errHandler.reset(parser)

                # process the comments
                compilation_unit = parser.nestMLCompilationUnit()

                # create a new visitor and return the new AST
                ast_builder_visitor = ASTBuilderVisitor(stream.tokens)
                ast = ast_builder_visitor.visit(compilation_unit)

                # update the corresponding symbol tables
                SymbolTable.initialize_symbol_table(ast.get_source_position())
                symbol_table_visitor = ASTSymbolTableVisitor()
                for neuron in ast.get_neuron_list():
                    neuron.accept(symbol_table_visitor)
                    SymbolTable.add_neuron_scope(name=neuron.get_name(),
                                                 scope=neuron.get_scope())
                self.assertTrue(isinstance(ast, ASTNestMLCompilationUnit))
        return
Ejemplo n.º 6
0
    def test(self):
        # print('Start special block parsing and AST-building test...'),
        input_file = FileStream(
            os.path.join(os.path.join(os.path.realpath(os.path.join(os.path.dirname(__file__), 'resources')),
                                      'BlockTest.nestml')))
        lexer = PyNestMLLexer(input_file)
        lexer._errHandler = BailErrorStrategy()
        lexer._errHandler.reset(lexer)

        # create a token stream
        stream = CommonTokenStream(lexer)
        stream.fill()

        # parse the file
        parser = PyNestMLParser(stream)
        parser._errHandler = BailErrorStrategy()
        parser._errHandler.reset(parser)

        compilation_unit = parser.nestMLCompilationUnit()
        ast_builder_visitor = ASTBuilderVisitor(stream.tokens)
        ast = ast_builder_visitor.visit(compilation_unit)
        self.assertTrue(isinstance(ast, ASTNestMLCompilationUnit))
Ejemplo n.º 7
0
    def test(self):
        model_files = []
        for dir in ['models',
                    os.path.join('tests', 'nest_tests', 'resources'),
                    os.path.join('tests', 'valid')]:
            model_files += glob.glob(os.path.realpath(os.path.join(os.path.dirname(__file__), os.path.join('..', dir, '*.nestml'))))

        assert len(model_files) > 0

        for filename in model_files:
            print("Processing " + os.path.basename(filename))
            input_file = FileStream(filename)
            lexer = PyNestMLLexer(input_file)
            lexer._errHandler = BailErrorStrategy()
            lexer._errHandler.reset(lexer)
            # create a token stream
            stream = CommonTokenStream(lexer)
            stream.fill()
            # parse the file
            parser = PyNestMLParser(stream)
            parser._errHandler = BailErrorStrategy()
            parser._errHandler.reset(parser)
            compilation_unit = parser.nestMLCompilationUnit()
            assert compilation_unit is not None