-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
633 lines (576 loc) · 11.2 KB
/
index.html
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
<!DOCTYPE html>
<html>
<head>
<title>Parsing: Regex and Grammars</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script src="remark/remark.js" type="text/javascript">
{
"highlightStyle": "solarized_light",
"highlightLanguage": "no-highlight"
}
</script>
<style type="text/css" media="screen">
/*
SOLARIZED HEX 16/8 TERMCOL XTERM/HEX L*A*B RGB HSB
--------- ------- ---- ------- ----------- ---------- ----------- -----------
base03 #002b36 8/4 brblack 234 #1c1c1c 15 -12 -12 0 43 54 193 100 21
base02 #073642 0/4 black 235 #262626 20 -12 -12 7 54 66 192 90 26
base01 #586e75 10/7 brgreen 240 #585858 45 -07 -07 88 110 117 194 25 46
base00 #657b83 11/7 bryellow 241 #626262 50 -07 -07 101 123 131 195 23 51
base0 #839496 12/6 brblue 244 #808080 60 -06 -03 131 148 150 186 13 59
base1 #93a1a1 14/4 brcyan 245 #8a8a8a 65 -05 -02 147 161 161 180 9 63
base2 #eee8d5 7/7 white 254 #e4e4e4 92 -00 10 238 232 213 44 11 93
base3 #fdf6e3 15/7 brwhite 230 #ffffd7 97 00 10 253 246 227 44 10 99
yellow #b58900 3/3 yellow 136 #af8700 60 10 65 181 137 0 45 100 71
orange #cb4b16 9/3 brred 166 #d75f00 50 50 55 203 75 22 18 89 80
red #dc322f 1/1 red 160 #d70000 50 65 45 220 50 47 1 79 86
magenta #d33682 5/5 magenta 125 #af005f 50 65 -05 211 54 130 331 74 83
violet #6c71c4 13/5 brmagenta 61 #5f5faf 50 15 -45 108 113 196 237 45 77
blue #268bd2 4/4 blue 33 #0087ff 55 -10 -45 38 139 210 205 82 82
cyan #2aa198 6/6 cyan 37 #00afaf 60 -35 -05 42 161 152 175 74 63
green #859900 2/2 green 64 #5f8700 60 -20 65 133 153 0 68 100 60
*/
body {
font-size: 1.3em;
}
.slide .inverse {
color: #839496; /* base0 */
background-color: #002b36; /* base03 */
}
.slide {
color: #657b83; /* base00 */
background-color: #fdf6e3; /* base3 */
}
.slide pre, .slide code {
color: #586e75; /* base01 */
background-color: #eee8d5; /* base2 */
}
.slide .inverse pre, .slide .inverse code {
color: #93a1a1; /* base1 */
background-color: #073642; /* base02 */
}
a:link, a:visited, a:hover, a:active {
color: #268bd2; /* blue */
}
.footnote {
position: absolute;
bottom: 3em;
}
.footnote.right {
right: 3em;
}
.code {
font-family: Monaco, monospace;
}
</style>
</head>
<body>
<textarea id="source">
name: title
layout: true
class: center, middle, inverse
.footnote.right[[Chicago Perl Mongers](http://chicago.pm.org)]
---
name: code
layout: true
class: middle
---
template: title
# Parsing: Regex and Grammars
## Practical Language Parsing
---
name: default
layout: true
class: center, middle
---
# You have a <br> structured document
YAML, JSON, XML
---
# You have a <br>*lot of*<br> structured documents
Config files, database dumps
---
# You have a <br>lot of<br> *wildly different*<br> structured documents
Web services, RPC
---
# Build a script every time?
Leave dozens of single-purpose scripts around
---
# Build a way to report on structured documents
Overengineering!
---
template: title
# `App::YAML::Filter`
# `yq`
---
template: title
# Basic Filters
---
# `{ foo: 1, bar: 2 }`
# `.foo` -> `1`
---
# `{ foo: 1, bar: 2 }`
# `.bar` -> `2`
---
template: title
# Check for Equality
## Binary operator
---
# `{ foo: 1, bar: 2 }`
# `\.foo == 1` -> `true`
---
# `{ foo: 1, bar: 2 }`
# `\.foo == 2` -> `false`
---
# `{ foo: 1, bar: 2 }`
# `\.bar == 1` -> `false`
---
# `{ foo: 1, bar: 2 }`
# `\.bar == 2` -> `true`
---
template: title
# Functions
---
# `{ foo: 1, bar: 2 }`
# `grep( .foo == 1 )` <br>`{ foo: 1, bar: 2 }`
# `grep( .foo == 2 )` <br>`empty`
---
template: title
# Regular Expressions
---
# Filters: `.foo`
```perl
$filter_re = qr{[.]\w+};
```
---
# Integers: `1`
```perl
$int_re = qr{\d+};
```
---
# Equality: `FILTER == INT`
```perl
$equal_re = qr{$filter_re\s+==\s+$int_re};
```
---
# Function: `grep( EQUALITY )`
```perl
$func_re = qr{(\w+)[(]\s+($equal_re)\s+[)]};
```
---
template: code
.center[
# Our Expressions
]
```perl
$filter_re = qr{[.]\w+};
$int_re = qr{\d+};
$equal_re = qr{$filter_re\s+==\s+$int_re};
$func_re = qr{(\w+)[(]\s+($equal_re)\s+[)]};
```
---
template: code
.center[
# Run Our Program
]
```perl
sub filter {
my ( $class, $program, $document ) = @_;
# ...
}
```
---
template: code
.center[
# Filters
]
```perl
if ( $program =~ m{^$filter_re$} ) {
my ( undef, $key ) = split /[.]/, $program;
return $document->{ $key };
}
```
---
template: code
.center[
# Integers
]
```perl
elsif ( $program =~ m{^$int_re$} ) {
return $program;
}
```
---
template: code
.center[
# Equality
]
```perl
elsif ( $program =~ m{^$equal_re$} ) {
my ( $lhs, $op, $rhs ) = split /\s+(==)\s+/, $program;
my $lhs_value = $class->filter( $lhs, $document );
my $rhs_value = $class->filter( $rhs, $document );
if ( $lhs_value == $rhs_value ) {
return 1;
}
else {
return 0;
}
}
```
---
template: code
.center[
# Function
]
```perl
elsif ( $program =~ m{^$func_re$} ) {
my ( $name, $argument ) = ( $1, $2 );
my $arg_val = $class->filter( $argument, $document );
if ( $name eq 'grep' ) {
if ( $arg_val ) {
return $document;
}
else {
return;
}
}
}
```
---
template: title
# Limitations Of Our Language
---
template: code
.center[
# Filter == Int
]
```perl
$filter_re = qr{[.]\w+};
$int_re = qr{\d+};
$equal_re = qr{$filter_re\s+==\s+$int_re};
```
---
template: code
.center[
# Filter|Int == Filter|Int
]
```perl
$filter_re = qr{[.]\w+};
$int_re = qr{\d+};
$equal_re = qr{(?:$filter_re|$int_re)\s+==\s+(?:$filter_re|$int_re)};
```
---
template: code
.center[
# Filter|Int == Filter|Int
]
```perl
$filter_re = qr{[.]\w+};
$int_re = qr{\d+};
$term_re = qr{$filter_re|$int_re};
$equal_re = qr{$term_re\s+==\s+$term_re};
```
---
template: code
.center[
# grep( EQUAL )
]
```perl
$func_re = qr{(\w+)[(]\s+($equal_re)\s+[)]};
```
---
template: code
.center[
# grep( FILTER|EQUAL )
]
```perl
$expr_re = qr{$equal_re|$filter_re};
$func_re = qr{(\w+)[(]\s+($expr_re)\s+[)]};
```
---
template: code
.center[
# Strings
]
```perl
use Regexp::Common;
my $string_re = $RE{delimited}{-delim=>q{'"}};
```
---
template: code
.center[
# Number Formats
## (Float, Hex, Oct, Bin)
]
```perl
use Regexp::Common;
my $evalnum_re = qr{(?:
0b $RE{num}{bin} |
0 $RE{num}{oct} |
0x $RE{num}{hex}
)}x;
```
---
# More Operators
## !=, >, <, eq, ne
---
# More Functions
## uniq, sort, length
---
# Array Indexing
# `.[0]`
---
# Nested Data Structures
# `.foo.bar`
---
template: code
.center[
# Recursive Parsing
`grep( length(.foo) == 1 )`
]
```perl
$expr_re = qr{$equal_re|$filter_re};
$func_re = qr{(\w+)[(]\s+($expr_re)\s+[)]};
```
```perl
$expr_re = qr{$equal_re|$filter_re|(\w+)[(]\s+(?0)\s+[)]};
$func_re = qr{(\w+)[(]\s+($expr_re)\s+[)]};
```
---
template: title
# Developing a Grammar
---
# Lexing
---
# Changing<br>`Text`<br>into<br>`Tokens`
---
# Parsing
---
# Using<br>`Tokens`<br>to <br>`do something`
---
template: title
# Parse::RecDescent
## Damien Conway
---
template: code
.center[
# Starting from Regex
]
```perl
$filter_re = qr{[.]\w+};
$int_re = qr{\d+};
$term_re = qr{$filter_re|$int_re};
$equal_re = qr{$term_re\s+==\s+$term_re};
$expr_re = qr{$equal_re|$filter_re};
$func_re = qr{(\w+)[(]\s+($expr_re)\s+[)]};
```
---
template: code
.center[
# P::RD Grammar
]
```
<autotree>
program: func | expr | int
filter: '.' <skip:""> /\w+/
int: /\d+/
term: filter | int
equal: lhs_term '==' rhs_term
expr: equal | filter
func: /\w+/ '(' expr ')'
lhs_term: term
rhs_term: term
```
---
# `<autotree>`
---
# Using <br>`Tokens`<br>to<br>`do something`
---
# Using <br>`Tokens`<br>to<br>`build a parse tree`
---
# `program: func | expr | int`
## An entry point
---
# `filter: '.' <skip:""> /\w+/`
## .foo
## No whitespace
---
# `equal: lhs_term '==' rhs_term`
## `<autotree>` wants unique names
---
template: code
.center[
# The Parse Tree
]
```
$ perl parse-recdescent.pl '.foo' < data.yml
```
```perl
bless( {
__RULE__ => "program",
expr => bless( {
__RULE__ => "expr",
filter => bless( {
__DIRECTIVE1__ => "\\s*",
__PATTERN1__ => "foo",
__RULE__ => "filter",
__STRING1__ => "."
}, 'filter' )
}, 'expr' )
}, 'program' )
```
---
template: code
.center[
# The Parse Tree
]
```
$ perl parse-recdescent.pl '.foo == 1' < data.yml
```
```perl
bless( {
__RULE__ => "program",
expr => bless( {
__RULE__ => "expr",
equal => bless( {
__RULE__ => "equal",
__STRING1__ => "==",
lhs_term => bless( {
__RULE__ => "lhs_term",
term => bless( {
__RULE__ => "term",
filter => bless( {
...
```
---
template: code
.center[
# Running the Program
]
```perl
my $parser = Parse::RecDescent->new( $grammar );
sub filter {
my ( $class, $program, $document ) = @_;
my $tree = $parser->program( $program );
return run_tree( $tree, $document );
}
```
---
template: code
.center[
# Running down the Tree
]
```perl
sub run_tree {
my ( $tree, $document ) = @_;
...
}
```
---
template: code
.center[
# Filters
]
```perl
if ( $tree->{filter} ) {
my ( $key ) = $tree->{filter}{__PATTERN1__};
return $document->{ $key };
}
```
---
template: code
.center[
# Ints
]
```perl
elsif ( $tree->{int} ) {
return $tree->{int}{__VALUE__};
}
```
---
template: code
.center[
# Equality
]
```perl
elsif ( $tree->{equal} ) {
my $lhs = $tree->{equal}{lhs_term};
my $rhs = $tree->{equal}{rhs_term};
my $lhs_value = run_tree( $lhs, $document );
my $rhs_value = run_tree( $rhs, $document );
if ( $lhs_value == $rhs_value ) {
return 1;
}
else {
return 0;
}
}
```
---
template: code
.center[
# Functions
]
```perl
elsif ( $tree->{func} ) {
my $arg_val = run_tree( $tree->{func}{expr}, $document );
if ( $tree->{func}{__PATTERN1__} eq 'grep' ) {
if ( $arg_val ) {
return $document;
}
else {
return;
}
}
}
```
---
template: code
.center[
# Intermediate Steps
]
```perl
elsif ( $tree->{expr} ) {
return run_tree( $tree->{expr}, $document );
}
elsif ( $tree->{term} ) {
return run_tree( $tree->{term}, $document );
}
```
---
template: title
# Guidelines
## Learn from my pain
---
template: code
.center[
# Build In Layers
]
* Primitives (int, string)
* Variables (filter)
* Operators (==)
* Functions
---
# Test in Layers!!
---
# Small, Tight Rules
---
# Parse Trees!
---
template: code
.center[
# Parser actions as normalization
]
* Evaled numbers (oct, bin, hex)
---
template: title
# Marpa
---
`this page unintentionally left blank`
---
template: title
# Thank You
</textarea>
<div id="slideshow"></div>
</body>
</html>