Browse code
Add a yaml package
Fernando Borretti authored on 31/01/2015 16:19:11
Showing 3 changed files
Showing 3 changed files
... | ... |
@@ -6,7 +6,7 @@ |
6 | 6 |
(:import-from :libyaml.macros |
7 | 7 |
:with-parser |
8 | 8 |
:with-event) |
9 |
- (:export :parse) |
|
9 |
+ (:export :parse-string) |
|
10 | 10 |
(:documentation "The YAML parser.")) |
11 | 11 |
(in-package :yaml.parser) |
12 | 12 |
|
... | ... |
@@ -28,50 +28,51 @@ |
28 | 28 |
(with-event (event) |
29 | 29 |
(loop do |
30 | 30 |
;; Parse the next event, checking for errors |
31 |
- (if (libyaml.parser:parse parser event) |
|
32 |
- ;; Decide what to do with the event |
|
33 |
- (let ((type (libyaml.event:event-type event))) |
|
34 |
- (flet ((add-to-output (data) |
|
35 |
- (vector-push-extend data output))) |
|
36 |
- (cond |
|
37 |
- ;; Stream events |
|
38 |
- ((eql type :stream-start-event) |
|
39 |
- ;; Do nothing |
|
40 |
- t) |
|
41 |
- ((eql type :stream-end-event) |
|
42 |
- (return-from parse-yaml output)) |
|
43 |
- ;; Document events, push them to the output list |
|
44 |
- ((or (eql type :document-start-event) |
|
45 |
- (eql type :document-end-event)) |
|
46 |
- (add-to-output (list type))) |
|
47 |
- ;; Alias and scalar event, push the type and data pair to |
|
48 |
- ;; the output list |
|
49 |
- ((eql type :alias-event) |
|
50 |
- (add-to-output |
|
51 |
- (cons type |
|
52 |
- (libyaml.event:event-alias-data event)))) |
|
53 |
- ((eql type :scalar-event) |
|
54 |
- (add-to-output |
|
55 |
- (cons type |
|
56 |
- (libyaml.event:event-scalar-data event)))) |
|
57 |
- ;; Sequence start and end events |
|
58 |
- ((eql type :sequence-start-event) |
|
59 |
- (add-to-output |
|
60 |
- (cons type |
|
61 |
- (libyaml.event:event-sequence-start-data event)))) |
|
62 |
- ((eql type :sequence-end-event) |
|
63 |
- (add-to-output (list type))) |
|
64 |
- ;; Mapping start and end events |
|
65 |
- ((eql type :mapping-start-event) |
|
66 |
- (add-to-output |
|
67 |
- (cons type |
|
68 |
- (libyaml.event:event-mapping-start-data event)))) |
|
69 |
- ((eql type :mapping-end-event) |
|
70 |
- (add-to-output (list type))) |
|
71 |
- (t |
|
72 |
- t)))) |
|
73 |
- ;; Signal an error |
|
74 |
- (signal-reader-error parser))))))) |
|
31 |
+ (let ((parsing-result (libyaml.parser:parse parser event))) |
|
32 |
+ (if parsing-result |
|
33 |
+ ;; Decide what to do with the event |
|
34 |
+ (let ((type (libyaml.event:event-type event))) |
|
35 |
+ (flet ((add-to-output (data) |
|
36 |
+ (vector-push-extend data output))) |
|
37 |
+ (cond |
|
38 |
+ ;; Stream events |
|
39 |
+ ((eql type :stream-start-event) |
|
40 |
+ ;; Do nothing |
|
41 |
+ t) |
|
42 |
+ ((eql type :stream-end-event) |
|
43 |
+ (return-from parse-yaml output)) |
|
44 |
+ ;; Document events, push them to the output list |
|
45 |
+ ((or (eql type :document-start-event) |
|
46 |
+ (eql type :document-end-event)) |
|
47 |
+ (add-to-output (list type))) |
|
48 |
+ ;; Alias and scalar event, push the type and data pair to |
|
49 |
+ ;; the output list |
|
50 |
+ ((eql type :alias-event) |
|
51 |
+ (add-to-output |
|
52 |
+ (cons type |
|
53 |
+ (libyaml.event:event-alias-data event)))) |
|
54 |
+ ((eql type :scalar-event) |
|
55 |
+ (add-to-output |
|
56 |
+ (cons type |
|
57 |
+ (libyaml.event:event-scalar-data event)))) |
|
58 |
+ ;; Sequence start and end events |
|
59 |
+ ((eql type :sequence-start-event) |
|
60 |
+ (add-to-output |
|
61 |
+ (cons type |
|
62 |
+ (libyaml.event:event-sequence-start-data event)))) |
|
63 |
+ ((eql type :sequence-end-event) |
|
64 |
+ (add-to-output (list type))) |
|
65 |
+ ;; Mapping start and end events |
|
66 |
+ ((eql type :mapping-start-event) |
|
67 |
+ (add-to-output |
|
68 |
+ (cons type |
|
69 |
+ (libyaml.event:event-mapping-start-data event)))) |
|
70 |
+ ((eql type :mapping-end-event) |
|
71 |
+ (add-to-output (list type))) |
|
72 |
+ (t |
|
73 |
+ t)))) |
|
74 |
+ ;; Signal an error |
|
75 |
+ (signal-reader-error parser)))))))) |
|
75 | 76 |
|
76 | 77 |
(defun parse-tokens (vector) |
77 | 78 |
(let ((contexts (list nil))) |
... | ... |
@@ -87,17 +88,21 @@ |
87 | 88 |
(cons :document con))))) |
88 | 89 |
;; Alias event |
89 | 90 |
((:alias-event &key anchor) |
90 |
- (print anchor)) |
|
91 |
+ (declare (ignore anchor)) |
|
92 |
+ t) |
|
91 | 93 |
;; Scalar |
92 | 94 |
((:scalar-event &key anchor tag value) |
95 |
+ (declare (ignore anchor tag)) |
|
93 | 96 |
(setf (first contexts) |
94 | 97 |
(append (first contexts) |
95 | 98 |
(list (yaml.scalar:parse-scalar value))))) |
96 | 99 |
;; Sequence start event |
97 | 100 |
((:sequence-start-event &key anchor tag) |
101 |
+ (declare (ignore anchor tag)) |
|
98 | 102 |
(push (list) contexts)) |
99 | 103 |
;; Mapping start event |
100 | 104 |
((:mapping-start-event &key anchor tag) |
105 |
+ (declare (ignore anchor tag)) |
|
101 | 106 |
(push (list) contexts)) |
102 | 107 |
;; End events |
103 | 108 |
((:sequence-end-event) |
... | ... |
@@ -113,17 +118,11 @@ |
113 | 118 |
(alexandria:plist-hash-table con :test #'equal)))))) |
114 | 119 |
;; Do nothing |
115 | 120 |
((t &rest rest) |
121 |
+ (declare (ignore rest)) |
|
116 | 122 |
t))) |
117 | 123 |
(first contexts))) |
118 | 124 |
|
119 | 125 |
;;; The public interface |
120 | 126 |
|
121 |
-(defgeneric parse (input) |
|
122 |
- (:documentation "Parse a YAML string or a pathname to a YAML file into Lisp |
|
123 |
- data.")) |
|
124 |
- |
|
125 |
-(defmethod parse ((input string)) |
|
126 |
- (parse-tokens (parse-yaml input))) |
|
127 |
- |
|
128 |
-(defmethod parse ((input pathname)) |
|
129 |
- (parse (uiop:read-file-string input))) |
|
127 |
+(defun parse-string (yaml-string) |
|
128 |
+ (parse-tokens (parse-yaml yaml-string))) |
130 | 129 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,16 @@ |
1 |
+(in-package :cl-user) |
|
2 |
+(defpackage yaml |
|
3 |
+ (:use :cl) |
|
4 |
+ (:export :parse) |
|
5 |
+ (:documentation "The main YAML interface.")) |
|
6 |
+(in-package :yaml) |
|
7 |
+ |
|
8 |
+(defgeneric parse (input) |
|
9 |
+ (:documentation "Parse a YAML string or a pathname to a YAML file into Lisp |
|
10 |
+ data.")) |
|
11 |
+ |
|
12 |
+(defmethod parse ((input string)) |
|
13 |
+ (yaml.parser:parse-string input)) |
|
14 |
+ |
|
15 |
+(defmethod parse ((input pathname)) |
|
16 |
+ (parse (uiop:read-file-string input))) |