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

Generate grammar comments #1335

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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 @@ -22,8 +22,11 @@ Entity:
(features+=Feature)*
'}';

/**
* This is a parser rule
*/
Feature:
(many?='many')? name=ID ':' type=[Type:QualifiedName];
(many?='many')? name=ID ':' /** This is an assignment */ type=[Type:QualifiedName];

QualifiedName returns string:
ID ('.' ID)*;
Expand Down
6 changes: 4 additions & 2 deletions examples/domainmodel/src/language-server/generated/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ export const DomainModelGrammar = (): Grammar => loadedDomainModelGrammar ?? (lo
"arguments": []
},
"deprecatedSyntax": false
}
},
"$comment": "/** This is an assignment */"
}
]
},
Expand All @@ -315,7 +316,8 @@ export const DomainModelGrammar = (): Grammar => loadedDomainModelGrammar ?? (lo
"fragment": false,
"hiddenTokens": [],
"parameters": [],
"wildcard": false
"wildcard": false,
"$comment": "/**\\n * This is a parser rule\\n */"
},
{
"$type": "ParserRule",
Expand Down
1 change: 1 addition & 0 deletions packages/langium-cli/src/generator/grammar-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function serializeGrammar(services: LangiumServices, grammars: Grammar[],
};
const serializedGrammar = services.serializer.JsonSerializer.serialize(grammar, {
space: production ? undefined : 2,
comments: true,
uriConverter
});
// The json serializer returns strings with \n line delimiter by default
Expand Down
11 changes: 6 additions & 5 deletions packages/langium/src/grammar/langium-grammar.langium
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/******************************************************************************
* Copyright 2021 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
// ******************************************************************************
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this comment formatting is changed to be not recognized as JSdoc, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct 👍

// Copyright 2021 TypeFox GmbH
// This program and the accompanying materials are made available under the
// terms of the MIT License, which is available in the project root.
// *****************************************************************************

grammar LangiumGrammar

entry Grammar:
Expand Down
5 changes: 4 additions & 1 deletion packages/langium/src/serializer/json-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ export class DefaultJsonSerializer implements JsonSerializer {
}
if (comments) {
astNode ??= { ...value };
(astNode as AstNodeWithComment).$comment = this.commentProvider.getComment(value);
const comment = this.commentProvider.getComment(value);
if (comment) {
(astNode as AstNodeWithComment).$comment = comment.replace(/\r/g, '');
}
}
return astNode ?? value;
} else {
Expand Down