-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisp.grammar.ts
More file actions
84 lines (79 loc) · 2.73 KB
/
Copy pathlisp.grammar.ts
File metadata and controls
84 lines (79 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {
syntax,
type InferSyntaxNodeType,
type InferSyntaxNodeTypeLookup,
type InferSyntaxRootNode,
type InferSyntaxTokenType,
type InferSyntaxTokenTypeLookup,
} from '@timkendrick/syntax';
// Ensure type declarations are exposed for full type inference
import './lisp.grammar.generated.d';
/**
* Lisp Parser
*
* This parser implements a grammar for Lisp-style S-expressions, supporting symbols,
* lists, and string literals in a traditional Lisp syntax.
*
* Supported syntax:
* - "hello, \"world\"" // String literals
* - symbol-name // Symbols
* - (+ 1 2 3) // Lists with items
*
* Grammar Rules:
* - Symbols: [^()" \t\n]+ - Non-whitespace, non-delimiter characters
* - Strings: "..." - Double-quoted string literals with escape sequences
* - Lists: (...) - Parenthesized expressions
* - Whitespace: Spaces, tabs, and newlines for separation
*
* AST Node Types:
* - Program: Root node containing list of newline-separated statements
* - List: Parenthesized list of expressions
* - Symbol: Symbolic identifier with string value
* - StringLiteral: String literal with source text
*
* Example usage:
* ```typescript
* const ast = parser.parse('(+ 1 2 3)');
* // Results in List node with symbols "+", "1", "2", "3"
* ```
*/
const parser = syntax(`
OPEN_PAREN ::= "("
CLOSE_PAREN ::= ")"
WHITESPACE ::= /[ \\t]+/
NEWLINE ::= /\\n+/
STRING ::= /"(?:[^"\\\\\\n]|\\\\.)*"/
NAME ::= /[^()" \\t\\n]+/
<Program> ::= {
: <optional_whitespace>,
statements: <statement_list>,
: <optional_whitespace>
}
<List> ::= {
: OPEN_PAREN <optional_whitespace> | OPEN_PAREN,
items: <expression_list>,
: <optional_whitespace> CLOSE_PAREN | CLOSE_PAREN
}
<Symbol> ::= {
value: <- NAME
}
<StringLiteral> ::= {
source: <- STRING
}
<expression> ::= <Symbol> | <List> | <StringLiteral>
<expression_list> ::= [<expression>, <whitespace>]
<statement_list> ::= [<expression>, <statement_separator>]
<statement_separator> ::= <optional_padding> NEWLINE <optional_whitespace>
<optional_padding> ::= WHITESPACE | ""
<whitespace> ::= WHITESPACE <optional_whitespace> | NEWLINE <optional_whitespace>
<optional_whitespace> ::= <whitespace> | ""
`);
// Export the node/token types that were inferred from the parser source
// (these are generated from this file's source code via codegen)
export type LispNode = InferSyntaxNodeType<typeof parser>;
export type LispToken = InferSyntaxTokenType<typeof parser>;
export type LispRootNode = InferSyntaxRootNode<typeof parser>;
export type LispNodes = InferSyntaxNodeTypeLookup<typeof parser>;
export type LispTokens = InferSyntaxTokenTypeLookup<typeof parser>;
// Export the parser for use in other files
export default parser;