Try it in the playground

Sub-languages for regex, JSON, XML and SQL

Importing a provider registers its $name syntax. The provider parses that syntax during compilation and returns a typed Abla expression. The expression it returns can still run at runtime: parsing the syntax early does not mean all values embedded in it are compile-time constants.

$json constructs a JSON value. $html constructs an HTML data model value; its braces can contain ordinary Abla expressions such as count + 1. #$jsons evaluates the JSON string result at compile time. This example reads the HTML element's tag; it does not start a web server or render a page.

See JSON, subparsers, and the stdlib module index for abla/html.

Runnable source:

import "abla/io"
import "abla/json"
import "abla/html"

fun main {
    val count = 2
    val document = $json {"answer": 42}
    val view = $html <div>{count + 1}</div>
    val encoded = #$jsons {"enabled": true}
    println("answer ${document.getInt("answer")}, tag ${view.tag}")
    println(encoded)
}
// output:
// answer 42, tag div
// {"enabled":true}

Match text with named captures

A $regex("pattern") literal checks the pattern during compilation. A named capture that always participates becomes a string field on the match. A capture that can be absent has type string?. The match itself can be null when the input does not match. For patterns supplied at runtime, use regexCompile from abla/regex.

Runnable source.

import "abla/io"
import "abla/regex"

fun main {
    val release = $regex("v(?<major>[0-9]+)\\.(?<minor>[0-9]+)")
    val match = release.matchFull("v2.14")
    if (match != null) {
        println("major ${match.major}, minor ${match.minor}")
    }
}
// output:
// major 2, minor 14

Write JSON into an existing buffer

Use $json to build a JSON tree. To send that value as text, pass it to jsonStringify. See replacing parallel key and value arrays. With $jsonw(buffer), the literal appends one encoded value to your ByteBuffer. Interpolated strings go through JSON escaping. For larger examples, see writing directly to a buffer. To spot manual JSON construction, build with --warn-json-construction and look for W_JSON_CONSTRUCTION suggestions.

Runnable source.

import "abla/bytes"
import "abla/io"
import "abla/json"

fun main {
    val name = "A & B"
    val out = byteBuffer()
    $jsonw(out) {"label": name, "inStock": true}
    println(out.toText())
}
// output:
// {"label":"A & B","inStock":true}

Build an XML element

The $xml parser checks tag names, nesting and namespace declarations. A braced expression in element content supplies text. The literal stores that text as a value, which a serializer can escape for markup. If you are already writing a document with the abla-xml package, its XmlWriter accepts $xmlw(writer) to stream an element directly. See XML literals for both forms.

Runnable source.

import "abla/io"
import "abla/xml"

fun main {
    val title = "A & B"
    val item = $xml <item id={7}>{title}</item>
    println(item.text())
}
// output:
// A & B

Bind values in an SQL statement

The $sql("...") literal accepts a supported PostgreSQL grammar. Give each Abla expression a PostgreSQL cast so the compiler can check the parameter type. The generated statement contains a numbered placeholder and a separate parameter value. Here we inspect both in a program that needs no database. You can pass the statement to a compatible PostgreSQL adapter to execute it.

Runnable source.

import "abla/io"
import "abla/sql"
import "abla/text"

fun main {
    val author = "O'Neil"
    val query = $sql("SELECT id FROM books WHERE author = ${author}::text")
    println("has placeholder: ${query.text.contains("\$1::text")}")
    println("bound author: ${query.parameters[0].value}")
}
// output:
// has placeholder: true
// bound author: O'Neil

Previous · Tour · Next

Built from compiler source ba5d6b7d. Examples and generated signatures follow that source revision.