Skip to content

Instantly share code, notes, and snippets.

@ponkore

ponkore/core.clj

Last active Aug 29, 2015
Embed
What would you like to do?
instaparse example 1 (clojure sexp grammer)
(ns parser.core
(:require [clojure.java.io :as io]
[clojure.string :as str]
[clojure.edn :as edn]
[instaparse.core :as insta]))
;; TODO comment and newline support
;; newline = '\n' | '\r\n'
;; <comment-start> = ';'
;; <comment> = comment-start #'.*' newline
;; :
(def sexp
(insta/parser
"S = ws* sexpr ws*
whitespace = ' ' | '\t'
<comma> = ','
<ws> = <whitespace> | <comma>
<lpar> = <'('>
<rpar> = <')'>
<map-start> = <'{'>
<map-end> = <'}'>
<set-start> = <'#{'>
<set-end> = <'}'>
<array-start> = <'['>
<array-end> = <']'>
integer = #'[+-]?\\d+'
double = #'[+-]?\\d+\\.\\d*'
<number> = ( integer | double )
<dquote> = '\"'
<not-dquote> = #'[^\"]'
string = <dquote> not-dquote* <dquote>
<identifier-class> = #'[\\p{Alpha}\\-_$!%&<>=][\\d\\p{Alpha}\\-_$!%&<>=]*'
identifier = identifier-class
keyword = <':'> identifier-class
quoted = <'\\''> ws* sexpr
<sexpr> = string | identifier | number | keyword | list | array | map | set | quoted
list = lpar ( sexpr | ws )* rpar
array = array-start ( sexpr | ws )* array-end
map-key = sexpr
map-val = sexpr
map = map-start ws* ( map-key ws* map-val ws* )* map-end
set = set-start ( sexpr | ws )* set-end
"))
;; (sexp " ( -999.99 888 [77.7 -66. ] a-b_c3$d {:d, 999, :e-e9_$e fff} #{x -0} ' (de-fg hi$ ) j \"string string\" \"\") ")
;; =>
;; [:S
;; [:list
;; [:double "-999.99"]
;; [:integer "888"]
;; [:array [:double "77.7"] [:double "-66."]]
;; [:identifier "a-b_c3$d"]
;; [:map
;; [:map-key [:keyword "d"]]
;; [:map-val [:integer "999"]]
;; [:map-key [:keyword "e-e9_$e"]]
;; [:map-val [:identifier "fff"]]]
;; [:set [:identifier "x"] [:integer "-0"]]
;; [:quoted [:list [:identifier "de-fg"] [:identifier "hi$"]]]
;; [:identifier "j"]
;; [:string "s" "t" "r" "i" "n" "g" " " "s" "t" "r" "i" "n" "g"]
;; [:string]]]
@ponkore

This comment has been minimized.

Copy link
Owner Author

@ponkore ponkore commented May 6, 2015

コメントと全角文字列のIDへの対応が残ってる...。

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.