Skip to content

Instantly share code, notes, and snippets.

@disco0
Forked from sogaiu/lpeg-in-fennel.fnl
Created June 8, 2021 18:05
Show Gist options
  • Save disco0/0f50ac7b759c92b1282f2ccf7656f5a1 to your computer and use it in GitHub Desktop.
Save disco0/0f50ac7b759c92b1282f2ccf7656f5a1 to your computer and use it in GitHub Desktop.
lpeg in fennel
>> (local lpeg (require :lpeg))
nil
>> (local equalcount (lpeg.P {1 "S"
.. "S" (+ (* "a" (lpeg.V "B")) (* "b" (lpeg.V "A")) "")
.. "A" (+ (* "a" (lpeg.V "S")) (* "b" (lpeg.V "A") (lpeg.V "A")))
.. "B" (+ (* "b" (lpeg.V "S")) (* "a" (lpeg.V "B") (lpeg.V "B")))}))
nil
>> (lpeg.match equalcount "aabb")
5
>> (lpeg.match equalcount "baabba")
7
>> (local V lpeg.V)
nil
>> (local equalcount (lpeg.P {1 "S"
.. "S" (+ (* "a" (V "B")) (* "b" (V "A")) "")
.. "A" (+ (* "a" (V "S")) (* "b" (V "A") (V "A")))
.. "B" (+ (* "b" (V "S")) (* "a" (V "B") (V "B")))}))
nil
>> (lpeg.match equalcount "aabb")
5
>> lpeg
{
:B #<function: 0x7f1ab7d31060>
:C #<function: 0x7f1ab7d30ca0>
:Carg #<function: 0x7f1ab7d2ff10>
:Cb #<function: 0x7f1ab7d30030>
:Cc #<function: 0x7f1ab7d30060>
:Cf #<function: 0x7f1ab7d314d0>
:Cg #<function: 0x7f1ab7d31500>
:Cmt #<function: 0x7f1ab7d312c0>
:Cp #<function: 0x7f1ab7d2ff80>
:Cs #<function: 0x7f1ab7d30c70>
:Ct #<function: 0x7f1ab7d30c40>
:P #<function: 0x7f1ab7d30b50>
:R #<function: 0x7f1ab7d31cc0>
:S #<function: 0x7f1ab7d31a30>
:V #<function: 0x7f1ab7d301c0>
:locale #<function: 0x7f1ab7d31b70>
:match #<function: 0x7f1ab7d31110>
:pcode #<function: 0x7f1ab7d31540>
:ptree #<function: 0x7f1ab7d30d10>
:setmaxstack #<function: 0x7f1ab7d2f570>
:type #<function: 0x7f1ab7d2f760>
:version #<function: 0x7f1ab7d2f550>
}
>> (local P lpeg.P)
nil
>> (local S lpeg.S)
nil
>> (local C lpeg.C)
nil
>> (local V lpeg.V)
nil
>> (local Cmt lpeg.Cmt)
nil
>> (local grammar
.. {1 "main"
.. "main" (C (^ (V "input") 1))
.. "input" (+ (V "gap") (V "form"))
.. "gap" (+ (V "ws") (V "comment"))
.. "ws" (^ (S " \f\n\r\t,") 1)
.. "comment" (* ";"
.. (^ (- (P 1) (S "\r\n"))
.. 0))
.. "form" (+ (V "boolean"))
.. "name-char" (- (P 1)
.. (S " \f\n\r\t,[]{}()'`~^@\";"))
.. "boolean" (* (+ (P "false") (P "true"))
.. (- (V "name-char")))
.. })
nil
>> (lpeg.match grammar "; hello")
"; hello"
>> (local grammar
.. {1 "main"
.. "main" (C (^ (V "input") 1))
.. "input" (+ (V "gap") (V "form"))
.. "gap" (+ (V "ws") (V "comment"))
.. "ws" (^ (S " \f\n\r\t,") 1)
.. "comment" (* ";"
.. (^ (- (P 1) (S "\r\n"))
.. 0))
.. "form" (+ (V "boolean") (V "nil") (V "number") (V "keyword")
.. (V "symbol") (V "string") (V "hash-map") (V "list")
.. (V "vector") (V "deref") (V "quasiquote") (V "quote")
.. (V "splice-unquote") (V "unquote") (V "with-meta"))
.. "name-char" (- (P 1)
.. (S " \f\n\r\t,[]{}()'`~^@\";"))
.. "boolean" (* (+ (P "false") (P "true"))
.. (- (V "name-char")))
.. "nil" (* (P "nil")
.. (- (V "name-char")))
.. "number" (Cmt (C (^ (- (P 1)
.. (S " \f\n\r\t,[]{}()'`~^@\";"))
.. 1))
.. (fn [s i a]
.. (values i (tonumber a))))
.. "keyword" (* ":"
.. (^ (V "name-char") 0))
.. "symbol" (^ (V "name-char") 1)
.. "string" (* (P "\"")
.. (^ (- (P 1)
.. (S "\"\\"))
.. 0)
.. (^ (* (P "\"")
.. (P 1)
.. (^ (- (P 1)
.. (S "\"\\"))
.. 0))
.. 0)
.. (+ (P "\"")
.. (P (fn [s i]
.. (error "unbalanced \"")))))
.. "hash-map" (* (P "{")
.. (^ (V "input") 0)
.. (+ (P "}")
.. (P (fn [s i]
.. (error "unbalanced }")))))
.. "list" (* (P "(")
.. (^ (V "input") 0)
.. (+ (P ")")
.. (P (fn [s i]
.. (error "unbalanced )")))))
.. "vector" (* (P "[")
.. (^ (V "input") 0)
.. (+ (P "]")
.. (P (fn [s i]
.. (error "unbalanced ]")))))
.. "deref" (* (P "@")
.. (V "form"))
.. "quasiquote" (* (P "`")
.. (V "form"))
.. "quote" (* (P "'")
.. (V "form"))
.. "splice-unquote" (* (P "~@")
.. (V "form"))
.. "unquote" (* (P "~")
.. (V "form"))
.. "with-meta" (* (P "^")
.. (V "form")
.. (^ (V "gap") 1)
.. (V "form"))
.. })
nil
>> (lpeg.match grammar "^{:a 1} [:x :y]")
"^{:a 1} [:x :y]" nil 1 nil nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment