User Guide
modtoken.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 "lexer/modtoken.hpp"
9 
10 namespace nmodl {
11 
12 using LocationType = nmodl::parser::location;
13 
14 std::string ModToken::position() const {
15  std::stringstream ss;
16  if (external) {
17  ss << "EXTERNAL";
18  } else if (start_line() == 0) {
19  ss << "UNKNOWN";
20  } else {
21  ss << pos;
22  }
23  return ss.str();
24 }
25 
26 std::ostream& operator<<(std::ostream& stream, const ModToken& mt) {
27  // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
28  stream << std::setw(15) << mt.name << " at [" << mt.position() << "]";
29  return stream << " type " << mt.token;
30 }
31 
32 ModToken operator+(ModToken const& adder1, ModToken const& adder2) {
33  LocationType sum_pos = adder1.pos + adder2.pos;
34  ModToken sum(adder1.name, adder1.token, sum_pos);
35 
36  return sum;
37 }
38 
39 } // namespace nmodl
nmodl::ModToken::token
int token
token value returned by lexer
Definition: modtoken.hpp:58
nmodl::LocationType
nmodl::parser::location LocationType
Definition: modtoken.cpp:12
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::ModToken::name
std::string name
name of the token
Definition: modtoken.hpp:55
nmodl::ModToken::position
std::string position() const
Return position of the token as string.
Definition: modtoken.cpp:14
nmodl::ModToken::external
bool external
true if token is externally defined variable (e.g. t, dt in NEURON)
Definition: modtoken.hpp:64
nmodl::operator+
ModToken operator+(ModToken const &adder1, ModToken const &adder2)
Definition: modtoken.cpp:32
nmodl::ModToken::pos
LocationType pos
position of token in the mod file
Definition: modtoken.hpp:61
nmodl::operator<<
std::ostream & operator<<(std::ostream &stream, const ModToken &mt)
Definition: modtoken.cpp:26
nmodl::ModToken::start_line
int start_line() const
Return line number where token started in the mod file.
Definition: modtoken.hpp:100
modtoken.hpp
nmodl::ModToken
Represent token returned by scanner.
Definition: modtoken.hpp:50