[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: YAML parser?

From: Matt Wette
Subject: Re: YAML parser?
Date: Sun, 23 Feb 2020 07:29:10 -0800
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1

On 2/21/20 8:12 PM, Aleix Conchillo Flaqué wrote:

does anyone know if there's any YAML parser for Guile? Haven't been able to
find any.



Hi Aleix,

I don't know of a YAML parser for Guile, but if you look at my email posted 2/22 I have a Guile package called NYACC.  This includes a "FFI Helper" that can generate the Guile FFI code based on yaml.h from libyaml.   The API is going to be C-like but if you do some
work to paste something on the front you will have something, I think.

Below I have a demo program and the yaml.ffi file used to generate yaml.scm.
When executed, the demo program outputs the following:

$ guile demo1.scm
#<yaml_node_t* 0x56353bc4af80>

The demo program: =====================================
(use-modules ((system foreign) #:prefix ffi:))
(use-modules (bytestructures guile))
(use-modules (system ffi-help-rt))
(use-modules (ffi yaml))

(define (sf fmt . args) (apply simple-format #t fmt args))

(define fopen
  (let ((~fopen (ffi:pointer->procedure
         '* (dynamic-func "fopen" (dynamic-link)) (list '* '*))))
    (lambda (filename mode)
      (~fopen (ffi:string->pointer filename) (ffi:string->pointer mode)))))

(define fclose
  (let ((~fclose (ffi:pointer->procedure
         ffi:int (dynamic-func "fclose" (dynamic-link)) (list '*))))
    (lambda (file)
      (~fclose file))))

(let* ((filename "demo1.yml")
       (parser (make-yaml_parser_t))
       (&parser (pointer-to parser))
       (document (make-yaml_document_t))
       (&document (pointer-to document))
       (file (fopen filename "r"))
       (root #f))
  (yaml_parser_initialize &parser)
  (yaml_parser_set_input_file &parser file)
  (yaml_parser_load &parser &document)
  (set! root (yaml_document_get_root_node &document))
  (sf "~S\n" root)
  (sf "~S\n" (wrap-yaml_node_type_t (fh-object-ref root 'type)))
  (yaml_document_delete &document)
  (yaml_parser_delete &parser)
  (fclose file))

The yaml.ffi file (included in nyacc): =========================
;; yaml.ffi                -*- Scheme -*-

;; Copyright (C) 2020 Matthew R. Wette
;; Copying and distribution of this file, with or without modification,
;; are permitted in any medium without royalty provided the copyright
;; notice and this notice are preserved.  This file is offered as-is,
;; without any warranty.

(define-ffi-module (ffi yaml)
  #:include '("yaml.h")
  #:library '("libyaml"))

;; --- last line ---

reply via email to

[Prev in Thread] Current Thread [Next in Thread]