-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda-calculus.grammar.ts
More file actions
86 lines (81 loc) · 2.6 KB
/
lambda-calculus.grammar.ts
File metadata and controls
86 lines (81 loc) · 2.6 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
85
86
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 './lambda-calculus.grammar.generated.d';
/**
* Lambda Calculus Parser
*
* This parser implements a grammar for lambda calculus expressions, a formal system
* in mathematical logic for expressing computation based on function abstraction
* and application.
*
* Supported syntax:
* - x // Variable
* - λx.x // Lambda definition
* - (λx.x y) // Function application (requires parentheses)
* - (λx.λy.(x y) f) // Function currying
*
* Grammar Rules:
* - Variables: [a-zA-Z][a-zA-Z0-9']* - Identifiers starting with letter
* - Lambda: λ or \ - Lambda abstraction marker
* - Dot: . - Separates parameter from body
* - Parentheses: () - Surrounds function applications
*
* AST Node Types:
* - Expression: Root wrapper containing a term
* - Lambda: Function abstraction with parameter and body
* - Application: Function application with function and argument
* - Variable: Variable reference with name
*
* Example usage:
* ```typescript
* const ast = parser.parse('(λx.λy.x y)');
* // Results in nested Lambda nodes representing curried function
* ```
*/
const parser = syntax(`
LAMBDA ::= /λ|\\\\/
DOT ::= "."
OPEN_PAREN ::= "("
CLOSE_PAREN ::= ")"
WHITESPACE ::= /\\s+/
VARIABLE ::= /[a-zA-Z][a-zA-Z0-9']*/
<Expression> ::= {
: <optional_whitespace>,
expression: <term>,
: <optional_whitespace>
}
<Lambda> ::= {
: LAMBDA,
parameter: <- VARIABLE,
: DOT,
body: <term>
}
<Application> ::= {
: OPEN_PAREN <optional_whitespace>,
function: <term>,
: <optional_whitespace>,
argument: <term>,
: <optional_whitespace> CLOSE_PAREN
}
<Variable> ::= {
name: <- VARIABLE
}
<term> ::= <Variable> | <Lambda> | <Application>
<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 LambdaCalculusNode = InferSyntaxNodeType<typeof parser>;
export type LambdaCalculusToken = InferSyntaxTokenType<typeof parser>;
export type LambdaCalculusRootNode = InferSyntaxRootNode<typeof parser>;
export type LambdaCalculusNodes = InferSyntaxNodeTypeLookup<typeof parser>;
export type LambdaCalculusTokens = InferSyntaxTokenTypeLookup<typeof parser>;
// Export the parser for use in other files
export default parser;