Skip to content

Instantly share code, notes, and snippets.

@gchiu
Created June 8, 2013 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gchiu/5733503 to your computer and use it in GitHub Desktop.
Save gchiu/5733503 to your computer and use it in GitHub Desktop.
First attempt to port clean-script to pretty indent r3 souce
Rebol [
file: clean-script.r3
]
script-cleaner: make object! [
out: none ; output text
spaced: off ; add extra bracket spacing
indent: "" ; holds indentation tabs
emit-line: func [] [append out newline]
emit-space: func [pos] [
append out either newline = last out [indent] [
pick [#" " ""] found? any [
spaced
not any [find "[(" last out find ")]" first pos]
]
]
]
emit: func [from to] [emit-space from append out to-string copy/part from to]
set 'clean-script func [
"Returns new script text with standard spacing (pretty printed)."
script "Original Script text"
/spacey "Optional spaces near brackets and parens"
/local str new
] [
spaced: found? spacey
clear indent
out: append clear copy script newline
parse script blk-rule: [
some [
str:
newline (emit-line) |
#";" [thru newline | to end] new: (emit str new) |
[#"[" ( print "left bracket" ) | #"(" ( print "left bracket" ) ] (emit str 1 append indent tab) blk-rule |
[#"]" | #")"] (remove indent emit str 1) break |
skip (
if string? str [ str: to binary! str]
set [value new] transcode/next str
print value
; emit str new
new: to string! new
) :new
]
]
remove out ; remove first char
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment