forked from bartuer/ydiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.rkt
82 lines (61 loc) · 1.79 KB
/
structs.rkt
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
#lang racket
(provide (all-defined-out))
;-------------------------------------------------------------
; data types
;-------------------------------------------------------------
(struct Node (type start end elts) #:transparent)
(define comment?
(lambda (n)
(and (Node? n) (eq? 'comment (Node-type n)))))
(define phantom?
(lambda (n)
(and (Node? n) (eq? 'phantom (Node-type n)))))
(define token?
(lambda (n)
(and (Node? n) (eq? 'token (Node-type n)))))
(define str?
(lambda (n)
(and (Node? n) (eq? 'str (Node-type n)))))
(define character?
(lambda (n)
(and (Node? n) (eq? 'char (Node-type n)))))
(define newline?
(lambda (n)
(and (Node? n) (eq? 'newline (Node-type n)))))
(define decode-ast
(lambda (exp)
(match exp
[`(Node ',type ,start ,end ,elts)
(Node start end type (decode-ast elts))]
[`(list ,elts ...)
(map decode-ast elts)]
[''() '()])))
(define get-symbol
(lambda (node)
(cond
[(token? node)
(string->symbol (Node-elts node))]
[else #f])))
;; Find the first node elements which matches a given tag.
(define get-tag
(lambda (node tag)
(cond
[(not (Node? node)) #f]
[(not (pair? (Node-elts node))) #f]
[(null? (Node-elts node)) #f]
[else
(let ([matches (filter (lambda (x)
(eq? (Node-type x) tag))
(Node-elts node))])
(cond
[(null? matches) #f]
[else (car matches)]))])))
;; Find the first node containing a given path of tags.
;; For example: '(function parameter) could match a function's parameter
(define match-tags
(lambda (e tags)
(cond
[(not (Node? e)) #f]
[(null? tags) e]
[else
(match-tags (get-tag e (car tags)) (cdr tags))])))