Terrace/packages/js/docs/core-api.inc.tce
2023-03-04 22:36:08 -05:00

75 lines
3.1 KiB
Plaintext

Heading 2 Core API
class mt-12
Markdown
**Note:** The Core API uses C-style conventions to optimize memory management
and improve portability to other environments and languages.
It is unwieldy and does not follow JavaScript best practices.
For most projects you'll want to use the [Document API](#document-api) instead.
It provides an ergonomic wrapper around the Core API and lets you focus on parsing
your documents.
Heading 3 LineData
class mb-4 mt-12
CodeBlock typescript
// Type Definition
// Holds the parsed information from each line.
type LineData = {
// Which character is being used for indentation. Avoids having to specify it on each parseLine call.
indent: string;
// How many indent characters are present in the current line before the first non-indent character.
level: number;
// The number of characters before the start of the line's "head" section.
// (Normally the same as `level`)
offsetHead: number;
// The number of characters before the start of the line's "tail" section.
offsetTail: number;
}
Heading 3 createLineData()
class mb-4 mt-12
Markdown
| Parameter | Type | Description
| -------------- | --------------------- | -----------------------------------------------------------------------
| indent | string | The character used for indentation in the document. Only a single character is permitted.
| **@returns** | [LineData](#line-data) | A LineData instance with the specified indent character and all other values initialized to 0.
Initialize a LineData instance with default values to pass to [parseLine()](#parse-line).
CodeBlock typescript
// Type Definition
function createLineData(indent: string = ' '): LineData
// Import Path
import { createLineData } from '@terrace-lang/js/parser'
// Usage
const lineData = createLineData(' ')
console.dir(lineData)
// { indent: ' ', level: 0, offsetHead: 0, offsetTail: 0 }
// Use the same lineData object for all calls to parseLine in the same document.
Heading 3 parseLine()
class mb-4 mt-12
Markdown
| Parameter | Type | Description
| -------------- | --------------------- | -----------------------------------------------------------------------
| line | string | A string containing a line to parse. Shouldn't end with a newline.
| lineData | [LineData](#line-data) | A LineData object to store information about the current line, from [createLineData()](#create-line-data).<br/>**Mutated in-place!**
Core Terrace parser function, sets `level`, `offsetHead`, and `offsetTail` in a [LineData](#line-data) object based on the passed line.
Note that this is a C-style function, `lineData` is treated as a reference and mutated in-place.
CodeBlock typescript
// Type Definition
function parseLine(lineData: LineData): LineData
// Import Path
import { createLineData, parseLine } from '@terrace-lang/js/parser'
// Usage
const lineData = createLineData(' ')
parseLine('title Example Title', lineData)
console.dir(lineData)
// { indent: ' ', level: 0, offsetHead: 0, offsetTail: 5 }