def setUp(self): Logger.init_logger(LoggingLevel.INFO) SymbolTable.initialize_symbol_table(ASTSourceLocation(start_line=0, start_column=0, end_line=0, end_column=0)) PredefinedUnits.register_units() PredefinedTypes.register_types() PredefinedVariables.register_variables() PredefinedFunctions.register_functions()
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) # create a token stream stream = CommonTokenStream(lexer) stream.fill() # parse the file parser = PyNestMLParser(stream) # 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
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) # create a token stream stream = CommonTokenStream(lexer) stream.fill() # parse the file parser = PyNestMLParser(stream) # 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
def update_symbol_table(self, neuron, kernel_buffers): """ Update symbol table and scope. """ SymbolTable.delete_neuron_scope(neuron.get_name()) symbol_table_visitor = ASTSymbolTableVisitor() symbol_table_visitor.after_ast_rewrite_ = True neuron.accept(symbol_table_visitor) SymbolTable.add_neuron_scope(neuron.get_name(), neuron.get_scope())
def setUp(self): PredefinedUnits.register_units() PredefinedTypes.register_types() PredefinedFunctions.register_functions() PredefinedVariables.register_variables() SymbolTable.initialize_symbol_table(ASTSourceLocation(start_line=0, start_column=0, end_line=0, end_column=0)) Logger.init_logger(LoggingLevel.INFO) self.target_path = str(os.path.realpath(os.path.join(os.path.dirname(__file__), os.path.join( os.pardir, 'target'))))
def setUp(self) -> None: PredefinedUnits.register_units() PredefinedTypes.register_types() PredefinedFunctions.register_functions() PredefinedVariables.register_variables() SymbolTable.initialize_symbol_table(ASTSourceLocation(start_line=0, start_column=0, end_line=0, end_column=0)) Logger.init_logger(LoggingLevel.INFO) self.target_path = str(os.path.realpath(os.path.join(os.path.dirname(__file__), os.path.join(os.pardir, 'target'))))
from pynestml.generated.PyNestMLLexer import PyNestMLLexer from pynestml.generated.PyNestMLParser import PyNestMLParser from pynestml.symbol_table.symbol_table import SymbolTable from pynestml.symbols.predefined_functions import PredefinedFunctions from pynestml.symbols.predefined_types import PredefinedTypes from pynestml.symbols.predefined_units import PredefinedUnits from pynestml.symbols.predefined_variables import PredefinedVariables from pynestml.utils.logger import LoggingLevel, Logger from pynestml.visitors.ast_builder_visitor import ASTBuilderVisitor # setups the infrastructure PredefinedUnits.register_units() PredefinedTypes.register_types() PredefinedFunctions.register_functions() PredefinedVariables.register_variables() SymbolTable.initialize_symbol_table( ASTSourceLocation(start_line=0, start_column=0, end_line=0, end_column=0)) Logger.init_logger(LoggingLevel.INFO) class ASTBuildingTest(unittest.TestCase): 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)))
from pynestml.meta_model.ast_source_location import ASTSourceLocation from pynestml.symbol_table.symbol_table import SymbolTable from pynestml.symbols.predefined_functions import PredefinedFunctions from pynestml.symbols.predefined_types import PredefinedTypes from pynestml.symbols.predefined_units import PredefinedUnits from pynestml.symbols.predefined_variables import PredefinedVariables from pynestml.utils.ast_nestml_printer import ASTNestMLPrinter from pynestml.utils.logger import LoggingLevel, Logger from pynestml.utils.model_parser import ModelParser # setups the infrastructure PredefinedUnits.register_units() PredefinedTypes.register_types() PredefinedFunctions.register_functions() PredefinedVariables.register_variables() SymbolTable.initialize_symbol_table(ASTSourceLocation(start_line=0, start_column=0, end_line=0, end_column=0)) Logger.init_logger(LoggingLevel.INFO) class NestMLPrinterTest(unittest.TestCase): """ Tests if the NestML printer works as intended. """ def test_block_with_variables_with_comments(self): block = '\n' \ '/* pre1\n' \ '* pre2\n' \ '*/\n' \ 'state: # in\n' \ 'end\n' \
def parse_model(cls, file_path=None): """ Parses a handed over model and returns the meta_model representation of it. :param file_path: the path to the file which shall be parsed. :type file_path: str :return: a new ASTNESTMLCompilationUnit object. :rtype: ASTNestMLCompilationUnit """ try: input_file = FileStream(file_path) except IOError: code, message = Messages.get_input_path_not_found(path=file_path) Logger.log_message(node=None, code=None, message=message, error_position=None, log_level=LoggingLevel.ERROR) return code, message = Messages.get_start_processing_file(file_path) Logger.log_message(node=None, code=code, message=message, error_position=None, log_level=LoggingLevel.INFO) # create a lexer and hand over the input lexer = PyNestMLLexer() lexer.removeErrorListeners() lexer.addErrorListener(ConsoleErrorListener()) lexerErrorListener = NestMLErrorListener() lexer.addErrorListener(lexerErrorListener) # lexer._errHandler = BailErrorStrategy() # N.B. uncomment this line and the next to halt immediately on lexer errors # lexer._errHandler.reset(lexer) lexer.inputStream = input_file # create a token stream stream = CommonTokenStream(lexer) stream.fill() if lexerErrorListener._error_occurred: code, message = Messages.get_lexer_error() Logger.log_message(node=None, code=None, message=message, error_position=None, log_level=LoggingLevel.ERROR) return # parse the file parser = PyNestMLParser(None) parser.removeErrorListeners() parser.addErrorListener(ConsoleErrorListener()) parserErrorListener = NestMLErrorListener() parser.addErrorListener(parserErrorListener) # parser._errHandler = BailErrorStrategy() # N.B. uncomment this line and the next to halt immediately on parse errors # parser._errHandler.reset(parser) parser.setTokenStream(stream) compilation_unit = parser.nestMLCompilationUnit() if parserErrorListener._error_occurred: code, message = Messages.get_parser_error() Logger.log_message(node=None, code=None, message=message, error_position=None, log_level=LoggingLevel.ERROR) return # create a new visitor and return the new AST ast_builder_visitor = ASTBuilderVisitor(stream.tokens) ast = ast_builder_visitor.visit(compilation_unit) # create and update the corresponding symbol tables SymbolTable.initialize_symbol_table(ast.get_source_position()) for neuron in ast.get_neuron_list(): neuron.accept(ASTSymbolTableVisitor()) SymbolTable.add_neuron_scope(neuron.get_name(), neuron.get_scope()) # store source paths for neuron in ast.get_neuron_list(): neuron.file_path = file_path ast.file_path = file_path return ast
def parse_model(cls, file_path=None): """ Parses a handed over model and returns the meta_model representation of it. :param file_path: the path to the file which shall be parsed. :type file_path: str :return: a new ASTNESTMLCompilationUnit object. :rtype: ASTNestMLCompilationUnit """ try: input_file = FileStream(file_path) except IOError: code, message = Messages.get_input_path_not_found(path=file_path) Logger.log_message(neuron=None, code=None, message=message, error_position=None, log_level=LoggingLevel.ERROR) return code, message = Messages.get_start_processing_file(file_path) Logger.log_message(neuron=None, code=code, message=message, error_position=None, log_level=LoggingLevel.INFO) # create a lexer and hand over the input lexer = PyNestMLLexer() lexer.removeErrorListeners() lexer.addErrorListener(ConsoleErrorListener()) lexerErrorListener = NestMLErrorListener() lexer.addErrorListener(lexerErrorListener) # lexer._errHandler = BailErrorStrategy() # N.B. uncomment this line and the next to halt immediately on lexer errors # lexer._errHandler.reset(lexer) lexer.inputStream = input_file # create a token stream stream = CommonTokenStream(lexer) stream.fill() if lexerErrorListener._error_occurred: code, message = Messages.get_lexer_error() Logger.log_message(neuron=None, code=None, message=message, error_position=None, log_level=LoggingLevel.ERROR) return # parse the file parser = PyNestMLParser(None) parser.removeErrorListeners() parser.addErrorListener(ConsoleErrorListener()) parserErrorListener = NestMLErrorListener() parser.addErrorListener(parserErrorListener) # parser._errHandler = BailErrorStrategy() # N.B. uncomment this line and the next to halt immediately on parse errors # parser._errHandler.reset(parser) parser.setTokenStream(stream) compilation_unit = parser.nestMLCompilationUnit() if parserErrorListener._error_occurred: code, message = Messages.get_parser_error() Logger.log_message(neuron=None, code=None, message=message, error_position=None, log_level=LoggingLevel.ERROR) return # create a new visitor and return the new AST ast_builder_visitor = ASTBuilderVisitor(stream.tokens) ast = ast_builder_visitor.visit(compilation_unit) # create and update the corresponding symbol tables SymbolTable.initialize_symbol_table(ast.get_source_position()) log_to_restore = copy.deepcopy(Logger.get_log()) counter = Logger.curr_message # replace all derived variables through a computer processable names: e.g. g_in''' -> g_in__ddd restore_differential_order = [] for ode in ASTUtils.get_all(ast, ASTOdeEquation): lhs_variable = ode.get_lhs() if lhs_variable.get_differential_order() > 0: lhs_variable.differential_order = lhs_variable.get_differential_order() - 1 restore_differential_order.append(lhs_variable) for shape in ASTUtils.get_all(ast, ASTOdeShape): lhs_variable = shape.get_variable() if lhs_variable.get_differential_order() > 0: lhs_variable.differential_order = lhs_variable.get_differential_order() - 1 restore_differential_order.append(lhs_variable) # than replace remaining variables for variable in ASTUtils.get_all(ast, ASTVariable): if variable.get_differential_order() > 0: variable.set_name(variable.get_name() + "__" + "d" * variable.get_differential_order()) variable.differential_order = 0 # now also equations have no ' at lhs. replace every occurrence of last d to ' to compensate for ode_variable in restore_differential_order: ode_variable.differential_order = 1 Logger.set_log(log_to_restore, counter) for neuron in ast.get_neuron_list(): neuron.accept(ASTSymbolTableVisitor()) SymbolTable.add_neuron_scope(neuron.get_name(), neuron.get_scope()) return ast