@@ -244,6 +244,7 @@ object to `peg.generate`. The following options are supported:
244
244
_ global initializer_ and the _ per-parse initializer_ . Unless the parser is
245
245
to be generated in different formats, it is recommended to rather import
246
246
dependencies from within the _ global initializer_ . (default: ` {} ` )
247
+ - ` error ` — a callback for errors. See [ Error Reporting] ( #error-reporting )
247
248
- ` exportVar ` — name of a global variable into which the parser object is
248
249
assigned to when no module loader is detected; valid only when ` format ` is
249
250
set to ` "globals" ` or ` "umd" ` (default: ` null ` )
@@ -254,6 +255,7 @@ object to `peg.generate`. The following options are supported:
254
255
` source ` property (default: ` undefined ` ). This object will be used even if
255
256
` options.grammarSource ` is redefined in the grammar. It is useful to attach
256
257
the file information to the errors, for example
258
+ - ` info ` — a callback for informational messages. See [ Error Reporting] ( #error-reporting )
257
259
- ` output ` — if set to ` "parser" ` , the method will return generated parser
258
260
object; if set to ` "source" ` , it will return parser source code as a string.
259
261
If set to ` "source-and-map" ` , it will return a [ ` SourceNode ` ] object; you can
@@ -265,6 +267,27 @@ object to `peg.generate`. The following options are supported:
265
267
> a not-empty string if you set this value to ` "source-and-map" `
266
268
- ` plugins ` — plugins to use. See the [ Plugins API] ( #plugins-api ) section
267
269
- ` trace ` — makes the parser trace its progress (default: ` false ` )
270
+ - ` warning ` — a callback for warnings. See [ Error Reporting] ( #error-reporting )
271
+
272
+ #### Error Reporting
273
+
274
+ While generating the parser, the compiler may throw a ` GrammarError ` which collects
275
+ all of the issues that were seen.
276
+
277
+ There is also another way to collect problems as fast as they are reported —
278
+ register one or more of these callbacks:
279
+
280
+ - ` error(stage: Stage, message: string, location?: LocationRange, notes?: DiagnosticNote[]): void `
281
+ - ` warning(stage: Stage, message: string, location?: LocationRange, notes?: DiagnosticNote[]): void `
282
+ - ` info(stage: Stage, message: string, location?: LocationRange, notes?: DiagnosticNote[]): void `
283
+
284
+ All parameters are the same as the parameters of the [ reporting API] ( #session-api )
285
+ except the first. The ` stage ` represent one of possible stages during which execution
286
+ a diagnostic was generated. This is a string enumeration, that currently has one of
287
+ three values:
288
+ - ` check `
289
+ - ` transform `
290
+ - ` generate `
268
291
269
292
[ `SourceNode` ] : https://github.com/mozilla/source-map#sourcenode
270
293
[ source-map/444 ] : https://github.com/mozilla/source-map/issues/444
@@ -764,6 +787,10 @@ note: Step 3: call itself without input consumption - left recursion
764
787
| ^^^^^
765
788
```
766
789
790
+ A plugin may register additional passes that can generate ` GrammarError ` s to report
791
+ about problems, but they shouldn't do that by throwing an instance of ` GrammarError ` .
792
+ They should use a [ session API] ( #session-api ) instead.
793
+
767
794
## Locations
768
795
769
796
During the parsing you can access to the information of the current parse location,
@@ -858,12 +885,14 @@ method.
858
885
the AST, add or remove nodes or their properties
859
886
- ` generate ` — passes used for actual code generating
860
887
861
- A plugin that implement a pass usually should push it to the end of one of that
862
- arrays . Pass is a simple function with signature ` pass(ast, options) ` :
888
+ A plugin that implement a pass usually should push it to the end of the correct
889
+ array . Pass is a simple function with signature ` pass(ast, options, session ) ` :
863
890
- ` ast ` — the AST created by the ` config.parser.parse() ` method
864
891
- ` options ` — compilation options passed to the ` peggy.compiler.compile() ` method.
865
892
If parser generation is started because ` generate() ` function was called that
866
893
is also an options, passed to the ` generate() ` method
894
+ - ` session ` — a [ ` Session ` ] ( #session-api ) object that allows raising errors,
895
+ warnings and informational messages
867
896
- ` reservedWords ` — string array with a list of words that shouldn't be used as
868
897
label names. This list can be modified by plugins. That property is not required
869
898
to be sorted or not contain duplicates, but it is recommend to remove duplicates.
@@ -873,6 +902,57 @@ method.
873
902
- ` options ` — build options passed to the ` generate() ` method. A best practice for
874
903
a plugin would look for its own options under a ` <plugin_name> ` key.
875
904
905
+ ### Session API
906
+
907
+ Each compilation request is represented by a ` Session ` instance. An object of this class
908
+ is created by the compiler and passed to an each pass as a 3rd parameter. The session
909
+ object gives access to the various compiler services. At the present time there is only
910
+ one such service: reporting of diagnostics.
911
+
912
+ All diagnostics are divided into three groups: errors, warnings and informational
913
+ messages. For each of them the ` Session ` object has a method, described below.
914
+
915
+ All reporting methods have an identical signature:
916
+
917
+ ``` typescript
918
+ (message : string , location ? : LocationRange , notes ? : DiagnosticNote []) => void ;
919
+ ```
920
+
921
+ - ` message ` : a main diagnostic message
922
+ - ` location ` : an optional location information if diagnostic is related to the grammar
923
+ source code
924
+ - ` notes ` : an array with additional details about diagnostic, pointing to the
925
+ different places in the grammar. For example, each note could be a location of
926
+ a duplicated rule definition
927
+
928
+ #### ` error(...) `
929
+
930
+ Reports an error. Compilation process is subdivided into pieces called _ stages_ and
931
+ each stage consist of one or more _ passes_ . Within the one stage all errors, reported
932
+ by different passes, are collected without interrupting the parsing process.
933
+
934
+ When all passes in the stage are completed, the stage is checked for errors. If one
935
+ was registered, a ` GrammarError ` with all found problems in the ` problems ` property
936
+ is thrown. If there are no errors, then the next stage is processed.
937
+
938
+ After processing all three stages (` check ` , ` transform ` and ` generate ` ) the compilation
939
+ process is finished.
940
+
941
+ The process, described above, means that passes should be careful about what they do.
942
+ For example, if you place your pass into the ` check ` stage there is no guarantee that
943
+ all rules exists, because checking for existing rules is also performed during the
944
+ ` check ` stage. On the contrary, passes in the ` transform ` and ` generate ` stages can be
945
+ sure that all rules exists, because that precondition was checked on the ` check ` stage.
946
+
947
+ #### ` warning(...) `
948
+
949
+ Reports a warning. Warnings are similar to errors, but they do not interrupt a compilation.
950
+
951
+ #### ` info(...) `
952
+
953
+ Report an informational message. This method can be used to inform user about significant
954
+ changes in the grammar, for example, replacing proxy rules.
955
+
876
956
## Compatibility
877
957
878
958
Both the parser generator and generated parsers should run well in the following
0 commit comments