-
-
Notifications
You must be signed in to change notification settings - Fork 217
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
Do we get comments from ohm? #480
Comments
Comments should definitely be included in your CST. Unfortunately, without seeing at least a snippet from your grammar, I can't tell you what's going on. A common thing for people to do is to extend the
Here, you'd be able to get to the comments by writing semantic actions for Hope that helps! |
Now I set this: ProgramItem = Struct
| Contract
| Primitive
| StaticFunction
| NativeFunction
| ProgramImport
| Trait
| Constant
| Comment
Statement = StatementLet
| StatementBlock
| StatementReturn
| StatementExpression
| StatementAssign
| StatementAugmentedAssign
| StatementCondition
| StatementWhile
| StatementRepeat
| StatementUntil
| StatementTry
| StatementForEach
| Comment
// Comments
lineTerminator = "\n" | "\r\n" | "\r" | "\u2028" | "\u2029"
Comment = MultiLineComment | SingleLineComment | SingleLineDocComment | SingleLineImportantComment
MultiLineComment = "/*" (~"*/" any)* "*/"
SingleLineComment = "//" ~(("!" | "/") any) (~lineTerminator any)*
SingleLineDocComment = "///" (~lineTerminator any)*
SingleLineImportantComment = "//!" (~lineTerminator any)* Then set it: semantics.addOperation<ASTNode>('resolve_program_item', {
MultiLineComment(_open, commentText, _close) {
return createNode({
kind: 'multiLineComment',
value: commentText.sourceString.trim(),
ref: createRef(this),
});
},
SingleLineComment(_open, commentText) {
return createNode({
kind: 'singleLineComment',
value: commentText.sourceString,
ref: createRef(this),
});
},
SingleLineDocComment(_open, commentText) {
return createNode({
kind: 'singleLineDocComment',
value: commentText.sourceString.trim(),
ref: createRef(this),
});
},
SingleLineImportantComment(_open, commentText) {
return createNode({
kind: 'singleLineImportantComment',
value: commentText.sourceString.trim(),
ref: createRef(this),
});
}, But finally got the result: {
id: 3,
kind: 'program',
entries: [
{
id: 1,
kind: 'multiLineComment',
value: 'This is a multi-line comment',
ref: ASTRef {}
},
{
id: 2,
kind: 'singleLineComment',
value: 'This is a single line comment\n' +
' //! This is a single line important comment\n' +
' /// This is a single line doc comment\n' +
' fun testFunc(a: Int): Int {\n' +
' let b: Int = a == 123 ? 1 : 2;\n' +
' return b;\n' +
' }',
ref: ASTRef {}
}
]
} |
Please take a look at this page, which discusses the difference between syntactic and lexical rules: https://ohmjs.org/docs/syntax-reference#syntactic-lexical Your comment rules are syntactic (their names begin w/ a capital letter) which means they're implicitly skipping spaces. I don't think that's what you want. (The idiom that I showed you in my first message, where you extend the |
Thanks buddy. Currently I write one prettier-plugin for tact. |
I use this: Comment {
space += comment
comment = "//" (~lineTerminator any)* -- singleLine
| "/*" (~"*/" any)* "*/" -- multiLine
lineTerminator = "\n" | "\r\n" | "\r" | "\u2028" | "\u2029"
} But it still throw error: const grammar = rawGrammar.createSemantics();
const semantics = grammar.addOperation('extractComment', {
comment(arg0) {
return arg0.sourceString;
},
});
const matchResult = rawGrammar.match(`// This is a single line comment
1211
const a = 1;
`);
if (matchResult.failed()) {
console.log('Error:', matchResult.message, matchResult.shortMessage);
}
const comment = semantics(matchResult).extractComment(); |
I'm trying to process comments too, and I'm a bit baffled by the comments from @alexwarth: as far as I can tell, anything under That is, comments are available in a |
I try to get comments on code, but its CST looks like can't get these.
The text was updated successfully, but these errors were encountered: