react-dsl-editor 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,86 +1,106 @@
1
- import { CSSProperties } from 'react';
2
1
  import { JSX } from 'react/jsx-runtime';
3
2
  import { TextareaHTMLAttributes } from 'react';
4
3
 
5
- export declare function alternative<T extends string>(type?: T, ...seq: Parse<T>[]): Parse<T>;
4
+ export declare function alternative<T extends string>(...seq: GrammarNode<T>[]): GrammarNode<T>;
6
5
 
7
- export declare function appendOffset<T>(error: ParserError<T>, offset: number): ParserError<T>;
6
+ export declare function asException<T extends string>(error: ParserError<T>): ParsingError;
8
7
 
9
- export declare function appendOffsets<T>(errors: ParserError<T>[], offsets: number): ParserError<T>[];
10
-
11
- export declare interface ASTNode<T extends string> {
12
- type: T;
8
+ export declare interface CSTNode<T extends string> {
13
9
  text: string;
14
- children?: ASTNode<T>[];
15
- errors: ParserError<T>[];
16
10
  offset: number;
11
+ end: number;
12
+ grammar: GrammarNode<T>;
13
+ children?: CSTNode<T>[];
14
+ }
15
+
16
+ export declare type CSTOf<T> = CSTNode<_NodeTypes<T>>;
17
+
18
+ export declare interface DSL<T extends string> {
19
+ cst: CSTNode<T>;
20
+ terminals: CSTNode<T>[];
21
+ result: ParserSuccess<T>;
17
22
  }
18
23
 
19
- export declare function DslEditor<T extends string>({ code, onChange, onParsed, grammar, wrap, suggestions: clientSuggestions, className, styles, ...textareaProps }: {
24
+ export declare function DslEditor<T extends string>({ code, onChange, onParsed, grammar, wrap, suggestions: clientSuggestions, className, ...textareaProps }: {
20
25
  code: string;
21
26
  onChange: (text: string) => void;
22
- onParsed?: (ast: ParserResult<T>) => void;
23
- grammar: Parse<T>;
27
+ onParsed?: (ast: ParserSuccess<T>) => void;
28
+ grammar: GrammarNode<T>;
24
29
  wrap?: boolean;
25
30
  className?: string;
26
- styles?: Partial<Record<T | 'error', CSSProperties>>;
27
- suggestions?: (type: T | 'error') => string[] | undefined;
31
+ suggestions?: (node: CSTNode<T>) => string[] | undefined;
28
32
  } & Omit<TextareaHTMLAttributes<HTMLTextAreaElement>, 'wrap' | 'onChange'>): JSX.Element;
29
33
 
30
- export declare function isParserError<T extends string>(result: ParserResult<T>): boolean;
34
+ export declare class DSLParser<T extends string> {
35
+ private readonly grammar;
36
+ constructor(grammar: GrammarNode<T>);
37
+ private _parse;
38
+ parse(input: string): DSL<T>;
39
+ }
31
40
 
32
- export declare function isParserSuccess<T extends string>(result: ParserResult<T>): boolean;
41
+ export declare const eof: GrammarNode;
33
42
 
34
- export declare type NodeTypes<T> = _NodeTypes<T> | 'error';
43
+ export declare function error<T extends string>(param: ParserError<T>): ParserError<T>;
35
44
 
36
- declare type _NodeTypes<T> = T extends Parse<infer U> ? U : never;
45
+ export declare interface GrammarNode<T extends string = never> {
46
+ type: T;
47
+ suggestions(): string[];
48
+ parse(text: string, context: ParserContext): ParserResult<T>;
49
+ children: GrammarNode<T>[];
50
+ meta?: Record<string, unknown>;
51
+ }
37
52
 
38
- export declare function optional<T extends string>(subparser: Parse<T>): Parse<T>;
53
+ export declare function isParserError<T extends string>(result: ParserResult<T>): result is ParserError<T>;
39
54
 
40
- export declare const optionalWhitespace: Parse<"optionalWhitespace">;
55
+ export declare function isParserSuccess<T extends string>(result: ParserResult<T>): result is ParserSuccess<T>;
41
56
 
42
- export declare type Parse<T extends string> = (text: string) => ParserResult<T>;
57
+ export declare function named<T extends string, K extends string>(name: T, child: GrammarNode<K>): GrammarNode<T | K>;
43
58
 
44
- export declare class Parser<T extends string> {
45
- private readonly parser;
46
- constructor(parser: Parse<T>);
47
- parse(input: string): ASTNode<T>;
59
+ export declare type NodeTypes<T> = _NodeTypes<T> | 'error';
60
+
61
+ declare type _NodeTypes<T> = T extends GrammarNode<infer U> ? U : never;
62
+
63
+ export declare function optional<T extends string>(child: GrammarNode<T>): GrammarNode<T>;
64
+
65
+ export declare interface ParserContext {
66
+ faultTolerant: boolean;
67
+ faultCorrection<T extends string>(parse: ParserResult<T>, grammar: GrammarNode<T>): ParserResult<T>;
48
68
  }
49
69
 
50
- export declare interface ParserError<T> {
51
- offset: number;
52
- expected: string | RegExp;
70
+ export declare interface ParserError<T extends string = never> {
71
+ grammar: GrammarNode<T>;
53
72
  got: string;
54
- type: T;
73
+ expected: string[];
55
74
  }
56
75
 
57
- export declare interface ParserResult<T extends string> {
58
- type: T;
76
+ export declare type ParserResult<T extends string = never> = ParserSuccess<T> | ParserError<T>;
77
+
78
+ export declare interface ParserSuccess<T extends string = never> {
59
79
  text: string;
60
- children?: ParserResult<T>[];
61
- errors: ParserError<T>[];
80
+ grammar: GrammarNode<T>;
81
+ children: ParserSuccess<T>[];
62
82
  }
63
83
 
64
84
  export declare class ParsingError extends Error {
65
85
  constructor(msg: string);
66
86
  }
67
87
 
68
- export declare function pattern(regex: string): Parse<'pattern'>;
88
+ export declare function pattern(regex: RegExp): GrammarNode<never>;
69
89
 
70
- export declare function pattern<T extends string>(regex: string, type: T): Parse<T>;
90
+ export declare const rational: GrammarNode<never>;
71
91
 
72
- export declare const rational: Parse<"rational">;
92
+ export declare function repeat<T extends string>(child: GrammarNode<T>, min?: number, max?: number): GrammarNode<T>;
73
93
 
74
- export declare function repeat<T extends string>(type: T | undefined, parser: Parse<T>, min?: number, max?: number): Parse<T>;
94
+ export declare function seq<T extends string>(...nodes: GrammarNode<T>[]): GrammarNode<T>;
75
95
 
76
- export declare function seq<T extends string>(type?: T, ...p: Parse<T>[]): Parse<T | 'optionalWhitespace'>;
96
+ export declare function sequence<T extends string>(...nodes: GrammarNode<T>[]): GrammarNode<T>;
77
97
 
78
- export declare function sequence<T extends string>(type?: T, ...seq: Parse<T>[]): Parse<T>;
98
+ export declare function success<T extends string>(param: ParserSuccess<T>): ParserSuccess<T>;
79
99
 
80
- export declare function term<T extends string>(str: T): Parse<T>;
100
+ export declare function term(str: string): GrammarNode<never>;
81
101
 
82
- export declare function term<T extends string>(str: string, type: T): Parse<T>;
102
+ export declare function visit<T extends string, V = string>(parserResult: ParserSuccess<T>, type: T, extractor?: (node: ParserSuccess<T>) => V): V[];
83
103
 
84
- export declare const ws: Parse<"space" | "ws">;
104
+ export declare const ws: GrammarNode<never>;
85
105
 
86
106
  export { }