@nextjournal/lezer-clojure
Version:
lezer-based Clojure(Script) grammar
328 lines (254 loc) • 7.8 kB
Plain Text
# Add
(+ 1 2)
==>
Program(List(Operator(Symbol),Number,Number))
# Numbers
42 +42 -42
==>
Program(Number,Number,Number)
# Number BigInt
42N +42N -42N 0 0N
==>
Program(Number,Number,Number,Number,Number)
# Number Leading Zero
042 +042 -042
==>
Program(Number,Number,Number)
# Number BigInt
9223372036854775808 -9223372036854775808 10000000000000000000000000000000000000000000000000 -10000000000000000000000000000000000000000000000000
==>
Program(Number,Number,Number,Number)
# Number Exponentials
0x42e +0x42e -0x42e
==>
Program(Number,Number,Number)
# Number Floating
42.23 +42.23 -42.23
==>
Program(Number,Number,Number)
# Number Floating
42.23 +42.23 -42.23
==>
Program(Number,Number,Number)
# Number BigDecimal
42.23M +42.23M -42.23M
==>
Program(Number,Number,Number)
# Number BigDecimal 2
1.0M +1.0M -1.0M +0.0M +0.M 0M -0.0M -0.M
==>
Program(Number,Number,Number,Number,Number,Number,Number,Number)
# Number Floating e
42.2e3 +42.2e+3 -42.2e-3
==>
Program(Number,Number,Number)
# Number Floating e3M
42.2e3M +42.2e+3M -42.2e-3M
==>
Program(Number,Number,Number)
# Number Double
+1.0e+1 +1.e+1 +1e+1 +1.0e1 +1.e1 +1e1
==>
Program(Number,Number,Number,Number,Number,Number)
# Number Double 2
+1.0e-1 +1.e-1 +1e-1 -1.0e1 -1.e1 -1e1 -1.0e-1 -1.e-1 -1e-1
==>
Program(Number,Number,Number,Number,Number,Number,Number,Number,Number)
# Number Double 3
+1.0 +1. 1.0 1. +0.0 0.0 -0.0 +0. -0. -1.0 -1.
==>
Program(Number,Number,Number,Number,Number,Number,Number,Number,Number,Number,Number)
# Number Ratio
4/2 +4/2 -4/2
==>
Program(Number,Number,Number)
# Literals
() {} []
==>
Program(List, Map, Vector)
# Sharps
#{} #"\d" #:kw{} #::{}
==>
Program(Set(Map), RegExp(String(StringContent)), NamespacedMap(KeywordPrefix,Map), NamespacedMap(KeywordPrefix,Map))
# Symbols
f1 foo/bar *+!-_? abc:def:ghi abc.def/ghi abc/def.ghi abc:def/ghi:jkl.mno a/ /
==>
Program(Symbol,Symbol,Symbol,Symbol,Symbol,Symbol,Symbol,Symbol,Symbol)
# Keywords
:hi :hi/ho ::ho ::hi/ho :*+!-_? :abc:def:ghi
==>
Program(Keyword,Keyword,Keyword,Keyword,Keyword,Keyword)
# Boolean
true false
==>
Program(Boolean,Boolean)
# Strings
"hello" "and
multiline"
==>
Program(String(StringContent), String(StringContent))
# Nil
nil
==>
Program(Nil)
# simple chars
\a \b \n \r \s
==>
Program(Character,Character,Character,Character,Character)
# other chars
\@ \; \[ \) \# \$ \' \\ \"
==>
Program(Character,Character,Character,Character,Character,Character,Character,Character,Character)
# unicode
\u0194 \uFFFF
==>
Program(Character,Character)
# octal
\o123 \o372 \o0
==>
Program(Character, Character, Character)
# special-case chars
\newline \space \backspace \tab \formfeed \return
==>
Program(Character,Character,Character,Character,Character,Character)
# Line comments
;; comment
;; another comment
==>
Program(LineComment, LineComment)
# Discard next form
:hello #_ :ignored
==>
Program(Keyword, Discard(Keyword))
# Discard next two forms
#_#_ :ignored also-ignored "not-ignored"
==>
Program(Discard(Discard(Keyword), Symbol), String(StringContent))
# Discarded Tagged Literal
#_#hello/world [1] :foo
==>
Program(Discard("#_", TaggedLiteral(ReaderTag("#") Vector("[",Number,"]"))), Keyword)
# Deref
@hello
==>
Program(Deref(Symbol))
# Quote
'bar
==> Program(Quote(Symbol))
# SyntaxQuote
`(1)
==> Program(SyntaxQuote(List(Number)))
# SyntaxQuote Unquote
`(vector ~x)
==> Program(SyntaxQuote(List(Operator(Symbol),Unquote(Symbol))))
# SyntaxQuote Unquote-Splice
`(~@x)
==> Program(SyntaxQuote(List(UnquoteSplice(Symbol))))
# Meta
^{:hello :world} :my-expr #^String :more ^:dynamic :test
==> Program(Meta(Metadata(Map(Keyword,Keyword)),Keyword),Meta(ReaderMetadata(Symbol),Keyword),Meta(Metadata(Keyword),Keyword))
# Meta Multiple
^:dynamic ^ints obj
==> Program(Meta(Metadata(Keyword),Meta(Metadata(Symbol),Symbol)))
# Meta Operator
(^:dynamic + 1)
==> Program(List(Meta(Metadata(Keyword),Operator(Symbol)), Number))
# Meta Multiple Operator
(^:dynamic ^ints + 1 2)
==> Program(List(Meta(Metadata(Keyword),Meta(Metadata(Symbol),Operator(Symbol))), Number, Number))
# Var
#'hello
==> Program(Var(Symbol))
# Reader Conditional
#?(:cljs "hello") #?@(:cljs ["hello"])
==> Program(ReaderConditional("#?", List(Keyword,String(StringContent))),ReaderConditional(Deref(List(Keyword,Vector(String(StringContent))))))
# Built-in Tagged Literal
#uuid "31d20ceb-c7c2-40bb-9bd5-1e1e05f57ea1"
==> Program(TaggedLiteral(ReaderTag("#"), String(StringContent)))
# Reader Tag with namespace
#my/foo [1]
==> Program(TaggedLiteral(ReaderTag("#"),Vector("[",Number,"]")))
# Tagged Literal with multiple tags
#tag/one #tag/two []
==> Program(TaggedLiteral(ReaderTag("#"), TaggedLiteral(ReaderTag("#"), Vector)))
# Symbolic Value
##Inf, ##-Inf
==> Program(SymbolicValue, SymbolicValue)
# Anonymous Function
#(inc %)
==> Program(AnonymousFunction("#", List(Operator(Symbol), Symbol)))
# Unbalanced map
{:x}
==> Program(Map(Keyword))
# Gensym
sym#
==> Program(Symbol)
# Earmuffs and channel macros
*sym* >! <! !x x? x! _ , . ..
==> Program(Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol)
# Def
(def foo)
==> Program(List("(",DefLike,VarName(Symbol),")"))
# Def + String
(defn foo "string")
==> Program(List(DefLike,VarName(Symbol),String(StringContent)))
# Def + DocString
(defn foo "string" :bar)
==> Program(List(DefLike,VarName(Symbol),DocString(String(StringContent)),Keyword))
# Meta Multiple Def
(^:hello ^world def foo)
==> Program(List(Meta(Metadata(Keyword),Meta(Metadata(Symbol),DefLike)), VarName(Symbol)))
# Meta Multiple VarName
(def ^:static ^clojure.lang.ChunkBuffer chunk-buffer)
==> Program(List(DefLike,Meta(Metadata(Keyword),Meta(Metadata(Symbol),VarName(Symbol)))))
# Def Defn
(def defn (fn []))
==> Program(List(DefLike,VarName(Symbol),List(Operator(Symbol),Vector)))
# Defonce
(defonce default "doc" default)
==> Program(List(DefLike,VarName(Symbol),DocString(String(StringContent)),Symbol))
# Def Default
(def default bar)
==> Program(List(DefLike,VarName(Symbol),Symbol))
# Defmacro
(defmacro ..)
==> Program(List(DefLike,VarName(Symbol)))
# Division
/
==> Program(Symbol)
# Constructor Call with Vector
#my.klass_or_type_or_record[:a :b :c]
==> Program(ConstructorCall(ConstructorPrefix("#"),Vector("[",Keyword,Keyword,Keyword,"]")))
# Constructor Call with Vector
#my.γωνστρυγτωρ[:a :b :c]
==> Program(ConstructorCall(ConstructorPrefix("#"),Vector("[",Keyword,Keyword,Keyword,"]")))
# Constructor Call with Record
#my.record{:a 1, :b 2}
==> Program(ConstructorCall(ConstructorPrefix("#"),Map("{",Keyword,Number,Keyword,Number,"}")))
# Constructor with Reader Tags
#read/this #my.constructor [] #read/that #my.record {}
==> Program(TaggedLiteral(ReaderTag("#"),ConstructorCall(ConstructorPrefix("#"),Vector("[","]"))),
TaggedLiteral(ReaderTag("#"),ConstructorCall(ConstructorPrefix("#"),Map("{","}"))))
# Namespace Form
(ns some.ns.name
"This is a docstring"
(:require [foo.bar]
[ban.dan]))
==> Program(List(NS,VarName(Symbol),DocString(String(StringContent)),List(Keyword,Vector(Symbol),Vector(Symbol))))
# Namespace Form with Meta
(ns ^:reload some.ns.name
"This is a docstring"
(:require [foo.bar]
[ban.dan]))
==> Program(List(NS,Meta(Metadata(Keyword), VarName(Symbol)),DocString(String(StringContent)),List(Keyword,Vector(Symbol),Vector(Symbol))))
# Namespace with just a docstring
(ns simple.ns "docstring")
==> Program(List(NS, VarName(Symbol), DocString(String(StringContent))))
# Namespace simplest form
(ns very.simple)
==> Program(List(NS, VarName(Symbol)))
# Namespace with attributes
(ns cljdocs.example.core
"This is a doc string, FYI :D"
{:author "John Doe"})
==> Program(List(NS, VarName(Symbol), DocString(String(StringContent)), Map(Keyword, String(StringContent))))