REBOL [ Title: "Parse REBOL Source" Author: "Brett Handley" File: %annotate-rebol-tokens.r Date: 22-Jul-2000 Purpose: {An example of how to do something with parsed REBOL source code.} Comment: {Based on parse-code.r by Carl Sassenrath.} Category: [script util text 2] ] ; This object will perform the annotations. explainer: make object! [ output: none initialise: does [output: copy {}] code-col-width: 40 change-set-get-words: true begin-block: func[x][ explanation x rejoin [indent "begins group."] append indent { } ] end-block: func[x] [ remove/part head indent 3 explanation x rejoin [indent "ends group."] ] token: func[x][ explanation mold :x rejoin [indent type? :x "."] ] comment: func[x][ explanation replace copy :x "^/" "" rejoin [indent "Comment"] ] spacer: function[a b][result][ result: copy {} insert/dup tail result " " add subtract max a b a 1 " " result ] indent: { } explanation: function[x c][code-part][ code-part: rejoin [newline indent x] append output rejoin [code-part spacer length? code-part code-col-width "; " c] ] ] annotate-rebol-tokens: func [ "Parse REBOL source code." text /code-width new-width [integer!] /local str new ][ if code-width [ explainer/code-col-width: new-width ] explainer/initialise parse text blk-rule: [ some [ ; repeat until done str: newline | #";" [thru newline | to end] new: (explainer/comment copy/part str new) | #"[" (explainer/begin-block "[") blk-rule | #"]" (explainer/end-block "]") | #"(" (explainer/begin-block "(") blk-rule | #")" (explainer/end-block ")") | skip (set [value new] load/next str explainer/token :value) :new ] ] explainer/output ] explain-load-code: func[x][ explainer/initialise explainer/code-col-width: 20 foreach w load x [ explainer/token :w ] explainer/output ] ;example: print annotate-rebol-tokens read %parse-code.r