forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
numbers.h
64 lines (52 loc) · 2.05 KB
/
numbers.h
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
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef VERIBLE_VERILOG_CST_NUMBERS_H_
#define VERIBLE_VERILOG_CST_NUMBERS_H_
#include <iosfwd>
#include <string>
#include "absl/strings/string_view.h"
namespace verilog {
namespace analysis {
// BasedNumber subdivides the information in a TK_BasedNumber token
// from verilog.lex.
// Generally, this has the form: \'[sS]?{base}[ ]*{digits}
// See verilog.lex for more details.
//
// Examples:
// 'b111 is (binary, unsigned, '111')
// 'h aaaa_5555 is (hex, unsigned, 'aaaa5555'
struct BasedNumber {
// Numeric base, one of [bdho].
char base;
// True if number was annotated as signed, otherwise is unsigned.
bool signedness;
// This is the literal value with underscores removed.
std::string literal;
// False if there is an error parsing a BasedNumber.
bool ok;
// Construct parses a literal string based on
// '{Dec|Bin|Oct|Hex}{Base|Digits}' from verilog.lex.
// base_sign is lexed as one token, e.g. 'b, 'sb (signed).
BasedNumber(absl::string_view base_sign, absl::string_view digits);
BasedNumber(char base_, bool sign_, absl::string_view text)
: base(base_), signedness(sign_), literal(text), ok(true) {}
bool operator==(const BasedNumber& rhs) const {
return base == rhs.base && signedness == rhs.signedness &&
literal == rhs.literal && ok && rhs.ok;
}
};
std::ostream& operator<<(std::ostream& stream, const BasedNumber& number);
} // namespace analysis
} // namespace verilog
#endif // VERIBLE_VERILOG_CST_NUMBERS_H_