User Guide
lexer.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 <string>
9
10
#include <catch2/catch_test_macros.hpp>
11
12
#include "
lexer/unit_lexer.hpp
"
13
#include "
parser/unit_driver.hpp
"
14
15
using namespace
nmodl
;
16
17
using
nmodl::parser::UnitDriver
;
18
using
nmodl::parser::UnitLexer
;
19
using
parser::UnitParser;
20
using
Token
= UnitParser::token;
21
using
TokenType
=
UnitParser::token_type
;
22
using
SymbolType
=
UnitParser::symbol_type
;
23
24
/// retrieve token type from lexer and check if it's of given type
25
bool
check_token_type
(
const
std::string& name,
TokenType
type) {
26
std::istringstream ss(name);
27
std::istream& in = ss;
28
29
UnitDriver
driver
;
30
UnitLexer
scanner(
driver
, &in);
31
auto
symbol = scanner.
next_token
();
32
return
symbol.type_get() == UnitParser::by_type(type).type_get();
33
}
34
35
TEST_CASE
(
"Unit Lexer tests for valid tokens"
,
"[lexer][unit]"
) {
36
SECTION(
"Tests for comments"
) {
37
REQUIRE(
check_token_type
(
"/ comment"
, Token::COMMENT));
38
REQUIRE(
check_token_type
(
"/comment"
, Token::COMMENT));
39
}
40
41
SECTION(
"Tests for doubles"
) {
42
REQUIRE(
check_token_type
(
"27.52"
, Token::DOUBLE));
43
REQUIRE(
check_token_type
(
"1.01325+5"
, Token::DOUBLE));
44
REQUIRE(
check_token_type
(
"1"
, Token::DOUBLE));
45
REQUIRE(
check_token_type
(
"-1.324e+10"
, Token::DOUBLE));
46
REQUIRE(
check_token_type
(
"1-1"
, Token::DOUBLE));
47
REQUIRE(
check_token_type
(
"|"
, Token::FRACTION));
48
REQUIRE(
check_token_type
(
".03"
, Token::DOUBLE));
49
REQUIRE(
check_token_type
(
"12345e-2"
, Token::DOUBLE));
50
}
51
52
SECTION(
"Tests for units"
) {
53
REQUIRE(
check_token_type
(
"*a*"
, Token::BASE_UNIT));
54
REQUIRE(
check_token_type
(
"*k*"
, Token::INVALID_BASE_UNIT));
55
REQUIRE(
check_token_type
(
"planck"
, Token::NEW_UNIT));
56
REQUIRE(
check_token_type
(
"mse-1"
, Token::NEW_UNIT));
57
REQUIRE(
check_token_type
(
"mA/cm2"
, Token::NEW_UNIT));
58
REQUIRE(
check_token_type
(
" m2"
, Token::UNIT_POWER));
59
REQUIRE(
check_token_type
(
" m"
, Token::UNIT));
60
REQUIRE(
check_token_type
(
" m_2"
, Token::UNIT));
61
REQUIRE(
check_token_type
(
" m_unit2"
, Token::UNIT));
62
REQUIRE(
check_token_type
(
"yotta-"
, Token::PREFIX));
63
}
64
65
SECTION(
"Tests for special characters"
) {
66
REQUIRE(
check_token_type
(
"-"
, Token::UNIT_PROD));
67
REQUIRE(
check_token_type
(
" / "
, Token::DIVISION));
68
REQUIRE(
check_token_type
(
"\n"
, Token::NEWLINE));
69
}
70
}
nmodl::TokenType
parser::NmodlParser::token_type TokenType
Definition:
main_nmodl.cpp:35
check_token_type
bool check_token_type(const std::string &name, TokenType type)
retrieve token type from lexer and check if it's of given type
Definition:
lexer.cpp:25
nmodl
encapsulates code generation backend implementations
Definition:
ast_common.hpp:26
unit_driver.hpp
driver
nmodl::parser::UnitDriver driver
Definition:
parser.cpp:28
TEST_CASE
TEST_CASE("Unit Lexer tests for valid tokens", "[lexer][unit]")
Definition:
lexer.cpp:35
unit_lexer.hpp
Token
parser::CParser::token Token
Definition:
main_c.cpp:28
nmodl::parser::UnitLexer::next_token
virtual UnitParser::symbol_type next_token()
Function for lexer to scan token (replacement for yylex())
nmodl::SymbolType
parser::NmodlParser::symbol_type SymbolType
Definition:
main_nmodl.cpp:33
symbol_type
void symbol_type(const std::string &name, T &value)
Definition:
modtoken.cpp:32
nmodl::parser::UnitLexer
Represent Lexer/Scanner class for Units parsing.
Definition:
unit_lexer.hpp:49
nmodl::parser::UnitDriver
Class that binds all pieces together for parsing C units.
Definition:
unit_driver.hpp:39
nmodl::token_type
TokenType token_type(const std::string &name)
Return token type for given token name.
Definition:
token_mapping.cpp:284
test
unit
units
lexer.cpp