FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tree-query/parser.d at main · CrazyPython/tree-query · GitHub
CrazyPython
tree-query
Repository navigation
Code
Issues
4
(4)
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tree-query
/
parser.d
Copy path
More file actions
More file actions
Latest commit
History
History
History
213 lines (212 loc) · 7.11 KB
Breadcrumbs
tree-query
/
parser.d
Copy path
File metadata and controls
213 lines (212 loc) · 7.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Count the indent level and return where the indent level stops in "i"
* A tab is counted as one indent.
* Params:
* i = the index to start looking from. This is mutated
* by reference to indicate the index the last index
* was.
* spaces_per_indent = the number of spaces to treat as one indent
*/
int
count_indents
(
ref
int
i,
string
input,
int
spaces_per_indent=
4
) {
int
nspaces =
0
;
if
(i
==
input.length
||
!
(input[i]
==
'
'
||
input[i]
==
'
\t
'
)) {
return
0
;
}
for
(; i
<
input.length;
++
i) {
switch
(input[i]) {
case
'
'
: nspaces
++
;
break
;
case
'
*
'
: nspaces
++
;
break
;
case
'
\t
'
: nspaces
+=
spaces_per_indent;
break
;
//
Commented out makes it only accept "-"
//
case '-': return nspaces / spaces_per_indent;
case
'
\n
'
: nspaces =
0
;
break
;
//
default : return 0;
default
:
return
nspaces
/
spaces_per_indent;
}
}
return
nspaces
/
spaces_per_indent;
}
unittest
{
int
i;
assert
(count_indents(i =
0
,
"
- hi
"
)
==
1
);
assert
(count_indents(i =
0
,
"
Foo
"
)
==
0
);
assert
(count_indents(i =
0
,
"
- hi
"
)
==
2
);
assert
(count_indents(i =
0
,
"
\t
- hey
"
)
==
1
);
}
void
doNothing
(T)(T _=
null
) {}
/*
* A streaming parser for Markdown trees. Emits a stream of parsing events.
* Params:
* ParseEventHandler = struct that handles a stream of parsing events.
* See [ExampleParseEventHandler] for an example.
* deleStart = function that is called with the return value of
* the ParseEventHandler's start(). Defaults to
* doNothing
* deleEnd = function that is called with the return value of
* the ParseEventHandler's end(). Defaults to
* doNothing
* These are all compile-time (template) parameters, thus the compiler will
* inline functions into generated code.
*/
void
parse
(ParseEventHandler,
alias
deleStart=doNothing,
alias
deleEnd=doNothing,
bool
relaxed=
false
,
Args
...
)
(
string
input,
int
spaces_per_indent,
string
title, Args args) {
import
std.traits
;
ParseEventHandler handler = ParseEventHandler(args);
//
const initial_indent_level =
int
current_indent_level;
//
BitType current_bits;
//
BitType[] stack;
int
line_content_start_index;
int
line_start_index;
//
Workwround for void not being a parameter type
void
emitBlockStart
(
string
nodeContent) {
static
if
(
is
(ReturnType
!
(handler.start)
==
void
)) {
handler.start(nodeContent);
deleStart();
}
else
deleStart(handler.start(nodeContent));
}
void
emitBlockEnd
() {
static
if
(
is
(ReturnType
!
(handler.end)
==
void
)) {
handler.end();
deleEnd();
}
else
deleEnd(handler.end());
}
emitBlockStart(title);
for
(
int
i =
0
; i
<
input.length;
++
i) {
//
emit end when on same or lower indent level, number based on difference
//
start on new lines
switch
(input[i]) {
case
'
\n
'
:
//
stack[0] & current_bits;
//
FIXME: Start and end based on "-"
const
nodeContent = input[line_start_index
..
i];
import
std.stdio
;
emitBlockStart(nodeContent);
i
++
;
line_start_index = i;
int
new_indent_level =
count_indents(i, input, spaces_per_indent);
line_content_start_index = i;
const
extraEndings = i
==
input.length ?
0
:
1
;
foreach
(_;
0
..
current_indent_level
-
new_indent_level
+
extraEndings) {
emitBlockEnd();
}
if
(relaxed
&&
new_indent_level
>
current_indent_level
+
1
) {
foreach
(_;
0
..
new_indent_level
-
current_indent_level
-
1
)
{
emitBlockStart(
"
"
);
}
}
current_indent_level = new_indent_level;
break
;
case
'
[
'
:
break
;
//
Skip across multiline constructs to prevent
//
detect_indent
case
'
`
'
:
if
(input[i
+
1
]
==
'
`
'
&&
input[i
+
2
]
==
'
`
'
) {
//
Skip 3 at a time for efficiency
//
TODO: SIMD this and/or PGO it
for
(i
+=
3
; i
<
input.length;
++
i) {
if
(input[i
-
2
]
==
'
`
'
&&
input[i
-
1
]
==
'
`
'
&&
input[i
-
0
]
==
'
`
'
) {
break
;
}
}
}
break
;
default
:
break
;
}
}
if
(line_content_start_index
!=
input.length) {
emitBlockStart(input[line_start_index
..
input.length]);
}
for
(
int
i =
0
; i
<
current_indent_level
+
1
;
++
i) {
emitBlockEnd();
}
//
End again because we started for the title
emitBlockEnd();
}
//
/ Test the stream of parser events
unittest
{
import
std.range
,
std.array
;
struct
ExampleParseEventHandler
{
string
start
(
string
text) {
return
"
START
"
~
text;
}
string
end
() {
return
"
END
"
;
}
}
string
sample =
"
- ab
\n\t
- b
\n
hello
\n\t
world
\n\t
foo
"
;
//
A trailing newline should not affect ther esult
foreach
(useTrailingNewline; [
false
,
true
]) {
string
result;
parse
!
(ExampleParseEventHandler, s
=>
result
~=
s, s
=>
result
~=
s)
(sample
~
(useTrailingNewline ?
"
\n
"
:
"
"
),
4
,
"
Title
"
);
import
std.stdio
;
debug
writeln(result);
assert
(result
==
"
STARTTitleSTART- abSTART
\t
- bENDENDSTARThelloSTART
\t
worldENDSTART
\t
fooENDENDEND
"
);
}
}
struct
A
{
}
private
struct
WithConstructor
{
string
[] member;
this
(
string
[] arg, A a) {
member = arg;
}
void
start
(
string
text) {
}
void
end
() {
}
}
unittest
{
A a;
parse
!
(WithConstructor)
(
"
Test
"
,
4
,
"
Title
"
, [
"
arg
"
], a);
}
unittest
{
import
std.stdio
;
struct
ExampleParseEventHandler
{
string
start
(
string
text) {
return
"
<div>
"
~
text;
}
string
end
() {
return
"
</div>
"
;
}
}
string
sample =
`
a
b
c
d
`
;
string
result;
parse
!
(ExampleParseEventHandler, s
=>
result
~=
s, s
=>
result
~=
s,
true
)
(sample,
1
,
"
Title
"
);
writeln(result);
assert
(result
==
"
<div>Title<div>a<div><div> b</div></div></div><div>c<div><div><div> d</div></div></div></div></div>
"
);
}
struct
ConvertToXMLEventHandler
{
string
start
(
string
text) {
return
"
<block>
"
~
text;
}
string
end
() {
return
"
</block>
"
;
}
}
/*
struct ConvertToOPMLEventHandler {
string start(string text) {
// todo: escpae
return `<outline text=` ~ text;
}
string end() {
return `</outline>`;
}
}
*/
Back
|
FazBrowse Home
|
New Git URL