User Guide
lookup.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <catch2/catch_test_macros.hpp>
9 
10 #include "ast/program.hpp"
11 #include "parser/nmodl_driver.hpp"
14 
15 using namespace nmodl;
16 using namespace visitor;
17 using namespace test_utils;
18 
19 using ast::AstNodeType;
21 
22 //=============================================================================
23 // Ast lookup visitor tests
24 //=============================================================================
25 
26 SCENARIO("Searching for ast nodes using AstLookupVisitor", "[visitor][lookup]") {
27  auto to_ast = [](const std::string& text) {
29  return driver.parse_string(text);
30  };
31 
32  GIVEN("A mod file with nodes of type NEURON, RANGE, BinaryExpression") {
33  std::string nmodl_text = R"(
34  NEURON {
35  RANGE tau, h
36  }
37 
38  DERIVATIVE states {
39  tau = 11.1
40  exp(tau)
41  h' = h + 2
42  }
43 
44  : My comment here
45  )";
46 
47  auto ast = to_ast(nmodl_text);
48 
49  WHEN("Looking for existing nodes") {
50  THEN("Can find RANGE variables") {
51  const auto& result = collect_nodes(*ast, {AstNodeType::RANGE_VAR});
52  REQUIRE(result.size() == 2);
53  REQUIRE(to_nmodl(result[0]) == "tau");
54  REQUIRE(to_nmodl(result[1]) == "h");
55  }
56 
57  THEN("Can find NEURON block") {
58  const auto& nodes = collect_nodes(*ast, {AstNodeType::NEURON_BLOCK});
59  REQUIRE(nodes.size() == 1);
60 
61  const std::string neuron_block = R"(
62  NEURON {
63  RANGE tau, h
64  })";
65  const auto& result = reindent_text(to_nmodl(nodes[0]));
66  const auto& expected = reindent_text(neuron_block);
67  REQUIRE(result == expected);
68  }
69 
70  THEN("Can find Binary Expressions and function call") {
71  const auto& result =
72  collect_nodes(*ast,
73  {AstNodeType::BINARY_EXPRESSION, AstNodeType::FUNCTION_CALL});
74  REQUIRE(result.size() == 4);
75  }
76  }
77 
78  WHEN("Looking for missing nodes") {
79  THEN("Can not find BREAKPOINT block") {
80  const auto& result = collect_nodes(*ast, {AstNodeType::BREAKPOINT_BLOCK});
81  REQUIRE(result.empty());
82  }
83  }
84  }
85 }
test_utils.hpp
nmodl::parser::NmodlDriver
Class that binds all pieces together for parsing nmodl file.
Definition: nmodl_driver.hpp:67
nmodl::to_nmodl
std::string to_nmodl(const ast::Ast &node, const std::set< ast::AstNodeType > &exclude_types)
Given AST node, return the NMODL string representation.
Definition: visitor_utils.cpp:234
nmodl::test_utils::reindent_text
std::string reindent_text(const std::string &text, int indent_level)
Reindent nmodl text for text-to-text comparison.
Definition: test_utils.cpp:53
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::ast::AstNodeType
AstNodeType
Enum type for every AST node type.
Definition: ast_decl.hpp:166
visitor_utils.hpp
Utility functions for visitors implementation.
program.hpp
Auto generated AST classes declaration.
driver
nmodl::parser::UnitDriver driver
Definition: parser.cpp:28
SCENARIO
SCENARIO("Searching for ast nodes using AstLookupVisitor", "[visitor][lookup]")
Definition: lookup.cpp:26
nmodl::parser::UnitDriver::parse_string
bool parse_string(const std::string &input)
parser Units provided as string (used for testing)
Definition: unit_driver.cpp:40
nmodl::collect_nodes
std::vector< std::shared_ptr< const ast::Ast > > collect_nodes(const ast::Ast &node, const std::vector< ast::AstNodeType > &types)
traverse node recursively and collect nodes of given types
Definition: visitor_utils.cpp:206
nmodl_driver.hpp