(samizdat read)

Reference

Overview

The samizdat reader: text to a tree of text and command calls, with no meaning given to the commands.

(import (samizdat read))

(match (read-source "Hello @em{you}." #:name "greeting")
  ((Ok src) (println (ats->string (record-ref src content))))
  ((Err why) (println why)))
;; "Hello " (em {"you"}) "."

The reader makes a vec of At items. An item is text or a command call. It does not run the commands. (samizdat expand) runs them.

The syntax.

A name is a letter, then letters, digits and - _ * /. A full stop, a comma, a colon, ! and ? end a name.

An @ right after a letter or a digit is text, so linus@example.com needs no escape. The exception is a name followed by [, { or |: word@note{...} is a command.

The arguments are bjodat, read by (text bjodat-core). A comma is not a separator. A document can start with a (meta ...) form, which is also bjodat.

The braces in a body must balance. A balanced pair is text. At the top level of a document, a brace is an ordinary character.

Indentation. A body of more than one line loses the blank line after its opening brace and the blank line before its closing brace. Its lines lose the indentation that they have in common. A first line with text after the opening brace is not changed.

See also: read-source, read-source-file, At, Call, Source, ats->string

Reference

Types

Functions

Types

At

union ↑ Types

An item of a document as the reader read it: text, or a command call.

Text
Text.
Cmd
A command call.

See also: Call, ats->string, at-lines

Call

record ↑ Types

A command call as it was written: its name, arguments and body, not yet expanded.

name
The name of the command, without the @.
args
The arguments in the square brackets, as bjodat values. A keyword and its value are two items. It is empty when there are no square brackets.
body
The body. It is None for a command written without braces, and Some for one with braces, also when they are empty.
line
The line of the @, counting from 1.
column
The column of the @, counting from 1.

The line and the column are for error messages. refuse in (samizdat expand) puts them in its message.

See also: At

Source

record ↑ Types

A document as the reader read it: its meta form and its items.

meta
The (meta ...) form, when the document starts with one.
content
The items after the meta form.

See also: read-source, read-source-file

Functions

read-source

function ↑ Functions

(: read-source (-> string (#:name string) (Result string Source)))
(read-source text #:name "input")

Reads the text of a document into a Source.

text
The text of the document.
#:name
The name of the document in an error message, usually the name of its file.
returns
The document, or Err with a message when the text cannot be read. The message has the name, the line and the column, and what was expected there.
(read-source "An @image[\"a.png\", \"b\"]" #:name "post.sz")
;; (Err "post.sz: Parse error at line 1, column 24: ...
;;       in the [...]: at line 1, column 18: a comma is not a separator here")

A byte order mark at the start is removed. A line can end with \r\n. A (meta ...) form that bjodat cannot read is also an Err.

See also: read-source-file, Source, ats->string

read-source-file

function ↑ Functions

(: read-source-file (-> string (Result string Source)))
(read-source-file path)

Reads a document from a file into a Source.

path
The path of the file. Error messages use it as the name of the document.
returns
The document, or Err with a message when the text cannot be read, as read-source returns.
raises System.IO.IOException
When the file cannot be opened, for example when it does not exist.

See also: read-source

ats->string

function ↑ Functions

(: ats->string (-> (Vec At) string))
(ats->string items)

Items as s-expressions, for a test or to look at what the reader read.

items
The items.
returns
The text. Text is a string literal. A command is (name arg ... {body ...}). A command without a body has no braces.

See also: At, read-source

at-lines

function ↑ Functions

(: at-lines (-> (Vec At) (Vec (Vec At))))
(at-lines items)

Items split into lines, for a command that reads its body one line at a time.

items
The items, such as the body of a call.
returns
One vec of items for each line. The newlines are removed, and adjacent text is joined. A command stays in the line where it starts.

@read-list and @read-table in (samizdat lightweight) use these three functions.

See also: at-line-blank?, at-line-indentation

at-line-blank?

function ↑ Functions

(: at-line-blank? (-> (Vec At) bool))
(at-line-blank? line)

Whether a line from at-lines holds only whitespace, or nothing.

line
The line.
returns
#t when the line has no command and its text is only whitespace.

See also: at-lines, at-line-indentation

at-line-indentation

function ↑ Functions

(: at-line-indentation (-> (Vec At) int))
(at-line-indentation line)

The indentation of a line from at-lines, in characters.

line
The line.
returns
The number of whitespace characters at the start of its text. It is 0 for a line that starts with a command, and for an empty line.

See also: at-lines, at-line-blank?