Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(ns phpon.core
(:require [instaparse.core :as insta]))
(def parse-awful
(insta/parser
"
<S> = (ARRAY / INTEGER / STRING)
ARRAY = <'a:'> SIZE <':'> REST
INTEGER = <'i:'> INT <';'>
STRING = <'s:'> SIZE <':\"'> REST
<SIZE> = INT
<INT> = #'\\p{Digit}+'
REST = #'.*'
"
))
(def parser
(insta/parser
"
<OBJECT> = SPACE* EXP SPACE*
<EXPS> = (SPACE* EXP SPACE* <COMMA> SPACE*)* EXP
<EXP> = (NUMBER / STRING / LIST / MAP)
MAP = <'['> SPACE* KEY_VALUES SPACE* <']'>
<KEY_VALUES> = (KEY_VALUE SPACE* <COMMA> SPACE*)* KEY_VALUE
KEY_VALUE = STRING SPACE* <ARROW> SPACE* EXP
LIST = <'['> EXPS <']'>
STRING = <'\\''> #'([^\\'\\\\]|\\\\.)*' <'\\''>
<NUMBER> = (INTEGER / DECIMAL)
DECIMAL = #'\\p{Digit}+\\.\\p{Digit}+'
INTEGER = #'\\p{Digit}+'
<ARROW> = '=>'
<SPACE> = <#'[ \t\n]+'>
<COMMA> = ','
"))
(defn ->sexp [[head & tail]]
(case head
(:KEY_VALUE) (into [] (map ->sexp tail))
(:MAP) (into {} (map ->sexp tail))
(:LIST) (into [] (map ->sexp tail))
(:STRING) (first tail)
(:INTEGER) (Integer. (first tail))
(:DECIMAL) (Double. (first tail))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.