-
Notifications
You must be signed in to change notification settings - Fork 15
/
Names.scala
196 lines (193 loc) · 8.01 KB
/
Names.scala
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
/* SPDX-FileCopyrightText: © 2022 Parsley Contributors <https://github.com/j-mie6/Parsley/graphs/contributors>
* SPDX-License-Identifier: BSD-3-Clause
*/
package parsley.token.names
import parsley.Parsley
import parsley.token.predicate.{CharPredicate, NotRequired}
/** This class defines a uniform interface for defining parsers for user-defined
* names (identifiers and operators), independent of how whitespace should be
* handled after the name.
*
* @since 4.0.0
* @note implementations of this class found within `Lexer` may employ sharing
* and refine the `def`s in this class into `val` or `lazy val` when overriding.
*
* @define disclaimer
* the exact behaviour of this parser is decided by the implementations given in
* `Lexer`, which will depend on user-defined configuration. Please see the
* relevant documentation of these specific objects.
*/
abstract class Names private[names] {
/** This parser will parse an identifier based on the
* defined identifier start and identifier letter. It
* is capable of handling unicode characters if the
* configuration permits. If hard keywords are specified
* by the configuration, this parser is not permitted
* to parse them.
*
* @example {{{
* // identifierStart = Basic(_.isLetter)
* // identifierLetter = Basic(_.isLetterOrDigit)
* // hardKeywords = Set("if", ...)
* scala> identifier.parse("x1")
* val res0 = Success("x1")
* scala> identifier.parse("1x")
* val res1 = Failure(...)
* scala> identifier.parse("")
* val res2 = Failure(...)
* scala> identifier.parse("iffy")
* val res3 = Success("iffy")
* scala> identifier.parse("if")
* val res4 = Failure(...)
* }}}
*
* @note $disclaimer
* @since 4.0.0
*/
def identifier: Parsley[String]
/** This combinator will parse an identifier based on the
* provided identifier start and described identifier letter. It
* is capable of handling unicode characters if the
* configuration permits.
*
* After parsing a valid identifier as in `identifier`,
* this combinator will verify that the first character
* matches the given parameter. If `NotRequired` is passed,
* this combinator will be equivalent to `identifier`.
*
* If hard keywords are specified
* by the configuration, this parser is not permitted
* to parse them.
*
* @example {{{
* // identifierStart = Basic(_.isLetter)
* // identifierLetter = Basic(_.isLetterOrDigit)
* // hardKeywords = Set("if", ...)
* scala> identifier(Basic(_.isLower)).parse("x1")
* val res0 = Success("x1")
* scala> identifier(Basic(_.isLower)).parse("X1")
* val res1 = Failure(...)
* scala> identifier(Basic(_.isLower)).parse("1x")
* val res2 = Failure(...)
* scala> identifier(Basic(_.isLower)).parse("")
* val res3 = Failure(...)
* scala> identifier(Basic(_.isLower)).parse("iffy")
* val res4 = Success("iffy")
* scala> identifier(Basic(_.isLower)).parse("if")
* val res5 = Failure(...)
* }}}
*
* @param startChar describes what the starting character must be
* @note $disclaimer
* @since 4.0.0
*/
def identifier(startChar: CharPredicate): Parsley[String]
/** This parser will parse a user-defined operator based on the
* defined operator start and operator letter. It
* is capable of handling unicode characters if the
* configuration permits. If hard operators are specified
* by the configuration, this parser is not permitted
* to parse them.
*
* @example {{{
* // operatorStart = Basic(Set('+', '-'))
* // operatorLetter = Basic(Set('+', '-', ':'))
* // hardKeywords = Set("+", "+:", ...)
* scala> userDefinedOperator.parse("-:")
* val res0 = Success("-:")
* scala> userDefinedOperator.parse("*:")
* val res1 = Failure(...)
* scala> userDefinedOperator.parse("")
* val res2 = Failure(...)
* scala> userDefinedOperator.parse("++")
* val res3 = Success("++")
* scala> userDefinedOperator.parse("+:")
* val res4 = Failure(...)
* scala> userDefinedOperator.parse("++:")
* val res5 = Success("++:")
* }}}
*
* @note $disclaimer
* @since 4.0.0
*/
def userDefinedOperator: Parsley[String]
/** This combinator will parse a user-defined operator based on the
* defined operator start and operator letter, refined by the
* provided `startChar` and `endChar`. It
* is capable of handling unicode characters if the
* configuration permits.
*
* After parsing a valid operator as in `userDefinedOperator`,
* this combinator will verify that the first and last characters
* match the given parameters. If `NotRequired` is passed to
* either argument, this will permit any character. Passing
* it to both arguments will be equivalent to `userDefinedOperator`.
*
* If hard operators are specified
* by the configuration, this parser is not permitted
* to parse them.
*
* @example {{{
* // operatorStart = Basic(Set('+', '-'))
* // operatorLetter = Basic(Set('+', '-', ':'))
* // hardKeywords = Set("+", "+:", ...)
* scala> userDefinedOperator(NotRequired, Basic(Set(':'))).parse("-:")
* val res0 = Success("-:")
* scala> userDefinedOperator(NotRequired, Basic(Set(':'))).parse("*:")
* val res1 = Failure(...)
* scala> userDefinedOperator(NotRequired, Basic(Set(':'))).parse("")
* val res2 = Failure(...)
* scala> userDefinedOperator(NotRequired, Basic(Set(':'))).parse("++")
* val res3 = Failure(...)
* scala> userDefinedOperator(NotRequired, Basic(Set(':'))).parse("+:")
* val res4 = Failure(...)
* scala> userDefinedOperator(NotRequired, Basic(Set(':'))).parse("++:")
* val res5 = Success("++:")
* }}}
*
* @param startChar describes what the starting character must be
* @param endChar describes what the final character must be
* @note $disclaimer
* @since 4.0.0
*/
def userDefinedOperator(startChar: CharPredicate, endChar: CharPredicate): Parsley[String]
/** This combinator will parse a user-defined operator based on the
* defined operator start and operator letter, refined by the
* provided `startChar`. It is capable of handling unicode characters if the
* configuration permits.
*
* After parsing a valid operator as in `userDefinedOperator`,
* this combinator will verify that the first character
* matches the given parameter. If `NotRequired` is passed it
* will be equivalent to `userDefinedOperator`.
*
* If hard operators are specified
* by the configuration, this parser is not permitted
* to parse them.
*
* @example {{{
* // operatorStart = Basic(Set('+', '-'))
* // operatorLetter = Basic(Set('+', '-', ':'))
* // hardKeywords = Set("+", "+:", ...)
* scala> userDefinedOperator(Basic(Set('+'))).parse("-:")
* val res0 = Failure(...)
* scala> userDefinedOperator(Basic(Set('+'))).parse("*:")
* val res1 = Failure(...)
* scala> userDefinedOperator(Basic(Set('+'))).parse("")
* val res2 = Failure(...)
* scala> userDefinedOperator(Basic(Set('+'))).parse("++")
* val res3 = Success("++")
* scala> userDefinedOperator(Basic(Set('+'))).parse("+:")
* val res4 = Failure(...)
* scala> userDefinedOperator(Basic(Set('+'))).parse("++:")
* val res5 = Success("++:")
* }}}
*
* @param startChar describes what the starting character must be
* @note $disclaimer
* @since 4.1.0
*/
final def userDefinedOperator(startChar: CharPredicate): Parsley[String] = userDefinedOperator(startChar, NotRequired)
// TODO: Two variants of the above that also have reasons that describe
// the requirements of the identifier/operator
}