1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2 * Use of this file is governed by the BSD 3-clause license that
3 * can be found in the LICENSE.txt file in the project root.
8 #include "antlr4-common.h"
13 // Helpers to convert certain unsigned symbols (e.g. Token::EOF) to their original numeric value (e.g. -1)
14 // and vice versa. This is needed mostly for intervals to keep their original order and for toString()
15 // methods to print the original numeric value (e.g. for tests).
16 size_t numericToSymbol(ssize_t v);
17 ssize_t symbolToNumeric(size_t v);
19 /// An immutable inclusive interval a..b
20 class ANTLR4CPP_PUBLIC Interval {
22 static const Interval INVALID;
24 // Must stay signed to guarantee the correct sort order.
29 explicit Interval(size_t a_, size_t b_); // For unsigned -> signed mappings.
30 Interval(ssize_t a_, ssize_t b_);
32 /// return number of elements between a and b inclusively. x..x is length 1.
33 /// if b < a, then length is 0. 9..10 has length 2.
34 size_t length() const;
36 bool operator == (const Interval &other) const;
38 size_t hashCode() const;
41 /// Does this start completely before other? Disjoint </summary>
42 bool startsBeforeDisjoint(const Interval &other) const;
45 /// Does this start at or before other? Nondisjoint </summary>
46 bool startsBeforeNonDisjoint(const Interval &other) const;
49 /// Does this.a start after other.b? May or may not be disjoint </summary>
50 bool startsAfter(const Interval &other) const;
53 /// Does this start completely after other? Disjoint </summary>
54 bool startsAfterDisjoint(const Interval &other) const;
57 /// Does this start after other? NonDisjoint </summary>
58 bool startsAfterNonDisjoint(const Interval &other) const;
61 /// Are both ranges disjoint? I.e., no overlap? </summary>
62 bool disjoint(const Interval &other) const;
65 /// Are two intervals adjacent such as 0..41 and 42..42? </summary>
66 bool adjacent(const Interval &other) const;
68 bool properlyContains(const Interval &other) const;
71 /// Return the interval computed from combining this and other </summary>
72 Interval Union(const Interval &other) const;
75 /// Return the interval in common between this and o </summary>
76 Interval intersection(const Interval &other) const;
78 std::string toString() const;