Skip to content

Instantly share code, notes, and snippets.

@ApricotLace
Created November 6, 2020 23:41
Show Gist options
  • Save ApricotLace/e37f0411d6efe6fbefc726517f4e4126 to your computer and use it in GitHub Desktop.
Save ApricotLace/e37f0411d6efe6fbefc726517f4e4126 to your computer and use it in GitHub Desktop.
(ns bonch.core
  (:require [tilakone.core :as tk :refer [_]]
            [clojure.string :as str]))
(def state-schema
  [{::tk/name        :start
    ::tk/transitions [{::tk/on #"[\[\]\(\)\{\}]",  ::tk/actions [[[:add :paren]]]}
                      {::tk/on #"[^\[\]\(\)\{\}\d\s;:]+" ::tk/to :symbol? ::tk/actions [[[:stash]]]}
                      {::tk/on #"[\s,\n]" ::tk/actions [[[:add :delimiter]]]}
                      {::tk/on #":" ::tk/to :keyword? ::tk/actions [[[:stash]]]}
                      {::tk/on #"\"" ::tk/to :string? ::tk/actions [[[:stash]]]}
                      {::tk/on #"\d" ::tk/to :number? ::tk/actions [[[:stash]]]}
                      {::tk/on #";"  ::tk/to :comment? ::tk/actions [[[:stash]]]}
                      {::tk/on _}]}
   {::tk/name        :comment?
    ::tk/transitions [{::tk/on #"[^\n]+"  ::tk/actions [[[:stash]]]}
                      {::tk/on #"[\n]"  ::tk/to :start ::tk/actions [[[:flush :comment] [:add :delimiter]]]}]}
   {::tk/name        :symbol?
    ::tk/transitions [{::tk/on #"[^\[\]\(\)\{\}:\s]+"  ::tk/actions [[[:stash]]]}
                      {::tk/on #"[\s,\n]"  ::tk/to :start ::tk/actions [[[:flush :symbol] [:add :delimiter]]]}
                      {::tk/on #"[\[\]\(\)\{\}]" ::tk/to :start ::tk/actions [[[:flush :symbol] [:add :paren]]]}]}
   {::tk/name        :keyword?
    ::tk/transitions [{::tk/on #"[^\[\]\(\)\{\}:\s]+"  ::tk/actions [[[:stash]]]}
                      {::tk/on #"[\s,\n]"  ::tk/to :start ::tk/actions [[[:flush :keyword] [:add :delimiter]]]}
                      {::tk/on #"[\[\]\(\)\{\}]" ::tk/to :start ::tk/actions [[[:flush :keyword] [:add :paren]]]}]}
   {::tk/name        :string?
    ::tk/transitions [{::tk/on #"[^\"]+"  ::tk/actions [[[:stash]]]}
                      {::tk/on #"[\"]"  ::tk/to :start ::tk/actions [[[:flush :string-literal] [:add :delimiter]]]}]}
   {::tk/name        :number?
    ::tk/transitions [{::tk/on #"[\d.]"  ::tk/actions [[[:stash]]]}
                      {::tk/on #"[\s,\n]"  ::tk/to :start ::tk/actions [[[:flush :number-literal] [:add :delimiter]]]}
                      {::tk/on #"[\[\]\(\)\{\}]" ::tk/to :start ::tk/actions [[[:flush :number-literal] [:add :paren]]]}]}])
(def process
  {::tk/states  state-schema
   ::tk/match?  (fn [{::tk/keys [signal on] :as fsm}]
                  (condp = (-> on type str)
                    "class java.util.regex.Pattern"
                    (boolean (re-find on (str signal)))
                    (= (str signal) on)))
   ::tk/action! (fn [{::tk/keys [action signal process] :as fsm}]
                  (println signal action (:tilakone.core/state process))
                  (reduce
                   (fn [acc act]
                     (let [[f & args] act]
                       (case f
                         :add (update-in acc [:tilakone.core/process :state :result] conj [(str signal) (first args)])
                         :stash (update-in acc [:tilakone.core/process :state :stash] str (str signal))
                         :flush (-> acc
                                    (update-in [:tilakone.core/process :state :result]
                                               conj [(get-in acc [:tilakone.core/process :state :stash])
                                                     (first args)])
                                    (assoc-in [:tilakone.core/process :state :stash] "")))))
                   fsm action))
   ::tk/state   :start
   :state       {:result []
                 :stash  ""}})
(->> [";;Fancy comment
(defn test [arg]
          (do {:foo 12.05 :boo \"test-string\"}
              (-> arg inc dec)))"]
     (map (partial reduce tk/apply-signal process))
     last
     :state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment