All Downloads are FREE. Search and download functionalities are using the official Maven repository.

angch.venice.1.12.30.source-code.list-parser.venice Maven / Gradle / Ivy

There is a newer version: 1.12.34
Show newest version
;;;;   __    __         _
;;;;   \ \  / /__ _ __ (_) ___ ___
;;;;    \ \/ / _ \ '_ \| |/ __/ _ \
;;;;     \  /  __/ | | | | (_|  __/
;;;;      \/ \___|_| |_|_|\___\___|
;;;;
;;;;
;;;; Copyright 2017-2024 Venice
;;;;
;;;; Licensed under the Apache License, Version 2.0 (the "License");
;;;; you may not use this file except in compliance with the License.
;;;; You may obtain a copy of the License at
;;;;
;;;;     http://www.apache.org/licenses/LICENSE-2.0
;;;;
;;;; Unless required by applicable law or agreed to in writing, software
;;;; distributed under the License is distributed on an "AS IS" BASIS,
;;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;;; See the License for the specific language governing permissions and
;;;; limitations under the License.

;;;; A simple Parsatron parser for lists

(do
  (load-module :parsatron)
  (ns-alias 'p 'parsatron)

  (p/defparser digits []
    (p/many1 (p/digit)))

  (p/defparser positive-int []
    (p/let->> [i (digits)]
      (p/always (int (apply str i)))))

  (p/defparser negative-int []
    (p/let->> [i (p/>> (p/char "-") (digits))]
      (p/always (int (str "-" (apply str i))))))

  (p/defparser lst-integer []
    (p/either (positive-int)
              (negative-int)))

  (p/defparser whitespaces []
    (p/many (p/token str/whitespace?)))

  (p/defparser whitespaces1 []
    (p/many1 (p/token str/whitespace?)))

  (p/defparser lst-string []
    (p/between (p/char "'")
               (p/char "'")
               (p/let->> [s (p/many (p/not-char "'"))]
                  (p/always (apply str s)))))

  (p/defparser lst-list-next-value []
    (p/>> (whitespaces)
          (p/char ",")
          (whitespaces)
          (lst-value)))

  (p/defparser lst-list-values []
    (p/let->> [first (lst-value)
               rest  (p/many (lst-list-next-value))]
      (p/always (into [ first ] rest))))

  (p/defparser lst-list []
    (p/between (p/>> (p/char "[")
                     (whitespaces))
               (p/>> (whitespaces)
                     (p/char "]"))
               (lst-list-values)))

  (p/defparser lst-dictionary-entry []
    (p/let->> [key (lst-value)
               _   (whitespaces1)
               val (lst-value)]
      (p/always [key val])))

  (p/defparser lst-dictionary-next-entry []
    (p/>> (whitespaces)
          (p/char ",")
          (whitespaces)
          (lst-dictionary-entry)))

  (p/defparser lst-dictionary-entries []
    (p/let->> [first (lst-dictionary-entry)
               rest  (p/many (lst-dictionary-next-entry))]
      (p/always (into [ first ] rest))))

  (p/defparser lst-dictionary []
    (p/between (p/>> (p/char "{") (whitespaces))
               (p/>> (whitespaces) (p/char "}"))
               (p/let->> [entries (lst-dictionary-entries)]
                  (p/always (into {} entries)))))

  (p/defparser lst-value []
    (p/choice (lst-integer)
              (lst-string)
              (lst-list)
              (lst-dictionary)))

  ;(p/run (lst-value) "42")
  ;(p/run (lst-value) "'42'")
  ;(p/run (lst-value) "[0, 1, 2, 3, 4]")
  ;(p/run (lst-value) "[0,1,2,3,4,[10,11,12], 'abc']")
  ;(p/run (lst-value) "{'a' 100, 'b' 200}")
  ;(p/run (lst-value) "[0,1,2,3,4,[10,11,12], 'abc', {'a' 100, 'b' 200}]")
)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy