-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrcReader.cs
More file actions
174 lines (173 loc) · 3.68 KB
/
BrcReader.cs
File metadata and controls
174 lines (173 loc) · 3.68 KB
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
#nullable enable
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static Brc.BrcGlobal;
namespace Brc
{
public class BrcReader
{
private readonly StringReader reader;
private readonly List<BrcSentence> sentences = [];
private List<List<BrcWord>> words = [];
private BrcReader(string filepath) => reader = new(File.ReadAllText(filepath));
private bool Read(out char value)
{
value = '\0';
int r = reader.Read();
if (r == -1) return false;
value = (char)r;
return true;
}
private bool Peek(out char value)
{
value = '\0';
int r = reader.Peek();
if (r == -1) return false;
value = (char)r;
return true;
}
private bool CheckRead(in char b)
{
if (Peek(out char c) && c == b)
{ Read(out c); return true; }
return false;
}
private void ReadWhitespace()
{
while (Peek(out char c) && char.IsWhiteSpace(c))
Read(out char _);
}
private bool Read(out int value)
{
value = 0;
bool r = false;
while (Peek(out char c) && char.IsDigit(c))
{
Read(out c);
value *= 10;
value += c - '0';
r = true;
}
return r;
}
private bool Read(out float value)
{
value = 0;
bool r = false;
while (Peek(out char c) && char.IsDigit(c))
{
Read(out c);
value *= 10;
value += c - '0';
r = true;
}
int id = 0;
if (CheckRead('.'))
while (Peek(out char c) && char.IsDigit(c))
{
Read(out c);
value *= 10;
value += c - '0';
id++;
}
for (; id > 0; id--)
value /= 10;
return r;
}
private void Read(int fpb, out BrcWord value)
{
string w = string.Empty;
int interval = 1;
while (Peek(out char c)
&& c is not (_separator or _sentence or _leftbucket or _rightbucket or _connect))
{
Read(out c);
if (c is _escape)
{
if (Peek(out char t))
{
Read(out t);
w += t;
}
}
else
w += c;
}
while (Peek(out char c) && c is _connect)
{
Read(out char _);
interval++;
}
value = new() { Word = w, Duration = (float)interval / fpb };
}
private void Wrap(int offset, int bpb, int fpb, float bpm)
{
BrcSentence brcSentence = new([.. words.Select(i => new BrcLine([.. i]))])
{
BeatsPerMinute = bpm,
BeatsPerBar = bpb,
NotePerBeat = fpb,
Offset = offset
};
words = [];
sentences.Add(brcSentence);
}
private Brc Read()
{
int off = 0, bpb = 8, fpb = 4, offl = 0;
float bpm = 120;
if (Peek(out char tmp1) && char.IsDigit(tmp1))
Read(out off);
ReadWhitespace();
bool flag = Peek(out char t);
while (flag)
{
ReadWhitespace();
switch (t)
{
case _sentence:
Read(out char _);
ReadWhitespace();
Wrap(offl, bpb, fpb, bpm);
break;
case _separator:
Read(out char _);
break;
case _leftbucket:
Read(out char _);
ReadWhitespace();
flag &= Read(out bpb);
ReadWhitespace();
flag &= CheckRead(_comma);
ReadWhitespace();
flag &= Read(out fpb);
ReadWhitespace();
if (CheckRead(_comma))
flag &= Read(out bpm);
ReadWhitespace();
if (CheckRead(_comma))
flag &= Read(out offl);
ReadWhitespace();
flag &= CheckRead(_rightbucket);
break;
case _newLine:
Read(out char _);
words.Add([]);
break;
case _escape or _connect or _:
Read(fpb, out BrcWord word);
if (words.Count == 0)
words.Add([]);
words.Last().Add(word);
break;
}
flag &= Peek(out t);
}
if (words.Count != 0)
Wrap(offl, bpb, fpb, bpm);
return new([.. sentences]) { Offset = off };
}
public static Brc Read(string filepath) => new BrcReader(filepath).Read();
}
}