From 44ebeee1bf1c85eb405334234351cefff37d7a8d Mon Sep 17 00:00:00 2001 From: Masataro Asai Date: Wed, 18 May 2016 18:47:06 +0900 Subject: [PATCH 1/2] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e4e5f6c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ \ No newline at end of file From e780581dd930a1ad67bb7408bef673d3aeaacd16 Mon Sep 17 00:00:00 2001 From: Masataro Asai Date: Wed, 18 May 2016 18:47:17 +0900 Subject: [PATCH 2/2] erlang-term-trivia --- erlang-term-trivia.asd | 14 +++++++++++++ trivia/impl.lisp | 46 ++++++++++++++++++++++++++++++++++++++++++ trivia/package.lisp | 9 +++++++++ 3 files changed, 69 insertions(+) create mode 100644 erlang-term-trivia.asd create mode 100644 trivia/impl.lisp create mode 100644 trivia/package.lisp diff --git a/erlang-term-trivia.asd b/erlang-term-trivia.asd new file mode 100644 index 0000000..5e4070d --- /dev/null +++ b/erlang-term-trivia.asd @@ -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")) + )))) diff --git a/trivia/impl.lisp b/trivia/impl.lisp new file mode 100644 index 0000000..15b782b --- /dev/null +++ b/trivia/impl.lisp @@ -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))) + diff --git a/trivia/package.lisp b/trivia/package.lisp new file mode 100644 index 0000000..49907db --- /dev/null +++ b/trivia/package.lisp @@ -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 + + ))