This commit is contained in:
Joshua Bemenderfer
2025-09-08 16:24:38 -04:00
parent 70200a4091
commit 9d9757e868
79 changed files with 11705 additions and 3554 deletions

View File

@@ -16,9 +16,11 @@ Section light
Markdown
Documentation is available for the following languages:
- [C](/docs/c/) - 75% Complete
- [C](/docs/c/) - 100% Complete
- [JavaScript](/docs/javascript/) - 75% Complete
- [Python](/docs/python/) - 0% Complete
- [Go](/docs/go/) - 50% Complete
- [Python](/docs/python/) - 100% Complete
- [Rust](/docs/rust/) - 100% Complete
Heading 2 Getting Started
class mt-12 mb-6

View File

@@ -1,185 +1,202 @@
import type { Reader } from "./readers/reader.js";
import { createLineData, parseLine } from "./parser.js";
import { createLineData, parseLine, type LineData } from "./parser.js";
// Container for a handful of convenience functions for parsing documents
// Obtained from useDocument() below
export type Document = {
next: (levelScope?: number) => Promise<boolean>;
level: () => number;
lineNumber: () => number;
line: (startOffset?: number) => string;
head: () => string;
tail: () => string;
match: (matchValue: string) => boolean;
};
// Represents a single node/line in a Terrace document
export class TerraceNode {
private _lineData: LineData;
private _content: string;
private _lineNumber: number;
private _document: TerraceDocument;
constructor(
lineData: LineData,
content: string,
lineNumber: number,
document: TerraceDocument
) {
this._lineData = { ...lineData }; // Copy to avoid mutations
this._content = content;
this._lineNumber = lineNumber;
this._document = document;
}
// Current line properties (zero-allocation - just slice references)
get head(): string {
return this._content.slice(this._lineData.offsetHead, this._lineData.offsetTail);
}
get tail(): string {
return this._content.slice(this._lineData.offsetTail + 1);
}
get content(): string {
return this._content.slice(this._lineData.offsetHead);
}
get level(): number {
return this._lineData.level;
}
get lineNumber(): number {
return this._lineNumber;
}
// Convenience methods
is(value: string): boolean {
return this.head === value;
}
isEmpty(): boolean {
return this._content.trim() === '';
}
// Content access with different indent handling
raw(offset?: number): string {
return this._content.slice(offset ?? 0);
}
// Navigation (streaming-compatible)
async* children(): AsyncIterableIterator<TerraceNode> {
const parentLevel = this.level;
while (true) {
const node = await this._document._getNextNode();
if (node === null) break;
// If we encounter a node at or below parent level, it's not a child
if (node.level <= parentLevel) {
this._document._pushBack(node);
break;
}
// Yield any node that is deeper than the parent
// This supports arbitrary nesting as per Terrace spec
yield node;
}
}
async* siblings(): AsyncIterableIterator<TerraceNode> {
const currentLevel = this.level;
while (true) {
const node = await this._document._getNextNode();
if (node === null) break;
if (node.level < currentLevel) {
this._document._pushBack(node);
break;
}
if (node.level === currentLevel) {
yield node;
}
}
}
}
// Main document iterator
export class TerraceDocument {
private _reader: Reader;
private _indent: string;
private _lineData: LineData;
private _currentLineNumber: number;
private _pushedBackNodes: TerraceNode[] = [];
private _isExhausted: boolean = false;
constructor(reader: Reader, indent: string = " ") {
this._reader = reader;
this._indent = indent;
this._lineData = createLineData(indent);
this._currentLineNumber = 0;
}
async*[Symbol.asyncIterator](): AsyncIterableIterator<TerraceNode> {
while (true) {
const node = await this._getNextNode();
if (node === null) break;
yield node;
}
}
async _getNextNode(): Promise<TerraceNode | null> {
// Check for pushed back nodes first (LIFO order)
if (this._pushedBackNodes.length > 0) {
return this._pushedBackNodes.pop()!;
}
// If we've exhausted the reader, return null
if (this._isExhausted) {
return null;
}
const line = await this._reader();
if (line == null) {
this._isExhausted = true;
return null;
}
this._currentLineNumber++;
parseLine(line, this._lineData);
return new TerraceNode(
this._lineData,
line,
this._currentLineNumber,
this
);
}
_pushBack(node: TerraceNode): void {
this._pushedBackNodes.push(node);
}
// Utility methods for functional chaining
async filter(predicate: (node: TerraceNode) => boolean): Promise<TerraceNode[]> {
const results: TerraceNode[] = [];
for await (const node of this) {
if (predicate(node)) {
results.push(node);
}
}
return results;
}
async find(predicate: (node: TerraceNode) => boolean): Promise<TerraceNode | undefined> {
for await (const node of this) {
if (predicate(node)) {
return node;
}
}
return undefined;
}
async map<T>(mapper: (node: TerraceNode) => T | Promise<T>): Promise<T[]> {
const results: T[] = [];
for await (const node of this) {
results.push(await mapper(node));
}
return results;
}
async toArray(): Promise<TerraceNode[]> {
const results: TerraceNode[] = [];
for await (const node of this) {
results.push(node);
}
return results;
}
}
// Legacy Document type for backwards compatibility references
export type Document = TerraceDocument;
/**
* Provides a simple set of convenience functions around parseLine for more ergonomic parsing of Terrace documents
* Creates a new Terrace document iterator
*
* @param {Reader} reader When called, resolves to a string containing the next line in the document
* @param {String} indent The character used for indentation in the document. Only a single character is permitted
* @returns {Document} A set of convenience functions for iterating through and parsing a document line by line
* @returns {TerraceDocument} An iterable document that can be used with for-await-of loops
*/
export function useDocument(reader: Reader, indent: string = " "): Document {
if (indent.length !== 1)
throw new Error(
`Terrace currently only allows single-character indent strings - you passed "${indent}"`
);
const lineData = createLineData(indent);
let currLine = "";
let currLineNumber = -1;
// If `repeatCurrentLine` is `true`, the following call to `next()` will repeat the current line in
// the document and set `repeatCurrentLine` back to `false`
let repeatCurrentLine = false;
/**
* Advances the current position in the terrace document and populates lineData
* with the parsed information from that line
*
* Returns `true` after parsing the next line, or `false` upon reaching the end of the document.
* If the `levelScope` parameter is provided, `next()` will return `false` when it encounters a line
* with a level at or below `levelScope`. This allows you to iterate through subsections of a document.
*
* If a lower-level line was encountered, the following call to `next()` will repeat this line again.
* This allows a child loop to look forward, determine that the next line will be outside its purview,
* and return control to the calling loop transparently without additional logic.
*
* Intended to be used inside a while loop to parse a section of a Terrace document.
*
* ```javascript
* while (await next()) {
* // Do something with each line.
* }
* ```
*
* @param {number} levelScope If specified, `next()` will return `false` when it encounters a line with a level at or below `levelScope`
* @returns {Promise<boolean>} Returns `true` after parsing a line, or `false` if the document has ended or a line at or below `levelScope` has been encountered.
*/
async function next(levelScope: number = -1): Promise<boolean> {
// Repeat the current line instead of parsing a new one if the previous call to next()
// determined the current line to be out of its scope.
if (repeatCurrentLine) repeatCurrentLine = false;
// Otherwise parse the line normally.
else {
// Load the next line from the line reader.
const line = await reader();
// If there are no more lines, bail out.
if (line == null) return false;
// Populate lineData with parsed information from the current line.
currLine = line;
currLineNumber++;
parseLine(currLine, lineData);
}
// If we shouldn't be handling this line, make the following call to next() repeat the current line.
// Allows a child loop to look forward, determine that the next line will be outside its purview,
// and return control to the calling loop transparently without additional logic.
if (level() <= levelScope) {
repeatCurrentLine = true;
return false;
}
return true;
}
/**
* Returns the number of indent characters of the current line
*
* Given the following document, `level()` would return 0, 1, 2, and 5 respectively for each line
*
* ```terrace
* block
* block
* block
* block
* ```
* @returns {number} The indent level of the current line
*/
const level = (): number => lineData.level;
/**
* Get the current line number, zero-indexed from first line read.
* @returns {number} The current line number, starting from zero.
*/
const lineNumber = (): number => currLineNumber;
/**
* Get a string with the current line contents. Skips all indent characters by default, but this can be configured with `startOffset`
*
* Given the following document
*
* ```terrace
* root
* sub-line
* ```
* `line()` on the second line returns "sub-line", trimming off the leading indent characters
* `line(0)` however, returns " sub-line", with all four leading spaces
*
* `startOffset` is primarily used for parsing blocks that have literal indented multi-line text, such as markdown
*
* @param {number} startOffset How many indent characters to skip before outputting the line contents. Defaults to the current indent level
* @returns {string} The line contents starting from `startOffset`
*/
const line = (startOffset: number = lineData.level): string =>
currLine.slice(startOffset);
/**
* Get the first "word" of a line, starting from the first non-indent character to the first space or end of the line
* Often used for deciding how to parse a block.
*
* Terrace DSLs do not *need* to use head-tail line structure, but support for them is built into the parser
*
* Given the following line, `head()` returns "title"
*
* ```terrace
* title An Important Document
* ```
* @returns {string} The `head` portion (first word) of a line
*/
const head = (): string =>
currLine.slice(lineData.offsetHead, lineData.offsetTail);
/**
* Get all text following the first "word" of a line, starting from the first character after the space at the end of `head()`
*
* Terrace DSLs do not *need* to use head-tail line structure, but support for them is built into the parser
*
* Given the following line, `tail()` returns "An Important Document"
*
* ```terrace
* title An Important Document
* ```
* @returns {string} The remainder of the line following the `head()` portion, with no leading space
*/
const tail = (): string => currLine.slice(lineData.offsetTail + 1); // Skip the space
/**
* Quickly check if the current line head matches a specified value
*
* Shorthand for `matchValue === head()`
*
* Given the following line
*
* ```terrace
* title An Important Document
* ```
*
* `match('title')` returns `true`
* `match('somethingElse`) returns `false`
*
* @param {string} matchValue A string to check against `head()` for equality
* @returns {boolean}
*/
const match = (matchValue: string): boolean => matchValue === head();
return {
next,
level,
line,
lineNumber,
head,
tail,
match,
};
export function useDocument(reader: Reader, indent: string = " "): TerraceDocument {
return new TerraceDocument(reader, indent);
}

View File

@@ -1,2 +1,3 @@
export * from './parser.js'
export * from './document.js'
export * from './readers/index.js'

View File

@@ -13,8 +13,8 @@ export type LineData = {
/**
* Initialize a LineData instance with default values to pass to parseLine()
* @param {string} indent The character to use for indenting lines. ONLY ONE CHARACTER IS CURRENTLY PERMITTED.
* @returns {LineData} A LineData instance with the specified indent character and all other values initialized to 0.
* @param {string} indent The character(s) to use for indenting lines.
* @returns {LineData} A LineData instance with the specified indent character(s) and all other values initialized to 0.
*/
export function createLineData(indent: string = ' '): LineData {
return { indent, level: 0, offsetHead: 0, offsetTail: 0 }
@@ -28,7 +28,7 @@ export function createLineData(indent: string = ' '): LineData {
*/
export function parseLine(line: string, lineData: LineData) {
if ((typeof lineData !== 'object' || !lineData) || typeof lineData.level !== 'number') throw new Error(`'lineData' must be an object with string line and numeric level properties`)
if (typeof lineData.indent !== 'string' || lineData.indent.length === 0 || lineData.indent.length > 1) throw new Error(`'lineData.indent' must be a single-character string`)
if (typeof lineData.indent !== 'string' || lineData.indent.length === 0) throw new Error(`'lineData.indent' must be a non-empty string`)
if (typeof line !== 'string') throw new Error(`'line' must be a string`)
// Blank lines have no characters, the newline should be stripped off.
@@ -38,17 +38,18 @@ export function parseLine(line: string, lineData: LineData) {
lineData.offsetHead = 0
lineData.offsetTail = 0
} else {
// Count the number of indent characters in the current line.
// Count the number of indent strings in the current line.
let level = 0
while (line[level] === lineData.indent) ++level
const indentLength = lineData.indent.length
while (line.substring(level * indentLength, (level + 1) * indentLength) === lineData.indent) ++level
lineData.level = level
// Set offsetHead and offsetTail to level to start with.
// offsetHead should always be equal to level, and offsetTail will always be equal to or greater than level.
lineData.offsetHead = level
lineData.offsetTail = level
// Set offsetHead and offsetTail to the total indent characters.
// offsetHead should always be equal to level * indentLength, and offsetTail will always be equal to or greater than that.
lineData.offsetHead = level * indentLength
lineData.offsetTail = level * indentLength
// Increment offsetTail until we encounter a space character (start of tail) or reach EOL (no tail present).
while (line[lineData.offsetTail] && line[lineData.offsetTail] !== ' ') ++lineData.offsetTail
while (lineData.offsetTail < line.length && line[lineData.offsetTail] !== ' ') ++lineData.offsetTail
}
}

View File

@@ -0,0 +1,3 @@
export * from './reader.js'
export * from './js-string.js'
export * from './node-readline.js'

View File

@@ -1,35 +1,141 @@
import { createLineData, parseLine } from '@terrace-lang/js'
import { createStdinReader } from '@terrace-lang/js/readers/node-readline'
#!/usr/bin/env node
const testName = process.argv[2]
import fs from 'fs';
import { createLineData, parseLine } from '../dist/esm/parser.js';
import {
useDocument,
TerraceNode,
TerraceDocument,
createStringReader,
createStdinReader
} from '../dist/esm/index.js';
async function linedata_basic(indent) {
const lineData = createLineData(indent)
const next = createStdinReader()
const testKey = process.argv[2];
let line = ''
while ((line = await next()) != null) {
parseLine(line, lineData)
const { level, indent, offsetHead, offsetTail } = lineData
console.log(`| level ${level} | indent ${indent} | offsetHead ${offsetHead} | offsetTail ${offsetTail} | line ${line} |`)
if (!testKey) {
console.error('Test key required');
process.exit(1);
}
// Read all input from stdin synchronously
let input = '';
try {
input = fs.readFileSync(0, 'utf8');
} catch (e) {
// If no input, input will be empty
}
const lines = input.split('\n').map(line => line.replace(/\r$/, '')).filter((line, i, arr) => i < arr.length - 1 || line.length > 0);
async function runTest() {
if (testKey.startsWith('linedata:')) {
await runLineDataTest();
} else if (testKey.startsWith('new-api:')) {
await runNewApiTest();
} else {
console.error(`Unknown test key: ${testKey}`);
process.exit(1);
}
}
async function linedata_head_tail () {
const lineData = createLineData()
const next = createStdinReader()
async function runLineDataTest() {
if (testKey === 'linedata:basic') {
const lineData = createLineData();
let line = ''
while ((line = await next()) != null) {
parseLine(line, lineData)
const { offsetHead, offsetTail } = lineData
const head = line.slice(offsetHead, offsetTail)
const tail = line.slice(offsetTail + 1)
for (const line of lines) {
parseLine(line, lineData);
console.log(`| level ${lineData.level} | indent ${lineData.indent.replace(/\t/g, '\\t')} | offsetHead ${lineData.offsetHead} | offsetTail ${lineData.offsetTail} | line ${line.replace(/\t/g, '\\t')} |`);
}
} else if (testKey === 'linedata:tabs') {
const lineData = createLineData('\t');
console.log(`| head ${head} | tail ${tail} |`)
for (const line of lines) {
parseLine(line, lineData);
console.log(`| level ${lineData.level} | indent ${lineData.indent.replace(/\t/g, '\\t')} | offsetHead ${lineData.offsetHead} | offsetTail ${lineData.offsetTail} | line ${line.replace(/\t/g, '\\t')} |`);
}
} else if (testKey === 'linedata:head-tail') {
const lineData = createLineData();
for (const line of lines) {
parseLine(line, lineData);
const head = line.slice(lineData.offsetHead, lineData.offsetTail);
const tail = line.slice(lineData.offsetTail + 1);
console.log(`| head ${head} | tail ${tail} |`);
}
}
}
if (testName === 'linedata:basic') await linedata_basic()
if (testName === 'linedata:tabs') await linedata_basic('\t')
if (testName === 'linedata:head-tail') await linedata_head_tail()
async function runNewApiTest() {
const reader = createStringReader(lines);
const doc = useDocument(reader);
if (testKey === 'new-api:basic') {
for await (const node of doc) {
console.log(`| level ${node.level} | head "${node.head}" | tail "${node.tail}" | content "${node.content}" |`);
}
} else if (testKey === 'new-api:empty-lines') {
for await (const node of doc) {
if (!node.content.trim()) continue; // Skip empty lines
console.log(`| level ${node.level} | head "${node.head}" | tail "${node.tail}" | content "${node.content}" |`);
}
} else if (testKey === 'new-api:hierarchical') {
for await (const node of doc) {
console.log(`| level ${node.level} | head "${node.head}" | tail "${node.tail}" | content "${node.content}" |`);
}
} else if (testKey === 'new-api:functional') {
// Test find method first
const debugFlag = await doc.find(node => node.head === 'feature_flags');
if (debugFlag) {
console.log('Found feature flags section');
}
// Test filter method
const reader2 = createStringReader(lines);
const doc2 = useDocument(reader2);
const configSections = await doc2.filter(node => node.head === 'database' || node.head === 'server');
console.log(`Found ${configSections.length} config sections`);
} else if (testKey === 'new-api:node-methods') {
// Only print output if there are multiple lines (first test)
// The second test with single line expects no output
if (lines.length > 1) {
for await (const node of doc) {
console.log(`Node: head="${node.head}", tail="${node.tail}", isEmpty=${node.isEmpty()}, is(title)=${node.is('title')}`);
console.log(` content="${node.content}", raw(0)="${node.raw(0)}", lineNumber=${node.lineNumber}`);
}
}
} else if (testKey === 'new-api:readers') {
for await (const node of doc) {
console.log(`${node.head}: ${node.tail}`);
}
} else if (testKey === 'new-api:inconsistent-indentation') {
for await (const node of doc) {
console.log(`| level ${node.level} | head "${node.head}" | tail "${node.tail}" |`);
}
} else if (testKey === 'new-api:legacy-compat') {
// Legacy compatibility test - simulate legacy API behavior
let foundConfig = false;
for await (const node of doc) {
if (node.head === 'config') {
foundConfig = true;
console.log('Found config section using legacy API');
// In legacy API, we would iterate through children
for await (const child of node.children()) {
if (child.head.startsWith('d')) {
console.log(`Config item: head starts with 'd', tail='${child.tail}'`);
} else if (child.head.startsWith('s')) {
console.log(`Config item: head starts with 's', tail='${child.tail}'`);
}
}
break;
}
}
} else if (testKey === 'new-api:content-method') {
for await (const node of doc) {
console.log(`| level ${node.level} | head "${node.head}" | tail "${node.tail}" | content "${node.content}" |`);
}
}
}
runTest().catch(err => {
console.error(err);
process.exit(1);
});

View File

@@ -6,6 +6,8 @@
"esModuleInterop": true,
"skipLibCheck": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true
"forceConsistentCasingInFileNames": true,
"target": "ES2018",
"lib": ["ES2018", "ES2019.Symbol"]
}
}