xref: /aosp_15_r20/external/emboss/compiler/front_end/error_examples (revision 99e0aae7469b87d12f0ad23e61142c2d74c1ef70)
1# Copyright 2019 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# Error definitions.
16#
17# Each error definition consists of an error message section, followed by one or
18# more examples of that error.  The examples are used to train the parser to
19# recognize the error.
20#
21# Each error example should contain the token '$ERR' immediately before the
22# token on which the error is triggered.
23#
24# An example may optionally contain the token '$ANY'.  If so, the corresponding
25# error message will be used as a default message for parser errors which occur
26# in the corresponding parser state.  This is roughly equivalent to repeating
27# the example with every erroneous token in place of the '$ANY'.
28
29================================================================================
30A name is required for a struct field.
31--------------------------------------------------------------------------------
32# First example.
33struct Foo:
34  0 [+1]   UInt  $ERR  # Missing name (end of struct).
35---
36# Second example -- error on newline token, not comment token.
37struct Foo:
38  0 [+1]   UInt  $ERR
39
40
41================================================================================
42Module attributes must appear before type definitions.
43--------------------------------------------------------------------------------
44struct Foo:
45  0 [+1]   UInt   field
46
47$ERR [attrib: VALUE]  # Attribute after types.
48
49
50================================================================================
51A type name must be CamelCase.
52--------------------------------------------------------------------------------
53struct $ERR foo: # snake_case
54---
55struct $ERR FOO: # SHOUTY
56---
57struct $ERR Foo_: # Not a valid identifier
58---
59bits $ERR foo:
60---
61bits $ERR FOO:
62---
63bits $ERR Foo_:
64---
65enum $ERR foo:
66---
67enum $ERR FOO:
68---
69enum $ERR Foo_:
70---
71external $ERR foo:
72---
73external $ERR FOO:
74---
75external $ERR Foo_:
76---
77bits Type:
78  bits $ERR foo:
79---
80bits Type:
81  bits $ERR FOO:
82---
83bits Type:
84  bits $ERR Foo_:
85---
86bits Type:
87  struct $ERR foo:
88---
89bits Type:
90  struct $ERR FOO:
91---
92bits Type:
93  struct $ERR Foo_:
94---
95bits Type:
96  enum $ERR foo:
97---
98bits Type:
99  enum $ERR FOO:
100---
101bits Type:
102  enum $ERR Foo_:
103---
104bits Type:
105  external $ERR foo:
106---
107bits Type:
108  external $ERR FOO:
109---
110bits Type:
111  external $ERR Foo_:
112
113
114================================================================================
115Expected type name.
116--------------------------------------------------------------------------------
117struct $ERR $ANY
118---
119bits $ERR $ANY
120---
121enum $ERR $ANY
122---
123external $ERR $ANY
124---
125bits Type:
126  bits $ERR $ANY
127---
128bits Type:
129  struct $ERR $ANY
130---
131bits Type:
132  enum $ERR $ANY
133---
134bits Type:
135  external $ERR $ANY
136
137
138================================================================================
139Expected ':' after type name.
140--------------------------------------------------------------------------------
141struct Type $ERR $ANY
142---
143bits Type $ERR $ANY
144---
145enum Type $ERR $ANY
146---
147external Type $ERR $ANY
148
149
150================================================================================
151Expected end of line.
152--------------------------------------------------------------------------------
153struct Type: $ERR $ANY
154---
155bits Type: $ERR $ANY
156---
157enum Type: $ERR $ANY
158---
159external Type: $ERR $ANY
160---
161bits Type:
162  enum Type: $ERR $ANY
163---
164bits Type:
165  struct Type: $ERR $ANY
166---
167bits Type:
168  external Type: $ERR $ANY
169---
170struct Type:
171  enum Type: $ERR $ANY
172---
173struct Type:
174  external Type: $ERR $ANY
175---
176struct Type:
177  bits Type: $ERR $ANY
178---
179bits Type:
180  if false: $ERR $ANY
181---
182struct Type:
183  if false: $ERR $ANY
184---
185struct Type:
186  0 [+0] bits: $ERR $ANY
187---
188struct Type:
189  if false:
190    0 [+0] bits: $ERR $ANY
191---
192struct Type:
193  0 [+0] enum name: $ERR $ANY
194---
195struct Type:
196  if false:
197    0 [+0] enum name: $ERR $ANY
198
199
200================================================================================
201Expected documentation, an import, a module attribute, or a type at top level.
202--------------------------------------------------------------------------------
203$ERR $ANY
204
205
206================================================================================
207Expected an import, a module attribute, or a type definition.
208--------------------------------------------------------------------------------
209-- doc
210$ERR $ANY
211
212
213================================================================================
214Expected an import, a module attribute, or a type definition.
215--------------------------------------------------------------------------------
216import "string" as name
217$ERR $ANY
218
219
220================================================================================
221Imports should follow the form 'import "file_name.emb" as name'.
222--------------------------------------------------------------------------------
223import $ERR $ANY
224---
225import "string" $ERR $ANY
226---
227import "string" as $ERR $ANY
228
229
230================================================================================
231An import statement must be on its own line.
232--------------------------------------------------------------------------------
233import "string" as name $ERR $ANY
234
235
236================================================================================
237Expected inline type definition or field definition.
238--------------------------------------------------------------------------------
239struct Type:
240  external Type:
241    --
242  $ERR $ANY
243
244
245================================================================================
246Nested type definitions must come before fields.
247--------------------------------------------------------------------------------
248struct Type:
249  VALUE [+0] Type name
250  $ERR struct
251---
252struct Type:
253  VALUE [+0] Type name
254  $ERR enum
255---
256struct Type:
257  VALUE [+0] Type name
258  $ERR bits
259
260
261================================================================================
262Type documentation must come before fields.
263--------------------------------------------------------------------------------
264struct Type:
265  VALUE [+0] Type name
266  $ERR -- doc
267
268
269================================================================================
270Expected field definition.
271--------------------------------------------------------------------------------
272struct Type:
273  VALUE [+0] Type name
274  $ERR $ANY
275
276
277================================================================================
278Expected documentation, attribute, or enum value for inline enum.
279--------------------------------------------------------------------------------
280struct Type:
281  VALUE [+0] enum name:
282    $ERR $ANY
283
284
285================================================================================
286Attributes must be of the form '[name: value]', '[(domain) name: value]', or '[$default name: value]'
287--------------------------------------------------------------------------------
288[ $ERR $ANY
289---
290struct Type:
291  VALUE [+0] Type name [name: $ERR $ANY
292---
293[( $ERR $ANY
294---
295[(name $ERR $ANY
296---
297[(name) $ERR $ANY
298---
299[$default $ERR $ANY
300---
301[name $ERR $ANY
302---
303[name: $ERR $ANY
304---
305[name: VALUE $ERR $ANY
306---
307[name: false $ERR $ANY
308---
309[name: name $ERR $ANY
310---
311[name: "string" $ERR $ANY
312---
313[name: Type $ERR $ANY
314---
315[name: 0 $ERR $ANY
316
317
318================================================================================
319Expected a variable, constant, or expression.
320--------------------------------------------------------------------------------
321# The top-of-stack parser states here are common to expression parsing; the
322# module-attribute prefix is just the shortest prefix that gets to an
323# expression.
324[name: ( $ERR $ANY
325---
326# Yes, '((' puts the parser in a slightly different state than '('.
327# Fortunately, '(((' and on are equivalent to '(('.
328[name: (( $ERR $ANY
329---
330[name: + $ERR $ANY
331---
332[name: - $ERR $ANY
333---
334[name: 0 != ( $ERR $ANY
335---
336struct Type:
337  ( $ERR $ANY
338
339
340================================================================================
341Documentation must come before any enumeration values.
342--------------------------------------------------------------------------------
343enum Type:
344  VALUE = VALUE
345    --
346  $ERR -- doc
347
348================================================================================
349Attributes must come before any enumeration values.
350--------------------------------------------------------------------------------
351enum Type:
352  VALUE = VALUE
353    --
354  $ERR [attribute: value]
355
356
357================================================================================
358Expected 'NAME = value' pair.
359--------------------------------------------------------------------------------
360enum Type:
361  VALUE = VALUE
362    --
363  $ERR $ANY
364
365
366================================================================================
367Documentation must come before attributes.
368--------------------------------------------------------------------------------
369[name: VALUE]
370$ERR -- doc
371---
372enum Type:
373  [name: "string"]
374  $ERR -- doc
375---
376external Type:
377  [name: "string"]
378  $ERR -- doc
379---
380struct Type:
381  [name: "string"]
382  $ERR -- doc
383---
384bits Type:
385  [name: "string"]
386  $ERR -- doc
387
388
389================================================================================
390Imports must come before module attributes.
391--------------------------------------------------------------------------------
392[name: VALUE]
393$ERR import
394
395
396================================================================================
397Expected an attribute or a type definition.
398--------------------------------------------------------------------------------
399[name: VALUE]
400$ERR $ANY
401
402
403================================================================================
404Expected documentation or attribute for field.  (Wrong indent?)
405--------------------------------------------------------------------------------
406struct Type :
407  VALUE [+0] Type name
408    $ERR $ANY
409
410
411================================================================================
412Documentation must come before nested types.
413--------------------------------------------------------------------------------
414struct Type:
415  enum Type:
416    VALUE = VALUE
417  $ERR -- doc
418
419
420================================================================================
421Attributes must come before nested types.
422--------------------------------------------------------------------------------
423struct Type:
424  enum Type:
425    VALUE = VALUE
426  $ERR [
427
428
429================================================================================
430Expected nested type or field definition.
431--------------------------------------------------------------------------------
432struct Type:
433  enum Type:
434    VALUE = VALUE
435  $ERR $ANY
436
437
438================================================================================
439Documentation must come before field definitions.
440--------------------------------------------------------------------------------
441struct Type:
442  VALUE [+0] Type name
443    --
444  $ERR -- doc
445
446
447================================================================================
448Attributes must come before field definitions.
449--------------------------------------------------------------------------------
450struct Type:
451  VALUE [+0] Type name
452    --
453  $ERR [
454
455
456================================================================================
457Nested types must come before field definitions.
458--------------------------------------------------------------------------------
459struct Type :
460  VALUE [+0] Type name
461    --
462  $ERR enum
463---
464struct Type :
465  VALUE [+0] Type name
466    --
467  $ERR struct
468---
469struct Type :
470  VALUE [+0] Type name
471    --
472  $ERR bits
473---
474struct Type :
475  VALUE [+0] Type name
476    --
477  $ERR external
478
479
480================================================================================
481Expected field definition.
482--------------------------------------------------------------------------------
483struct Type :
484  VALUE [+0] Type name
485    --
486  $ERR $ANY
487
488
489================================================================================
490Expected attribute or end of line.
491--------------------------------------------------------------------------------
492struct Type:
493  VALUE [+0] Type name [name: VALUE] $ERR $ANY
494
495
496================================================================================
497Module documentation must come before imports.
498--------------------------------------------------------------------------------
499import "string" as name
500$ERR -- doc
501
502
503================================================================================
504Body of a type definition must be non-empty and indented.
505--------------------------------------------------------------------------------
506bits Type:
507$ERR $ANY
508---
509external Type:
510$ERR $ANY
511---
512struct Type:
513$ERR $ANY
514---
515enum Type:
516$ERR $ANY
517
518
519================================================================================
520Expected documentation, attribute, nested type, or field definition.
521--------------------------------------------------------------------------------
522bits Type:
523  $ERR $ANY
524---
525struct Type:
526  $ERR $ANY
527
528
529================================================================================
530Expected documentation or attribute.
531--------------------------------------------------------------------------------
532external Type:
533  $ERR $ANY
534
535
536================================================================================
537Expected documentation, attribute, or enumeration value.
538--------------------------------------------------------------------------------
539enum Type:
540  $ERR $ANY
541
542
543================================================================================
544Enumerations may not contain subtypes.
545--------------------------------------------------------------------------------
546enum Type:
547  $ERR bits
548---
549enum Type:
550  $ERR struct
551---
552enum Type:
553  $ERR external
554---
555enum Type:
556  $ERR enum
557
558
559================================================================================
560Expected operator or closing parenthesis.
561--------------------------------------------------------------------------------
562[name: (name $ERR $ANY
563---
564[name: (VALUE $ERR $ANY
565---
566[name: (false $ERR $ANY
567---
568[name: (0 $ERR $ANY
569---
570[name: ((false) $ERR $ANY
571
572
573================================================================================
574Expected arithmetic operator or closing parenthesis.
575--------------------------------------------------------------------------------
576[name: (0 != 0 $ERR $ANY
577---
578[name: (0 != VALUE $ERR $ANY
579---
580[name: (0 != false $ERR $ANY
581
582
583================================================================================
584Expected arithmetic operator or end of attribute.
585--------------------------------------------------------------------------------
586[name: 0 != VALUE $ERR $ANY
587---
588[name: 0 != 0 $ERR $ANY
589---
590[name: 0 != false $ERR $ANY
591
592
593================================================================================
594Expected operator or '.'.
595--------------------------------------------------------------------------------
596[name: 0 != name $ERR $ANY
597
598
599================================================================================
600Expected operator or closing parenthesis; enumerated values do not have subfields.
601--------------------------------------------------------------------------------
602[name: (VALUE $ERR .
603
604
605================================================================================
606Expected name.
607--------------------------------------------------------------------------------
608[name: Type. $ERR $ANY
609
610
611================================================================================
612Field locations must be of the form 'start [+length]'.  Missing '+'?
613--------------------------------------------------------------------------------
614struct Foo:
615  0 [ $ERR 0
616---
617struct Foo:
618  0 [ $ERR name
619---
620struct Foo:
621  0 [ $ERR VALUE
622---
623struct Foo:
624  0 [ $ERR Type
625---
626struct Foo:
627  0 [ $ERR 0
628---
629struct Foo:
630  0 [ $ERR name
631---
632struct Foo:
633  0 [ $ERR VALUE
634---
635struct Foo:
636  0 [ $ERR Type
637
638
639================================================================================
640Right operand must be an expression.
641--------------------------------------------------------------------------------
642[name: 0 * $ERR $ANY
643---
644[name: 0 + $ERR $ANY
645---
646[name: 0 - $ERR $ANY
647---
648[name: 0 == $ERR $ANY
649---
650[name: 0 != $ERR $ANY
651---
652[name: VALUE * $ERR $ANY
653---
654[name: VALUE + $ERR $ANY
655---
656[name: VALUE - $ERR $ANY
657---
658[name: VALUE == $ERR $ANY
659---
660[name: VALUE != $ERR $ANY
661
662
663================================================================================
664Attribute must appear on its own line.
665--------------------------------------------------------------------------------
666[name: false] $ERR $ANY
667
668
669================================================================================
670Expected subtype or enumeration name.
671--------------------------------------------------------------------------------
672[name: (Type. $ERR $ANY
673
674
675================================================================================
676Attribute missing closing ']'.
677--------------------------------------------------------------------------------
678[name: (0) $ERR
679
680
681================================================================================
682Expected operator or end of attribute.
683--------------------------------------------------------------------------------
684[name: (0) $ERR $ANY
685
686
687================================================================================
688Expected subfield name.
689--------------------------------------------------------------------------------
690[name: (name. $ERR $ANY
691---
692[name: name. $ERR $ANY
693---
694[name: 0 == name. $ERR $ANY
695
696
697================================================================================
698Expected expression for size of field.
699--------------------------------------------------------------------------------
700struct Foo:
701  0 [+ $ERR $ANY
702
703
704================================================================================
705Unexpected enum name; possibly incorrect indentation.
706--------------------------------------------------------------------------------
707enum Type:
708  VALUE = VALUE
709    --
710    $ERR VALUE = VALUE
711
712
713================================================================================
714Space is required after '--'.
715--------------------------------------------------------------------------------
716enum Type:
717  VALUE = VALUE
718    --
719    $ERR --No space
720
721
722================================================================================
723Expected documentation or next value.
724--------------------------------------------------------------------------------
725enum Type:
726  VALUE = VALUE
727    --
728    $ERR $ANY
729
730
731================================================================================
732Expected '=' after enumerated name.
733--------------------------------------------------------------------------------
734enum Type:
735  VALUE $ERR $ANY
736
737
738================================================================================
739Parentheses are required to mix '&&' and '||'.
740--------------------------------------------------------------------------------
741struct Foo:
742  if a && b && c $ERR ||
743---
744struct Foo:
745  if a || b || c $ERR &&
746
747
748================================================================================
749Greater-than comparisons may only be chained with equality and greater-than.
750--------------------------------------------------------------------------------
751struct Foo:
752  if a > b $ERR <
753---
754struct Foo:
755  if a > b $ERR <=
756
757
758================================================================================
759Less-than comparisons may only be chained with equality and less-than.
760--------------------------------------------------------------------------------
761struct Foo:
762  if a < b $ERR >
763---
764struct Foo:
765  if a < b $ERR >=
766
767
768================================================================================
769Chained or nested choice operations ('?:') require explicit parentheses.
770--------------------------------------------------------------------------------
771struct Foo:
772  if a ? b : c $ERR ?
773---
774struct Foo:
775  if a ? b $ERR ?
776
777
778================================================================================
779Virtual fields are not allowed in anonymous 'bits' constructs; place them in the enclosing type.
780--------------------------------------------------------------------------------
781struct Foo:
782  0 [+1]  bits:
783    $ERR let x = 0
784