1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-24 17:27:18 +00:00

Add quote special to peg syntax to make captures terser.

This commit is contained in:
Calvin Rose 2019-01-17 19:28:42 -05:00
parent 95f2bbe0a0
commit c4114fbcdb
3 changed files with 14 additions and 12 deletions

View File

@ -138,6 +138,7 @@ that can make many grammars simpler.
| ------- | ---------------- |
| `(capture patt ?tag)` | Captures all of the text in patt if patt matches, If patt contains any captures, then those captures will be pushed to the capture stack before the total text. |
| `(<- patt ?tag)` | Alias for `(capture patt ?tag)` |
| `(quote patt ?tag)` | Another alias for `(capture patt ?tag)`. This allows code like `'patt` to capture pattern. |
| `(group patt ?tag) ` | Captures an array of all of the captures in patt.
| `(replace patt subst ?tag)` | Replaces the captures produced by patt by applying subst to them. If subst is a table or struct, will push `(get subst last-capture)` to the capture stack after removing the old captures. If a subst is a function, will call subst with the captures of patt as arguments and push the result to the capture stack. Otherwise, will push subst literally to the capture stack. |
| `(/ patt subst ?tag)` | Alias for `(replace patt subst ?tag)` |

View File

@ -878,6 +878,7 @@ static const SpecialPair specials[] = {
{"not", spec_not},
{"opt", spec_opt},
{"position", spec_position},
{"quote", spec_capture},
{"range", spec_range},
{"replace", spec_replace},
{"sequence", spec_sequence},

View File

@ -42,13 +42,13 @@
:symchars (+ (range "09" "AZ" "az" "\x80\xFF") (set "$%&*+-./:<=>?@^_|"))
:token (some :symchars)
:hex (range "09" "af" "AF")
:escape (* "\\" (+ (set "ntr0\"\\e")
(* "h" :hex :hex)
:escape (* "\\" (+ (set "ntrzf0\"\\e")
(* "x" :hex :hex)
(error (constant "bad hex escape"))))
:comment ,(<-c :comment ~(* "#" (any (if-not (+ "\n" -1) 1))))
:symbol (/ (<- :token) ,color-symbol)
:symbol (/ ':token ,color-symbol)
:keyword ,(<-c :keyword ~(* ":" (any :symchars)))
:constant ,(<-c :constant ~(+ "true" "false" "nil"))
:bytes (* "\"" (any (+ :escape (if-not "\"" 1))) "\"")
@ -56,25 +56,25 @@
:buffer ,(<-c :string ~(* "@" :bytes))
:long-bytes {:delim (some "`")
:open (capture :delim :n)
:close (cmt (* (not (> -1 "`")) (-> :n) (<- :delim)) ,=)
:close (cmt (* (not (> -1 "`")) (-> :n) ':delim) ,=)
:main (drop (* :open (any (if-not :close 1)) :close))}
:long-string ,(<-c :string :long-bytes)
:long-buffer ,(<-c :string ~(* "@" :long-bytes))
:number (/ (cmt (<- :token) ,check-number) ,(partial paint :number))
:number (/ (cmt ':token ,check-number) ,(partial paint :number))
:raw-value (+ :comment :constant :number :keyword
:string :buffer :long-string :long-buffer
:parray :barray :ptuple :btuple :struct :dict :symbol)
:value (* (? (<- (some (+ :ws :readermac)))) :raw-value (<- (any :ws)))
:value (* (? '(some (+ :ws :readermac))) :raw-value '(any :ws))
:root (any :value)
:root2 (any (* :value :value))
:ptuple (* (<- "(") :root (+ (<- ")") (error "")))
:btuple (* (<- "[") :root (+ (<- "]") (error "")))
:struct (* (<- "{") :root2 (+ (<- "}") (error "")))
:parray (* (<- "@") :ptuple)
:barray (* (<- "@") :btuple)
:dict (* (<-"@") :struct)
:ptuple (* '"(" :root (+ '")" (error "")))
:btuple (* '"[" :root (+ '"]" (error "")))
:struct (* '"{" :root2 (+ '"}" (error "")))
:parray (* '"@" :ptuple)
:barray (* '"@" :btuple)
:dict (* '"@" :struct)
:main (+ (% :root) (error ""))})