Skip to content

Commit

Permalink
Move TypeSignature into field of Function (#11364)
Browse files Browse the repository at this point in the history
Move type-signature lines into `Function` field. Also implements #11293.

Stacked on #11346.
  • Loading branch information
kazcw authored Oct 22, 2024
1 parent d278ad6 commit 4e4a1e1
Show file tree
Hide file tree
Showing 13 changed files with 770 additions and 336 deletions.
22 changes: 21 additions & 1 deletion app/ydoc-shared/src/ast/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class Abstractor {
}
case RawAst.Tree.Type.Function: {
const name = this.abstractTree(tree.name)
const signatureLine = tree.signatureLine && {
signature: this.abstractTypeSignature(tree.signatureLine.signature),
newlines: Array.from(tree.signatureLine.newlines, this.abstractToken.bind(this)),
}
const private_ = tree.private && this.abstractToken(tree.private)
const argumentDefinitions = Array.from(tree.args, arg => ({
open: arg.open && this.abstractToken(arg.open),
Expand All @@ -186,7 +190,15 @@ class Abstractor {
}))
const equals = this.abstractToken(tree.equals)
const body = tree.body !== undefined ? this.abstractTree(tree.body) : undefined
node = Function.concrete(this.module, private_, name, argumentDefinitions, equals, body)
node = Function.concrete(
this.module,
signatureLine,
private_,
name,
argumentDefinitions,
equals,
body,
)
break
}
case RawAst.Tree.Type.Ident: {
Expand Down Expand Up @@ -408,6 +420,14 @@ class Abstractor {
throw new Error('Unreachable: Splice in non-interpolated text field')
}
}

private abstractTypeSignature(signature: RawAst.TypeSignature) {
return {
name: this.abstractTree(signature.name),
operator: this.abstractToken(signature.operator),
type: this.abstractTree(signature.typeNode),
}
}
}

declare const nodeKeyBrand: unique symbol
Expand Down
31 changes: 30 additions & 1 deletion app/ydoc-shared/src/ast/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ type StructuralField<T extends TreeRefs = RawRefs> =
| TextElement<T>
| ArgumentDefinition<T>
| VectorElement<T>
| TypeSignature<T>
| SignatureLine<T>

/** Type whose fields are all suitable for storage as `Ast` fields. */
interface FieldObject<T extends TreeRefs> {
Expand Down Expand Up @@ -566,6 +568,10 @@ function mapRefs<T extends TreeRefs, U extends TreeRefs>(
field: VectorElement<T>,
f: MapRef<T, U>,
): VectorElement<U>
function mapRefs<T extends TreeRefs, U extends TreeRefs>(
field: SignatureLine<T>,
f: MapRef<T, U>,
): SignatureLine<U>
function mapRefs<T extends TreeRefs, U extends TreeRefs>(
field: FieldData<T>,
f: MapRef<T, U>,
Expand Down Expand Up @@ -2029,7 +2035,19 @@ interface ArgumentType<T extends TreeRefs = RawRefs> {
type: T['ast']
}

interface TypeSignature<T extends TreeRefs = RawRefs> {
name: T['ast']
operator: T['token']
type: T['ast']
}

interface SignatureLine<T extends TreeRefs = RawRefs> {
signature: TypeSignature<T>
newlines: T['token'][]
}

export interface FunctionFields {
signatureLine: SignatureLine | undefined
private_: NodeChild<SyncTokenId> | undefined
name: NodeChild<AstId>
argumentDefinitions: ArgumentDefinition[]
Expand Down Expand Up @@ -2068,6 +2086,7 @@ export class Function extends Ast {
/** TODO: Add docs */
static concrete(
module: MutableModule,
signatureLine: SignatureLine<OwnedRefs> | undefined,
private_: NodeChild<Token> | undefined,
name: NodeChild<Owned>,
argumentDefinitions: ArgumentDefinition<OwnedRefs>[],
Expand All @@ -2077,6 +2096,7 @@ export class Function extends Ast {
const base = module.baseObject('Function')
const id_ = base.get('id')
const fields = composeFieldData(base, {
signatureLine: signatureLine && mapRefs(signatureLine, ownedToRaw(module, id_)),
private_,
name: concreteChild(module, name, id_),
argumentDefinitions: argumentDefinitions.map(def => mapRefs(def, ownedToRaw(module, id_))),
Expand All @@ -2099,6 +2119,7 @@ export class Function extends Ast {
return MutableFunction.concrete(
module,
undefined,
undefined,
unspaced(Ident.newAllowingOperators(module, name)),
argumentDefinitions,
spaced(makeEquals()),
Expand Down Expand Up @@ -2140,7 +2161,15 @@ export class Function extends Ast {

/** TODO: Add docs */
*concreteChildren(_verbatim?: boolean): IterableIterator<RawNodeChild> {
const { private_, name, argumentDefinitions, equals, body } = getAll(this.fields)
const { signatureLine, private_, name, argumentDefinitions, equals, body } = getAll(this.fields)
if (signatureLine) {
const { signature, newlines } = signatureLine
const { name, operator, type } = signature
yield name
yield operator
yield type
yield* newlines
}
if (private_) yield private_
yield name
for (const def of argumentDefinitions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ public void malformedTypeException() throws Exception {
parse(
"""
fan_out_to_columns : Table -> Text | Integer -> (Any -> Vector Any) -> | Nothing -> Problem_Behavior -> Table | Nothing
fan_out_to_columns table text_or_integer any_to_vector_any wat problem_behavior = Nothing
""");
assertSingleSyntaxError(
ir, Syntax.UnexpectedExpression$.MODULE$, "Unexpected expression", 48, 119);
Expand Down
Loading

0 comments on commit 4e4a1e1

Please sign in to comment.