You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+85-5Lines changed: 85 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,25 +3,41 @@
3
3
4
4
# Flexer
5
5
6
-
Flexer is a small library for building lexers in Swift.
6
+
Flexer is a small library for building lexers in Swift. It is compatible with all Apple platforms.
7
7
8
8
- API tailored for hand-written parsing
9
9
- Fully Swift `String`-compatible
10
10
- Based around `Sequence` and `IteratorProtocol` procotols
11
11
12
12
It turns out that Swift's `Sequence` and `Iterator` concepts work pretty well for processing tokens. They make for a familiar API that also offers a surprsing amount of power. Flexer builds on these concepts with some new protocols that are made specifically for lexing, but are generally applicable to all `Sequence` types.
Core to lexing is the ability to look ahead at future tokens without advancing. Flexer implements look-ahead with a protocol called `LookAheadIteratorProtocol`. The whole implemenation is inspired by the `lazy` property of `Sequence`, and works very similarly.
17
33
18
34
```swift
19
35
let lookAheadSequence = anySequence.lookAhead
20
36
21
-
37
+
let next = lookAheadSequence.peek()
22
38
```
23
39
24
-
The main work of building your lexer is then defining a Sequence type of tokens. All of lexing facilities you might need can then be exposed with a simple`typealias`.
40
+
The main work of building your lexer is then defining a Sequence type of tokens. All of the lexing facilities you might need can then be exposed with a `typealias`.
Your custom token sequence can be built by creating a struct that conforms to `Sequence`. To make this easier, Flexer includes a type that can be used as a foundation for creating more complex token streams, called `BasicTextCharacterSequence`. It is a sequence of `BasicTextCharacter` elements. It breaks up a string into commonly-needed tokens, catagorized by kind and range within the source string. This approach uses the `Token` type, which stores a kind and a range within the source string.
39
55
40
-
It is usually much easier to build up more complex lexing functionality with the convenience of Swift switch pattern matching, instead of having to worry about the underlying characters and ranges themselves.
56
+
It is usually much easier to build up more complex lexing functionality with the convenience of Swift switch pattern matching, instead of having to worry about the underlying characters and ranges themselves. You can do this by wrapping up a `BasicTextCharacterSequence` in your own custom sequence.
57
+
58
+
Here's a fully-functioning example that produces four different token types. It shows off some of the scanning and look-ahead facilities that can be handy both for constructing and also using your lexer.
We'd love to hear from you! Get in touch via [twitter](https://twitter.com/chimehq), an issue, or a pull request.
41
121
42
-
Flexer also includes a generic `Token` struct, that you might find handy for defining your own custom token types. `BasicTextCharacter` is implemented in terms of `Token`.
122
+
Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.
0 commit comments