You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reserved: Improve error when reserved word used as a label name and add 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.
<p>Match the expression and remember its match result under given label.
568
-
The label must be a JavaScript identifier.</p>
569
+
The label must be a JavaScript identifier, but not in the list of reserved words.
570
+
By default this is a list of <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_keywords_as_of_ecmascript_2015">JavaScript reserved words</a>,
571
+
but <ahref="#plugins-api">plugins</a> can change it.</p>
569
572
570
573
<p>Labeled expressions are useful together with actions, where saved match
571
574
results can be accessed by action's JavaScript code.</p>
<p>Match the expression and if the label exists, remember its match result
578
581
under given label. The label must be a JavaScript identifier if it
579
-
exists.</p>
582
+
exists, but not in the list of reserved words.
583
+
By default this is a list of <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_keywords_as_of_ecmascript_2015">JavaScript reserved words</a>,
584
+
but <ahref="#plugins-api">plugins</a> can change it.</p>
580
585
581
586
<p>Return the value of this expression from the rule, or "pluck" it. You
582
587
may not have an action for this rule. The expression must not be a
<p>Changing this behavior may be a breaking change and will not to be done before
808
813
Peggy 2.0. You can join to the discussion for this topic on the <ahref="https://github.com/peggyjs/peggy/discussions/15">GitHub Discussions page</a>.</p>
809
814
815
+
<h2id="plugins-api">Plugins API</h2>
816
+
817
+
<p>A plugin is an object with the <code>use(config, options)</code> method. That method will be
818
+
called for all plugins in the <code>options.plugins</code> array, supplied to the <code>generate()</code>
819
+
method.</p>
820
+
821
+
<p><code>use</code> accepts those parameters:</p>
822
+
823
+
<h3><code>config</code></h3>
824
+
<p>Object with the following properties:</p>
825
+
826
+
<dl>
827
+
<dt><code>parser</code></dt>
828
+
<dd><code>Parser</code> object, by default the <code>peggy.parser</code> instance. That object
829
+
will be used to parse the grammar. Plugin can replace this object</dd>
830
+
831
+
<dt><code>passes</code></dt>
832
+
<dd>
833
+
<p>Mapping <code>{ [stage: string]: Pass[] }</code> that represents compilation
834
+
stages that would applied to the AST, returned by the <code>parser</code> object. That
835
+
mapping will contain at least the following keys:</p>
836
+
837
+
<ul>
838
+
<li><code>check</code> — passes that check AST for correctness. They shouldn't change the AST</li>
839
+
<li><code>transform</code> — passes that performs various optimizations. They can change
840
+
the AST, add or remove nodes or their properties</li>
841
+
<li><code>generate</code> — passes used for actual code generating</li>
842
+
</ul>
843
+
844
+
<p>Plugin that implement a pass usually should push it to the end of one of that
845
+
arrays. Pass is a simple function with signature <code>pass(ast, options)</code>:</p>
846
+
847
+
<ul>
848
+
<li><code>ast</code> — the AST created by the <code>config.parser.parse()</code> method</li>
849
+
<li><code>options</code> — compilation options passed to the <code>peggy.compiler.compile()</code> method.
850
+
If parser generation is started because <code>generate()</code> function was called that
851
+
is also an options, passed to the <code>generate()</code> method</li>
852
+
</ul>
853
+
</dd>
854
+
855
+
<dt><code>reservedWords</code></dt>
856
+
<dd>
857
+
<p>String array with a list of words that shouldn't be used as
858
+
label names. This list can be modified by plugins. That property is not required
859
+
to be sorted or not contain duplicates, but it is recommend to remove duplicates.</p>
860
+
861
+
<p>Default list contains <ahref="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_keywords_as_of_ecmascript_2015">JavaScript reserved words</a>, and can be found
862
+
in the <code>peggy.RESERVED_WORDS</code> property.</p>
863
+
</dd>
864
+
</dl>
865
+
866
+
<h3><code>options</code></h3>
867
+
<dd>Build options passed to the <code>generate()</code> method. A best practice for
868
+
a plugin would look for its own options under a <code><plugin_name></code> key.</dd>
869
+
810
870
<h2id="compatibility">Compatibility</h2>
811
871
812
872
<p>Both the parser generator and generated parsers should run well in the
0 commit comments