git.fiddlerwoaroof.com
Browse code

Throw everything out

Fernando Borretti authored on 29/01/2015 23:57:28
Showing 9 changed files
1 1
deleted file mode 100755
... ...
@@ -1,30 +0,0 @@
1
-`cl-yaml` is a [libyaml](http://pyyaml.org/wiki/LibYAML)-based YAML parser for Common Lisp.
2
-
3
-# Usage
4
-
5
-```lisp
6
-CL-YAML> (yaml:parse #p"network/hosts.yaml")
7
-{"prod" => ("something.herokuapp.com" 6767), "db" => ("somewhere.org" 5432)}
8
-CL-YAML> (yaml:parse "{arch: x86-64, cc: clang, user: eudoxia}")
9
-{"arch" => "x86-64", "cc" => "clang", "user" => "eudoxia"}
10
-CL-USER> (yaml:emit (list "foo" "bar"))
11
-"[\"foo\", \"bar\"]"
12
-CL-USER> (yaml:emit '((a 1) (b 2) (c 3)))
13
-"[[A, 1], [B, 2], [C, 3]]"
14
-```
15
-
16
-[Hash table syntax](http://frank.kank.net/essays/hash.html) is used in the examples. I recommend at least using its hash table printer because the default is basically useless.
17
-
18
-# Installation
19
-
20
-You need `libyaml` for this to work.
21
-
22
-# License
23
-
24
-Copyright (C) 2013 Fernando Borretti
25
-
26
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
27
-
28
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
29
-
30
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 0
deleted file mode 100644
... ...
@@ -1,11 +0,0 @@
1
-(defsystem cl-yaml-test
2
-  :author "Fernando Borretti"
3
-  :license "MIT"
4
-  :depends-on (:cl-yaml
5
-               :fiveam)
6
-  :components ((:module "t"
7
-                :serial t
8
-                :components
9
-                ((:file "cl-yaml")
10
-		 (:file "bench"))))
11
-  :perform (load-op :after (op c) (asdf:clear-system c)))
12 0
deleted file mode 100755
... ...
@@ -1,76 +0,0 @@
1
-(in-package :cl-user)
2
-(defpackage cl-yaml-asd
3
-  (:use :cl :asdf))
4
-(in-package :cl-yaml-asd)
5
-
6
-(defclass c->so (source-file) ())
7
-
8
-(defmethod source-file-type ((c c->so) (s module)) "c")
9
-
10
-(defmethod output-files ((operation compile-op) (f c->so))
11
-  (values
12
-    (list
13
-      (make-pathname :name "yaml_wrapper"
14
-                     :type #+unix "so" #+darwin "dylib" #+windows "dll"
15
-                     :defaults
16
-                     (merge-pathnames
17
-                      (make-pathname :directory '(:relative :up))
18
-                      (component-pathname f)))) t))
19
-
20
-(defmethod perform ((o load-op) (c c->so)) t)
21
-
22
-(defparameter +c-flags+ "-Wall -Wextra -c -fPIC -O0 -g")
23
-(defparameter +linker-flags+ "-lyaml")
24
-
25
-(defun comp (file out)
26
-  (format t "cc ~A -o out.o ~A && cc out.o -shared -o ~A ~A && rm out.o"
27
-          (namestring file) +c-flags+ (namestring out) +linker-flags+)
28
-  (format nil "cc ~A -o out.o ~A && cc out.o -shared -o ~A ~A && rm out.o"
29
-          (namestring file) +c-flags+ (namestring out) +linker-flags+))
30
-
31
-(defmethod perform ((o compile-op) (c c->so))
32
-  (if (not (zerop (run-shell-command
33
-                   (comp (make-pathname :name "yaml"
34
-                                        :type "c"
35
-                                        :defaults
36
-                                        (merge-pathnames
37
-                                         "src"
38
-                                         (component-pathname c)))
39
-                         (make-pathname :name "yaml_wrapper"
40
-                                        :type "so"
41
-                                        :defaults
42
-                                        (merge-pathnames
43
-                                         (make-pathname :directory '(:relative :up))
44
-                                         (component-pathname c)))))))
45
-      (error 'operation-error :component c :operation o)
46
-      t))
47
-
48
-(defsystem cl-yaml
49
-  :version "0.2"
50
-  :author "Fernando Borretti"
51
-  :license "MIT"
52
-  :depends-on (:cffi
53
-               :split-sequence)
54
-  :serial t
55
-  :components ((:module "src"
56
-                :serial t
57
-                :components
58
-                ((:static-file "yaml.h")
59
-                 (c->so "yaml" :depends-on ("yaml.h"))
60
-                 (:file "ffi")
61
-                 (:file "cl-yaml")))
62
-               (:module "spec"))
63
-  :description ""
64
-  :long-description
65
-  #.(with-open-file (stream (merge-pathnames
66
-                             #p"README.md"
67
-                             (or *load-pathname* *compile-file-pathname*))
68
-                            :if-does-not-exist nil
69
-                            :direction :input)
70
-      (when stream
71
-        (let ((seq (make-array (file-length stream)
72
-                               :element-type 'character
73
-                               :fill-pointer t)))
74
-          (setf (fill-pointer seq) (read-sequence seq stream))
75
-          seq)))
76
-  :in-order-to ((test-op (load-op cl-yaml-test))))
77 0
deleted file mode 100755
... ...
@@ -1,143 +0,0 @@
1
-(defpackage :yaml
2
-  (:use :cl :split-sequence :libyaml)
3
-  (:import-from :cffi
4
-                :with-foreign-string
5
-                :foreign-string-to-lisp)
6
-  (:export :parse
7
-           :emit))
8
-(in-package :yaml)
9
-
10
-(defun clean (tokens)
11
-  "I am not a clever man."
12
-  (remove-if
13
-   #'(lambda (tok)
14
-       (or (eq (first tok) :stream-start)
15
-           (eq (first tok) :stream-end)))
16
-   tokens))
17
-
18
-(defparameter *delimiters*
19
-  (list :seq-start :seq-end :map-start :map-end
20
-        :stream-start :stream-end :doc-start :doc-end))
21
-
22
-(defun group-documents (tokens)
23
-  (remove-if #'(lambda (seq)
24
-                 (or (eql (length seq) 0)
25
-                     (and (eql (length seq) 1)
26
-                          (member (first (elt seq 0))
27
-                                  *delimiters*))))
28
-             (split-sequence-if
29
-              #'(lambda (tok)
30
-                  (or (eq (first tok) :doc-start)
31
-                      (eq (first tok) :doc-end)))
32
-              tokens)))
33
-
34
-(defun process (str &optional (len (length str)))
35
-  (let ((tok-list (tokenize str len))
36
-        (tokens (make-array 64 :fill-pointer 0 :adjustable t)))
37
-    (if (list-err tok-list)
38
-        (error "Parsing error")
39
-        (progn
40
-          (loop for i from 0 to (list-len tok-list) do
41
-            (let* ((tok (nth-tok tok-list i))
42
-                   (type (gethash (tok-type tok) +enum+)))
43
-              (if type
44
-                  (progn
45
-                    (vector-push-extend (list type
46
-                                              (tok-value tok)
47
-                                              (tok-anchor tok))
48
-                                        tokens)
49
-                    (destroy-nth-tok tok-list i)))))
50
-          (destroy-token-list tok-list)
51
-          (group-documents (clean tokens))))))
52
-
53
-(defmacro with-preserved-case (&rest code)
54
-  `(unwind-protect
55
-     (progn
56
-       (setf (readtable-case *readtable*) :preserve)
57
-       ,@code)
58
-     (setf (readtable-case *readtable*) :upcase)))
59
-
60
-(defun extract-type (val)
61
-  (handler-case
62
-    (let ((res
63
-	   (if (position #\Space val)
64
-	       val
65
-	       (with-preserved-case
66
-	         (read-from-string val)))))
67
-      (if (symbolp res)
68
-	  (symbol-name res)
69
-	  res))
70
-    (error () val)))
71
-
72
-(defun parse% (documents)
73
-  (loop for tokens in documents collecting
74
-    (let ((contexts (list nil))
75
-          (aliases  (make-hash-table :test #'equal)))
76
-      (loop for tok across tokens do
77
-        (let ((type   (first tok))
78
-              (val    (second tok))
79
-              (anchor (third tok)))
80
-	  (cond
81
-	    ((eq type :seq-start)
82
-	     (push (list) contexts))
83
-	    ((eq type :seq-end)
84
-	     (let ((con (pop contexts)))
85
-	       (setf (first contexts) (append (first contexts) (list con)))))
86
-	    ((eq type :map-start)
87
-	     (push (list) contexts))
88
-	    ((eq type :map-end)
89
-	     (let ((con (pop contexts)))
90
-	       (setf (first contexts)
91
-                     (append (first contexts)
92
-                             (list
93
-                              (alexandria:plist-hash-table con :test #'equal))))))
94
-            ((eq type :alias)
95
-             (setf (gethash val aliases) (first contexts)))
96
-            (anchor
97
-             (setf (first contexts)
98
-                   (append (first contexts)
99
-                           (list (gethash val aliases)))))
100
-	    (t
101
-	     (setf (first contexts)
102
-                   (append (first contexts)
103
-                           (list (extract-type val))))))))
104
-             (caar contexts))))
105
-        
106
-(defun post-process (documents)
107
-  (if (cdr documents)
108
-      (mapcar #'(lambda (doc) (list :doc doc)) documents)
109
-      (car documents)))
110
-
111
-(defun slurp-stream (stream)
112
-  (let ((seq (make-string (file-length stream))))
113
-    (read-sequence seq stream)
114
-    seq))
115
-
116
-(defun parse (src)
117
-  (typecase src
118
-    (string
119
-     (post-process (parse% (process src (length src)))))
120
-    (pathname
121
-     (let ((str (with-open-file
122
-                    (stream src :direction :input :if-does-not-exist :error)
123
-                  (slurp-stream stream))))
124
-       (post-process
125
-        (parse%
126
-         (process str (length str))))))
127
-    (t
128
-      (error "Unknown input to yaml:load."))))
129
-
130
-(defun emit (obj)
131
-  (typecase obj
132
-    (number
133
-      (princ-to-string obj))
134
-    (string
135
-      (format nil "~S" obj))
136
-    (symbol
137
-      (format nil "~A" obj))
138
-    (list
139
-      (format nil "[~{~A~#[~:;, ~]~}]" (mapcar #'emit obj)))
140
-    (hash-table
141
-     (format nil "{~{~A~#[~:;, ~]~}}"
142
-	     (loop for key being the hash-keys of obj collecting
143
-		  (format nil "~A : ~A" key (gethash key obj)))))))
144 0
deleted file mode 100644
... ...
@@ -1,81 +0,0 @@
1
-(defpackage :libyaml
2
-  (:use :cl :cffi)
3
-  (:export :tokenize
4
-           :list-len
5
-           :list-err
6
-           :nth-tok
7
-           :destroy-nth-tok
8
-           :tok-type
9
-           :tok-value
10
-           :tok-anchor
11
-           :destroy-token-list
12
-           :+enum+
13
-           :+scalar+
14
-           :+alias+
15
-           :+seq-start+
16
-           :+seq-end+
17
-           :+map-start+
18
-           :+map-end+
19
-           :+doc-start+
20
-           :+doc-end+
21
-           :+stream-start+
22
-           :+stream-end+))
23
-(in-package :libyaml)
24
-
25
-(load-foreign-library
26
-  (namestring
27
-    (make-pathname :name "yaml_wrapper"
28
-                   :type #+unix "so" #+darwin "dylib" #+windows "dll"
29
-                   :defaults (asdf::component-relative-pathname
30
-                               (asdf:find-system :cl-yaml)))))
31
-
32
-(defcfun ("tokenize" tokenize) :pointer (str :string) (len :int))
33
-
34
-;; Accessors
35
-
36
-(defcfun ("list_len" list-len) :int (list :pointer))
37
-(defcfun ("nth_tok" nth-tok) :pointer (list :pointer) (n :int))
38
-(defcfun ("destroy_nth_tok" destroy-nth-tok) :void (list :pointer) (n :int))
39
-(defcfun ("list_err" list-err) :string (list :pointer))
40
-(defcfun ("tok_type" tok-type) :int (tok :pointer))
41
-(defcfun ("tok_value" tok-value) :string (tok :pointer))
42
-(defcfun ("tok_anchor" tok-anchor) :string (tok :pointer))
43
-
44
-(defcfun ("destroyTokenList" destroy-token-list) :void (list :pointer))
45
-
46
-;; Enum values
47
-
48
-(defcfun ("enum_scalar" enum-scalar) :int)
49
-(defcfun ("enum_alias" enum-alias) :int)
50
-(defcfun ("enum_seq_start" enum-seq-start) :int)
51
-(defcfun ("enum_seq_end" enum-seq-end) :int)
52
-(defcfun ("enum_map_start" enum-map-start) :int)
53
-(defcfun ("enum_map_end" enum-map-end) :int)
54
-(defcfun ("enum_doc_start" enum-doc-start) :int)
55
-(defcfun ("enum_doc_end" enum-doc-end) :int)
56
-(defcfun ("enum_stream_start" enum-stream-start) :int)
57
-(defcfun ("enum_stream_end" enum-stream-end) :int)
58
-
59
-(defparameter +enum+ (make-hash-table))
60
-
61
-(setf (gethash (enum-scalar) +enum+) :scalar)
62
-(setf (gethash (enum-alias) +enum+) :alias)
63
-(setf (gethash (enum-seq-start) +enum+) :seq-start)
64
-(setf (gethash (enum-seq-end) +enum+) :seq-end)
65
-(setf (gethash (enum-map-start) +enum+) :map-start)
66
-(setf (gethash (enum-map-end) +enum+) :map-end)
67
-(setf (gethash (enum-doc-start) +enum+) :doc-start)
68
-(setf (gethash (enum-doc-end) +enum+) :doc-end)
69
-(setf (gethash (enum-stream-start) +enum+) :stream-start)
70
-(setf (gethash (enum-stream-end) +enum+) :stream-end)
71
-
72
-(defparameter +scalar+ (enum-scalar))
73
-(defparameter +alias+ (enum-alias))
74
-(defparameter +seq-start+ (enum-seq-start))
75
-(defparameter +seq-end+ (enum-seq-end))
76
-(defparameter +map-start+ (enum-map-start))
77
-(defparameter +map-end+ (enum-map-end))
78
-(defparameter +doc-start+ (enum-doc-start))
79
-(defparameter +doc-end+ (enum-doc-end))
80
-(defparameter +stream-start+ (enum-stream-start))
81
-(defparameter +stream-end+ (enum-stream-end))
82 0
deleted file mode 100755
... ...
@@ -1,139 +0,0 @@
1
-#include "yaml.h"
2
-
3
-TokenList* createTokenList(void) {
4
-  TokenList* list = (TokenList*)malloc(sizeof(TokenList));
5
-  list->data = (Token*)malloc(sizeof(Token)*LIST_CHUNK_SIZE);
6
-  list->len = 0;
7
-  list->cap = LIST_CHUNK_SIZE;
8
-  list->err = NULL;
9
-  return list;
10
-}
11
-
12
-void appendToken(TokenList* list, Token tok) {
13
-  if((list->len + 1) == list->cap) {
14
-    list->cap += LIST_CHUNK_SIZE;
15
-    list->data = (Token*)realloc(list->data,list->cap*sizeof(Token));
16
-  }
17
-  list->data[list->len] = tok;
18
-  list->len++;
19
-}
20
-
21
-void destroyTokenList(TokenList* list) {
22
-  free(list->data);
23
-  free(list);
24
-}
25
-
26
-const char* copy(const char* source) {
27
-  return source;
28
-}
29
-
30
-TokenList* tokenize(const char* str, size_t len) {
31
-  /* Initialization */
32
-  yaml_parser_t parser;
33
-  yaml_event_t  event;
34
-  TokenList* tokens = createTokenList();
35
-
36
-  if(str == NULL) {
37
-    tokens->err = "Can't parse a null string.";
38
-    return tokens;
39
-  }
40
-  if(len == 0) {
41
-    tokens->err = "Can't parse a bstring with length zero.";
42
-    return tokens;
43
-  }
44
-  if(!yaml_parser_initialize(&parser)) {
45
-    tokens->err = "Could not initialize parser.";
46
-    return tokens;
47
-  }
48
-  yaml_parser_set_input_string(&parser, (const unsigned char*)str, len);
49
-
50
-  do {
51
-    Token tok;
52
-    size_t value_len = 0;
53
-    size_t anchor_len = 0;
54
-    tok.type = 0;
55
-    tok.value = NULL;
56
-    tok.anchor = NULL;
57
-    if(!yaml_parser_parse(&parser, &event)) {
58
-      tokens->err = "Parsing error";
59
-      return tokens;
60
-    }
61
-    tok.type = event.type;
62
-    switch(event.type) {
63
-    case YAML_SCALAR_EVENT:
64
-      tok.value = (char*)event.data.scalar.value;
65
-      tok.anchor = (char*)event.data.scalar.anchor;
66
-      if(tok.value) {
67
-        value_len = strlen(tok.value);
68
-        tok.value = malloc(value_len+1);
69
-        strcpy(tok.value, (const char*)event.data.scalar.value);
70
-      }
71
-      if(tok.anchor) {
72
-        anchor_len = strlen(tok.anchor);
73
-        tok.anchor = malloc(anchor_len+1);
74
-        strcpy(tok.anchor, (const char*)event.data.scalar.anchor);
75
-      }
76
-      break;
77
-    case YAML_ALIAS_EVENT:
78
-      tok.value = (char*)event.data.alias.anchor;
79
-      break;
80
-    default:
81
-      /* The token only carries type information */
82
-      break;
83
-    }
84
-    appendToken(tokens,tok);
85
-    if(event.type != YAML_STREAM_END_EVENT)
86
-      yaml_event_delete(&event);
87
-  } while(event.type != YAML_STREAM_END_EVENT);
88
-  
89
-  /* Finalize */
90
-  yaml_event_delete(&event);
91
-  yaml_parser_delete(&parser);
92
-
93
-  return tokens;
94
-}
95
-
96
-
97
-/* Accessors */
98
-
99
-size_t list_len(TokenList* list) {
100
-  return list->len;
101
-}
102
-
103
-Token* nth_tok(TokenList* list, size_t n) {
104
-  return &list->data[n];
105
-}
106
-
107
-void destroy_nth_tok(TokenList* list, size_t n) {
108
-  free(list->data[n].value);
109
-  free(list->data[n].anchor);
110
-}
111
-
112
-const char* list_err(TokenList* list) {
113
-  return list->err;
114
-}
115
-
116
-int tok_type(Token* tok) {
117
-  return tok->type;
118
-}
119
-
120
-const char* tok_value(Token* tok) {
121
-  return tok->value;
122
-}
123
-
124
-const char* tok_anchor(Token* tok) {
125
-  return tok->anchor;
126
-}
127
-
128
-/* Enum values */
129
-
130
-int enum_scalar() { return YAML_SCALAR_EVENT; }
131
-int enum_alias() { return YAML_ALIAS_EVENT; }
132
-int enum_seq_start() { return YAML_SEQUENCE_START_EVENT; }
133
-int enum_seq_end() { return YAML_SEQUENCE_END_EVENT; } 
134
-int enum_map_start() { return YAML_MAPPING_START_EVENT; }
135
-int enum_map_end() { return YAML_MAPPING_END_EVENT; }
136
-int enum_doc_start() { return YAML_DOCUMENT_START_EVENT; }
137
-int enum_doc_end() { return YAML_DOCUMENT_END_EVENT; }
138
-int enum_stream_start() { return YAML_STREAM_START_EVENT; }
139
-int enum_stream_end() { return YAML_STREAM_END_EVENT; }
140 0
deleted file mode 100644
... ...
@@ -1,52 +0,0 @@
1
-#include <yaml.h>
2
-#define LIST_CHUNK_SIZE 128
3
-
4
-typedef struct {
5
-  char* anchor;
6
-  char* value;
7
-  int type;
8
-} Token;
9
-
10
-/* To prevent frequent reallocations, lists initially
11
-  allocates space for 64 objects, and adds another 64
12
-  every time the list needs more space */
13
-
14
-/* TokenList */
15
-
16
-typedef struct {
17
-  Token* data;
18
-  size_t len;
19
-  size_t cap;
20
-  const char* err;
21
-} TokenList;
22
-
23
-TokenList* createTokenList(void);
24
-void appendToken(TokenList* list, Token tok);
25
-void destroyTokenList(TokenList* list);
26
-const char* copy(const char* source);
27
-
28
-TokenList* tokenize(const char* str, size_t len);
29
-
30
-
31
-/* Accessors */
32
-
33
-size_t list_len(TokenList* list);
34
-Token* nth_tok(TokenList* list, size_t n);
35
-void destroy_nth_tok(TokenList* list, size_t n);
36
-const char* list_err(TokenList* list);
37
-int tok_type(Token* tok);
38
-const char* tok_value(Token* tok);
39
-const char* tok_anchor(Token* tok);
40
-
41
-/* Enum values */
42
-
43
-int enum_scalar();
44
-int enum_alias();
45
-int enum_seq_start();
46
-int enum_seq_end();
47
-int enum_map_start();
48
-int enum_map_end();
49
-int enum_doc_start();
50
-int enum_doc_end();
51
-int enum_stream_start();
52
-int enum_stream_end();
53 0
deleted file mode 100644
... ...
@@ -1,23 +0,0 @@
1
-(in-package :yaml-test)
2
-
3
-(defun make-gen ()
4
-  (lambda () (random 99)))
5
-
6
-(defun generate-list (gen &optional (len 100))
7
-  (yaml:emit (loop repeat len collecting (funcall gen))))
8
-
9
-(defun benchmark (str)
10
-  (format t "Bytes: ~A~&Time to parse:~&" (length str))
11
-  (time (yaml:parse str))
12
-  (terpri))
13
-
14
-(defun benchmark-len (n)
15
-  (benchmark (generate-list (make-gen) n)))
16
-
17
-(defun run-benchmarks ()
18
-  (let ((gen (make-gen)))
19
-    (loop for i from 1 to 256 do
20
-	 (benchmark (generate-list (* 64 i))))))
21
-
22
-(export 'benchmark-len)
23
-(export 'run-benchmarks)
24 0
deleted file mode 100644
... ...
@@ -1,39 +0,0 @@
1
-(defpackage yaml-test
2
-  (:use :cl :fiveam)
3
-  (:import-from :yaml
4
-                :parse
5
-                :emit))
6
-(in-package :yaml-test)
7
-
8
-(def-suite yaml
9
-  :description "General unit tests.")
10
-
11
-(test finishes
12
-  (finishes
13
-    (parse "foo")
14
-    (parse "bar")
15
-    (parse "[1,2,3]")
16
-    (parse "[[a,b],[c,d]]")
17
-    (parse "{a : 1, b : 2, c : 3}")
18
-    (parse "{first : [1,2,{a : 1, b : 2}], second: {fst : a, snd : b}}")))
19
-
20
-(test flat-int
21
-  (is (equal (parse "[1,2,3]") (list 1 2 3))))
22
-
23
-(test flat-str
24
-  (is (equal (parse "[\"foo\",\"bar\"]") (list "foo" "bar"))))
25
-
26
-(test nested
27
-  (is (equal (parse "[[a,1],[b,2],[c,3]]")
28
-             (list (list "a" 1)
29
-                   (list "b" 2)
30
-                   (list "c" 3)))))
31
-
32
-(test flat-map
33
-  (is (equalp (parse "{a : 1, b : 2}")
34
-              (let ((map (make-hash-table :test #'equal)))
35
-                (setf (gethash "a" map) 1
36
-                      (gethash "b" map) 2)
37
-                map))))
38
-
39
-(run!)