-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from guicho271828/migrate-trivia
trivia patterns
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
(asdf:defsystem :erlang-term-trivia | ||
:description "Trivia extensions for matching Erlang terms." | ||
:depends-on (:erlang-term :trivia) | ||
:components | ||
((:module :trivia | ||
:components | ||
((:file "package") | ||
(:file "binary" | ||
:depends-on ("package")) | ||
(:file "erlang-string" | ||
:depends-on ("package")) | ||
(:file "tuple" | ||
:depends-on ("package")) | ||
)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
(in-package :erlang-term-trivia) | ||
|
||
(defpattern binary (&rest segments) | ||
"Pattern matching for Erlang binaries. | ||
Example: | ||
(match (binary 1 2 88) | ||
((binary 1 b \"X\") b)) | ||
=> 2 | ||
" | ||
`(class erlang-binary | ||
:bytes (vector | ||
,@(flatten-string-patterns-to-bytes segments)))) | ||
|
||
(defun flatten-string-patterns-to-bytes (patterns) | ||
(reduce #'(lambda (pattern acc) | ||
(if (stringp pattern) | ||
(nconc (string-to-byte-list pattern) acc) | ||
(cons pattern acc))) | ||
patterns | ||
:initial-value nil | ||
:from-end t)) | ||
|
||
(defpattern erlang-string (string) | ||
"Pattern for matching \"Erlang strings\", lists of integers in the ascii range. | ||
Example: | ||
(match (list 104 101 108 108 111) | ||
((erlang-string \"hello\") t)) | ||
=> t | ||
" | ||
`(list ,@(string-to-byte-list string))) | ||
|
||
|
||
(defpattern tuple (&rest patterns) | ||
"Pattern for matching Erlang tuples. | ||
Example: | ||
(match (tuple 1 2 3) | ||
((tuple a b c) (+ a b c))) | ||
=> 6" | ||
`(class erlang-tuple | ||
:elements (simple-vector ,@patterns))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
(defpackage #:erlang-term-trivia | ||
(:documentation "Trivia extension for matching Erlang terms.") | ||
(:nicknames #:etf-trivia) | ||
(:use #:cl #:erlang-term #:etf-bops #:trivia) | ||
(:export | ||
|
||
#:erlang-string | ||
|
||
)) |