On this page:
syntax-raw-property
syntax-raw-prefix-property
syntax-raw-suffix-property
syntax-raw-inner-prefix-property
syntax-raw-inner-suffix-property
syntax-raw-tail-property
syntax-raw-opaque-content-property
syntax-opaque-raw-property
8.17

5.2 Source Locations and Raw-Text Properties🔗ℹ

 (require shrubbery/property) package: shrubbery-lib

The result of parse-all records source locations in the syntax object representing a shrubbery form. Source-location information and other properties for a shrubbery (), [], {}, or '' is associated with the parens, brackets, braces, or quotes identifier in the representation. Similarly, source-location information and properties for a : block or | alternatives are recorded on a block or alts identifier. For all compound forms, including group and multi, the source location on the representation’s head identifier spans the compound’s form. For an operator, source-location information and properties are associated to the operator identifier, and not the wrapper op identifier. Each structuring identifier like group, block, parens, or op has the 'identifier-as-keyword syntax property as #t.

The result of parse-all has source locations copied to the S-expression “parentheses” of a syntax object representing a compound form, but only as a convenience; the intent is that information is more permanently and reliably associated to each compound form’s head identifier. Along similar lines, spanning source locations for compound forms tend not to be maintained as syntax objects are manipulated, and the intent is that spanning locations are reconstructed as needed, especially for group and multi forms. A useful convention may be to treat source locations the S-expression “parentheses” of a compound form as a cache for spanning information computed from the form’s content.

A syntax object produced by parse-all includes raw text properties that allow the original shrubbery text to be reconstructed from the syntax object. This raw text information is distributed among syntax objects in a way that is preserved to a useful degree on terms as they are rearranged by enforestation and macro expansion. The shrubbery-syntax->string function reconstructs source text based on raw text properties in a syntax object. The shrubbery/property module exports functions to access and update properties, which can be helpful to avoid typos that are all too easy when writing the key directly as a quoted symbol.

Raw-text property values are trees of strings: a string, an empty list, or a pair containing two trees. The source raw text is reconstructed through a preorder traversal of the tree. The parse-all function attaches raw text properties only to atom terms and the head identifiers of compound terms, not counting multi. A head op, multi, or parsed is normally not consulted for syntax properties, but parse-all associates empty raw text to a head op or multi. For a parsed representation, the third component of the parsed list is consulted for 'opaque-raw by functions like shrubbery-syntax->string.

The main raw text property key is 'raw, but the following list of all raw text keys is in the order that they contribute to reconstructed text:

Each of these properties is normally preserved (in the sense of a true fourth argument to syntax-property), except for 'opaque-raw, which is intended for use in intermediate, short-term mixtures of shrubbery forms and S-expressions.

procedure

(syntax-raw-property stx)  any/c

  stx : syntax?
(syntax-raw-property stx val)  syntax?
  stx : syntax?
  val : any/c
Adjusts or inspects the 'raw preserved raw text property.

For example, parse-all will parse the input 0x11 as the number 17 with a 'raw property value "0x11". The input "\u3BB" will be parsed as the string "λ" with a 'raw property value "\"\\u3BB\"".

procedure

(syntax-raw-prefix-property stx)  any/c

  stx : syntax?
(syntax-raw-prefix-property stx val)  syntax?
  stx : syntax?
  val : any/c

procedure

(syntax-raw-suffix-property stx)  any/c

  stx : syntax?
(syntax-raw-suffix-property stx val)  syntax?
  stx : syntax?
  val : any/c
Adjusts or inspects the 'raw-prefix or 'raw-suffix preserved raw text property.

For example, parse-all will parse the input  1 + 2 // done into the S-expression representation (multi (group 1 (op +) 2)). The syntax object for group will have a 'raw-prefix value equivalent to " " and a 'raw-suffix value equivalent to " // done", but possibly within a tree structure instead of a single string. The syntax object for 1 and will have a 'raw-suffix value equivalent to " ", while the syntax object for + and will have a 'raw-suffix value equivalent to "  " (i.e., two spaces).

procedure

(syntax-raw-inner-prefix-property stx)  any/c

  stx : syntax?
(syntax-raw-inner-prefix-property stx val)  syntax?
  stx : syntax?
  val : any/c

procedure

(syntax-raw-inner-suffix-property stx)  any/c

  stx : syntax?
(syntax-raw-inner-suffix-property stx val)  syntax?
  stx : syntax?
  val : any/c
Adjusts or inspects the 'raw-inner-prefix or 'raw-inner-suffix preserved raw text property.

The parse-all function will parse the input @x into an S-expression representation x with a 'raw-inner-prefix property "@". The parse-all function never produces a syntax object with 'raw-inner-suffix.

procedure

(syntax-raw-tail-property stx)  any/c

  stx : syntax?
(syntax-raw-tail-property stx val)  syntax?
  stx : syntax?
  val : any/c
Adjusts or inspects the 'raw-tail preserved raw text property.

For example, the input (1 + 2) * 4 //done will be parsed into the S-expression representation (multi (group (parens (group 1 (op +) 2)) (op *) 4)). The syntax object for the outer group will have a 'raw-suffix value equivalent to " // done". The syntax object for parens will have a 'raw value equivalent to "(", a 'raw-tail value equivalent to ")", and a 'raw-suffix value equivalent to " ". The inner group syntax object will have no properties or ones with values that are equivalent to empty strings.

procedure

(syntax-raw-opaque-content-property stx)  any/c

  stx : syntax?
(syntax-raw-opaque-content-property stx    
  val)  syntax?
  stx : syntax?
  val : any/c
Adjusts or inspects the 'raw-opaque-content preserved raw text property.

procedure

(syntax-opaque-raw-property stx)  any/c

  stx : syntax?
(syntax-opaque-raw-property stx val)  syntax?
  stx : syntax?
  val : any/c
Adjusts or inspects the 'opaque-raw non-preserved raw text property. Unlike 'raw and similar properties, this one is associated with the S-expression list for a group, parens, etc., form, and not with the leading tag identifier.

The 'opaque-raw property is useful in macro expansion to record a macro’s input to its output—not only in source location, but also in source text.