File: lexer.cpp

package info (click to toggle)
nmodl 0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,992 kB
  • sloc: cpp: 28,492; javascript: 9,841; yacc: 2,804; python: 1,967; lex: 1,674; xml: 181; sh: 136; ansic: 37; makefile: 18; pascal: 7
file content (70 lines) | stat: -rw-r--r-- 2,695 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*************************************************************************
 * Copyright (C) 2018-2022 Blue Brain Project
 *
 * This file is part of NMODL distributed under the terms of the GNU
 * Lesser General Public License. See top-level LICENSE file for details.
 *************************************************************************/

#include <string>

#include <catch2/catch_test_macros.hpp>

#include "lexer/unit_lexer.hpp"
#include "parser/unit_driver.hpp"

using namespace nmodl;

using nmodl::parser::UnitDriver;
using nmodl::parser::UnitLexer;
using parser::UnitParser;
using Token = UnitParser::token;
using TokenType = UnitParser::token_type;
using SymbolType = UnitParser::symbol_type;

/// retrieve token type from lexer and check if it's of given type
bool check_token_type(const std::string& name, TokenType type) {
    std::istringstream ss(name);
    std::istream& in = ss;

    UnitDriver driver;
    UnitLexer scanner(driver, &in);
    auto symbol = scanner.next_token();
    return symbol.type_get() == UnitParser::by_type(type).type_get();
}

TEST_CASE("Unit Lexer tests for valid tokens", "[lexer][unit]") {
    SECTION("Tests for comments") {
        REQUIRE(check_token_type("/ comment", Token::COMMENT));
        REQUIRE(check_token_type("/comment", Token::COMMENT));
    }

    SECTION("Tests for doubles") {
        REQUIRE(check_token_type("27.52", Token::DOUBLE));
        REQUIRE(check_token_type("1.01325+5", Token::DOUBLE));
        REQUIRE(check_token_type("1", Token::DOUBLE));
        REQUIRE(check_token_type("-1.324e+10", Token::DOUBLE));
        REQUIRE(check_token_type("1-1", Token::DOUBLE));
        REQUIRE(check_token_type("|", Token::FRACTION));
        REQUIRE(check_token_type(".03", Token::DOUBLE));
        REQUIRE(check_token_type("12345e-2", Token::DOUBLE));
    }

    SECTION("Tests for units") {
        REQUIRE(check_token_type("*a*", Token::BASE_UNIT));
        REQUIRE(check_token_type("*k*", Token::INVALID_BASE_UNIT));
        REQUIRE(check_token_type("planck", Token::NEW_UNIT));
        REQUIRE(check_token_type("mse-1", Token::NEW_UNIT));
        REQUIRE(check_token_type("mA/cm2", Token::NEW_UNIT));
        REQUIRE(check_token_type(" m2", Token::UNIT_POWER));
        REQUIRE(check_token_type(" m", Token::UNIT));
        REQUIRE(check_token_type(" m_2", Token::UNIT));
        REQUIRE(check_token_type(" m_unit2", Token::UNIT));
        REQUIRE(check_token_type("yotta-", Token::PREFIX));
    }

    SECTION("Tests for special characters") {
        REQUIRE(check_token_type("-", Token::UNIT_PROD));
        REQUIRE(check_token_type(" / ", Token::DIVISION));
        REQUIRE(check_token_type("\n", Token::NEWLINE));
    }
}