• Re: Simple Parser Lisp

    From B. Pym@21:1/5 to All on Mon Sep 2 11:18:42 2024
    If I want to parse a file:

    stuff
    stuff
    stuff
    stuff
    stuff
    ...
    ...
    //[start]
    1. Read This text here
    2. Read This text here
    3. Read This text here
    //[end]
    ...
    ...


    One Way,

    (defun read-//[start]-to-//[end] (file)
    (with-open-file (*standard-input* file :direction :input)
    (loop for line = (read-line)
    while (not (string= "//[start]" line :end2 (min (length line) 9))))
    (loop for line = (read-line)
    while (not (string= "//[end]" line :end2 (min 7 (length line)))) collect
    line)))

    CL-USER 1 > (read-//[start]-to-//[end] "test.txt")
    ("1. Read This text here" "2. Read This text here" "3. Read This text here")

    Gauche Scheme

    (use srfi-42) ;; list-ec

    (define (read-start-to-end file)
    (with-input-from-file file (lambda()
    (until (equal? "//[start]" (read-line)))
    (list-ec (:while (:generator line read-line)
    (not (equal? line "//[end]")))
    line))))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)