File: egrammar.ra

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (889 lines) | stat: -rw-r--r-- 31,416 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
# vim: syntax=ruby

# Parser using the Pops model, expression based

class Puppet::Pops::Parser::Parser

token STRING DQPRE DQMID DQPOST
token WORD
token LBRACK  RBRACK LBRACE RBRACE SYMBOL FARROW COMMA TRUE
token FALSE EQUALS APPENDS DELETES LESSEQUAL NOTEQUAL DOT COLON LLCOLLECT RRCOLLECT
token QMARK WSLPAREN LPAREN RPAREN ISEQUAL GREATEREQUAL GREATERTHAN LESSTHAN
token IF ELSE
token DEFINE ELSIF VARIABLE CLASS INHERITS NODE BOOLEAN
token NAME SEMIC CASE DEFAULT AT ATAT LCOLLECT RCOLLECT CLASSREF
token NOT OR AND UNDEF PARROW PLUS MINUS TIMES DIV LSHIFT RSHIFT UMINUS
token MATCH NOMATCH REGEX IN_EDGE OUT_EDGE IN_EDGE_SUB OUT_EDGE_SUB
token IN UNLESS PIPE
token LAMBDA SELBRACE
token NUMBER
token HEREDOC SUBLOCATE
token RENDER_STRING RENDER_EXPR EPP_START EPP_END EPP_END_TRIM
token FUNCTION
token TYPE
token PRIVATE ATTR
token PLAN APPLY
token LOW

prechigh
  left  HIGH
  left  SEMIC
  left  PIPE
  left  LPAREN WSLPAREN
  left  RPAREN
  left  DOT
  nonassoc EPP_START
  left  LBRACK LISTSTART
  left  RBRACK
  left  LCOLLECT LLCOLLECT
  right NOT
  nonassoc SPLAT
  nonassoc UMINUS
  left  IN
  left  MATCH NOMATCH
  left  TIMES DIV MODULO
  left  MINUS PLUS
  left  LSHIFT RSHIFT
  left  NOTEQUAL ISEQUAL
  left  GREATEREQUAL GREATERTHAN LESSTHAN LESSEQUAL
  left  QMARK
  left  AND
  left  OR
  left  LBRACE
  left  SELBRACE
  left  RBRACE
  right AT ATAT
  right APPENDS DELETES EQUALS
  left  IN_EDGE OUT_EDGE IN_EDGE_SUB OUT_EDGE_SUB
  left  FARROW
  left  COMMA
  nonassoc RENDER_EXPR
  nonassoc RENDER_STRING
  left  LOW
preclow

rule
# Produces [Model::Program] with a body containing what was parsed
program
  : statements     { result = create_program(Factory.block_or_expression(val[0])) }
  | epp_expression { result = create_program(val[0]) }
  |                { result = create_empty_program }

# Produces a semantic model (non validated, but semantically adjusted).
statements
  : syntactic_statements { result = transform_calls(val[0]) }

# Collects sequence of elements into a list that the statements rule can transform
# (Needed because language supports function calls without parentheses around arguments).
# Produces Array<Model::Expression>
#
syntactic_statements
  : syntactic_statement                            { result = [val[0]]}
  | syntactic_statements SEMIC syntactic_statement { result = val[0].push val[2] }
  | syntactic_statements syntactic_statement       { result = val[0].push val[1] }

# Produce a single expression or Array of expression
# This exists to handle multiple arguments to non parenthesized function call. If e is expression,
# the a program can consists of e [e,e,e] where the first may be a name of a function to call.
#
syntactic_statement
  : assignment                             =LOW { result = val[0] }
  | syntactic_statement COMMA assignment   =LOW { result = aryfy(val[0]).push(val[1]).push(val[2]) }

# Assignment (is right recursive since assignment is right associative)
assignment
  : relationship =LOW
  | relationship EQUALS  assignment         { result = val[0].set(val[2])      ; loc result, val[1] }
  | relationship APPENDS assignment         { result = val[0].plus_set(val[2]) ; loc result, val[1] }
  | relationship DELETES assignment         { result = val[0].minus_set(val[2]); loc result, val[1] }

# Argument is like assignment but permits KEY_ENTRY which it converts it to a HASH_UNFOLDED
argument
  : assignment
  | hashpair { result = Factory.HASH_UNFOLDED([val[0]]); loc result, val[0] }

# Repeated adjacent HASH_UNFOLDED entries merged into one HASH_UNFOLDED
arguments
  : argument                            { result = [val[0]] }
  | arguments COMMA argument            { result = Factory.ARGUMENTS(val[0], val[2]) }

relationship
  : resource =LOW
  | relationship IN_EDGE      resource      { result = val[0].relop(val[1][:value], val[2]); loc result, val[1] }
  | relationship IN_EDGE_SUB  resource      { result = val[0].relop(val[1][:value], val[2]); loc result, val[1] }
  | relationship OUT_EDGE     resource      { result = val[0].relop(val[1][:value], val[2]); loc result, val[1] }
  | relationship OUT_EDGE_SUB resource      { result = val[0].relop(val[1][:value], val[2]); loc result, val[1] }

#-- RESOURCE
#
resource
  : expression = LOW

    #---VIRTUAL
  | AT resource {
      result = val[1]
      unless Factory.set_resource_form(result, 'virtual')
        # This is equivalent to a syntax error - additional semantic restrictions apply
        error val[0], "Virtual (@) can only be applied to a Resource Expression"
      end
      # relocate the result
      loc result, val[0], val[1]
    }

    #---EXPORTED
  | ATAT resource {
      result = val[1]
      unless Factory.set_resource_form(result, 'exported')
        # This is equivalent to a syntax error - additional semantic restrictions apply
        error val[0], "Exported (@@) can only be applied to a Resource Expression"
      end
      # relocate the result
      loc result, val[0], val[1]
    }

    #---RESOURCE TITLED 3x and 4x
  | resource LBRACE expression COLON attribute_operations additional_resource_bodies RBRACE {
      bodies = [Factory.RESOURCE_BODY(val[2], val[4])] + val[5]
      result = Factory.RESOURCE(val[0], bodies)
      loc result, val[0], val[6]
    }

    #---CLASS RESOURCE
  | CLASS LBRACE resource_bodies endsemi RBRACE {
        result = Factory.RESOURCE(Factory.fqn(token_text(val[0])), val[2])
        loc result, val[0], val[4]
    }

    # --RESOURCE 3X Expression
    #   Handles both 3x overrides and defaults (i.e. single resource_body without title colon)
    #   Slated for possible deprecation since it requires transformation and mix static/evaluation check
    #
  | resource LBRACE attribute_operations endcomma RBRACE {
       result = case Factory.resource_shape(val[0])
       when :resource, :class
        # This catches deprecated syntax.
        # If the attribute operations does not include +>, then the found expression
        # is actually a LEFT followed by LITERAL_HASH
        #
        unless tmp = transform_resource_wo_title(val[0], val[2], val[1], val[4])
          error val[1], "Syntax error resource body without title or hash with +>"
        end
        tmp
      when :defaults
        Factory.RESOURCE_DEFAULTS(val[0], val[2])
      when :override
        # This was only done for override in original - TODO should it be here at all
        Factory.RESOURCE_OVERRIDE(val[0], val[2])
      else
        error val[0], "Expression is not valid as a resource, resource-default, or resource-override"
      end
     loc result, val[0], val[4]
    }

  resource_body
    : expression COLON attribute_operations endcomma { result = Factory.RESOURCE_BODY(val[0], val[2]) }

  resource_bodies
    : resource_body                         =HIGH { result = [val[0]] }
    | resource_bodies SEMIC resource_body   =HIGH { result = val[0].push val[2] }

  # This is a rule for the intermediate state where RACC has seen enough tokens to understand that
  # what is expressed is a Resource Expression, it now has to get to the finishing line
  #
  additional_resource_bodies
    : endcomma                         { result = [] }
    | endcomma SEMIC                   { result = [] }
    | endcomma SEMIC resource_bodies endsemi { result = val[2] }

#-- EXPRESSION
#
expression
  : primary_expression
  | call_function_expression
  | bracketed_expression
  | expression IN           expression                 { result = val[0].in val[2]        ; loc result, val[1] }
  | expression MATCH        expression                 { result = val[0] =~  val[2]       ; loc result, val[1] }
  | expression NOMATCH      expression                 { result = val[0].mne val[2]       ; loc result, val[1] }
  | expression PLUS         expression                 { result = val[0] +   val[2]       ; loc result, val[1] }
  | expression MINUS        expression                 { result = val[0] -   val[2]       ; loc result, val[1] }
  | expression DIV          expression                 { result = val[0] /   val[2]       ; loc result, val[1] }
  | expression TIMES        expression                 { result = val[0] *   val[2]       ; loc result, val[1] }
  | expression MODULO       expression                 { result = val[0] %   val[2]       ; loc result, val[1] }
  | expression LSHIFT       expression                 { result = val[0] <<  val[2]       ; loc result, val[1] }
  | expression RSHIFT       expression                 { result = val[0] >>  val[2]       ; loc result, val[1] }
  |            MINUS        expression         =UMINUS { result = val[1].minus            ; loc result, val[0] }
  |            TIMES        expression         =SPLAT  { result = val[1].unfold           ; loc result, val[0] }
  | expression NOTEQUAL     expression                 { result = val[0].ne  val[2]       ; loc result, val[1] }
  | expression ISEQUAL      expression                 { result = val[0].eq  val[2]       ; loc result, val[1] }
  | expression GREATERTHAN  expression                 { result = val[0] >   val[2]       ; loc result, val[1] }
  | expression GREATEREQUAL expression                 { result = val[0] >=  val[2]       ; loc result, val[1] }
  | expression LESSTHAN     expression                 { result = val[0] <   val[2]       ; loc result, val[1] }
  | expression LESSEQUAL    expression                 { result = val[0] <=  val[2]       ; loc result, val[1] }
  |            NOT          expression                 { result = val[1].not              ; loc result, val[0] }
  | expression AND          expression                 { result = val[0].and val[2]       ; loc result, val[1] }
  | expression OR           expression                 { result = val[0].or  val[2]       ; loc result, val[1] }
  | expression QMARK selector_entries                  { result = val[0].select(*val[2])  ; loc result, val[0] }
  |            LPAREN       assignment RPAREN          { result = val[1].paren            ; loc result, val[0] }
  |            WSLPAREN     assignment RPAREN          { result = val[1].paren          ; loc result, val[0] }

bracketed_expression
  : expression LBRACK access_args endcomma RBRACK =LBRACK { result = val[0].access(val[2]); loc result, val[0], val[4] }

access_args
  : access_arg                   { result = [val[0]] }
  | access_args COMMA access_arg { result = Factory.ARGUMENTS(val[0], val[2]) }

access_arg
  : expression
  | hashpair { result = Factory.HASH_UNFOLDED([val[0]]); loc result, val[0] }

#---EXPRESSIONS
#   (i.e. "argument list")
#
# This expression list can not contain function calls without parentheses around arguments
# Produces Array<Model::Expression>
#
expressions
  : expression                   { result = [val[0]] }
  | expressions COMMA expression { result = val[0].push(val[2]) }

primary_expression
  : variable
  | call_method_with_lambda_expression
  | collection_expression
  | case_expression
  | if_expression
  | unless_expression
  | definition_expression
  | hostclass_expression
  | plan_expression
  | apply_expression
  | node_definition_expression
  | epp_render_expression
  | function_definition
  | type_alias
  | type_definition
  | reserved_word
  | array
  | hash
  | regex
  | quotedtext
  | type
  | NUMBER       { result = Factory.NUMBER(val[0][:value])          ; loc result, val[0] }
  | BOOLEAN      { result = Factory.literal(val[0][:value])         ; loc result, val[0] }
  | DEFAULT      { result = Factory.literal(:default)               ; loc result, val[0] }
  | UNDEF        { result = Factory.literal(:undef)                 ; loc result, val[0] }
  | NAME         { result = Factory.QNAME_OR_NUMBER(val[0][:value]) ; loc result, val[0] }


#---CALL FUNCTION
#
# Produces Model::CallNamedFunction

call_function_expression
  : call_function_start arguments endcomma RPAREN {
      result = Factory.CALL_NAMED(val[0], true, val[1])
      loc result, val[0], val[3]
    }
  | call_function_start RPAREN {
      result = Factory.CALL_NAMED(val[0], true, [])
      loc result, val[0], val[1]
    }
  | call_function_start arguments endcomma RPAREN lambda {
      result = Factory.CALL_NAMED(val[0], true, val[1])
      loc result, val[0], val[4]
      result.lambda = val[4]
    }
  | call_function_start RPAREN  lambda {
      result = Factory.CALL_NAMED(val[0], true, [])
      loc result, val[0], val[2]
      result.lambda = val[2]
    }

call_function_start
  : expression LPAREN { result = val[0] }
  | TYPE LPAREN { result = Factory.QNAME(val[0][:value]); loc result, val[0] }

#---CALL METHOD
#
call_method_with_lambda_expression
  : call_method_expression         =LOW { result = val[0] }
  | call_method_expression lambda       { result = val[0]; val[0].lambda = val[1] }

  call_method_expression
    : named_access LPAREN arguments RPAREN { result = Factory.CALL_METHOD(val[0], val[2]); loc result, val[1], val[3] }
    | named_access LPAREN RPAREN             { result = Factory.CALL_METHOD(val[0], []); loc result, val[1], val[3] }
    | named_access =LOW                      { result = Factory.CALL_METHOD(val[0], []); loc result, val[0] }

  named_access
    : expression DOT NAME {
        result = val[0].dot(Factory.fqn(val[2][:value]))
        loc result, val[1], val[2]
      }
    | expression DOT TYPE {
        result = val[0].dot(Factory.fqn(val[2][:value]))
        loc result, val[1], val[2]
      }

#---LAMBDA
#
lambda
  : lambda_parameter_list opt_return_type lambda_rest {
      result = Factory.LAMBDA(val[0][:value], val[2][:value], val[1])
      loc result, val[0][:start], val[2][:end]
    }

lambda_rest
  : LBRACE statements RBRACE  { result = {:end => val[2], :value =>val[1] } }
  | LBRACE RBRACE             { result = {:end => val[1], :value => nil } }


lambda_parameter_list
  : PIPE PIPE                     { result = {:start => val[0], :value => [] } }
  | PIPE parameters endcomma PIPE { result = {:start => val[0], :value => val[1] } }

#---CONDITIONALS

#--IF
#
if_expression
  : IF if_part {
      result = val[1]
      loc(result, val[0], val[1])
    }

  # Produces Model::IfExpression
  if_part
    : expression LBRACE statements RBRACE else {
        result = Factory.IF(val[0], Factory.block_or_expression(val[2], val[1], val[3]), val[4])
        loc(result, val[0], (val[4] ? val[4] : val[3]))
      }
    | expression LBRACE RBRACE else {
        result = Factory.IF(val[0], nil, val[3])
        loc(result, val[0], (val[3] ? val[3] : val[2]))
      }

  # Produces [Model::Expression, nil] - nil if there is no else or elsif part
  else
    : # nothing
    | ELSIF if_part {
        result = val[1]
        loc(result, val[0], val[1])
      }
    | ELSE LBRACE statements RBRACE {
        result = Factory.block_or_expression(val[2], val[1], val[3])
      }
    | ELSE LBRACE RBRACE {
        result = nil # don't think a nop is needed here either
      }

#--UNLESS
#
unless_expression
  : UNLESS expression LBRACE statements RBRACE unless_else {
      result = Factory.UNLESS(val[1], Factory.block_or_expression(val[3], val[2], val[4]), val[5])
      loc result, val[0], val[4]
    }
  | UNLESS expression LBRACE RBRACE unless_else {
      result = Factory.UNLESS(val[1], nil, val[4])
      loc result, val[0], val[4]
    }

  # Different from else part of if, since "elsif" is not supported, but 'else' is
  #
  # Produces [Model::Expression, nil] - nil if there is no else or elsif part
  unless_else
    : # nothing
    | ELSE LBRACE statements RBRACE {
        result = Factory.block_or_expression(val[2], val[1], val[3])
      }
    | ELSE LBRACE RBRACE {
        result = nil # don't think a nop is needed here either
      }

#--- CASE EXPRESSION
#
case_expression
  : CASE expression LBRACE case_options RBRACE {
      result = Factory.CASE(val[1], *val[3])
      loc result, val[0], val[4]
    }

  # Produces Array<Model::CaseOption>
  case_options
    : case_option               { result = [val[0]] }
    | case_options case_option  { result = val[0].push val[1] }

  # Produced Model::CaseOption (aka When)
  case_option
    : expressions COLON LBRACE options_statements RBRACE {
        result = Factory.WHEN(val[0], val[3]); loc result, val[1], val[4]
      }

  options_statements
    : nil
    | statements

  # This special construct is required or racc will produce the wrong result when the selector entry
  # LHS is generalized to any expression (LBRACE looks like a hash). Thus it is not possible to write
  # a selector with a single entry where the entry LHS is a hash.
  # The SELBRACE token is a LBRACE that follows a QMARK, and this is produced by the lexer with a lookback
  # Produces Array<Model::SelectorEntry>
  #
  selector_entries
    : selector_entry
    | SELBRACE selector_entry_list endcomma RBRACE {
        result = val[1]
      }

  # Produces Array<Model::SelectorEntry>
  selector_entry_list
    : selector_entry                           { result = [val[0]] }
    | selector_entry_list COMMA selector_entry { result = val[0].push val[2] }

  # Produces a Model::SelectorEntry
  # This FARROW wins over FARROW in Hash
  selector_entry
    : expression FARROW expression { result = Factory.MAP(val[0], val[2]) ; loc result, val[1] }

#---COLLECTION
#
# A Collection is a predicate applied to a set of objects with an implied context (used variables are
# attributes of the object.
# i.e. this is equivalent to source.select(QUERY).apply(ATTRIBUTE_OPERATIONS)
#
collection_expression
  : expression collect_query LBRACE attribute_operations endcomma RBRACE {
      result = Factory.COLLECT(val[0], val[1], val[3])
      loc result, val[0], val[5]
    }
  | expression collect_query =LOW {
      result = Factory.COLLECT(val[0], val[1], [])
      loc result, val[0], val[1]
    }

  collect_query
    : LCOLLECT  optional_query RCOLLECT  { result = Factory.VIRTUAL_QUERY(val[1])   ; loc result, val[0], val[2] }
    | LLCOLLECT optional_query RRCOLLECT { result = Factory.EXPORTED_QUERY(val[1])  ; loc result, val[0], val[2] }

  optional_query
    : nil
    | expression

#---ATTRIBUTE OPERATIONS (Not an expression)
#
attribute_operations
  :                                                { result = [] }
  | attribute_operation                            { result = [val[0]] }
  | attribute_operations COMMA attribute_operation { result = val[0].push(val[2]) }

  # Produces String
  # QUESTION: Why is BOOLEAN valid as an attribute name?
  #
  attribute_name
    : NAME
    | keyword

  # In this version, illegal combinations are validated instead of producing syntax errors
  # (Can give nicer error message "+> is not applicable to...")
  # Produces Model::AttributeOperation
  #
  attribute_operation
    : attribute_name FARROW expression {
        result = Factory.ATTRIBUTE_OP(val[0][:value], '=>', val[2])
        loc result, val[0], val[2]
      }
    | attribute_name PARROW expression {
        result = Factory.ATTRIBUTE_OP(val[0][:value], '+>', val[2])
        loc result, val[0], val[2]
      }
  | TIMES FARROW expression {
      result = Factory.ATTRIBUTES_OP(val[2]) ; loc result, val[0], val[2]
   }

#---DEFINE
#
# Produces Model::Definition
#
definition_expression
  : DEFINE classname parameter_list LBRACE opt_statements RBRACE {
      definition = Factory.DEFINITION(classname(val[1][:value]), val[2], val[4])
      loc(definition, val[0], val[5])
      result = add_definition(definition)
      # New lexer does not keep track of this, this is done in validation
      if @lexer.respond_to?(:'indefine=')
        @lexer.indefine = false
      end
    }

#--PLAN
plan_expression
  : PLAN stacked_classname parameter_list LBRACE opt_statements RBRACE {
      # Remove this plan's name from the namestack as all nested plans have been parsed
      namepop
      definition = Factory.PLAN(classname(val[1][:value]), val[2], val[4])
      loc(definition, val[0], val[5])
      result = add_definition(definition)
    }

apply_expression
  : APPLY LPAREN arguments endcomma RPAREN LBRACE statements RBRACE {
      result = Factory.APPLY(val[2], Factory.APPLY_BLOCK(val[6]))
      loc result, val[0], val[7]
    }
  | APPLY LPAREN arguments endcomma RPAREN LBRACE RBRACE {
      result = Factory.APPLY(val[2], Factory.APPLY_BLOCK([]))
      loc result, val[0], val[6]
    }

#---HOSTCLASS
#
# Produces Model::HostClassDefinition
#
hostclass_expression
  : CLASS stacked_classname parameter_list classparent LBRACE opt_statements RBRACE {
      # Remove this class' name from the namestack as all nested classes have been parsed
      namepop
      definition = Factory.HOSTCLASS(classname(val[1][:value]), val[2], token_text(val[3]), val[5])
      loc(definition, val[0], val[6])
      result = add_definition(definition)
    }

  # Record the classname so nested classes gets a fully qualified name at parse-time
  # This is a separate rule since racc does not support intermediate actions.
  #
  stacked_classname
    : classname { namestack(val[0][:value]) ; result = val[0] }

  opt_statements
    : statements
    | nil

  # Produces String, name or nil result
  classparent
    : nil
    | INHERITS classnameordefault { result = val[1] }

  # Produces String (this construct allows a class to be named "default" and to be referenced as
  # the parent class.
  # TODO: Investigate the validity
  # Produces a String (classname), or a token (DEFAULT).
  #
  classnameordefault
    : classname
    | DEFAULT

#---NODE
#
# Produces Model::NodeDefinition
#
node_definition_expression
  : NODE hostnames endcomma nodeparent LBRACE statements RBRACE {
      definition = Factory.NODE(val[1], val[3], val[5])
      loc(definition, val[0], val[6])
      result = add_definition(definition)
    }
  | NODE hostnames endcomma nodeparent LBRACE RBRACE {
      definition = Factory.NODE(val[1], val[3], nil)
      loc(definition, val[0], val[5])
      result = add_definition(definition)
    }

  # Hostnames is not a list of names, it is a list of name matchers (including a Regexp).
  # (The old implementation had a special "Hostname" object with some minimal validation)
  #
  # Produces Array<Model::LiteralExpression>
  #
  hostnames
    : hostname                 { result = [result] }
    | hostnames COMMA hostname { result = val[0].push(val[2]) }

  # Produces a LiteralExpression (string, :default, or regexp)
  # String with interpolation is validated for better error message
  hostname
    : dotted_name
    | quotedtext
    | DEFAULT     { result = Factory.literal(:default); loc result, val[0] }
    | regex

  dotted_name
    : name_or_number   { result = Factory.literal(val[0][:value]); loc result, val[0] }
    | dotted_name DOT name_or_number { result = Factory.concat(val[0], '.', val[2][:value]); loc result, val[0], val[2] }

  name_or_number
    : NAME
    | NUMBER

  # Produces Expression, since hostname is an Expression
  nodeparent
    : nil
    | INHERITS hostname { result = val[1] }

#---FUNCTION DEFINITION
#
function_definition
  : FUNCTION classname parameter_list opt_return_type LBRACE opt_statements RBRACE {
      definition = Factory.FUNCTION(val[1][:value], val[2], val[5], val[3])
      loc(definition, val[0], val[6])
      result = add_definition(definition)
    }

opt_return_type
  : RSHIFT type { result = val[1] }
  | RSHIFT type LBRACK access_args RBRACK { result = val[1].access(val[3]) ; loc result, val[1], val[4] }
  | nil

#---NAMES AND PARAMETERS COMMON TO SEVERAL RULES
#   Produces String
#   TODO: The error that "class" is not a valid classname is bad - classname rule is also used for other things
classname
  : NAME
  | WORD
  | CLASSREF
  | CLASS { error val[0], "'class' keyword not allowed at this location" }
  | STRING { error val[0], "A quoted string is not valid as a name here" }

# Produces Array<Model::Parameter>
parameter_list
  : nil                                   { result = [] }
  | LPAREN  RPAREN                        { result = [] }
  | WSLPAREN  RPAREN                      { result = [] }
  | LPAREN parameters endcomma RPAREN     { result = val[1] }
  | WSLPAREN parameters endcomma RPAREN   { result = val[1] }

# Produces Array<Model::Parameter>
parameters
  : parameter                  { result = [val[0]] }
  | parameters COMMA parameter { result = val[0].push(val[2]) }

# Produces Model::Parameter
parameter
  : untyped_parameter
  | typed_parameter

untyped_parameter
  : regular_parameter
  | splat_parameter

regular_parameter
  : VARIABLE EQUALS expression { result = Factory.PARAM(val[0][:value], val[2]) ; loc result, val[0] }
  | VARIABLE                   { result = Factory.PARAM(val[0][:value]); loc result, val[0] }

splat_parameter
  : TIMES regular_parameter { result = val[1]; val[1].captures_rest }

typed_parameter
  : parameter_type untyped_parameter { val[1].type_expr(val[0]) ; result = val[1] }

parameter_type
  : type { result = val[0] }
  | type LBRACK access_args RBRACK { result = val[0].access(val[2]) ; loc result, val[0], val[3] }

#--TYPE ALIAS
type_alias
  : type_alias_lhs EQUALS type hash {
      definition = Factory.TYPE_ASSIGNMENT(val[0], Factory.KEY_ENTRY(val[2], val[3]))
      loc(definition, val[0], val[3])
      result = add_definition(definition)
    }
  | type_alias_lhs EQUALS type LBRACK access_args endcomma RBRACK {
      definition = Factory.TYPE_ASSIGNMENT(val[0], val[2].access(val[4]))
      loc(definition, val[0], val[5])
      result = add_definition(definition)
    }
  | type_alias_lhs EQUALS type {
      definition = Factory.TYPE_ASSIGNMENT(val[0], val[2])
      loc(definition, val[0], val[2])
      result = add_definition(definition)
    }
  | type_alias_lhs EQUALS hash {
      definition = Factory.TYPE_ASSIGNMENT(val[0], val[2])
      loc(definition, val[0], val[2])
      result = add_definition(definition)
    }
  | type_alias_lhs EQUALS array {
      definition = Factory.TYPE_ASSIGNMENT(val[0], val[2])
      loc(definition, val[0], val[4])
      result = add_definition(definition)
    }

type_alias_lhs
  : TYPE parameter_type { result = val[1] }

#--TYPE definition
# TODO: Uses the optional_statements rule temporarily since the actual body awaits final spec on methods and attributes.
type_definition
  : TYPE CLASSREF LBRACE optional_statements RBRACE {
        definition = Factory.TYPE_DEFINITION(val[1][:value], nil, val[3])
        loc(definition, val[0], val[4])
        result = add_definition(definition)
      }
  | TYPE CLASSREF INHERITS CLASSREF LBRACE optional_statements RBRACE {
        definition = Factory.TYPE_DEFINITION(val[1][:value], val[3][:value], val[5])
        loc(definition, val[0], val[6])
        result = add_definition(definition)
      }

#--VARIABLE
#
variable
  : VARIABLE {
     fqn = Factory.fqn(val[0][:value])
     loc(fqn, val[0])
     fqn['offset'] += 1
     fqn['length'] -= 1
     result = fqn.var
     loc(result, val[0])
  }

#---RESERVED WORDS
#
reserved_word
  : PRIVATE   { result = Factory.RESERVED(val[0][:value]) ; loc result, val[0] }
  | ATTR      { result = Factory.RESERVED(val[0][:value]) ; loc result, val[0] }

#---LITERALS (dynamic and static)
#

array
  : LISTSTART collection_entries endcomma  RBRACK { result = Factory.LIST(val[1]); loc result, val[0], val[3] }
  | LISTSTART                              RBRACK { result = Factory.literal([]) ; loc result, val[0], val[1] }
  | LBRACK    collection_entries endcomma  RBRACK { result = Factory.LIST(val[1]); loc result, val[0], val[3] }
  | LBRACK                                 RBRACK { result = Factory.literal([]) ; loc result, val[0], val[1] }

hash
  : LBRACE hashpairs RBRACE       { result = Factory.HASH(val[1]); loc result, val[0], val[2] }
  | LBRACE hashpairs COMMA RBRACE { result = Factory.HASH(val[1]); loc result, val[0], val[3] }
  | LBRACE RBRACE                 { result = Factory.literal({}) ; loc result, val[0], val[1] }

  hashpairs
    : hashpair                 { result = [val[0]] }
    | hashpairs COMMA hashpair { result = val[0].push val[2] }

  hashpair
    : hash_entry FARROW hash_entry { result = Factory.KEY_ENTRY(val[0], val[2]); loc result, val[1] }

collection_entry:
  : argument
  | collection_entry_keyword { result = Factory.literal(val[0][:value]) ; loc result, val[0] }

# Like collection_entry but will not allow nested hashpair
hash_entry:
  : assignment
  | collection_entry_keyword { result = Factory.literal(val[0][:value]) ; loc result, val[0] }

collection_entries
  : collection_entry                           { result = [val[0]] }
  | collection_entries COMMA collection_entry  { result = Factory.ARGUMENTS(val[0], val[2]) }

# Keywords valid as a collection value
collection_entry_keyword
  : TYPE
  | FUNCTION

quotedtext
  : string
  | dq_string
  | heredoc

string
  : STRING  { result = Factory.literal(val[0][:value]) ; loc result, val[0] }
  | WORD    { result = Factory.literal(val[0][:value]) ; loc result, val[0] }

dq_string       : dqpre dqrval           { result = Factory.STRING(val[0], *val[1]) ; loc result, val[0], val[1][-1] }
dqpre           : DQPRE                  { result = Factory.literal(val[0][:value]); loc result, val[0] }
dqpost          : DQPOST                 { result = Factory.literal(val[0][:value]); loc result, val[0] }
dqmid           : DQMID                  { result = Factory.literal(val[0][:value]); loc result, val[0] }
dqrval          : text_expression dqtail { result = [val[0]] + val[1] }
text_expression : assignment             { result = Factory.TEXT(val[0]) }

dqtail
  : dqpost        { result = [val[0]] }
  | dqmid dqrval  { result = [val[0]] + val[1] }

heredoc
  : HEREDOC sublocated_text  { result = Factory.HEREDOC(val[0][:value], val[1]); loc result, val[0] }

sublocated_text
  : SUBLOCATE string    { result = Factory.SUBLOCATE(val[0], val[1]); }
  | SUBLOCATE dq_string { result = Factory.SUBLOCATE(val[0], val[1]); }

epp_expression
  : EPP_START epp_parameters_list optional_statements { result = Factory.EPP(val[1], val[2]); loc result, val[0] }

optional_statements
  :
  | statements

epp_parameters_list
  :                                  =LOW{ result = nil }
  | PIPE PIPE                        { result = [] }
  | PIPE parameters endcomma PIPE    { result = val[1] }

epp_render_expression
  : RENDER_STRING                  { result = Factory.RENDER_STRING(val[0][:value]); loc result, val[0] }
  | RENDER_EXPR expression epp_end { result = Factory.RENDER_EXPR(val[1]); loc result, val[0], val[2] }
  | RENDER_EXPR LBRACE statements RBRACE epp_end { result = Factory.RENDER_EXPR(Factory.block_or_expression(val[2], val[1], val[3])); loc result, val[0], val[4] }

epp_end
  : EPP_END
  | EPP_END_TRIM

type     : CLASSREF { result = Factory.QREF(val[0][:value])  ; loc result, val[0] }

regex
  : REGEX { result = Factory.literal(val[0][:value]); loc result, val[0] }

#---MARKERS, SPECIAL TOKENS, SYNTACTIC SUGAR, etc.

endcomma
  : #
  | COMMA { result = nil }

endsemi
  : #
  | SEMIC

keyword
  : AND
  | CASE
  | CLASS
  | DEFAULT
  | DEFINE
  | ELSE
  | ELSIF
  | IF
  | IN
  | INHERITS
  | NODE
  | OR
  | UNDEF
  | UNLESS
  | TYPE
  | ATTR
  | FUNCTION
  | PRIVATE
  | PLAN
  | APPLY

nil
  : { result = nil}

end

---- header ----
require_relative '../../../puppet'
require_relative '../../../puppet/pops'

module Puppet
  class ParseError < Puppet::Error; end
  class ImportError < Racc::ParseError; end
  class AlreadyImportedError < ImportError; end
end

---- inner ----

# Make emacs happy
# Local Variables:
# mode: ruby
# End: