Skip to content

Commit

Permalink
Merge pull request #4 from guicho271828/migrate-trivia
Browse files Browse the repository at this point in the history
trivia patterns
  • Loading branch information
flambard committed May 22, 2016
2 parents 92d86e3 + e780581 commit 2402a7c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*~
14 changes: 14 additions & 0 deletions erlang-term-trivia.asd
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"))
))))
46 changes: 46 additions & 0 deletions trivia/impl.lisp
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)))

9 changes: 9 additions & 0 deletions trivia/package.lisp
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

))

0 comments on commit 2402a7c

Please sign in to comment.