Preparation for new partial syntax
This commit is contained in:
parent
437e91a004
commit
84cfc5863e
7 changed files with 99 additions and 36 deletions
|
|
@ -1,4 +1,5 @@
|
|||
export { parseDocument } from './parser';
|
||||
export type { FoundSyntax, ParseDocumentResult, ParseError } from './parser';
|
||||
export type { ParseDocumentResult } from './parser';
|
||||
export type { FoundSyntax, ParseError } from './parse_errors';
|
||||
export { ConcreteSyntax, Expr } from './syntax';
|
||||
export type { ConcreteSyntax as ConcreteSyntaxNode, Expr as ExprNode } from './syntax';
|
||||
|
|
|
|||
60
src/new_syntax.ts
Normal file
60
src/new_syntax.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import type { CodePointSpan } from 'source-region';
|
||||
import type { ParseError } from './parse_errors';
|
||||
|
||||
export type ConcreteSyntaxResult =
|
||||
| { tag: "valid", value: ValidConcreteSyntax }
|
||||
| { tag: "invalid", value: PartialConcreteSyntax }
|
||||
|
||||
// The main constraints are
|
||||
// - `ValidConcreteSyntax` should be a subtype of `PartialConcreteSyntax`
|
||||
// - if `PartialConcreteSyntax` doesn't contain any sort of error nodes, we should be able to coerce it to `ValidConcreteSyntax` without rebuilding the whole tree
|
||||
export type ValidConcreteSyntax = Program<{ span: CodePointSpan }, never>
|
||||
export type PartialConcreteSyntax = Program<{ span: CodePointSpan }, ConcreteError >
|
||||
|
||||
export type ConcreteError = ConcreteErrorNode[] // Can't be empty array.
|
||||
export type ConcreteErrorNode = {
|
||||
span: CodePointSpan,
|
||||
error: ParseError,
|
||||
panickedOver?: CodePointSpan,
|
||||
}
|
||||
|
||||
export type DelimiterToken =
|
||||
| { tag: "open-paren"; span: CodePointSpan }
|
||||
| { tag: "close-paren"; span: CodePointSpan }
|
||||
| { tag: "open-bracket"; span: CodePointSpan }
|
||||
| { tag: "close-bracket"; span: CodePointSpan };
|
||||
|
||||
export type Program<Info, Error> = {
|
||||
expressions: Expr<Info, Error>[],
|
||||
error?: Error,
|
||||
} & Info
|
||||
|
||||
export type Expr<Info, Error> =
|
||||
| Literal<Info, Error>
|
||||
| List<Info, Error>
|
||||
| { tag: "error-expression", error: Error } & Info // This is for errors that don't really correspond to any sort of node. Unknown errors.
|
||||
|
||||
export type List<Info, Error> =
|
||||
{ tag: "list", open: DelimiterToken, items: ListItem<Info, Error>[], close?: DelimiterToken, error?: Error } & Info
|
||||
|
||||
export type ListItem<Info, Error> =
|
||||
| Expr<Info, Error>
|
||||
| { tag: "error-list-separator", error: Error } & Info
|
||||
|
||||
export type Literal<Info, Error> =
|
||||
// === number ===
|
||||
| { tag: "number", value: number } & Info
|
||||
| { tag: "error-number", error: Error } & Info
|
||||
// === identifier ===
|
||||
| { tag: "identifier", value: Identifier } & Info
|
||||
| { tag: "error-identifier", error: Error } & Info
|
||||
|
||||
export type Identifier = string
|
||||
|
||||
|
||||
|
||||
export namespace ConcreteError {
|
||||
export function single(node: ConcreteErrorNode): ConcreteError {
|
||||
return [node];
|
||||
}
|
||||
}
|
||||
33
src/parse_errors.ts
Normal file
33
src/parse_errors.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import type { CodePoint, CodePointSpan } from 'source-region';
|
||||
|
||||
export type ParseError =
|
||||
| {
|
||||
tag: "expected-expression";
|
||||
span: CodePointSpan;
|
||||
found: FoundSyntax;
|
||||
}
|
||||
| {
|
||||
tag: "expected-close-paren";
|
||||
span: CodePointSpan;
|
||||
openParen: CodePointSpan;
|
||||
found: FoundSyntax;
|
||||
}
|
||||
| {
|
||||
tag: "unexpected-close-paren";
|
||||
span: CodePointSpan;
|
||||
}
|
||||
| {
|
||||
tag: "unexpected-code-point";
|
||||
span: CodePointSpan;
|
||||
found: FoundSyntax;
|
||||
}
|
||||
| {
|
||||
tag: "invalid-number";
|
||||
span: CodePointSpan;
|
||||
text: string;
|
||||
reason: "unsafe-integer";
|
||||
};
|
||||
|
||||
export type FoundSyntax =
|
||||
| { tag: "code-point"; value: CodePoint; span: CodePointSpan }
|
||||
| { tag: "eof"; span: CodePointSpan };
|
||||
|
|
@ -12,6 +12,7 @@ import type {
|
|||
CodePointSpan,
|
||||
SourceRegion,
|
||||
} from 'source-region';
|
||||
import type { FoundSyntax, ParseError } from './parse_errors';
|
||||
import { consumeWhile, consumeWhile1, skipWhile } from './recognizers';
|
||||
import { ConcreteSyntax } from './syntax';
|
||||
|
||||
|
|
@ -42,38 +43,6 @@ export type ParseDocumentResult = {
|
|||
errors: ParseError[];
|
||||
};
|
||||
|
||||
export type ParseError =
|
||||
| {
|
||||
tag: "expected-expression";
|
||||
span: CodePointSpan;
|
||||
found: FoundSyntax;
|
||||
}
|
||||
| {
|
||||
tag: "expected-close-paren";
|
||||
span: CodePointSpan;
|
||||
openParen: CodePointSpan;
|
||||
found: FoundSyntax;
|
||||
}
|
||||
| {
|
||||
tag: "unexpected-close-paren";
|
||||
span: CodePointSpan;
|
||||
}
|
||||
| {
|
||||
tag: "unexpected-code-point";
|
||||
span: CodePointSpan;
|
||||
found: FoundSyntax;
|
||||
}
|
||||
| {
|
||||
tag: "invalid-number";
|
||||
span: CodePointSpan;
|
||||
text: string;
|
||||
reason: "unsafe-integer";
|
||||
};
|
||||
|
||||
export type FoundSyntax =
|
||||
| { tag: "code-point"; value: CodePoint; span: CodePointSpan }
|
||||
| { tag: "eof"; span: CodePointSpan };
|
||||
|
||||
export function parseDocument(region: SourceRegion): ParseDocumentResult {
|
||||
return new Parser(region).parseDocument();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { createMemo, createSignal, Show } from 'solid-js';
|
|||
import { sourceText } from 'source-region';
|
||||
import type { CodePointSpan, SourceRegion, SourceText } from 'source-region';
|
||||
import { parseDocument } from '../parser';
|
||||
import type { ParseError } from '../parser';
|
||||
import type { ParseError } from '../parse_errors';
|
||||
import type { ConcreteSyntax } from '../syntax';
|
||||
import { spanLabel } from './format';
|
||||
import { PaneHeader, PaneSplitter } from './Pane';
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { For, Show } from 'solid-js';
|
||||
import type { JSX } from 'solid-js';
|
||||
import type { CodePointSpan } from 'source-region';
|
||||
import type { ParseError } from '../parser';
|
||||
import type { ParseError } from '../parse_errors';
|
||||
import type { ConcreteSyntax } from '../syntax';
|
||||
import { Expr } from '../syntax';
|
||||
import { errorDetail, errorLabel, errorTitle } from './format';
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import type { CodePointSpan } from 'source-region';
|
||||
import type { FoundSyntax, ParseError } from '../parser';
|
||||
import type { FoundSyntax, ParseError } from '../parse_errors';
|
||||
|
||||
export function errorTitle(error: ParseError): string {
|
||||
switch (error.tag) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue