-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchar.lisp
99 lines (66 loc) · 2.73 KB
/
char.lisp
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
;;;; Parsers for character inputs.
(in-package :maxpc.char)
(defun ?char (char &optional (case-sensitive-p t))
"*Arguments and Values:*
_char_—a _character_.
_case‑sensitive‑p_—a _generalized boolean_. The default is _true_.
*Description:*
{?char} matches _char_. {?char} is case sensitive unless _case‑sensitive‑p_
is _false_.
*Exceptional Situations:*
If the next element is not a _character_ an _error_ of _type_ {type-error}
is signaled."
(if case-sensitive-p
(?test ('char= char))
(?test ('char-equal char))))
(defun ?string (string &optional (case-sensitive-p t))
"*Arguments and Values:*
_string_—a _string_.
_case‑sensitive‑p_—a _generalized boolean_. The default is _true_.
*Description:*
{?string} matches the _characters_ in _string_ in sequence. {?string} is
case sensitive unless _case‑sensitive‑p_ is _false_.
*Exceptional Situations:*
If an element attempted to be matched is not a _character_ an _error_ of
_type_ {type-error} is signaled."
(apply '?seq (loop for char across string collect
(?char char case-sensitive-p))))
(defparameter *whitespace* '(#\Tab #\Newline #\Vt #\Ff #\Return #\Space)
"*Value Type:*
a _list_ of _characters_.
*Description:*
The _value_ of {*whitespace*} is a _list_ of _characters_ considered
to be _whitespace characters_.")
(defun ?whitespace ()
"*Description:*
{?whitespace} matches an element that is a member of {*whitespace*}.
*Exceptional Situations:*
If the next element is not a _character_ an _error_ of _type_ {type-error}
is signaled."
(?test ('member *whitespace* :test 'char=)))
(defun ?newline ()
"*Description:*
{?newline} matches the {#\\\\Newline} _character_."
(?char #\Newline))
(defun =line (&optional keep-newline-p)
"*Arguments and Values:*
_keep‑newline‑p_—a _generalized boolean_. The default is _false_.
*Description:*
{=line} matches zero or more _characters_ in sequence followed by a
{#\\\\Newline} _character_ or the end of input, and produces the _string_ of
_characters_ as its result value. The terminating {#\\\\Newline} _character_
is not included in _string_ unless _keep‑newline‑p_ is _true_.
*Examples:*
#code#
(parse (format nil \"foo~%bar~%baz\") (%any (=line)))
→ (\"foo\" \"bar\" \"baz\"), T, T
#
*Exceptional Situations:*
If an element attempted to be matched is not a _character_ an _error_ of
_type_ {type-error} is signaled."
(=destructure (line _)
(%or (=list (=subseq (%any (?not (?newline)))) (?newline))
(=list (=subseq (%some (?not (?end)))) (?end)))
(if keep-newline-p
(format nil "~a~%" #1=(coerce line 'string))
#1#)))