Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace private modifier node with field on supporting types #11346

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const cases = [
'foo = case x of\n 4',
'foo = case x of\n 4->',
'foo = if cond.x else.y',
'private foo = 23',
'foreign 4',
'foreign 4 * 4',
'foreign foo = "4"',
Expand Down Expand Up @@ -113,6 +114,7 @@ const cases = [
"'''\n \\nEscape at tart\n",
'Point x_val = my_point',
'type Foo\n Bar (a : B =C.D)',
'type Foo\n private Bar',
'type Foo\n ## Bar\n Baz',
'x = """\n Indented multiline\nx',
"x =\n x = '''\n x\nx",
Expand Down
3 changes: 2 additions & 1 deletion app/ydoc-shared/src/ast/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class Abstractor {
}
case RawAst.Tree.Type.Function: {
const name = this.abstractTree(tree.name)
const private_ = tree.private && this.abstractToken(tree.private)
const argumentDefinitions = Array.from(tree.args, arg => ({
open: arg.open && this.abstractToken(arg.open),
open2: arg.open2 && this.abstractToken(arg.open2),
Expand All @@ -184,7 +185,7 @@ class Abstractor {
}))
const equals = this.abstractToken(tree.equals)
const body = tree.body !== undefined ? this.abstractTree(tree.body) : undefined
node = Function.concrete(this.module, name, argumentDefinitions, equals, body)
node = Function.concrete(this.module, private_, name, argumentDefinitions, equals, body)
break
}
case RawAst.Tree.Type.Ident: {
Expand Down
7 changes: 6 additions & 1 deletion app/ydoc-shared/src/ast/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,7 @@ interface ArgumentType<T extends TreeRefs = RawRefs> {
}

export interface FunctionFields {
private_: NodeChild<SyncTokenId> | undefined
name: NodeChild<AstId>
argumentDefinitions: ArgumentDefinition[]
equals: NodeChild<SyncTokenId>
Expand Down Expand Up @@ -1886,6 +1887,7 @@ export class Function extends Ast {

static concrete(
module: MutableModule,
private_: NodeChild<Token> | undefined,
name: NodeChild<Owned>,
argumentDefinitions: ArgumentDefinition<OwnedRefs>[],
equals: NodeChild<Token>,
Expand All @@ -1894,6 +1896,7 @@ export class Function extends Ast {
const base = module.baseObject('Function')
const id_ = base.get('id')
const fields = composeFieldData(base, {
private_,
name: concreteChild(module, name, id_),
argumentDefinitions: argumentDefinitions.map(def => mapRefs(def, ownedToRaw(module, id_))),
equals,
Expand All @@ -1913,6 +1916,7 @@ export class Function extends Ast {
// type methods.
return MutableFunction.concrete(
module,
undefined,
unspaced(Ident.newAllowingOperators(module, name)),
argumentDefinitions,
spaced(makeEquals()),
Expand Down Expand Up @@ -1951,7 +1955,8 @@ export class Function extends Ast {
}

*concreteChildren(_verbatim?: boolean): IterableIterator<RawNodeChild> {
const { name, argumentDefinitions, equals, body } = getAll(this.fields)
const { private_, name, argumentDefinitions, equals, body } = getAll(this.fields)
if (private_) yield private_
yield name
for (const def of argumentDefinitions) {
const { open, open2, suspension, pattern, type, close2, defaultValue, close } = def
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public void illegalForeignBody4() throws Exception {
public void illegalPrivateVariableDeclaration() throws Exception {
var ir = parseBlock("private var = 42");
assertSingleSyntaxError(
ir, Syntax.UnexpectedExpression$.MODULE$, "Unexpected expression", 0, 16);
ir, Syntax.UnexpectedExpression$.MODULE$, "Unexpected expression", 0, 7);
}

@Test
Expand All @@ -485,14 +485,24 @@ public void illegalPrivateKeywordUseInType() throws Exception {
18);
}

@Test
public void illegalPrivateKeywordRepeatedDeclarations() throws Exception {
var ir = parse("""
private
private
""");
assertSingleSyntaxError(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we (should we) verify:

private

private plus x y = x + y

works? CCing @AdRiley.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no risk of rejecting this due to the one-private-declaration-per-module rule--the second private is not a declaration but a field of the Function.

ir, Syntax.UnexpectedExpression$.MODULE$, "Unexpected expression", 8, 15);
}

@Test
public void illegalPrivateKeywordUseInMethodBody() throws Exception {
var ir = parse("""
method =
private priv_nested_method x = x
""");
assertSingleSyntaxError(
ir, Syntax.UnexpectedExpression$.MODULE$, "Unexpected expression", 13, 45);
ir, Syntax.UnexpectedExpression$.MODULE$, "Unexpected expression", 13, 20);
}

@Test
Expand Down Expand Up @@ -656,6 +666,9 @@ private void assertSingleSyntaxError(IR ir, Syntax.Reason type, String msg, int

private List<Syntax> assertIR(IR ir, Class<Syntax> type, int count) {
var errors = ir.preorder().filter(type::isInstance).map(type::cast);
if (ir.diagnostics() != null)
errors =
errors.prependedAll(ir.diagnostics().toList().filter(type::isInstance).map(type::cast));
assertEquals("Expecting errors: " + errors, count, errors.size());
return errors;
}
Expand Down
Loading
Loading