File: InfoIndent.h

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (125 lines) | stat: -rw-r--r-- 3,825 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
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
#pragma once
#include "Core/Object.h"
#include "Core/EnginePtr.h"

namespace storm {
	namespace syntax {
		STORM_PKG(lang.bnf);

		/**
		 * Kind of indentation for a production.
		 */
		enum IndentType {
			STORM_NAME(indentNone, none),

			// Increase indentation one level.
			STORM_NAME(indentIncrease, increase),

			// Decrease indentation one level.
			STORM_NAME(indentDecrease, decrease),

			// Increase indentation one level as long as no other considered nodes affected
			// indentation (except for align directives).
			STORM_NAME(indentWeakIncrease, weakIncrease),

			// Align with the start of the token before the indented range. Only the leafmost
			// alignment is considered when indentation information is generated.
			STORM_NAME(indentAlignBegin, alignBegin),

			// Align with the end of the token before the indented range. Only the leafmost
			// alignment is considered when indentation information is generated.
			STORM_NAME(indentAlignEnd, alignEnd),
		};

		// Get the symbol for an indentation type.
		Str *STORM_FN indentSymbol(EnginePtr e, IndentType i);


		/**
		 * Information about indentation in an InfoNode. This is only needed in the minority of
		 * nodes, so it is only created when it is actually needed.
		 */
		class InfoIndent : public Object {
			STORM_CLASS;
		public:
			// Create.
			STORM_CTOR InfoIndent(Nat start, Nat end, IndentType type);

			// Start and end position of the intended range.
			Nat start;
			Nat end;

			// Type of indentation.
			IndentType type;

			// Is 'i' included in the range?
			Bool STORM_FN contains(Nat i) const;

			// To string.
			virtual void STORM_FN toS(StrBuf *to) const;
		};


		/**
		 * Represents possible results from an indent-query. Either: 'n levels of indentation' or
		 * 'same indentation as line on offset n'.
		 */
		class TextIndent {
			STORM_VALUE;
		public:
			// Create, zero levels of indentation.
			STORM_CTOR TextIndent();

			// Is this an alignment indentation?
			Bool STORM_FN isAlign() const;

			// Access the indentation level. Returns zero if we're containing a character offset.
			Int STORM_FN level() const;
			void STORM_FN level(Int level);

			// Access the current offset. Returns zero if we're not containing a character offset.
			Nat STORM_FN alignAs() const;
			void STORM_FN alignAs(Nat offset);

			// Have we seen some indentation directive?
			Bool STORM_FN seenIndent() const;

			// Apply indentation of a parent node, assuming it is applicable to us.
			// Provided is the offset of the start of the repetition for the parent node,
			// and the current token id.
			void STORM_FN applyParent(InfoIndent *info, Nat current, Nat repOffsetBegin, Nat repOffsetEnd);

			// Increase any offsets in this structure by 'n'.
			void STORM_FN offset(Nat n);

			// Compare.
			inline Bool STORM_FN operator ==(TextIndent o) const { return (value & ~indentMask) == (o.value & ~indentMask); }
			inline Bool STORM_FN operator !=(TextIndent o) const { return !(*this == o); }

			// Output.
			void STORM_FN toS(StrBuf *to) const;

		private:
			// Store the result as follows:
			// topmost bit indicates that we have seen a indentIncrease or indentDecrease.
			// second topmost is set if we contain an offset into the file.
			// the rest of the bits either contains a 2:s complement of the indentation level
			// or an unsigned number representing the offset.
			Nat value;

			// Masks.
			static const Nat indentMask   = 0x80000000;
			static const Nat relativeMask = 0x40000000;
			static const Nat signMask     = 0x20000000;
			static const Nat dataMask     = 0x1FFFFFFF;
		};

		// Output.
		wostream &operator <<(wostream &to, TextIndent i);

		// Create initialized TextIndent instances.
		TextIndent STORM_FN indentLevel(Int level);
		TextIndent STORM_FN indentAs(Nat offset);

	}
}