r - ast_expression.go
1 package r
2
3 import (
4 "vimagination.zapto.org/parser"
5 )
6
7 // Expression represents either a FlowControl, FunctionDefinition, or
8 // QueryExpression.
9 type Expression struct {
10 FlowControl *FlowControl
11 FunctionDefinition *FunctionDefinition
12 QueryExpression *QueryExpression
13 Comments [2]Comments
14 Tokens Tokens
15 }
16
17 func (e *Expression) parse(r *rParser) error {
18 var err error
19
20 e.Comments[0] = r.AcceptRunWhitespaceComments()
21
22 r.AcceptRunWhitespace()
23
24 s := r.NewGoal()
25
26 switch tk := r.Peek(); tk {
27 case parser.Token{Type: TokenKeyword, Data: "if"}, parser.Token{Type: TokenKeyword, Data: "while"}, parser.Token{Type: TokenKeyword, Data: "repeat"}, parser.Token{Type: TokenKeyword, Data: "for"}:
28 e.FlowControl = new(FlowControl)
29 err = e.FlowControl.parse(&s)
30 case parser.Token{Type: TokenKeyword, Data: "function"}:
31 e.FunctionDefinition = new(FunctionDefinition)
32 err = e.FunctionDefinition.parse(&s)
33 default:
34 e.QueryExpression = new(QueryExpression)
35
36 err = e.QueryExpression.parse(&s)
37 }
38
39 if err != nil {
40 return r.Error("Expression", err)
41 }
42
43 r.Score(s)
44
45 e.Comments[1] = r.AcceptRunWhitespaceCommentsNoNewline()
46 e.Tokens = r.ToTokens()
47
48 return nil
49 }
50
51 // CompoundExpression represents a series of expressions, wrapped in braces,
52 // and seperated by semi-colons, commas, and newlines.
53 type CompoundExpression struct {
54 Expressions []Expression
55 Comments Comments
56 Tokens Tokens
57 }
58
59 func (c *CompoundExpression) parse(r *rParser) error {
60 r.AcceptToken(parser.Token{Type: TokenGrouping, Data: "{"})
61
62 s := r.NewGoal()
63
64 s.AcceptRunWhitespace()
65
66 for !s.AcceptToken(parser.Token{Type: TokenGrouping, Data: "}"}) {
67 r.AcceptRunWhitespaceNoComment()
68
69 s = r.NewGoal()
70
71 var e Expression
72
73 if err := e.parse(&s); err != nil {
74 return s.Error("CompoundExpression", err)
75 }
76
77 c.Expressions = append(c.Expressions, e)
78
79 r.Score(s)
80
81 s = r.NewGoal()
82
83 s.AcceptRunWhitespace()
84
85 if s.AcceptToken(parser.Token{Type: TokenGrouping, Data: "}"}) {
86 break
87 }
88
89 r.AcceptRunWhitespaceNoNewLine()
90
91 if !r.AcceptToken(parser.Token{Type: TokenExpressionTerminator, Data: ";"}) && !r.Accept(TokenLineTerminator) {
92 return r.Error("CompoundExpression", ErrMissingTerminator)
93 }
94
95 s = r.NewGoal()
96
97 s.AcceptRunWhitespace()
98 }
99
100 r.AcceptRunWhitespaceNoComment()
101
102 c.Comments = r.AcceptRunWhitespaceComments()
103
104 r.AcceptRunWhitespace()
105
106 r.AcceptToken(parser.Token{Type: TokenGrouping, Data: "}"})
107
108 c.Tokens = r.ToTokens()
109
110 return nil
111 }
112
113 // FlowControl represents an If-, While-, Repeat-, or For-Control.
114 type FlowControl struct {
115 IfControl *IfControl
116 WhileControl *WhileControl
117 RepeatControl *RepeatControl
118 ForControl *ForControl
119 Tokens Tokens
120 }
121
122 func (f *FlowControl) parse(r *rParser) error {
123 var err error
124
125 s := r.NewGoal()
126 switch r.Peek() {
127 case parser.Token{Type: TokenKeyword, Data: "if"}:
128 f.IfControl = new(IfControl)
129
130 err = f.IfControl.parse(&s)
131 case parser.Token{Type: TokenKeyword, Data: "while"}:
132 f.WhileControl = new(WhileControl)
133
134 err = f.WhileControl.parse(&s)
135 case parser.Token{Type: TokenKeyword, Data: "repeat"}:
136 f.RepeatControl = new(RepeatControl)
137
138 err = f.RepeatControl.parse(&s)
139 case parser.Token{Type: TokenKeyword, Data: "for"}:
140 f.ForControl = new(ForControl)
141
142 err = f.ForControl.parse(&s)
143 }
144
145 if err != nil {
146 return r.Error("FlowControl", err)
147 }
148
149 r.Score(s)
150
151 f.Tokens = r.ToTokens()
152
153 return nil
154 }
155
156 // IfControl represents a conditional branch and optional else.
157 type IfControl struct {
158 Cond FormulaeExpression
159 Expr Expression
160 Else *Expression
161 Comments [4]Comments
162 Tokens Tokens
163 }
164
165 func (i *IfControl) parse(r *rParser) error {
166 r.AcceptToken(parser.Token{Type: TokenKeyword, Data: "if"})
167
168 i.Comments[0] = r.AcceptRunWhitespaceComments()
169
170 r.AcceptRunWhitespace()
171
172 if !r.AcceptToken(parser.Token{Type: TokenGrouping, Data: "("}) {
173 return r.Error("IfControl", ErrMissingOpeningParen)
174 }
175
176 i.Comments[1] = r.AcceptRunWhitespaceComments()
177
178 r.AcceptRunWhitespace()
179
180 s := r.NewGoal()
181
182 if err := i.Cond.parse(&s); err != nil {
183 return r.Error("IfControl", err)
184 }
185
186 r.Score(s)
187
188 i.Comments[2] = r.AcceptRunWhitespaceComments()
189
190 r.AcceptRunWhitespace()
191
192 if !r.AcceptToken(parser.Token{Type: TokenGrouping, Data: ")"}) {
193 return r.Error("IfControl", ErrMissingClosingParen)
194 }
195
196 r.AcceptRunWhitespaceNoComment()
197
198 s = r.NewGoal()
199
200 if err := i.Expr.parse(&s); err != nil {
201 return r.Error("IfControl", err)
202 }
203
204 r.Score(s)
205
206 s = r.NewGoal()
207
208 s.AcceptRunWhitespace()
209
210 if s.AcceptToken(parser.Token{Type: TokenKeyword, Data: "else"}) {
211 i.Comments[3] = r.AcceptRunWhitespaceComments()
212
213 r.AcceptRunWhitespace()
214 r.AcceptToken(parser.Token{Type: TokenKeyword, Data: "else"})
215 r.AcceptRunWhitespaceNoComment()
216
217 s := r.NewGoal()
218 i.Else = new(Expression)
219
220 if err := i.Else.parse(&s); err != nil {
221 return r.Error("IfControl", err)
222 }
223
224 r.Score(s)
225 }
226
227 i.Tokens = r.ToTokens()
228
229 return nil
230 }
231
232 // WhileControl represents a looping branch with a single condition.
233 type WhileControl struct {
234 Cond FormulaeExpression
235 Expr Expression
236 Comments [3]Comments
237 Tokens Tokens
238 }
239
240 func (w *WhileControl) parse(r *rParser) error {
241 r.AcceptToken(parser.Token{Type: TokenKeyword, Data: "while"})
242
243 w.Comments[0] = r.AcceptRunWhitespaceComments()
244
245 r.AcceptRunWhitespace()
246
247 if !r.AcceptToken(parser.Token{Type: TokenGrouping, Data: "("}) {
248 return r.Error("WhileControl", ErrMissingOpeningParen)
249 }
250
251 w.Comments[1] = r.AcceptRunWhitespaceComments()
252
253 r.AcceptRunWhitespace()
254
255 s := r.NewGoal()
256
257 if err := w.Cond.parse(&s); err != nil {
258 return r.Error("WhileControl", err)
259 }
260
261 r.Score(s)
262
263 w.Comments[2] = r.AcceptRunWhitespaceComments()
264
265 r.AcceptRunWhitespace()
266
267 if !r.AcceptToken(parser.Token{Type: TokenGrouping, Data: ")"}) {
268 return r.Error("WhileControl", ErrMissingClosingParen)
269 }
270
271 r.AcceptRunWhitespaceNoComment()
272
273 s = r.NewGoal()
274
275 if err := w.Expr.parse(&s); err != nil {
276 return r.Error("WhileControl", err)
277 }
278
279 r.Score(s)
280
281 w.Tokens = r.ToTokens()
282
283 return nil
284 }
285
286 // RepeatControl represents a looping branch.
287 type RepeatControl struct {
288 Expr Expression
289 Tokens Tokens
290 }
291
292 func (rc *RepeatControl) parse(r *rParser) error {
293 r.AcceptToken(parser.Token{Type: TokenKeyword, Data: "repeat"})
294 r.AcceptRunWhitespaceNoComment()
295
296 s := r.NewGoal()
297
298 if err := rc.Expr.parse(&s); err != nil {
299 return r.Error("RepeatControl", err)
300 }
301
302 r.Score(s)
303
304 rc.Tokens = r.ToTokens()
305
306 return nil
307 }
308
309 // ForControl represents a looping branch over an expression.
310 type ForControl struct {
311 Var *Token
312 List FormulaeExpression
313 Expr Expression
314 Comments [5]Comments
315 Tokens Tokens
316 }
317
318 func (f *ForControl) parse(r *rParser) error {
319 r.AcceptToken(parser.Token{Type: TokenKeyword, Data: "for"})
320
321 f.Comments[0] = r.AcceptRunWhitespaceComments()
322
323 r.AcceptRunWhitespace()
324
325 if !r.AcceptToken(parser.Token{Type: TokenGrouping, Data: "("}) {
326 return r.Error("ForControl", ErrMissingOpeningParen)
327 }
328
329 f.Comments[1] = r.AcceptRunWhitespaceComments()
330
331 r.AcceptRunWhitespace()
332
333 if !r.Accept(TokenIdentifier) {
334 return r.Error("ForControl", ErrMissingIdentifier)
335 }
336
337 f.Var = r.GetLastToken()
338 f.Comments[2] = r.AcceptRunWhitespaceComments()
339
340 r.AcceptRunWhitespace()
341
342 if !r.AcceptToken(parser.Token{Type: TokenKeyword, Data: "in"}) {
343 return r.Error("ForControl", ErrMissingIn)
344 }
345
346 f.Comments[3] = r.AcceptRunWhitespaceComments()
347
348 r.AcceptRunWhitespace()
349
350 s := r.NewGoal()
351
352 if err := f.List.parse(&s); err != nil {
353 return r.Error("ForControl", err)
354 }
355
356 r.Score(s)
357
358 f.Comments[4] = r.AcceptRunWhitespaceComments()
359
360 r.AcceptRunWhitespace()
361
362 if !r.AcceptToken(parser.Token{Type: TokenGrouping, Data: ")"}) {
363 return r.Error("ForControl", ErrMissingClosingParen)
364 }
365
366 r.AcceptRunWhitespaceNoComment()
367
368 s = r.NewGoal()
369
370 if err := f.Expr.parse(&s); err != nil {
371 return r.Error("ForControl", err)
372 }
373
374 r.Score(s)
375
376 f.Tokens = r.ToTokens()
377
378 return nil
379 }
380
381 // FunctionDefinition represents a defined function.
382 type FunctionDefinition struct {
383 ArgList ArgList
384 Body Expression
385 Comments Comments
386 Tokens Tokens
387 }
388
389 func (f *FunctionDefinition) parse(r *rParser) error {
390 r.AcceptToken(parser.Token{Type: TokenKeyword, Data: "function"})
391
392 f.Comments = r.AcceptRunWhitespaceComments()
393
394 r.AcceptRunWhitespace()
395
396 if !r.AcceptToken(parser.Token{Type: TokenGrouping, Data: "("}) {
397 return r.Error("FunctionDefinition", ErrMissingOpeningParen)
398 }
399
400 r.AcceptRunWhitespaceNoComment()
401
402 s := r.NewGoal()
403
404 if err := f.ArgList.parse(&s); err != nil {
405 return r.Error("FunctionDefinition", err)
406 }
407
408 r.Score(s)
409 r.AcceptRunWhitespace()
410 r.AcceptToken(parser.Token{Type: TokenGrouping, Data: ")"})
411 r.AcceptRunWhitespace()
412
413 s = r.NewGoal()
414
415 if err := f.Body.parse(&s); err != nil {
416 return r.Error("FunctionDefinition", err)
417 }
418
419 r.Score(s)
420
421 f.Tokens = r.ToTokens()
422
423 return nil
424 }
425
426 // ArgList represents a series af arguments accepted by a FunctionDefinition.
427 type ArgList struct {
428 Args []Argument
429 Comments Comments
430 Tokens Tokens
431 }
432
433 func (a *ArgList) parse(r *rParser) error {
434 s := r.NewGoal()
435 s.AcceptRunWhitespace()
436
437 if s.AcceptToken(parser.Token{Type: TokenGrouping, Data: ")"}) || s.Peek().Type == parser.TokenDone {
438 a.Comments = r.AcceptRunWhitespaceComments()
439 } else {
440 for !s.AcceptToken(parser.Token{Type: TokenGrouping, Data: ")"}) {
441 r.AcceptRunWhitespaceNoComment()
442
443 s = r.NewGoal()
444
445 var arg Argument
446
447 if err := arg.parse(&s); err != nil {
448 return r.Error("ArgList", err)
449 }
450
451 r.Score(s)
452
453 a.Args = append(a.Args, arg)
454 s = r.NewGoal()
455
456 s.AcceptRunWhitespace()
457
458 if tk := s.Peek(); tk == (parser.Token{Type: TokenGrouping, Data: ")"}) || tk.Type == parser.TokenDone {
459 break
460 } else if !s.AcceptToken(parser.Token{Type: TokenExpressionTerminator, Data: ","}) {
461 return s.Error("ArgList", ErrMissingTerminator)
462 }
463
464 r.Score(s)
465
466 s = r.NewGoal()
467 s.AcceptRunWhitespace()
468 }
469 }
470
471 a.Tokens = r.ToTokens()
472
473 return nil
474 }
475
476 type Argument struct {
477 Identifier *Token
478 Default *Expression
479 Comments [2]Comments
480 Tokens Tokens
481 }
482
483 // Argument represents a single param accepted by a FunctionDefinition and a
484 // possible default value.
485 func (a *Argument) parse(r *rParser) error {
486 a.Comments[0] = r.AcceptRunWhitespaceComments()
487
488 r.AcceptRunWhitespace()
489
490 if !r.Accept(TokenIdentifier) && !r.AcceptToken(parser.Token{Type: TokenEllipsis, Data: "..."}) {
491 return r.Error("Argument", ErrMissingIdentifier)
492 }
493
494 a.Identifier = r.GetLastToken()
495
496 if a.Identifier.Type == TokenIdentifier {
497 s := r.NewGoal()
498
499 s.AcceptRunWhitespace()
500
501 if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "="}) {
502 a.Comments[1] = r.AcceptRunWhitespaceComments()
503
504 r.AcceptRunWhitespace()
505 r.AcceptToken(parser.Token{Type: TokenOperator, Data: "="})
506 r.AcceptRunWhitespaceNoComment()
507
508 s = r.NewGoal()
509 a.Default = new(Expression)
510
511 if err := a.Default.parse(&s); err != nil {
512 return r.Error("Argument", err)
513 }
514
515 r.Score(s)
516 } else {
517 a.Comments[1] = r.AcceptRunWhitespaceComments()
518 }
519 } else {
520 a.Comments[1] = r.AcceptRunWhitespaceComments()
521 }
522
523 a.Tokens = r.ToTokens()
524
525 return nil
526 }
527
528 // QueryExpression represents a help command.
529 type QueryExpression struct {
530 AssignmentExpression *AssignmentExpression
531 QueryExpression *QueryExpression
532 Comments [2]Comments
533 Tokens Tokens
534 }
535
536 func (q *QueryExpression) parse(r *rParser) error {
537 if r.AcceptToken(parser.Token{Type: TokenOperator, Data: "?"}) {
538 q.Comments[1] = r.AcceptRunWhitespaceComments()
539
540 r.AcceptRunWhitespace()
541
542 s := r.NewGoal()
543 q.QueryExpression = new(QueryExpression)
544
545 if err := q.QueryExpression.parse(&s); err != nil {
546 return r.Error("QueryExpression", err)
547 }
548
549 r.Score(s)
550 } else {
551 s := r.NewGoal()
552 q.AssignmentExpression = new(AssignmentExpression)
553
554 if err := q.AssignmentExpression.parse(&s); err != nil {
555 return r.Error("QueryExpression", err)
556 }
557
558 r.Score(s)
559
560 s = r.NewGoal()
561
562 s.AcceptRunWhitespaceNoNewLine()
563
564 if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "?"}) {
565 q.Comments[0] = r.AcceptRunWhitespaceComments()
566
567 r.AcceptRunWhitespace()
568 r.AcceptToken(parser.Token{Type: TokenOperator, Data: "?"})
569
570 q.Comments[1] = r.AcceptRunWhitespaceComments()
571
572 r.AcceptRunWhitespace()
573
574 s = r.NewGoal()
575 q.QueryExpression = new(QueryExpression)
576
577 if err := q.QueryExpression.parse(&s); err != nil {
578 return r.Error("QueryExpression", err)
579 }
580
581 r.Score(s)
582 }
583 }
584
585 q.Tokens = r.ToTokens()
586
587 return nil
588 }
589
590 // AssignmentType defines the type of assignment in AssignmentExpression.
591 type AssignmentType uint8
592
593 const (
594 AssignmentNone AssignmentType = iota
595 AssignmentEquals
596 AssignmentLeftAssign
597 AssignmentRightAssign
598 AssignmentLeftParentAssign
599 AssignmentRightParentAssign
600 )
601
602 // AssignmentExpression represents a binding of an expression value.
603 type AssignmentExpression struct {
604 FormulaeExpression FormulaeExpression
605 AssignmentType AssignmentType
606 AssignmentExpression *AssignmentExpression
607 Comments [2]Comments
608 Tokens Tokens
609 }
610
611 func (a *AssignmentExpression) parse(r *rParser) error {
612 s := r.NewGoal()
613
614 if err := a.FormulaeExpression.parse(&s); err != nil {
615 return r.Error("AssignmentExpression", err)
616 }
617
618 r.Score(s)
619
620 s = r.NewGoal()
621
622 s.AcceptRunWhitespaceNoNewLine()
623
624 if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "="}) {
625 a.AssignmentType = AssignmentEquals
626 } else if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "<-"}) {
627 a.AssignmentType = AssignmentLeftAssign
628 } else if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "->"}) {
629 a.AssignmentType = AssignmentRightAssign
630 } else if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "<<-"}) {
631 a.AssignmentType = AssignmentLeftParentAssign
632 } else if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "->>"}) {
633 a.AssignmentType = AssignmentRightParentAssign
634 }
635
636 if a.AssignmentType != AssignmentNone {
637 a.Comments[0] = r.AcceptRunWhitespaceComments()
638
639 r.AcceptRunWhitespaceNoNewLine()
640 r.Next()
641
642 a.Comments[1] = r.AcceptRunWhitespaceComments()
643
644 r.AcceptRunWhitespace()
645
646 s = r.NewGoal()
647 a.AssignmentExpression = new(AssignmentExpression)
648
649 if err := a.AssignmentExpression.parse(&s); err != nil {
650 return r.Error("AssignmentExpression", err)
651 }
652
653 r.Score(s)
654 }
655
656 a.Tokens = r.ToTokens()
657
658 return nil
659 }
660
661 // FormulaeExpression represents a model formula.
662 type FormulaeExpression struct {
663 OrExpression *OrExpression
664 FormulaeExpression *FormulaeExpression
665 Comments Comments
666 Tokens Tokens
667 }
668
669 func (f *FormulaeExpression) parse(r *rParser) error {
670 s := r.NewGoal()
671
672 if s.Peek() != (parser.Token{Type: TokenOperator, Data: "~"}) {
673 f.OrExpression = new(OrExpression)
674
675 if err := f.OrExpression.parse(&s); err != nil {
676 return r.Error("FormulaeExpression", err)
677 }
678
679 r.Score(s)
680
681 s = r.NewGoal()
682
683 s.AcceptRunWhitespaceNoNewLine()
684 }
685
686 if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "~"}) {
687 r.Score(s)
688
689 f.Comments = r.AcceptRunWhitespaceComments()
690
691 r.AcceptRunWhitespace()
692
693 s = r.NewGoal()
694 f.FormulaeExpression = new(FormulaeExpression)
695
696 if err := f.FormulaeExpression.parse(&s); err != nil {
697 return r.Error("FormulaeExpression", err)
698 }
699
700 r.Score(s)
701 }
702
703 f.Tokens = r.ToTokens()
704
705 return nil
706 }
707
708 // OrType defines the type of an OrExpression.
709 type OrType uint8
710
711 const (
712 OrNone OrType = iota
713 OrVectorized
714 OrNotVectorized
715 )
716
717 // OrExpression represents one of two Or expressions.
718 type OrExpression struct {
719 AndExpression AndExpression
720 OrType OrType
721 OrExpression *OrExpression
722 Comments [2]Comments
723 Tokens Tokens
724 }
725
726 func (o *OrExpression) parse(r *rParser) error {
727 s := r.NewGoal()
728
729 if err := o.AndExpression.parse(&s); err != nil {
730 return r.Error("OrExpression", err)
731 }
732
733 r.Score(s)
734
735 s = r.NewGoal()
736
737 s.AcceptRunWhitespaceNoNewLine()
738
739 if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "|"}) {
740 o.OrType = OrVectorized
741 } else if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "||"}) {
742 o.OrType = OrNotVectorized
743 }
744
745 if o.OrType != OrNone {
746 o.Comments[0] = r.AcceptRunWhitespaceCommentsNoNewline()
747
748 r.AcceptRunWhitespaceNoNewLine()
749 r.Next()
750
751 o.Comments[1] = r.AcceptRunWhitespaceComments()
752
753 r.AcceptRunWhitespace()
754
755 s = r.NewGoal()
756 o.OrExpression = new(OrExpression)
757
758 if err := o.OrExpression.parse(&s); err != nil {
759 return r.Error("OrExpression", err)
760 }
761
762 r.Score(s)
763 }
764
765 o.Tokens = r.ToTokens()
766
767 return nil
768 }
769
770 // AndType defines the type of an AndExpression.
771 type AndType uint8
772
773 const (
774 AndNone AndType = iota
775 AndVectorized
776 AndNotVectorized
777 )
778
779 // AndExpression represents one of two And expressions.
780 type AndExpression struct {
781 NotExpression NotExpression
782 AndType AndType
783 AndExpression *AndExpression
784 Comments [2]Comments
785 Tokens Tokens
786 }
787
788 func (a *AndExpression) parse(r *rParser) error {
789 s := r.NewGoal()
790
791 if err := a.NotExpression.parse(&s); err != nil {
792 return r.Error("AndExpression", err)
793 }
794
795 r.Score(s)
796
797 s = r.NewGoal()
798
799 s.AcceptRunWhitespaceNoNewLine()
800
801 if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "&"}) {
802 a.AndType = AndVectorized
803 } else if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "&&"}) {
804 a.AndType = AndNotVectorized
805 }
806
807 if a.AndType != AndNone {
808 a.Comments[0] = r.AcceptRunWhitespaceCommentsNoNewline()
809
810 r.AcceptRunWhitespaceNoNewLine()
811 r.Next()
812
813 a.Comments[1] = r.AcceptRunWhitespaceComments()
814
815 r.AcceptRunWhitespace()
816
817 s = r.NewGoal()
818 a.AndExpression = new(AndExpression)
819
820 if err := a.AndExpression.parse(&s); err != nil {
821 return r.Error("AndExpression", err)
822 }
823
824 r.Score(s)
825 }
826
827 a.Tokens = r.ToTokens()
828
829 return nil
830 }
831
832 // NotExpression represents a possibly negated expression.
833 type NotExpression struct {
834 Nots uint
835 RelationalExpression RelationalExpression
836 Comments []Comments
837 Tokens Tokens
838 }
839
840 func (n *NotExpression) parse(r *rParser) error {
841 for r.AcceptToken(parser.Token{Type: TokenOperator, Data: "!"}) {
842 n.Nots++
843
844 n.Comments = append(n.Comments, r.AcceptRunWhitespaceComments())
845
846 r.AcceptRunWhitespace()
847 }
848
849 s := r.NewGoal()
850
851 if err := n.RelationalExpression.parse(&s); err != nil {
852 return r.Error("NotExpression", err)
853 }
854
855 r.Score(s)
856
857 n.Tokens = r.ToTokens()
858
859 return nil
860 }
861
862 // RelationalOperator defines the type of relationship for a
863 // RelationalExpression.
864 type RelationalOperator uint8
865
866 const (
867 RelationalNone RelationalOperator = iota
868 RelationalGreaterThan
869 RelationalGreaterThanOrEqual
870 RelationalLessThan
871 RelationalLessThanOrEqual
872 RelationalEqual
873 RelationalNotEqual
874 )
875
876 // RelationalExpression represents a logical relationship between two
877 // expressions.
878 type RelationalExpression struct {
879 AdditionExpression AdditionExpression
880 RelationalOperator RelationalOperator
881 RelationalExpression *RelationalExpression
882 Comments [2]Comments
883 Tokens Tokens
884 }
885
886 func (re *RelationalExpression) parse(r *rParser) error {
887 s := r.NewGoal()
888
889 if err := re.AdditionExpression.parse(&s); err != nil {
890 return r.Error("RelationalExpression", err)
891 }
892
893 r.Score(s)
894
895 s = r.NewGoal()
896
897 s.AcceptRunWhitespaceNoNewLine()
898
899 if s.AcceptToken(parser.Token{Type: TokenOperator, Data: ">"}) {
900 re.RelationalOperator = RelationalGreaterThan
901 } else if s.AcceptToken(parser.Token{Type: TokenOperator, Data: ">="}) {
902 re.RelationalOperator = RelationalGreaterThanOrEqual
903 } else if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "<"}) {
904 re.RelationalOperator = RelationalLessThan
905 } else if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "<="}) {
906 re.RelationalOperator = RelationalLessThanOrEqual
907 } else if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "=="}) {
908 re.RelationalOperator = RelationalEqual
909 } else if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "!="}) {
910 re.RelationalOperator = RelationalNotEqual
911 }
912
913 if re.RelationalOperator != RelationalNone {
914 re.Comments[0] = r.AcceptRunWhitespaceCommentsNoNewline()
915
916 r.AcceptRunWhitespaceNoNewLine()
917 r.Next()
918
919 re.Comments[1] = r.AcceptRunWhitespaceComments()
920
921 r.AcceptRunWhitespace()
922
923 s = r.NewGoal()
924 re.RelationalExpression = new(RelationalExpression)
925
926 if err := re.RelationalExpression.parse(&s); err != nil {
927 return r.Error("RelationalExpression", err)
928 }
929
930 r.Score(s)
931 }
932
933 re.Tokens = r.ToTokens()
934
935 return nil
936 }
937
938 // AdditionType determines the type of a AdditionExpression.
939 type AdditionType uint8
940
941 const (
942 AdditionNone AdditionType = iota
943 AdditionAdd
944 AdditionSubtract
945 )
946
947 // AdditionExpression represents a binary adding or subtracting of two
948 // expressions.
949 type AdditionExpression struct {
950 MultiplicationExpression MultiplicationExpression
951 AdditionType AdditionType
952 AdditionExpression *AdditionExpression
953 Comments [2]Comments
954 Tokens Tokens
955 }
956
957 func (a *AdditionExpression) parse(r *rParser) error {
958 s := r.NewGoal()
959
960 if err := a.MultiplicationExpression.parse(&s); err != nil {
961 return r.Error("AdditionExpression", err)
962 }
963
964 r.Score(s)
965
966 s = r.NewGoal()
967
968 s.AcceptRunWhitespaceNoNewLine()
969
970 if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "+"}) {
971 a.AdditionType = AdditionAdd
972 } else if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "-"}) {
973 a.AdditionType = AdditionSubtract
974 }
975
976 if a.AdditionType != AdditionNone {
977 a.Comments[0] = r.AcceptRunWhitespaceCommentsNoNewline()
978
979 r.AcceptRunWhitespaceNoNewLine()
980 r.Next()
981
982 a.Comments[1] = r.AcceptRunWhitespaceComments()
983
984 r.AcceptRunWhitespace()
985
986 s = r.NewGoal()
987 a.AdditionExpression = new(AdditionExpression)
988
989 if err := a.AdditionExpression.parse(&s); err != nil {
990 return r.Error("AdditionExpression", err)
991 }
992
993 r.Score(s)
994 }
995
996 a.Tokens = r.ToTokens()
997
998 return nil
999 }
1000
1001 // MultiplicationType determines the type of a MultiplicationExpression.
1002 type MultiplicationType uint8
1003
1004 const (
1005 MultiplicationNone MultiplicationType = iota
1006 MultiplicationMultiply
1007 MultiplicationDivide
1008 )
1009
1010 type MultiplicationExpression struct {
1011 PipeOrSpecialExpression PipeOrSpecialExpression
1012 MultiplicationType MultiplicationType
1013 MultiplicationExpression *MultiplicationExpression
1014 Comments [2]Comments
1015 Tokens Tokens
1016 }
1017
1018 // MultiplicationExpression represents a binary multiplication or division of
1019 // two expressions.
1020 func (m *MultiplicationExpression) parse(r *rParser) error {
1021 s := r.NewGoal()
1022
1023 if err := m.PipeOrSpecialExpression.parse(&s); err != nil {
1024 return r.Error("MultiplicationExpression", err)
1025 }
1026
1027 r.Score(s)
1028
1029 s = r.NewGoal()
1030
1031 s.AcceptRunWhitespaceNoNewLine()
1032
1033 if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "*"}) {
1034 m.MultiplicationType = MultiplicationMultiply
1035 } else if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "/"}) {
1036 m.MultiplicationType = MultiplicationDivide
1037 }
1038
1039 if m.MultiplicationType != MultiplicationNone {
1040 m.Comments[0] = r.AcceptRunWhitespaceCommentsNoNewline()
1041
1042 r.AcceptRunWhitespaceNoNewLine()
1043 r.Next()
1044
1045 m.Comments[1] = r.AcceptRunWhitespaceComments()
1046
1047 r.AcceptRunWhitespace()
1048
1049 s = r.NewGoal()
1050 m.MultiplicationExpression = new(MultiplicationExpression)
1051
1052 if err := m.MultiplicationExpression.parse(&s); err != nil {
1053 return r.Error("MultiplicationExpression", err)
1054 }
1055
1056 r.Score(s)
1057 }
1058
1059 m.Tokens = r.ToTokens()
1060
1061 return nil
1062 }
1063
1064 // PipeOrSpecialExpression represetns either a pipe (|>) or special (%%) binary
1065 // operation.
1066 type PipeOrSpecialExpression struct {
1067 SequenceExpression SequenceExpression
1068 Operator *Token
1069 PipeOrSpecialExpression *PipeOrSpecialExpression
1070 Comments [2]Comments
1071 Tokens Tokens
1072 }
1073
1074 func (p *PipeOrSpecialExpression) parse(r *rParser) error {
1075 s := r.NewGoal()
1076
1077 if err := p.SequenceExpression.parse(&s); err != nil {
1078 return r.Error("PipeOrSpecialExpression", err)
1079 }
1080
1081 r.Score(s)
1082
1083 s = r.NewGoal()
1084
1085 s.AcceptRunWhitespaceNoNewLine()
1086
1087 if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "|>"}) || s.Accept(TokenSpecialOperator) {
1088 p.Operator = s.GetLastToken()
1089
1090 p.Comments[0] = r.AcceptRunWhitespaceCommentsNoNewline()
1091
1092 r.AcceptRunWhitespaceNoNewLine()
1093 r.Next()
1094
1095 p.Comments[1] = r.AcceptRunWhitespaceComments()
1096
1097 r.AcceptRunWhitespace()
1098
1099 s = r.NewGoal()
1100 p.PipeOrSpecialExpression = new(PipeOrSpecialExpression)
1101
1102 if err := p.PipeOrSpecialExpression.parse(&s); err != nil {
1103 return r.Error("PipeOrSpecialExpression", err)
1104 }
1105
1106 r.Score(s)
1107 }
1108
1109 p.Tokens = r.ToTokens()
1110
1111 return nil
1112 }
1113
1114 // SequenceExpression represents a sequencing operation.
1115 type SequenceExpression struct {
1116 UnaryExpression UnaryExpression
1117 SequenceExpression *SequenceExpression
1118 Comments [2]Comments
1119 Tokens Tokens
1120 }
1121
1122 func (se *SequenceExpression) parse(r *rParser) error {
1123 s := r.NewGoal()
1124
1125 if err := se.UnaryExpression.parse(&s); err != nil {
1126 return r.Error("SequenceExpression", err)
1127 }
1128
1129 r.Score(s)
1130
1131 s = r.NewGoal()
1132
1133 s.AcceptRunWhitespaceNoNewLine()
1134
1135 if s.AcceptToken(parser.Token{Type: TokenOperator, Data: ":"}) {
1136 se.Comments[0] = r.AcceptRunWhitespaceComments()
1137
1138 r.AcceptRunWhitespace()
1139 r.Next()
1140
1141 se.Comments[1] = r.AcceptRunWhitespaceComments()
1142
1143 r.AcceptRunWhitespace()
1144
1145 s = r.NewGoal()
1146 se.SequenceExpression = new(SequenceExpression)
1147
1148 if err := se.SequenceExpression.parse(&s); err != nil {
1149 return r.Error("SequenceExpression", err)
1150 }
1151
1152 r.Score(s)
1153 }
1154
1155 se.Tokens = r.ToTokens()
1156
1157 return nil
1158 }
1159
1160 // UnaryType determines the type of operation in a UnaryExpression.
1161 type UnaryType uint8
1162
1163 const (
1164 UnaryAdd UnaryType = iota
1165 UnaryMinus
1166 )
1167
1168 // UnaryExpression represents a unary addition or subtraction.
1169 type UnaryExpression struct {
1170 UnaryType []UnaryType
1171 ExponentiationExpression ExponentiationExpression
1172 Comments []Comments
1173 Tokens Tokens
1174 }
1175
1176 func (u *UnaryExpression) parse(r *rParser) error {
1177 for {
1178 if r.AcceptToken(parser.Token{Type: TokenOperator, Data: "+"}) {
1179 u.UnaryType = append(u.UnaryType, UnaryAdd)
1180 u.Comments = append(u.Comments, r.AcceptRunWhitespaceComments())
1181
1182 r.AcceptRunWhitespace()
1183 } else if r.AcceptToken(parser.Token{Type: TokenOperator, Data: "-"}) {
1184 u.UnaryType = append(u.UnaryType, UnaryMinus)
1185 u.Comments = append(u.Comments, r.AcceptRunWhitespaceComments())
1186
1187 r.AcceptRunWhitespace()
1188 } else {
1189 break
1190 }
1191 }
1192
1193 s := r.NewGoal()
1194
1195 if err := u.ExponentiationExpression.parse(&s); err != nil {
1196 return r.Error("UnaryExpression", err)
1197 }
1198
1199 r.Score(s)
1200
1201 u.Tokens = r.ToTokens()
1202
1203 return nil
1204 }
1205
1206 // ExponentiationExpression represents a exponentiation operation.
1207 type ExponentiationExpression struct {
1208 SubsetExpression SubsetExpression
1209 ExponentiationExpression *ExponentiationExpression
1210 Comments [2]Comments
1211 Tokens Tokens
1212 }
1213
1214 func (e *ExponentiationExpression) parse(r *rParser) error {
1215 s := r.NewGoal()
1216
1217 if err := e.SubsetExpression.parse(&s); err != nil {
1218 return r.Error("ExponentiationExpression", err)
1219 }
1220
1221 r.Score(s)
1222
1223 s = r.NewGoal()
1224
1225 s.AcceptRunWhitespaceNoNewLine()
1226
1227 if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "^"}) {
1228 e.Comments[0] = r.AcceptRunWhitespaceComments()
1229
1230 r.AcceptRunWhitespace()
1231 r.Next()
1232
1233 e.Comments[1] = r.AcceptRunWhitespaceComments()
1234
1235 r.AcceptRunWhitespace()
1236
1237 s = r.NewGoal()
1238 e.ExponentiationExpression = new(ExponentiationExpression)
1239
1240 if err := e.ExponentiationExpression.parse(&s); err != nil {
1241 return r.Error("ExponentiationExpression", err)
1242 }
1243
1244 r.Score(s)
1245 }
1246
1247 e.Tokens = r.ToTokens()
1248
1249 return nil
1250 }
1251
1252 // SubsetType determines the type of a SubsetExpression.
1253 type SubsetType uint8
1254
1255 const (
1256 SubsetNone SubsetType = iota
1257 SubsetList
1258 SubsetStructure
1259 )
1260
1261 // SubsetExpression represents a subsetting operation.
1262 type SubsetExpression struct {
1263 ScopeExpression ScopeExpression
1264 SubsetType SubsetType
1265 SubsetExpression *SubsetExpression
1266 Comments [2]Comments
1267 Tokens Tokens
1268 }
1269
1270 func (se *SubsetExpression) parse(r *rParser) error {
1271 s := r.NewGoal()
1272
1273 if err := se.ScopeExpression.parse(&s); err != nil {
1274 return r.Error("SubsetExpression", err)
1275 }
1276
1277 r.Score(s)
1278
1279 s = r.NewGoal()
1280
1281 s.AcceptRunWhitespaceNoNewLine()
1282
1283 if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "$"}) {
1284 se.SubsetType = SubsetList
1285 } else if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "@"}) {
1286 se.SubsetType = SubsetStructure
1287 }
1288
1289 if se.SubsetType != SubsetNone {
1290 se.Comments[0] = r.AcceptRunWhitespaceComments()
1291
1292 r.AcceptRunWhitespace()
1293 r.Next()
1294
1295 se.Comments[1] = r.AcceptRunWhitespaceComments()
1296
1297 r.AcceptRunWhitespace()
1298
1299 s = r.NewGoal()
1300 se.SubsetExpression = new(SubsetExpression)
1301
1302 if err := se.SubsetExpression.parse(&s); err != nil {
1303 return r.Error("SubsetExpression", err)
1304 }
1305
1306 r.Score(s)
1307 }
1308
1309 se.Tokens = r.ToTokens()
1310
1311 return nil
1312 }
1313
1314 // ScopeExpression represents a scoping operation.
1315 type ScopeExpression struct {
1316 IndexOrCallExpression IndexOrCallExpression
1317 ScopeExpression *ScopeExpression
1318 Comments [2]Comments
1319 Tokens Tokens
1320 }
1321
1322 func (se *ScopeExpression) parse(r *rParser) error {
1323 s := r.NewGoal()
1324
1325 if err := se.IndexOrCallExpression.parse(&s); err != nil {
1326 return r.Error("ScopeExpression", err)
1327 }
1328
1329 r.Score(s)
1330
1331 s = r.NewGoal()
1332
1333 s.AcceptRunWhitespaceNoNewLine()
1334
1335 if s.AcceptToken(parser.Token{Type: TokenOperator, Data: "::"}) {
1336 se.Comments[0] = r.AcceptRunWhitespaceComments()
1337
1338 r.AcceptRunWhitespace()
1339 r.Next()
1340
1341 se.Comments[1] = r.AcceptRunWhitespaceComments()
1342
1343 r.AcceptRunWhitespace()
1344
1345 s = r.NewGoal()
1346 se.ScopeExpression = new(ScopeExpression)
1347
1348 if err := se.ScopeExpression.parse(&s); err != nil {
1349 return r.Error("ScopeExpression", err)
1350 }
1351
1352 r.Score(s)
1353 }
1354
1355 se.Tokens = r.ToTokens()
1356
1357 return nil
1358 }
1359
1360 // IndexOrCallExpression represents a possible indexing or function calling
1361 // operation.
1362 type IndexOrCallExpression struct {
1363 SimpleExpression *SimpleExpression
1364 IndexOrCallExpression *IndexOrCallExpression
1365 Index *Index
1366 Call *Call
1367 Comments Comments
1368 Tokens Tokens
1369 }
1370
1371 func (i *IndexOrCallExpression) parse(r *rParser) error {
1372 s := r.NewGoal()
1373 i.SimpleExpression = new(SimpleExpression)
1374
1375 if err := i.SimpleExpression.parse(&s); err != nil {
1376 return r.Error("IndexOrCallExpression", err)
1377 }
1378
1379 r.Score(s)
1380
1381 Loop:
1382 for {
1383 i.Tokens = r.ToTokens()
1384 s = r.NewGoal()
1385
1386 var (
1387 index *Index
1388 comments Comments
1389 call *Call
1390 err error
1391 )
1392
1393 comments = s.AcceptRunWhitespaceCommentsNoNewline()
1394
1395 s.AcceptRunWhitespaceNoNewLine()
1396
1397 switch s.Peek() {
1398 case parser.Token{Type: TokenGrouping, Data: "["}, parser.Token{Type: TokenGrouping, Data: "[["}:
1399 r.Score(s)
1400
1401 s = r.NewGoal()
1402 index = new(Index)
1403 err = index.parse(&s)
1404 case parser.Token{Type: TokenGrouping, Data: "("}:
1405 r.Score(s)
1406
1407 s = r.NewGoal()
1408 call = new(Call)
1409 err = call.parse(&s)
1410 default:
1411 break Loop
1412 }
1413
1414 if err != nil {
1415 return r.Error("IndexOrCallExpression", err)
1416 }
1417
1418 r.Score(s)
1419
1420 j := new(IndexOrCallExpression)
1421 *j = *i
1422
1423 *i = IndexOrCallExpression{
1424 IndexOrCallExpression: j,
1425 Index: index,
1426 Call: call,
1427 Comments: comments,
1428 }
1429 }
1430
1431 return nil
1432 }
1433
1434 // SimpleExpression represents either an Identifier, a Constant, an Ellipsis, a
1435 // ParenthesizedExpression, or a CompoundExpression.
1436 type SimpleExpression struct {
1437 Identifier *Token
1438 Constant *Token
1439 Ellipsis *Token
1440 ParenthesizedExpression *ParenthesizedExpression
1441 CompoundExpression *CompoundExpression
1442 Tokens Tokens
1443 }
1444
1445 func (a *SimpleExpression) parse(r *rParser) error {
1446 if r.Accept(TokenIdentifier) {
1447 a.Identifier = r.GetLastToken()
1448 } else if r.Accept(TokenStringLiteral, TokenNumericLiteral, TokenIntegerLiteral, TokenComplexLiteral, TokenBooleanLiteral, TokenNull, TokenNA) {
1449 a.Constant = r.GetLastToken()
1450 } else if r.Accept(TokenEllipsis) {
1451 a.Ellipsis = r.GetLastToken()
1452 } else if tk := r.Peek(); tk == (parser.Token{Type: TokenGrouping, Data: "("}) {
1453 s := r.NewGoal()
1454 a.ParenthesizedExpression = new(ParenthesizedExpression)
1455
1456 if err := a.ParenthesizedExpression.parse(&s); err != nil {
1457 return r.Error("SimpleExpression", err)
1458 }
1459
1460 r.Score(s)
1461 } else if tk == (parser.Token{Type: TokenGrouping, Data: "{"}) {
1462 s := r.NewGoal()
1463 a.CompoundExpression = new(CompoundExpression)
1464
1465 if err := a.CompoundExpression.parse(&s); err != nil {
1466 return r.Error("SimpleExpression", err)
1467 }
1468
1469 r.Score(s)
1470 } else {
1471 return r.Error("SimpleExpression", ErrInvalidSimpleExpression)
1472 }
1473
1474 a.Tokens = r.ToTokens()
1475
1476 return nil
1477 }
1478
1479 type ParenthesizedExpression struct {
1480 Expression Expression
1481 Tokens Tokens
1482 }
1483
1484 func (p *ParenthesizedExpression) parse(r *rParser) error {
1485 r.AcceptToken(parser.Token{Type: TokenGrouping, Data: "("})
1486 r.AcceptRunWhitespaceNoComment()
1487
1488 s := r.NewGoal()
1489
1490 if err := p.Expression.parse(&s); err != nil {
1491 return r.Error("ParenthesizedExpression", err)
1492 }
1493
1494 r.Score(s)
1495 r.AcceptRunWhitespace()
1496
1497 if !r.AcceptToken(parser.Token{Type: TokenGrouping, Data: ")"}) {
1498 return r.Error("ParenthesizedExpression", ErrMissingClosingParen)
1499 }
1500
1501 p.Tokens = r.ToTokens()
1502
1503 return nil
1504 }
1505
1506 // Index represents either a single or double bracketed indexing operation.
1507 type Index struct {
1508 Double bool
1509 Args []IndexExpression
1510 Tokens Tokens
1511 }
1512
1513 func (i *Index) parse(r *rParser) error {
1514 if r.AcceptToken(parser.Token{Type: TokenGrouping, Data: "[["}) {
1515 i.Double = true
1516 } else {
1517 r.AcceptToken(parser.Token{Type: TokenGrouping, Data: "["})
1518 }
1519
1520 s := r.NewGoal()
1521 s.AcceptRunWhitespace()
1522
1523 if !i.Double && s.AcceptToken(parser.Token{Type: TokenGrouping, Data: "]"}) {
1524 r.Score(s)
1525 } else {
1526 for {
1527 r.AcceptRunWhitespaceNoComment()
1528
1529 s = r.NewGoal()
1530
1531 var h IndexExpression
1532
1533 if err := h.parse(&s); err != nil {
1534 return r.Error("Index", err)
1535 }
1536
1537 i.Args = append(i.Args, h)
1538
1539 r.Score(s)
1540
1541 r.AcceptRunWhitespace()
1542
1543 if i.Double {
1544 if !r.AcceptToken(parser.Token{Type: TokenGrouping, Data: "]]"}) {
1545 return r.Error("Index", ErrMissingClosingDoubleBracket)
1546 }
1547
1548 break
1549 } else if r.AcceptToken(parser.Token{Type: TokenGrouping, Data: "]"}) {
1550 break
1551 } else if !r.AcceptToken(parser.Token{Type: TokenExpressionTerminator, Data: ","}) {
1552 return r.Error("Index", ErrMissingComma)
1553 }
1554 }
1555 }
1556
1557 i.Tokens = r.ToTokens()
1558
1559 return nil
1560 }
1561
1562 // IndexExpression represents a single expression within an Index.
1563 type IndexExpression struct {
1564 QueryExpression QueryExpression
1565 Comments [2]Comments
1566 Tokens
1567 }
1568
1569 func (i *IndexExpression) parse(r *rParser) error {
1570 i.Comments[0] = r.AcceptRunWhitespaceComments()
1571
1572 r.AcceptRunWhitespace()
1573
1574 s := r.NewGoal()
1575
1576 if err := i.QueryExpression.parse(&s); err != nil {
1577 return r.Error("IndexExpression", err)
1578 }
1579
1580 r.Score(s)
1581
1582 i.Comments[1] = r.AcceptRunWhitespaceComments()
1583 i.Tokens = r.ToTokens()
1584
1585 return nil
1586 }
1587
1588 // Call represents the arguments passed to a function.
1589 type Call struct {
1590 Args []Arg
1591 Comments Comments
1592 Tokens Tokens
1593 }
1594
1595 func (c *Call) parse(r *rParser) error {
1596 r.AcceptToken(parser.Token{Type: TokenGrouping, Data: "("})
1597
1598 s := r.NewGoal()
1599 s.AcceptRunWhitespace()
1600
1601 if s.AcceptToken(parser.Token{Type: TokenGrouping, Data: ")"}) {
1602 c.Comments = r.AcceptRunWhitespaceComments()
1603
1604 r.AcceptRunWhitespace()
1605 r.AcceptToken(parser.Token{Type: TokenGrouping, Data: ")"})
1606 } else {
1607 for !s.AcceptToken(parser.Token{Type: TokenGrouping, Data: ")"}) {
1608 r.AcceptRunWhitespaceNoComment()
1609
1610 s := r.NewGoal()
1611
1612 var a Arg
1613
1614 if err := a.parse(&s); err != nil {
1615 return r.Error("Call", err)
1616 }
1617
1618 r.Score(s)
1619
1620 c.Args = append(c.Args, a)
1621
1622 r.AcceptRunWhitespace()
1623
1624 if r.AcceptToken(parser.Token{Type: TokenGrouping, Data: ")"}) {
1625 break
1626 } else if !r.AcceptToken(parser.Token{Type: TokenExpressionTerminator, Data: ","}) {
1627 return r.Error("Call", ErrMissingComma)
1628 }
1629
1630 s = r.NewGoal()
1631
1632 s.AcceptRunWhitespace()
1633 }
1634 }
1635
1636 for len(c.Args) > 0 && len(c.Args[len(c.Args)-1].Tokens) == 0 {
1637 c.Args = c.Args[:len(c.Args)-1]
1638 }
1639
1640 c.Tokens = r.ToTokens()
1641
1642 return nil
1643 }
1644
1645 // Arg represents a single argument passed to a function.
1646 type Arg struct {
1647 QueryExpression *QueryExpression
1648 Ellipsis *Token
1649 Comments [2]Comments
1650 Tokens Tokens
1651 }
1652
1653 func (a *Arg) parse(r *rParser) error {
1654 a.Comments[0] = r.AcceptRunWhitespaceComments()
1655
1656 r.AcceptRunWhitespace()
1657
1658 if r.Accept(TokenEllipsis) {
1659 a.Ellipsis = r.GetLastToken()
1660 } else if tk := r.Peek(); tk != (parser.Token{Type: TokenGrouping, Data: ")"}) && tk != (parser.Token{Type: TokenExpressionTerminator, Data: ","}) && tk.Type != parser.TokenDone {
1661 s := r.NewGoal()
1662 a.QueryExpression = new(QueryExpression)
1663
1664 if err := a.QueryExpression.parse(&s); err != nil {
1665 return r.Error("Arg", err)
1666 }
1667
1668 r.Score(s)
1669 }
1670
1671 a.Comments[1] = r.AcceptRunWhitespaceComments()
1672 a.Tokens = r.ToTokens()
1673
1674 return nil
1675 }
1676