diff --git a/extra/arrays/nested-syntax/authors.txt b/extra/arrays/nested-syntax/authors.txt new file mode 100644 index 0000000000..f13c9c1e77 --- /dev/null +++ b/extra/arrays/nested-syntax/authors.txt @@ -0,0 +1 @@ +Joe Groff diff --git a/extra/arrays/nested-syntax/nested-syntax-docs.factor b/extra/arrays/nested-syntax/nested-syntax-docs.factor new file mode 100644 index 0000000000..7933aa0882 --- /dev/null +++ b/extra/arrays/nested-syntax/nested-syntax-docs.factor @@ -0,0 +1,29 @@ +USING: help.markup help.syntax ; +IN: arrays.nested-syntax + +HELP: {{ +{ $syntax "{{ zim zang ;; zoop ;; zidilly zam ;; ... }}" } +{ $description "Shorthand for a literal array of arrays. Subarrays are separated by the " { $link POSTPONE: ;; } " token." } +{ $examples "The following blocks of code push an equivalent array onto the stack:" { $example "{{ 1 ;; 2 3 ;; 4 5 6 }}" } { $example "{ { 1 } { 2 3 } { 4 5 6 } }" } } ; + +HELP: H{{ +{ $syntax "H{{ zim zang ;; zoop zidilly ;; zam zung ;; ... }}" } +{ $description "Shorthand for a literal hashtable. Key-value pairs are separated by the " { $link POSTPONE: ;; } " token." } +{ $examples "The following blocks of code push an equivalent hash table onto the stack:" { $example "H{{ \"Monday\" 1 ;; \"Tuesday\" 2 ;; \"Wednesday\" 3 ;; \"Thursday\" 4 }}" } { $example "H{ { \"Monday\" 1 } { \"Tuesday\" 2 } { \"Wednesday\" 3 } { \"Thursday\" 4 } }" } } ; + +HELP: [[ +{ $syntax "[[ foo ;; bar bas ;; qux quux quuuux ;; ... ]]" } +{ $description "Shorthand for a literal array of quotations. Each quotation is separated by the " { $link POSTPONE: ;; } " token." } +{ $examples "The following blocks of code are equivalent:" { $example "[[ 1+ ;; 2 + ]] cleave" } { $example "{ [ 1+ ] [ 2 + ] } cleave" } } ; + +{ POSTPONE: {{ POSTPONE: H{{ POSTPONE: [[ } related-words + +HELP: ;; +{ $description "Separator token used in the " { $link POSTPONE: {{ } ", " { $link POSTPONE: H{{ } ", and " { $link POSTPONE: [[ } " literal syntaxes." } ; + +HELP: }} +{ $description "Delimiter token used to close the " { $link POSTPONE: {{ } " and " { $link POSTPONE: H{{ } " literal syntaxes." } ; + +HELP: ]] +{ $description "Delimiter token used to close the " { $link POSTPONE: [[ } " literal syntax." } ; + diff --git a/extra/arrays/nested-syntax/nested-syntax-tests.factor b/extra/arrays/nested-syntax/nested-syntax-tests.factor new file mode 100644 index 0000000000..a709840be4 --- /dev/null +++ b/extra/arrays/nested-syntax/nested-syntax-tests.factor @@ -0,0 +1,11 @@ +USING: arrays.nested-syntax kernel tools.test ; +IN: arrays.nested-syntax.tests + +[ { { 1 } { 2 3 } { 4 5 6 } } ] +[ {{ 1 ;; 2 3 ;; 4 5 6 }} ] unit-test + +[ H{ { "foo" 1 } { "bar" 2 } { "bas" 3 } } ] +[ H{{ "foo" 1 ;; "bar" 2 ;; "bas" 3 }} ] unit-test + +[ { [ drop ] [ nip ] } ] +[ [[ drop ;; nip ]] ] unit-test diff --git a/extra/arrays/nested-syntax/nested-syntax.factor b/extra/arrays/nested-syntax/nested-syntax.factor new file mode 100644 index 0000000000..9fae0fba9f --- /dev/null +++ b/extra/arrays/nested-syntax/nested-syntax.factor @@ -0,0 +1,10 @@ +USING: arrays hashtables kernel parser quotations sequences splitting ; +IN: arrays.nested-syntax + +: ;; ( -- * ) ";; can only be used in [[ ]] , {{ }} , or H{{ }} blocks" throw ; +DEFER: ]] delimiter +DEFER: }} delimiter + +: [[ \ ]] [ { POSTPONE: ;; } split [ >quotation ] map ] parse-literal ; parsing +: {{ \ }} [ { POSTPONE: ;; } split [ >array ] map ] parse-literal ; parsing +: H{{ \ }} [ { POSTPONE: ;; } split >hashtable ] parse-literal ; parsing diff --git a/extra/arrays/nested-syntax/summary.txt b/extra/arrays/nested-syntax/summary.txt new file mode 100644 index 0000000000..a8d507f2ca --- /dev/null +++ b/extra/arrays/nested-syntax/summary.txt @@ -0,0 +1 @@ +Shorthand syntax for defining arrays of quotations or arrays of arrays diff --git a/extra/arrays/nested-syntax/tags.txt b/extra/arrays/nested-syntax/tags.txt new file mode 100644 index 0000000000..f4274299b1 --- /dev/null +++ b/extra/arrays/nested-syntax/tags.txt @@ -0,0 +1 @@ +extensions