Macaw.Frame.TextForm behaviour (macaw_frame v0.1.0)

Copy Markdown View Source

A text form of the frame document (spec/02 §11.3): a notation a person types, read into a fragment of the frame document type and judged as any value is. A text form never defines a second document type.

A text form is a module implementing this behavior: name/0, the first word of a document written in it; parse/2, the text read into a fragment and its source map (%{path => {line, column, length}}, lines and columns from 1, columns in grapheme clusters), or positioned faults; and optionally print/1 and vocabulary/0.

The registry is explicit and default closed (registry/1), built from the modules a host lists; a name two forms claim is a fault of the registry. read/3 reads a text: the form named by its first word, the kernel's bytes limit first, parse/2, then Macaw.Frame.Document.check/2 of the fragment, every fault re-rooted at its position as {[:text, line, column], {:at, path, reason}}. Never a raise, never an atom. The second cut's diagram forms are Macaw.Frame.TextForms' (flowchart, sequence, state, class, ER, Gantt and timeline); a host lists the ones it reads.

iex> Macaw.Frame.TextForm.registry([])
{:ok, %{}}
iex> Macaw.Frame.TextForm.read("flowchart\nA --> B", %{})
{:error, [{[:text, 1, 1], {:unknown, :text_form, "flowchart"}}]}

Summary

Types

Where a path of the fragment was written: line, column and length, from 1.

A positioned fault: a kernel fault whose path names the position.

A registry of text forms by name.

A fragment's source map: each path the parser wrote, and where.

Callbacks

The form's name, unique in a registry, the first word of a document written in it.

The text read into a fragment and its source map, or every positioned fault.

The fragment written back in the form, where it can be.

The words the form reserves, for an editor's completion.

Functions

A positioned fault in words: line 3, column 5: …; any other fault as Macaw.Frame.Document.format/1 words it.

A text read into a fragment and judged (§11.3): {:ok, fragment}, or every fault ordered by position. opts are Macaw.Frame.Document.check/2's, and limits: whose bytes bounds the text.

The registry of the forms modules lists (§11.3): {:ok, %{name => module}}, or every fault: a module that is no text form {[:text_forms, i], {:type, :text_form}}, a name two forms claim {[:text_forms, name], {:named_twice, module, other}}.

Types

position()

@type position() :: {pos_integer(), pos_integer(), non_neg_integer()}

Where a path of the fragment was written: line, column and length, from 1.

positioned_fault()

@type positioned_fault() :: {[:text | pos_integer()], term()}

A positioned fault: a kernel fault whose path names the position.

registry()

@type registry() :: %{required(String.t()) => module()}

A registry of text forms by name.

source_map()

@type source_map() :: %{required([term()]) => position()}

A fragment's source map: each path the parser wrote, and where.

Callbacks

name()

@callback name() :: String.t()

The form's name, unique in a registry, the first word of a document written in it.

parse(text, opts)

@callback parse(text :: String.t(), opts :: keyword()) ::
  {:ok, map(), source_map()} | {:error, [positioned_fault()]}

The text read into a fragment and its source map, or every positioned fault.

print(fragment)

(optional)
@callback print(fragment :: map()) :: {:ok, String.t()} | :error

The fragment written back in the form, where it can be.

vocabulary()

(optional)
@callback vocabulary() :: %{keywords: [String.t()]}

The words the form reserves, for an editor's completion.

Functions

format(fault)

@spec format(term()) :: String.t()

A positioned fault in words: line 3, column 5: …; any other fault as Macaw.Frame.Document.format/1 words it.

iex> Macaw.Frame.TextForm.format({[:text, 3, 5], {:unknown, :text_form, "x"}})
"line 3, column 5: no registered text form is named \"x\""

read(text, registry, opts \\ [])

@spec read(term(), term(), term()) ::
  {:ok, map()} | {:error, [Macaw.Base.Fault.t(), ...]}

A text read into a fragment and judged (§11.3): {:ok, fragment}, or every fault ordered by position. opts are Macaw.Frame.Document.check/2's, and limits: whose bytes bounds the text.

registry(modules)

@spec registry(term()) :: {:ok, registry()} | {:error, [Macaw.Base.Fault.t(), ...]}

The registry of the forms modules lists (§11.3): {:ok, %{name => module}}, or every fault: a module that is no text form {[:text_forms, i], {:type, :text_form}}, a name two forms claim {[:text_forms, name], {:named_twice, module, other}}.