[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master d98e8c8 05/60: Add url-encoded string parser
From: |
Junpeng Qiu |
Subject: |
[elpa] master d98e8c8 05/60: Add url-encoded string parser |
Date: |
Tue, 25 Oct 2016 17:45:12 +0000 (UTC) |
branch: master
commit d98e8c8d559a4b7c1cf9616b960a2af3ce5ca97b
Author: Junpeng Qiu <address@hidden>
Commit: Junpeng Qiu <address@hidden>
Add url-encoded string parser
---
parsec.el | 30 ++++++++++++++++++++++++--
url-str-parser.el | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 89 insertions(+), 2 deletions(-)
diff --git a/parsec.el b/parsec.el
index 7bca129..a86629d 100644
--- a/parsec.el
+++ b/parsec.el
@@ -161,16 +161,22 @@
(let ((res (make-symbol "results")))
`(let (,res)
(pl-try
- (while (not (eobp))
- (push ,parser ,res)))
+ (while (not (eobp))
+ (push ,parser ,res)))
(nreverse ,res))))
+(defmacro pl-many1 (parser)
+ `(cons ,parser (pl-many ,parser)))
+
(defun pl-list-to-string (l)
(mapconcat #'identity l ""))
(defmacro pl-many-as-string (parser)
`(mapconcat #'identity (pl-many ,parser) ""))
+(defmacro pl-many1-as-string (parser)
+ `(mapconcat #'identity (pl-many1 ,parser) ""))
+
(defmacro pl-endby (parser end)
`(pl-many (pl-return ,parser
,end)))
@@ -180,5 +186,25 @@
(cons ,parser (pl-many (pl-and ,separator ,parser)))
nil))
+
+(defun pl-just (x) (cons 'Just x))
+
+(defvar pl-nothing 'Nothing)
+
+(defun pl-maybe-p (x)
+ (or (eq x pl-nothing)
+ (and
+ (consp x)
+ (eq (car x) 'Just))))
+
+(defmacro pl-make-maybe (&rest body)
+ (let ((res (make-symbol "result")))
+ `(let ((,res (pl-try
+ ,@body)))
+ (if (pl-msg-p ,res)
+ pl-nothing
+ (pl-just ,res)))))
+
+
(provide 'parsec)
;;; parsec.el ends here
diff --git a/url-str-parser.el b/url-str-parser.el
new file mode 100644
index 0000000..fbb747e
--- /dev/null
+++ b/url-str-parser.el
@@ -0,0 +1,61 @@
+;;; url-str-parser.el --- URL-encoded query string parser using parsec.el -*-
lexical-binding: t; -*-
+
+;; Copyright (C) 2016 Junpeng Qiu
+
+;; Author: Junpeng Qiu <address@hidden>
+;; Keywords: extensions
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Ref: http://book.realworldhaskell.org/read/using-parsec.html
+
+;;; Code:
+
+
+(defun url-str-query ()
+ (pl-sepby (url-str-pair) (pl-ch ?&)))
+
+(defun url-str-pair ()
+ (cons
+ (pl-many1-as-string (url-str-char))
+ (pl-make-maybe (pl-and (pl-ch ?=) (pl-many-as-string (url-str-char))))))
+
+(defun url-str-char ()
+ (pl-or (pl-re "[a-zA-z0-9$_.!*'(),-]")
+ (pl-and (pl-ch ?+) " ")
+ (url-str-hex)))
+
+(defun url-str-hex ()
+ (pl-and
+ (pl-ch ?%)
+ (format "%c"
+ (string-to-number (format "%s%s"
+ (pl-re "[0-9a-zA-z]")
+ (pl-re "[0-9a-zA-z]"))
+ 16))))
+
+(defun url-str-parse (input)
+ (with-temp-buffer
+ (insert input)
+ (goto-char (point-min))
+ (url-str-query)))
+
+(url-str-parse "foo=bar&a%21=b+c")
+(url-str-parse "foo=&a%21=b+c")
+(url-str-parse "foo&a%21=b+c")
+
+(provide 'url-str-parser)
+;;; url-str-parser.el ends here
- [elpa] master da878fa 18/60: Use parsec-query for other return values, (continued)
- [elpa] master da878fa 18/60: Use parsec-query for other return values, Junpeng Qiu, 2016/10/25
- [elpa] master 054a753 06/60: Rename to parsec, Junpeng Qiu, 2016/10/25
- [elpa] master 2cce1e3 37/60: Rename parsec-from-just to parsec-from-maybe, Junpeng Qiu, 2016/10/25
- [elpa] master cdb8c0d 47/60: Remove unused symbols, Junpeng Qiu, 2016/10/25
- [elpa] master fdb3d99 44/60: Fix some code in simple-csv-parser.el, Junpeng Qiu, 2016/10/25
- [elpa] master 701964f 57/60: Prepare headers for GNU ELPA, Junpeng Qiu, 2016/10/25
- [elpa] master cbd3352 55/60: Add parsec-lookahead, Junpeng Qiu, 2016/10/25
- [elpa] master a951fbe 41/60: Unify tag names, Junpeng Qiu, 2016/10/25
- [elpa] master dfb3af0 60/60: Add 'packages/parsec/' from commit '8f0c266d8b9b0ee5fcf9b80c518644b2849ff3b3', Junpeng Qiu, 2016/10/25
- [elpa] master 6cc3100 07/60: Fix indentations, Junpeng Qiu, 2016/10/25
- [elpa] master d98e8c8 05/60: Add url-encoded string parser,
Junpeng Qiu <=
- [elpa] master a268b9f 08/60: Add more APIs, Junpeng Qiu, 2016/10/25
- [elpa] master 13637e1 15/60: Rename parsec-do-parse to parsec-with-input, Junpeng Qiu, 2016/10/25
- [elpa] master ba23b01 19/60: Add more APIs, Junpeng Qiu, 2016/10/25
- [elpa] master f3ce070 09/60: Add simple scheme parser, Junpeng Qiu, 2016/10/25
- [elpa] master 9bc1d77 28/60: Move examples to under sub-directory, Junpeng Qiu, 2016/10/25
- [elpa] master 52f1886 33/60: Update pjson, Junpeng Qiu, 2016/10/25
- [elpa] master 7e65db0 51/60: Merge pull request #1 from tarsius/master, Junpeng Qiu, 2016/10/25
- [elpa] master 311a5ca 49/60: Fix typo, Junpeng Qiu, 2016/10/25
- [elpa] master e6d5f38 46/60: Update the comment in source file, Junpeng Qiu, 2016/10/25
- [elpa] master 8c22dcb 39/60: Add :group example, Junpeng Qiu, 2016/10/25