1. Introduction
This is the reference document for Sweet.js. For a gentle explaination of Sweet’s concepts see the tutorial.
2. Command Line API
-
--out-file <file>
: write result to file -
--out-dir <dir>
: write result to directory -
--no-babel
: do not use babel backend
3. Binding Forms
3.1. syntax
syntax <name> = <init>
Bind <name>
to the result of evaluating <init>
in the compiletime environment. Scoping follows let
(i.e. block scoped with a temporal dead zone).
If the result of evaluating <init>
is a function, then the result is a syntax transformer.
4. Syntax Transformer
transformer : (TransformerContext) -> List(Syntax)
A syntax transformer is a function bound to a compile-time name. A syntax transformer is invoked with a transformer context that provides access to the syntax at the call-site and returns a list of syntax objects.
4.1. Transformer Context
A transformer context is an iterable object that provides access to syntax at the call-site of a syntax transformer.
TransformerContext = { next: (string?) -> { done: boolean, value: Syntax } }
Each call to next
returns the syntax object following the transformer call. If next
is called with a string, the specified grammar production is matched.
5. Syntax Objects
Syntax objects represent the syntax from the source program. Syntax objects have a number of methods to inspect their contents.
5.1. .val()
Returns a nullable value representing the textual value of the syntax. For example, the .val()
of the identifier foo
is the string "foo"
. Some syntax objects (in particular delimiters) do not have a reasonable representation of their syntax and so .val()
returns null
in these cases.
5.2. .lineNumber()
Returns the original line number for this syntax object.
5.3. .inner()
If the syntax object is a delimiter, returns a transformer context iterator into the delimiter. Otherwise, throw an exception.
5.4. .isIdentifier()
Returns true if the syntax object is an identifier.
5.5. .isBooleanLiteral()
Returns true if the syntax object is a boolean literal.
5.6. .isNullliteral()
Returns true if the syntax object is a null literal.
5.7. .isNumericLiteral()
Returns true if the syntax object is a numeric literal.
5.8. .isStringLiteral()
Returns true if the syntax object is a string literal.
5.9. .isKeyword()
Returns true if the syntax object is a keyword.
5.10. .isPunctuator()
Returns true if the syntax object is a puncuator.
5.11. .isRegularExpression()
Returns true if the syntax object is a regular expression literal.
5.12. .isTemplate()
Returns true if the syntax object is a template literal.
5.13. .isDelimiter()
Returns true if the syntax object is a delimiter.
5.14. .isParens()
Returns true if the syntax object is a parenthesis delimiter (e.g. ( … )
).
5.15. .isBrackets()
Returns true if the syntax object is a bracket delimiter (e.g. [ … ]
).
5.16. .isBraces()
Returns true if the syntax object is a braces delimiter (e.g. { … }
).
5.17. .isSyntaxTemplate()
Returns true if the syntax object is a syntax template.
6. Syntax Templates
Syntax templates construct a list of syntax objects from a literal representation using backtick (#`foo bar baz`
). They are similar to ES2015 templates but with the special sweet.js specific #
template tag.
Syntax templates support interpolations just like normal templates via ${…}
:
syntax m = function (ctx) {
return #`${ctx.next().value} + 24`;
}
m 42
The expressions inside an interpolation must evaluate to a syntax object, an array, a list, or an transformer context.