File: recursive.bs

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; 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 (187 lines) | stat: -rw-r--r-- 3,550 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use parser;
use core:io;

Buffer? Bytes(RecursiveBinaryState state, Nat count) {
	Nat from = state.pos;
	Nat to = from + count;
	if (to > state.input.count) {
		state.error("Expected ${count} bytes, but only ${state.input.count - from} remain.");
		return null;
	}

	state.pos = to;
	state.input.cut(from, to);
}

Str? Chars(RecursiveStrState state, Nat count) {
	if (count == 0)
		return "";

	// Step most codepoints in C++.
	var start = state.pos;
	var iter = start + (count - 1);
	var end = iter + 1;

	// If we failed to go the last step, then we did not have enough.
	if (iter == end) {
		state.error("Expected ${count} codepoints, but reached end of the input too early.");
		return null;
	}

	state.pos = end;
	state.input.cut(start, end);
}

private Nat findIndent(RecursiveStrState state, Nat tabWidth) {
	Nat width = 0;
	Str:Iter pos = state.pos;
	do {
		var ch = pos.v;
		if (ch == ' ') {
			width++;
		} else if (ch == '\t') {
			width += tabWidth;
		} else {
			state.pos = pos;
			return width;
		}

		++pos;
	}
}

Nat? Indent(RecursiveStrState state) {
	IndentTab(state, 4);
}

Nat? IndentTab(RecursiveStrState state, Nat tabWidth) {
	findIndent(state, tabWidth);
}

Nat? MinIndent(RecursiveStrState state, Nat indent) {
	MinIndentTab(state, 4, indent);
}

Nat? MinIndentTab(RecursiveStrState state, Nat tabWidth, Nat indent) {
	Nat i = findIndent(state, tabWidth);
	if (i < indent)
		return null;
	else
		return i;
}

Bool ExactIndent(RecursiveStrState state, Nat indent) {
	ExactIndentTab(state, 4, indent);
}

Bool ExactIndentTab(RecursiveStrState state, Nat tabWidth, Nat indent) {
	Nat consumed = 0;
	Str:Iter pos = state.pos;
	Str:Iter end = state.input.end;
	while (consumed < indent) {
		if (pos == end)
			return false;

		var ch = pos.v;
		if (ch == ' ')
			consumed++;
		else if (ch == '\t')
			consumed += tabWidth;
		else {
			return false;
		}

		++pos;
	}

	state.pos = pos;
	return true;
}

Bool enough(RecursiveBinaryState state, Nat count) {
	if (state.pos + count > state.input.count) {
		state.error("Expected ${count} bytes, but only ${state.input.count} remain.");
		return false;
	} else {
		return true;
	}
}

Byte? Nat8(RecursiveBinaryState this) {
	if (!enough(this, 1))
		return null;

	return input[pos++];
}

Nat? Nat16LE(RecursiveBinaryState this) {
	if (!enough(this, 2))
		return null;

	Nat result = input[pos++].nat;
	result |= input[pos++].nat << 8b;
	return result;
}

Nat? Nat16BE(RecursiveBinaryState this) {
	if (!enough(this, 2))
		return null;

	Nat result = input[pos++].nat << 8;
	result |= input[pos++].nat;
	return result;
}

Nat? Nat32LE(RecursiveBinaryState this) {
	if (!enough(this, 4))
		return null;

	Nat result = input[pos++].nat;
	result |= input[pos++].nat << 8;
	result |= input[pos++].nat << 16;
	result |= input[pos++].nat << 24;
	return result;
}

Nat? Nat32BE(RecursiveBinaryState this) {
	if (!enough(this, 4))
		return null;

	Nat result = input[pos++].nat << 24;
	result |= input[pos++].nat << 16;
	result |= input[pos++].nat << 8;
	result |= input[pos++].nat;
	return result;
}

Int? Int16LE(RecursiveBinaryState this) {
	unless (v = Nat16LE(this))
		return null;

	// Sign extend.
	if (v > 0x8000)
		v |= 0xFFFF0000;
	v.int;
}

Int? Int16BE(RecursiveBinaryState this) {
	unless (v = Nat16BE(this))
		return null;

	// Sign extend.
	if (v > 0x8000)
		v |= 0xFFFF0000;
	v.int;
}

Int? Int32LE(RecursiveBinaryState this) {
	unless (v = Nat32BE(this))
		return null;
	v.int;
}

Int? Int32BE(RecursiveBinaryState this) {
	unless (v = Nat32BE(this))
		return null;
	v.int;
}