Skip to content

Commit

Permalink
Add ability to extract comments from the grammar
Browse files Browse the repository at this point in the history
All comments stored in the `comments` property of the `grammar` node.
Comments extracted only if the `extractComments` options set to `true` when you generate parser.
This property is object with mapping start offset of comment to comment object, that looks like:

```js
{
  text: 'text in the comment, just after // or /* and before */',
  multiline: true|false,// true for /**/ comments, false for // comments
  location: location()
}
```
  • Loading branch information
Mingun committed Jan 24, 2018
1 parent b6bc0d9 commit ac8d2b7
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 200 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,25 @@ object to `peg.generate`. The following options are supported:
* `plugins` — plugins to use
* `trace` — makes the parser trace its progress (default: `false`)

Also you can supply boolean option `extractComments` (default: `false`).
When set to `true`, parser will be collect all comments in the grammar to the object
in the `comments` property in the `grammar` AST node. This key contains mapping from
offset position of start location of the comment (i.e. `//` or `/*`) to comment object
itself. Comment object has following structure:

```js
{
text: 'all text between /* or */, or // and end of line',
multiline: true|false,
location: location()
}
```

When set to `false`, `comments` will be set to `null`.

This option not impact to the generated parser but only to grammar AST, which
can be used by plugins.

Using the Parser
----------------

Expand Down
3 changes: 2 additions & 1 deletion lib/parser/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ exports.Node = Node;
class Grammar extends Node {

// Creates a new AST
constructor( initializer, rules, location ) {
constructor( initializer, rules, comments, location ) {

super( "grammar", location );

this.initializer = initializer;
this.comments = comments;
this.rules = rules;

}
Expand Down
Loading

0 comments on commit ac8d2b7

Please sign in to comment.