@@ -3,13 +3,14 @@ import { withPhantomData, type WithPhantomData } from '../../phantom-data.js'
33import type { Writable } from '../../utility-types.js'
44import type { ElaborationError , InvalidSyntaxTreeError } from '../errors.js'
55import type { Atom , Molecule , SyntaxTree } from '../parsing.js'
6+ import type { Expression } from './expression.js'
7+ import type { KeyPath } from './key-path.js'
8+ import { isKeyword , type Keyword } from './keyword.js'
69import {
710 makeObjectNode ,
811 makeUnelaboratedObjectNode ,
9- type KeyPath ,
1012 type ObjectNode ,
11- } from '../semantics.js'
12- import type { Expression } from './expression.js'
13+ } from './object-node.js'
1314import {
1415 extractStringValueIfPossible ,
1516 updateValueAtKeyPathInSemanticGraph ,
@@ -32,16 +33,13 @@ export type KeywordHandler = (
3233 context : ExpressionContext ,
3334) => KeywordElaborationResult
3435
35- export type KeywordModule < Keyword extends `@${string } `> = {
36- readonly isKeyword : ( input : string ) => input is Keyword
37- readonly handlers : Readonly < Record < Keyword , KeywordHandler > >
38- }
36+ export type KeywordHandlers = Readonly < Record < Keyword , KeywordHandler > >
3937
4038export const elaborate = (
4139 program : SyntaxTree ,
42- keywordModule : KeywordModule < `@${ string } ` > ,
40+ keywordHandlers : KeywordHandlers ,
4341) : Either < ElaborationError , ElaboratedSemanticGraph > =>
44- elaborateWithContext ( program , keywordModule , {
42+ elaborateWithContext ( program , keywordHandlers , {
4543 location : [ ] ,
4644 program :
4745 typeof program === 'string'
@@ -51,19 +49,19 @@ export const elaborate = (
5149
5250export const elaborateWithContext = (
5351 program : SyntaxTree ,
54- keywordModule : KeywordModule < `@${ string } ` > ,
52+ keywordHandlers : KeywordHandlers ,
5553 context : ExpressionContext ,
5654) : Either < ElaborationError , ElaboratedSemanticGraph > =>
5755 either . map (
5856 typeof program === 'string'
5957 ? handleAtomWhichMayNotBeAKeyword ( program )
60- : elaborateWithinMolecule ( program , keywordModule , context ) ,
58+ : elaborateWithinMolecule ( program , keywordHandlers , context ) ,
6159 withPhantomData < Elaborated > ( ) ,
6260 )
6361
6462const elaborateWithinMolecule = (
6563 molecule : Molecule ,
66- keywordModule : KeywordModule < `@${ string } ` > ,
64+ keywordHandlers : KeywordHandlers ,
6765 context : ExpressionContext ,
6866) : Either < ElaborationError , SemanticGraph > => {
6967 const possibleExpressionAsObjectNode : Writable < ObjectNode > = makeObjectNode (
@@ -83,7 +81,7 @@ const elaborateWithinMolecule = (
8381 } else {
8482 const elaborationResult = elaborateWithinMolecule (
8583 value ,
86- keywordModule ,
84+ keywordHandlers ,
8785 {
8886 location : [ ...context . location , key ] ,
8987 program : updatedProgram ,
@@ -148,7 +146,7 @@ const elaborateWithinMolecule = (
148146 ...possibleExpressionAsObjectNode ,
149147 0 : possibleKeywordAsString ,
150148 } ,
151- keywordModule ,
149+ keywordHandlers ,
152150 {
153151 program : updatedProgram ,
154152 location : context . location ,
@@ -158,32 +156,29 @@ const elaborateWithinMolecule = (
158156 }
159157}
160158
161- const handleObjectNodeWhichMayBeAExpression = < Keyword extends `@${ string } ` > (
159+ const handleObjectNodeWhichMayBeAExpression = (
162160 node : ObjectNode & { readonly 0 : Atom } ,
163- keywordModule : KeywordModule < Keyword > ,
161+ keywordHandlers : KeywordHandlers ,
164162 context : ExpressionContext ,
165163) : Either < ElaborationError , SemanticGraph > => {
166164 const { 0 : possibleKeyword , ...possibleArguments } = node
167- return option . match (
168- option . fromPredicate ( possibleKeyword , keywordModule . isKeyword ) ,
169- {
170- none : ( ) =>
171- / ^ @ [ ^ @ ] / . test ( possibleKeyword )
172- ? either . makeLeft ( {
173- kind : 'unknownKeyword' ,
174- message : `unknown keyword: \`${ possibleKeyword } \`` ,
175- } )
176- : either . makeRight ( {
177- ...node ,
178- 0 : unescapeKeywordSigil ( possibleKeyword ) ,
179- } ) ,
180- some : keyword =>
181- keywordModule . handlers [ keyword ] (
182- makeObjectNode ( { ...possibleArguments , 0 : keyword } ) ,
183- context ,
184- ) ,
185- } ,
186- )
165+ return option . match ( option . fromPredicate ( possibleKeyword , isKeyword ) , {
166+ none : ( ) =>
167+ / ^ @ [ ^ @ ] / . test ( possibleKeyword )
168+ ? either . makeLeft ( {
169+ kind : 'unknownKeyword' ,
170+ message : `unknown keyword: \`${ possibleKeyword } \`` ,
171+ } )
172+ : either . makeRight ( {
173+ ...node ,
174+ 0 : unescapeKeywordSigil ( possibleKeyword ) ,
175+ } ) ,
176+ some : keyword =>
177+ keywordHandlers [ keyword ] (
178+ makeObjectNode ( { ...possibleArguments , 0 : keyword } ) ,
179+ context ,
180+ ) ,
181+ } )
187182}
188183
189184const handleAtomWhichMayNotBeAKeyword = (
0 commit comments