Last active
August 11, 2025 12:54
-
-
Save sogaiu/5dedc9a997bc3c3a014c081e1789e329 to your computer and use it in GitHub Desktop.
some peg work
This file contains hidden or 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
(def g0 | |
~{:main (* (any (+ (group :delim) (group :pre) (group :raw) ':ch)) -1) | |
:ch (+ (* `\` 1) 1) | |
:ws (+ :s -1) | |
:delim (+ :st-delim :em-delim :ln-delim) | |
:st-delim (+ (* (constant :open) '(+ "**" "__") (not :ws)) | |
(* (constant :close) '(+ "**" "__") (not (> -3 :ws)))) | |
:em-delim (+ (* (constant :open) '(+ "*" "_") (not :ws)) | |
(* (constant :close) '(+ "*" "_") (not (> -2 :ws)))) | |
:ln-delim (+ (* (constant :open) '"[") | |
(* (constant :close) '"][]") | |
(* (constant :close) '(* "](" (some (if-not ")" :ch)) ")"))) | |
:pre (* (constant :pre) "`" '(some (if-not "`" 1)) "`") | |
:raw (* (constant :raw) (only-tags (<- (some "`") :rd)) '(to (backmatch :rd))) | |
}) | |
(comment | |
(peg/match g0 "*abc*") | |
# => | |
@[@[:open "*"] "a" "b" "c" @[:close "*"]] | |
(peg/match g0 "_xyyz_") | |
# => | |
@[@[:open "_"] "x" "y" "y" "z" @[:close "_"]] | |
(peg/match g0 "`smile`") | |
# => | |
@[@[:pre "smile"]] | |
(peg/match g0 "``a``") | |
# => | |
@[@[:raw "a"] "`" "`"] | |
) | |
(def g0 | |
~{:main (sequence (any (choice (group :delim) | |
(group :pre) | |
(group :raw) | |
(capture :ch))) | |
-1) | |
:ch (choice (sequence `\` 1) 1) | |
:ws (choice :s -1) | |
:delim (choice :st-delim :em-delim :ln-delim) | |
:st-delim (choice (sequence (constant :open) | |
(capture (choice "**" "__")) | |
(not :ws)) | |
(sequence (constant :close) | |
(capture (choice "**" "__")) | |
(not (look -3 :ws)))) | |
:em-delim (choice (sequence (constant :open) | |
(capture (choice "*" "_")) | |
(not :ws)) | |
(sequence (constant :close) | |
(capture (choice "*" "_")) | |
(not (look -2 :ws)))) | |
:ln-delim (choice (sequence (constant :open) | |
(capture "[")) | |
(sequence (constant :close) | |
(capture "][]")) | |
(sequence (constant :close) | |
(capture (sequence "](" | |
(some (if-not ")" :ch)) | |
")")))) | |
:pre (sequence (constant :pre) | |
"`" | |
(capture (some (if-not "`" 1))) | |
"`") | |
:raw (sequence (constant :raw) | |
(only-tags (capture (some "`") :rd)) | |
(capture (to (backmatch :rd)))) | |
}) | |
(comment | |
(peg/match g0 "*abc*") | |
# => | |
@[@[:open "*"] "a" "b" "c" @[:close "*"]] | |
(peg/match g0 "_xyyz_") | |
# => | |
@[@[:open "_"] "x" "y" "y" "z" @[:close "_"]] | |
(peg/match g0 "`smile`") | |
# => | |
@[@[:pre "smile"]] | |
(peg/match g0 "``a``") | |
# => | |
@[@[:raw "a"] "`" "`"] | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment