Skip to content

Better handling of reserved words + ability to redefine them + Plugins API doc#150

Merged
hildjj merged 4 commits intopeggyjs:mainfrom
Mingun:reserved
May 28, 2021
Merged

Better handling of reserved words + ability to redefine them + Plugins API doc#150
hildjj merged 4 commits intopeggyjs:mainfrom
Mingun:reserved

Conversation

@Mingun
Copy link
Copy Markdown
Member

@Mingun Mingun commented May 22, 2021

There is port of my PR pegjs/pegjs#552 with additions:

  • Improved error when reserved word used as a label
  • Added ability to redefine list of reserved words (useful for 3rd party plugins that generate code for other language)
  • Document Plugins API

Copy link
Copy Markdown
Contributor

@hildjj hildjj left a comment

Choose a reason for hiding this comment

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

Many of these are minor nits, but the ones about the passes structure need more attention than the others.

<link rel="stylesheet" href="/css/common.css">
<link rel="stylesheet" href="/css/layout-default.css">
<link rel="stylesheet" href="/css/content.css">
<link rel="stylesheet" href="./css/common.css">
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agree with this, but let's make the change in the rest of the html files in docs as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Actually, that shouldn't be here -- I was just testing how the documentation looks and accidently commit that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I use light-server to serve the docs locally, but it would be nice for them to work with a file: URL.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've reverted that in this PR. It was committed accidently and I have no ideas how it will work on the real site.

parser: peg.parser,
passes: convertPasses(peg.compiler.passes)
passes: convertPasses(peg.compiler.passes),
reservedWords: peg.RESERVED_WORDS.slice(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

src/parser.pegjs Outdated
};
}}
{
var reservedWords = new Set(options.reservedWords);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's make sure to test the output of Typescript in old browsers to ensure this gets polyfilled adequately.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

According to MDN, and our compatibility list we are losing Internet Explorer. Neither TypeScript, nor rollup, nor terser does not polyfill Set, so we are not ready to use Set here. I've even checked TypeScript in the ES3 mode. Nothing.

So I change it to the ordinary array. That should not be a big problem, because linear search in the list won't be much worse because that list hardly ever will big enough to see Set benefits

- `config` — object with the following properties:
- `parser` — `Parser` object, by default the `peg.parser` instance. That object
will be used to parse the grammar. Plugin can replace this object
- `passes` — mapping `{ [stage: string]: Pass[] }` that represents compilation
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this points out that compiler.compile and the code that uses it in peg.js is overly-confusing and relies on Object.keys to be consistent in the face of plugins making changes to the passes.

Let's look at that code as we document this interface, and see if it needs work.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Actually, according to the MDN, all works because of implicit assumption that keys always will be returned in the insertion order (which is equal to the declaration order for object literals). But I thin that this is implementation detail, conceptually my description should remains unchanged even if we change implementation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As a plugin author, I want to add a pass that is between check and transform. I guess I could do it without breaking other plugins that did the same thing by creating a new object, traversing Object.keys(passes), inserting my pass after check, and re-assigning passes. I guess that's close enough.

will be used to parse the grammar. Plugin can replace this object
- `passes` — mapping `{ [stage: string]: Pass[] }` that represents compilation
stages that would applied to the AST, returned by the `parser` object. That
mapping will contain at least the following keys:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"at least" implies that plugins can add passes, which I think they can, but it will be hard for them to add them in the middle at the moment.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, you right. I use that turn of speech to show that in the future list of stages could be expanded. For example, in my implementation of template rules (pegjs/pegjs#45) I've needed a precheck stage. I'm not sure, that this was right choice, although. If someday I returns to that problem I review that choice.

@hildjj hildjj added this to the 1.2 milestone May 22, 2021
@Mingun Mingun requested a review from hildjj May 27, 2021 15:45
Copy link
Copy Markdown
Contributor

@hildjj hildjj left a comment

Choose a reason for hiding this comment

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

Minor grammar nits, rebase, and I'll merge.

Mingun added 4 commits May 28, 2021 20:09
…dd documentation for plugins API

Before this commit error looks like (for input `start = break:'a'`)

> Expected "!", "$", "&", "(", "*", "+", ".", "/", "/*", "//", ";", "?", character class, code block, comment, end of line, identifier, literal, or whitespace but ":" found.

After this error looks like

> Expected identifier but reserved word "break" found.
@Mingun
Copy link
Copy Markdown
Member Author

Mingun commented May 28, 2021

Done.

@hildjj hildjj merged commit b7f262c into peggyjs:main May 28, 2021
@Mingun Mingun deleted the reserved branch May 28, 2021 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants