Skip to content

Instantly share code, notes, and snippets.

@aturley
Created November 1, 2013 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aturley/7265301 to your computer and use it in GitHub Desktop.
Save aturley/7265301 to your computer and use it in GitHub Desktop.
(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