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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include "stdafx.h"
#include "InfoIndent.h"
#include "Core/StrBuf.h"
#include "Core/Str.h"
namespace storm {
namespace syntax {
/**
* IndentType.
*/
Str *indentSymbol(EnginePtr e, IndentType i) {
const wchar *symbol = S("<unknown>");
switch (i) {
case indentNone:
symbol = S("none");
break;
case indentIncrease:
symbol = S("+");
break;
case indentDecrease:
symbol = S("-");
break;
case indentWeakIncrease:
symbol = S("?");
break;
case indentAlignBegin:
symbol = S("@");
break;
case indentAlignEnd:
symbol = S("$");
break;
}
return new (e.v) Str(symbol);
}
/**
* InfoIndent.
*/
InfoIndent::InfoIndent(Nat start, Nat end, IndentType type) : start(start), end(end), type(type) {}
Bool InfoIndent::contains(Nat i) const {
return i >= start && i < end;
}
void InfoIndent::toS(StrBuf *to) const {
*to << L"[" << start << L"-" << end << L"]" << indentSymbol(to->engine(), type);
}
/**
* TextIndent.
*/
TextIndent::TextIndent() : value(0) {}
Bool TextIndent::isAlign() const {
return (value & relativeMask) != 0;
}
Int TextIndent::level() const {
if (!isAlign()) {
// Sign-extend to the last bit.
Nat r = value & dataMask;
if (value & signMask)
return r | relativeMask | indentMask;
else
return r;
} else {
return 0;
}
}
void TextIndent::level(Int v) {
value = (Nat(v) & dataMask) | indentMask;
}
Nat TextIndent::alignAs() const {
if (isAlign())
return value & dataMask;
else
return 0;
}
void TextIndent::alignAs(Nat offset) {
value = offset | relativeMask | (value & indentMask);
}
Bool TextIndent::seenIndent() const {
return (value & indentMask) != 0;
}
void TextIndent::applyParent(InfoIndent *info, Nat current, Nat offsetBegin, Nat offsetEnd) {
Bool inside = info->contains(current);
switch (info->type) {
case indentIncrease:
if (!isAlign())
level(level() + inside);
break;
case indentDecrease:
if (!isAlign())
level(level() - inside);
break;
case indentWeakIncrease:
if (!isAlign() && !seenIndent() && inside) {
level(1);
}
break;
case indentAlignBegin:
if (isAlign()) {
// We only care about the leafmost one.
} else if (inside) {
alignAs(offsetBegin);
}
break;
case indentAlignEnd:
if (isAlign()) {
// We only care about the leafmost one.
} else if (inside) {
alignAs(offsetEnd);
}
break;
}
}
void TextIndent::offset(Nat n) {
if (isAlign())
alignAs(alignAs() + n);
}
void TextIndent::toS(StrBuf *to) const {
if (isAlign())
*to << S("align as ") << alignAs();
else
*to << S("indent ") << level() << S(" levels");
}
wostream &operator <<(wostream &to, TextIndent i) {
if (i.isAlign())
to << L"align as " << i.alignAs();
else
to << L"indent " << i.level() << L" levels";
return to;
}
TextIndent indentLevel(Int level) {
TextIndent i;
i.level(level);
return i;
}
TextIndent indentAs(Nat offset) {
TextIndent i;
i.alignAs(offset);
return i;
}
}
}
|