Created
November 1, 2013 13:18
-
-
Save aturley/7265301 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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