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.

Chapter 36<br />

Support for Domain-Specific <strong>Language</strong>s<br />

To support syntax for domain-specific languages, and to allow the <strong>Fortress</strong> language to grow with time, programmers<br />

can extend the basic syntax of <strong>Fortress</strong> in their programs. Such extensions are possible through the use of syntax<br />

expanders. Syntax expanders must be defined in the top level of a simple component.<br />

36.1 Definitions of Syntax Expanders<br />

Syntax:<br />

Decl ::= ExternalSyntax<br />

ExternalSyntax ::= syntax OpenExpander Id CloseExpander = Expr<br />

OpenExpander ::= Id | LeftEncloser<br />

CloseExpander ::= Id | RightEncloser | end<br />

<strong>The</strong> definition of a syntax expander starts with the special reserved word syntax , followed by an opening delimiter,<br />

followed by a contents parameter, followed by a terminating delimiter, followed by an = , and a subexpression. <strong>The</strong><br />

opening delimiter must be either an identifier or the opening member of an enclosing operator. <strong>The</strong> contents parameter<br />

must be an identifier (see Section 5.15). <strong>The</strong> terminating delimiter must be either an identifier, the terminating member<br />

of an enclosing operator, or the special reserved word end . If either the opening delimiter or the closing delimiter is<br />

part of an enclosing operator, the opening and closing delimiters must both be parts of enclosing operators, and they<br />

must match, or it is a static error. Because delimiters conceptually delimit blocks, just as do and end delimit blocks,<br />

delimiters of syntax expanders are rendered as special reserved words. <strong>The</strong> subexpression of a syntax expander has<br />

type <strong>Fortress</strong>.Ast.SyntaxTree . This SyntaxTree must be that of a <strong>Fortress</strong> expression. Here is an example:<br />

syntaxsql e end = parseSQL(e)<br />

where parseSQL is a function that takes an argument of type <strong>Fortress</strong>.Lang.SourceAssembly (a sequence of<br />

Unicode characters and abstract syntax trees), interprets it as an SQL query, and returns an expression with type<br />

SyntaxTree consisting of constructor calls to SQL syntax nodes (defined in some SQL library).<br />

At a use site, all characters between the opening delimiter and the terminating delimiter are turned into a SourceAssembly<br />

(see Section 36.4 for a more detailed description of how this conversion is achieved). <strong>The</strong> resulting SourceAssembly<br />

is bound to the contents parameter of the syntax expander. <strong>The</strong> use site is then expanded by evaluating the body of the<br />

expander. Every use site of a syntax expander must occur in an expression context, or it is a static error.<br />

For example, we could define parseSQL so that a use site such as:<br />

241

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

Saved successfully!

Ooh no, something went wrong!