19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Because syntax expanders are defined at the top level of program components, and because they are syntactically<br />

distinguished, they can be identified before scanning or parsing (but after ASCII conversion). Use sites are then<br />

identified and expanded before parsing occurs.<br />

Syntax expansion takes a sequence of Unicode characters and yields a sequence of Unicode characters and syntax<br />

trees, where all the syntax expanders have been replaced by syntax trees. This result has type SourceAssembly.<br />

Syntax expansion proceeds from left to right as follows. First, the source is scanned for opening delimiters of syntax<br />

expanders, stopping at the leftmost one. We call this scanning <strong>Fortress</strong>-source scanning.<br />

If the opening delimiter of a syntax expander is encountered during <strong>Fortress</strong>-source scanning, the source is scanned<br />

rightward until the first occurrence of either an opening delimiter of some syntax expander (possibly another use of the<br />

same expander), or the terminating delimiter of that syntax expander, is found. (If no matching terminating delimiter<br />

is found in the remainder of the program, it is a static error.) We call this scanning expander scanning.<br />

If an opening delimiter is encountered before the terminating delimiter, there is a nested use site of another syntax<br />

expander. <strong>The</strong> nested use site is processed, and then expander scanning continues rightward of that use site. Thus, the<br />

processing of syntax expanders is recursive, syntax expanders may be nested arbitrarily, and expanders are processed<br />

from the leftmost-innermost occurrence outward and rightward.<br />

When the terminating delimiter is encountered during expander scanning, the scanning is terminated, the resulting<br />

SourceAssembly is bound to the contents parameter of the expander, and then the body of the expander is evaluated.<br />

<strong>The</strong> result of evaluating this body has type SyntaxTree, and it is placed into the resulting SourceAssembly in place<br />

of the expander in the scanned source.<br />

36.4.1 Introduced Variable Names<br />

Often, when expanding concrete syntax for a domain-specific language, it is useful to introduce variable binding<br />

constructs into the resulting SyntaxTree. It is required that such bindings, in general, respect the rules of hygiene<br />

and referential transparency [7]. Several aspects of the <strong>Fortress</strong> semantics allow the library programmer to ensure<br />

referential transparency of syntax expanders:<br />

1. A component exporting an API A that includes a syntax expander must not import APIs that are not also<br />

imported by A.<br />

2. Syntax expanders must expand to SyntaxTrees of expressions.<br />

3. Shadowing of identifiers is not allowed.<br />

Thus, syntax expanders cannot expand into new top-level identifiers. Moreover, provided that the library programmer<br />

is careful to ensure that all top-level identifier references appearing in expanded code are fully qualified identifiers<br />

exported by APIs, all top-level identifiers referred to in the definition of a syntax expander are visible at all use sites.<br />

To ensure hygiene, all variables bound in a SyntaxTree resulting from an expansion are renamed, following the<br />

syntax-case system of Dybvig et al. [9].<br />

36.4.2 Comments and Syntax Expansion<br />

Comments are not recognized before syntax expansion (the embedded syntax may have its own commenting syntax):<br />

during expander scanning, an opening or terminating delimiter that occurs in what appears to be a <strong>Fortress</strong> comment<br />

is nonetheless recognized as an opening or terminating delimiter for that expander. Comments can be viewed as uses<br />

of a syntax expander, with special opening and terminating delimiters.<br />

243

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!