View Javadoc

1   /* Generated By:JavaCC: Do not edit this line. PoshParser.java */
2   package cz.cuni.amis.pogamut.sposh.elements;
3   
4   import cz.cuni.amis.pogamut.shady.ArgString;
5   import java.util.Collections;
6   import java.util.LinkedList;
7   import java.util.List;
8   
9   public class PoshParser implements PoshParserConstants {
10      final static class Packet<FIRST, SECOND> {
11          Packet(FIRST first, SECOND second) {
12              this.first = first;
13              this.second = second;
14          }
15          public FIRST first;
16          public SECOND second;
17      }
18  
19      /**
20       * Take the token COMMENT and unescape its content.
21       */
22      private String unescape(String comment)  throws ParseException {
23          // comment token must have at least starting and ending double quote
24          int commentLength = comment.length();
25          if (commentLength < 2)
26              throw new ParseException();
27          if (comment.charAt(0) != '"' || comment.charAt(commentLength - 1) != '"')
28              throw new ParseException();
29  
30          String commentContent = comment.substring(1, commentLength - 1);
31          // ArgString throws different ParseException that this parser.
32          try {
33              return ArgString.unescape(commentContent);
34          } catch (Exception ex) {
35              throw new ParseException(ex.getMessage());
36          }
37      }
38  
39  /**
40   * FIXME: According to official grammar, plan can consists only from single
41   * competence or action pattern. Sorry, but I don't allow this for now.
42   * plan ::= "(" [ "(" <docstring> ]
43                  ( "(" <competence> | <action-pattern> )*
44                    "(" <drive-collection>
45                  ( "(" <competence> | <action-pattern> )* ")" 
46   */
47    final public PoshPlan parsePlan() throws ParseException {
48          PoshPlan plan = LapElementsFactory.createPlan();
49      jj_consume_token(LBRACE);
50      jj_consume_token(LBRACE);
51      planFirstElement(plan);
52      jj_consume_token(0);
53                                                          {if (true) return plan;}
54      throw new Error("Missing return statement in function");
55    }
56  
57  /**
58   * [ "(" <docstring> ]
59   * Part of plan rule. 
60   */
61    final public void planFirstElement(PoshPlan plan) throws ParseException {
62          ActionPattern ap;
63          Competence c;
64          DriveCollection dc;
65          Adopt ad;
66      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
67      case DOCUMENTATION:
68        docString(plan);
69        jj_consume_token(LBRACE);
70        planFirstHalf(plan);
71        break;
72      case AP:
73        ap = actionPattern();
74              plan.addActionPattern(ap);
75              // ex.currentToken = token;
76  
77        jj_consume_token(LBRACE);
78        planFirstHalf(plan);
79        break;
80      case COMPETENCE:
81        c = competence();
82              plan.addCompetence(c);
83              // ex.currentToken = token;
84  
85        jj_consume_token(LBRACE);
86        planFirstHalf(plan);
87        break;
88      case AD:
89        ad = adapt();
90                          plan.addAdopt(ad);
91                          // ex.currentToken = token;
92  
93        jj_consume_token(LBRACE);
94        planFirstHalf(plan);
95        break;
96      case DC:
97        driveCollection(plan.getDriveCollection());
98        planSecondHalf(plan);
99        break;
100     default:
101       jj_la1[0] = jj_gen;
102       jj_consume_token(-1);
103       throw new ParseException();
104     }
105   }
106 
107 /**
108  * First half of plan rule.
109  * ( "(" <competence> | <action-pattern> )*
110  */
111   final public void planFirstHalf(PoshPlan plan) throws ParseException {
112         ActionPattern ap;
113         Competence c;
114         Adopt ad;
115     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
116     case AP:
117       ap = actionPattern();
118             try {
119                 plan.addActionPattern(ap);
120             } catch (ParseException ex) {
121                 ex.currentToken = token;
122                 {if (true) throw ex;}
123             }
124       jj_consume_token(LBRACE);
125       planFirstHalf(plan);
126       break;
127     case COMPETENCE:
128       c = competence();
129             try {
130                 plan.addCompetence(c);
131             } catch (ParseException ex) {
132                 ex.currentToken = token;
133                 {if (true) throw ex;}
134             }
135       jj_consume_token(LBRACE);
136       planFirstHalf(plan);
137       break;
138     case AD:
139       ad = adapt();
140                         try {
141                 plan.addAdopt(ad);
142             } catch (ParseException ex) {
143                 ex.currentToken = token;
144                 {if (true) throw ex;}
145             }
146       jj_consume_token(LBRACE);
147       planFirstHalf(plan);
148       break;
149     case DC:
150       driveCollection(plan.getDriveCollection());
151       planSecondHalf(plan);
152       break;
153     default:
154       jj_la1[1] = jj_gen;
155       jj_consume_token(-1);
156       throw new ParseException();
157     }
158   }
159 
160 /**
161  * ( "(" <competence> | <action-pattern> )* 
162  *  Part of plan() rule
163  */
164   final public void planSecondHalf(PoshPlan plan) throws ParseException {
165     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
166     case LBRACE:
167       jj_consume_token(LBRACE);
168       planSecondHalfElements(plan);
169       break;
170     case RBRACE:
171       jj_consume_token(RBRACE);
172       break;
173     default:
174       jj_la1[2] = jj_gen;
175       jj_consume_token(-1);
176       throw new ParseException();
177     }
178   }
179 
180 /**
181  * ( <competence> | <action-pattern> ) planSecondHalf(plan)
182  */
183   final public void planSecondHalfElements(PoshPlan plan) throws ParseException {
184         ActionPattern ap;
185         Competence c;
186         Adopt ad;
187     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
188     case AP:
189       ap = actionPattern();
190             try {
191                 plan.addActionPattern(ap);
192             } catch (ParseException ex) {
193                 ex.currentToken = token;
194                 {if (true) throw ex;}
195             }
196       planSecondHalf(plan);
197       break;
198     case COMPETENCE:
199       c = competence();
200             try {
201                 plan.addCompetence(c);
202             } catch (ParseException ex) {
203                 ex.currentToken = token;
204                 {if (true) throw ex;}
205             }
206       planSecondHalf(plan);
207       break;
208     case AD:
209       ad = adapt();
210             try {
211                 plan.addAdopt(ad);
212             } catch (ParseException ex) {
213                 ex.currentToken = token;
214                 {if (true) throw ex;}
215             }
216       planSecondHalf(plan);
217       break;
218     default:
219       jj_la1[3] = jj_gen;
220       jj_consume_token(-1);
221       throw new ParseException();
222     }
223   }
224 
225 /**
226  * docstring ::= DOCUMENTATION COMMENT COMMENT COMMENT ")" 
227  */
228   final public void docString(PoshPlan plan) throws ParseException {
229     Token name, author, info;
230     jj_consume_token(DOCUMENTATION);
231     name = jj_consume_token(COMMENT);
232                      plan.setName(unescape(name.toString()));
233     author = jj_consume_token(COMMENT);
234                        plan.setAuthor(unescape(author.toString()));
235     info = jj_consume_token(COMMENT);
236                      plan.setInfo(unescape(info.toString()));
237     jj_consume_token(RBRACE);
238   }
239 
240 /**
241  * Take passed drive collection and put the data (name, drives) into it.
242  * <pre>
243  * driveCollection() :: "DC" <name> [ <goal> ] "(" "drives" ( "(" <drive-element>+ ")" )+ ")" ")"
244  * </pre>
245  * XXX: It has extra braces around <drive-element>, remove them
246  */
247   final public void driveCollection(DriveCollection dc) throws ParseException {
248     Token name;
249     DriveElement drive;
250     FormalParameters formalParameters = new FormalParameters();
251     jj_consume_token(DC);
252     name = jj_consume_token(NAME);
253                        dc.setName(name.toString());
254     driveCollectionGoal(dc, formalParameters);
255     label_1:
256     while (true) {
257       jj_consume_token(LBRACE);
258       label_2:
259       while (true) {
260         drive = driveElement(formalParameters);
261                                                      dc.addDrive(drive);
262         switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
263         case LBRACE:
264           ;
265           break;
266         default:
267           jj_la1[4] = jj_gen;
268           break label_2;
269         }
270       }
271       jj_consume_token(RBRACE);
272       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
273       case LBRACE:
274         ;
275         break;
276       default:
277         jj_la1[5] = jj_gen;
278         break label_1;
279       }
280     }
281     jj_consume_token(RBRACE);
282     jj_consume_token(RBRACE);
283   }
284 
285 /**
286  * Process goal of DC (if exists).
287  * ( NIL | "(" <goal> | ) "(" <DRIVES>
288  * Part of driveCollection() rule.
289  */
290   final public void driveCollectionGoal(DriveCollection dc, FormalParameters formalParameters) throws ParseException {
291     List<Sense> goalSenses;
292     jj_consume_token(LBRACE);
293     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
294     case GOAL:
295       goalSenses = goal(formalParameters);
296                                            dc.getGoal().addAll(goalSenses);
297       jj_consume_token(LBRACE);
298       jj_consume_token(DRIVES);
299       break;
300     case DRIVES:
301       jj_consume_token(DRIVES);
302       break;
303     default:
304       jj_la1[6] = jj_gen;
305       jj_consume_token(-1);
306       throw new ParseException();
307     }
308   }
309 
310 /**
311  * Process goal sense and return them.
312  * goal ::= GOAL <senses> ")"
313  */
314   final public List<Sense> goal(FormalParameters formalParameters) throws ParseException {
315     List<Sense> senses;
316     jj_consume_token(GOAL);
317     senses = senses(formalParameters);
318     jj_consume_token(RBRACE);
319      {if (true) return senses;}
320     throw new Error("Missing return statement in function");
321   }
322 
323 /**
324  * Create a drive and return it.
325  * <pre>
326  * drive-element ::= "(" NAME ( NIL | "(" <trigger> | ) NAME ( NIL | "(" <freq> | ) <opt-comment> ")"
327  * </pre>
328  */
329   final public DriveElement driveElement(FormalParameters formalParameters) throws ParseException {
330     Token name;
331     Packet<Arguments, Freq> packet;
332     Token actionName;
333     List<Sense> triggerSenses;
334     String comment;
335     jj_consume_token(LBRACE);
336     name = jj_consume_token(NAME);
337     triggerSenses = driveElementTrigger(formalParameters);
338     actionName = jj_consume_token(NAME);
339     packet = driveElementActionFreq(formalParameters);
340     comment = optComment();
341     jj_consume_token(RBRACE);
342             {if (true) return LapElementsFactory.createDriveElement(name.toString(), triggerSenses, new PrimitiveCall(actionName.toString(), packet.first), packet.second, comment);}
343     throw new Error("Missing return statement in function");
344   }
345 
346 /**
347  * Nonterminal for possible arguments of the action call of the drive action and 
348  * the frequency.
349  * <code>
350  * driveElementActionFreq:: ( <nil> | "(" ( <freq> | <args> <freq> ) | )
351  * </code>
352  * I should probably use lookahead of 2 or more, but I really need this fast 
353  * for continuous syntax highlighting.  
354  */
355   final public Packet<Arguments, Freq> driveElementActionFreq(FormalParameters formalParameters) throws ParseException {
356     Arguments arguments = new Arguments();
357     Freq freq = Freq.INFINITE;
358     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
359     case NIL:
360       jj_consume_token(NIL);
361             {if (true) return new Packet<Arguments, Freq>(arguments, freq);}
362       break;
363     case LBRACE:
364       jj_consume_token(LBRACE);
365       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
366       case HOURS:
367       case MINUTES:
368       case SECONDS:
369       case HZ:
370       case PM:
371       case NONE:
372         freq = freq();
373                     {if (true) return new Packet<Arguments, Freq>(arguments, freq);}
374         break;
375       case NIL:
376       case TRUE:
377       case FALSE:
378       case NUMFLOAT:
379       case NUMINT:
380       case NAME:
381       case STRINGVALUE:
382       case COMMENT:
383       case VARIABLE:
384         arguments = callParametersList(formalParameters);
385         freq = driveElementFreq();
386                     {if (true) return new Packet<Arguments, Freq>(arguments, freq);}
387         break;
388       default:
389         jj_la1[7] = jj_gen;
390         jj_consume_token(-1);
391         throw new ParseException();
392       }
393       break;
394     default:
395       jj_la1[8] = jj_gen;
396             {if (true) return new Packet<Arguments, Freq>(arguments, freq);}
397     }
398     throw new Error("Missing return statement in function");
399   }
400 
401 /**
402  * Read trigger (list of senses) and return it.
403  * <p/>
404  * <code>( NIL | "(" <trigger> | ) </code>
405  * Part of driveElement rule
406  */
407   final public List<Sense> driveElementTrigger(FormalParameters formalParameters) throws ParseException {
408     List<Sense> senses;
409     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
410     case NIL:
411       jj_consume_token(NIL);
412       {if (true) return Collections.<Sense>emptyList();}
413       break;
414     case LBRACE:
415       jj_consume_token(LBRACE);
416       senses = trigger(formalParameters);
417       {if (true) return senses;}
418       break;
419     default:
420       jj_la1[9] = jj_gen;
421       {if (true) return Collections.<Sense>emptyList();}
422     }
423     throw new Error("Missing return statement in function");
424   }
425 
426 /**
427  * Get specified frequency. If no frequency specified, return {@link Freq#INFINITE}.
428  * ( NIL | "(" <freq> | )
429  * Part of driveElementRule
430  */
431   final public Freq driveElementFreq() throws ParseException {
432     Freq _freq;
433     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
434     case NIL:
435       jj_consume_token(NIL);
436       {if (true) return Freq.INFINITE;}
437       break;
438     case LBRACE:
439       jj_consume_token(LBRACE);
440       _freq = freq();
441      {if (true) return _freq;}
442       break;
443     default:
444       jj_la1[10] = jj_gen;
445      {if (true) return Freq.INFINITE;}
446     }
447     throw new Error("Missing return statement in function");
448   }
449 
450 /**
451  * competence ::= C NAME ( NIL | "(" <time> | ) ( NIL | "(" <goal> | ) "(" ELEMENTS <competence-priorities> ")" <opt-comment> ")" 
452  */
453   final public Competence competence() throws ParseException {
454         Token _name;
455         FormalParameters params = new FormalParameters();
456         List<CompetenceElement> competenceElements;
457         String _comment="";
458     jj_consume_token(COMPETENCE);
459     _name = jj_consume_token(NAME);
460     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
461     case VARS:
462       params = parameters();
463       break;
464     default:
465       jj_la1[11] = jj_gen;
466       ;
467     }
468     jj_consume_token(LBRACE);
469     jj_consume_token(ELEMENTS);
470     competenceElements = competencePriorities(params);
471     jj_consume_token(RBRACE);
472     _comment = optComment();
473     jj_consume_token(RBRACE);
474                 {if (true) return new Competence(_name.toString(), params, competenceElements);}
475     throw new Error("Missing return statement in function");
476   }
477 
478 /**
479  * Declare parameters in C or AP.
480  * parameters:: "vars" "(" varDeclaration() ( "," varDeclaration() )* ")"
481  */
482   final public FormalParameters parameters() throws ParseException {
483     FormalParameters.Parameter param = null;
484     FormalParameters params = new FormalParameters();
485     jj_consume_token(VARS);
486     jj_consume_token(LBRACE);
487     param = varDeclaration();
488           params.add(param);
489     label_3:
490     while (true) {
491       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
492       case COMMA:
493         ;
494         break;
495       default:
496         jj_la1[12] = jj_gen;
497         break label_3;
498       }
499       jj_consume_token(COMMA);
500       param = varDeclaration();
501           try {
502             params.add(param);
503           } catch (IllegalArgumentException ex) {
504             ParseException parseException = new ParseException(ex.getMessage());
505             parseException.currentToken = token;
506             {if (true) throw parseException;}
507           }
508     }
509     jj_consume_token(RBRACE);
510          {if (true) return params;}
511     throw new Error("Missing return statement in function");
512   }
513 
514 /**
515  * varDeclare:: <VARIABLE> "=" value()
516  */
517   final public FormalParameters.Parameter varDeclaration() throws ParseException {
518     Token variable = null;
519     Object defaultValue = null;
520     variable = jj_consume_token(VARIABLE);
521     jj_consume_token(EQUAL_SIGN);
522     defaultValue = value();
523                                                    {if (true) return new FormalParameters.Parameter(variable.toString(), defaultValue);}
524     throw new Error("Missing return statement in function");
525   }
526 
527 /**
528  * ( NIL | "(" <time> | ) ( NIL | "(" <goal> | ) "(" ELEMENTS
529  * Part of competence() rule, Really ugly, but it has to check 8 combinations.
530  */
531 /*
532 TimeGoal competenceTimeGoal(FormalParameters formalParameters):
533 {
534 	TimeGoal tg = new TimeGoal();
535 }
536 {
537     <NIL> tg.goal=competenceGoal(formalParameters) { return tg; }
538 |
539     "(" ( 
540 			 		tg.time=time()   tg.goal=competenceGoal(formalParameters) { return tg;} 
541 			 | 
542 					<ELEMENTS> { return tg;}
543 		   | 
544 			    tg.goal=goal(formalParameters) "(" <ELEMENTS> { return tg;}
545 			 	 )
546 }*/
547 
548 /**
549  * ( NIL | "(" <goal> | ) "(" ELEMENTS
550  * Part of competence() rule
551  */
552   final public List<Sense> competenceGoal(FormalParameters formalParameters) throws ParseException {
553     List<Sense> goalSenses;
554     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
555     case NIL:
556       jj_consume_token(NIL);
557       jj_consume_token(LBRACE);
558       jj_consume_token(ELEMENTS);
559                           {if (true) return Collections.<Sense>emptyList();}
560       break;
561     case LBRACE:
562       jj_consume_token(LBRACE);
563       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
564       case GOAL:
565         goalSenses = goal(formalParameters);
566         break;
567       case ELEMENTS:
568         jj_consume_token(ELEMENTS);
569                                                           {if (true) return Collections.<Sense>emptyList();}
570         break;
571       default:
572         jj_la1[13] = jj_gen;
573         jj_consume_token(-1);
574         throw new ParseException();
575       }
576       jj_consume_token(LBRACE);
577       jj_consume_token(ELEMENTS);
578                                                                                                                     {if (true) return goalSenses;}
579       break;
580     default:
581       jj_la1[14] = jj_gen;
582       jj_consume_token(-1);
583       throw new ParseException();
584     }
585     throw new Error("Missing return statement in function");
586   }
587 
588 /**
589  * competence-priorities ::= <competence-elements>+
590  * competence-elements ::= "(" <competence-element>+ ")"
591  *
592  * => competence-priorities ::= ( "(" <competence-element>+ ")" )+.
593  */
594   final public List<CompetenceElement> competencePriorities(FormalParameters formalParameters) throws ParseException {
595     CompetenceElement ce;
596     List<CompetenceElement> result = new LinkedList<CompetenceElement>();
597     label_4:
598     while (true) {
599       jj_consume_token(LBRACE);
600       label_5:
601       while (true) {
602         ce = competenceElement(formalParameters);
603                     result.add(ce);
604         switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
605         case LBRACE:
606           ;
607           break;
608         default:
609           jj_la1[15] = jj_gen;
610           break label_5;
611         }
612       }
613       jj_consume_token(RBRACE);
614       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
615       case LBRACE:
616         ;
617         break;
618       default:
619         jj_la1[16] = jj_gen;
620         break label_4;
621       }
622     }
623         {if (true) return result;}
624     throw new Error("Missing return statement in function");
625   }
626 
627 /**
628  * competence-element ::= "(" NAME [ "(" <trigger> ] NAME [ INTNUM ] <opt-comment> ")"
629  */
630   final public CompetenceElement competenceElement(FormalParameters formalParameters) throws ParseException {
631         CompetenceElement _element;
632         Token name= null;
633         int retries;
634         PrimitiveCall actionCall = null;
635         List<Sense> triggerSenses = Collections.<Sense>emptyList();
636         String comment;
637     jj_consume_token(LBRACE);
638     name = jj_consume_token(NAME);
639     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
640     case LBRACE:
641       jj_consume_token(LBRACE);
642       triggerSenses = trigger(formalParameters);
643       break;
644     default:
645       jj_la1[17] = jj_gen;
646       ;
647     }
648     actionCall = senseCall(formalParameters);
649     retries = optRetries();
650     comment = optComment();
651     jj_consume_token(RBRACE);
652         {if (true) return new CompetenceElement(name.toString(), triggerSenses, actionCall, retries, comment);}
653     throw new Error("Missing return statement in function");
654   }
655 
656 /**
657  * Optional number of retries. If missing, return {@link CompetenceElement#INFINITE_RETRIES}.
658  */
659   final public int optRetries() throws ParseException {
660     Token retries;
661     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
662     case NUMINT:
663       retries = jj_consume_token(NUMINT);
664                        {if (true) return Integer.parseInt(retries.toString());}
665       break;
666     default:
667       jj_la1[18] = jj_gen;
668      {if (true) return CompetenceElement.INFINITE_RETRIES;}
669     }
670     throw new Error("Missing return statement in function");
671   }
672 
673 /**
674  * Optional comment, if no comment, return blank string.
675  * opt-comment ::= COMMENT | 
676  * @return Comment or if comment is missing, blank string.
677  */
678   final public String optComment() throws ParseException {
679         Token _comment;
680     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
681     case COMMENT:
682       _comment = jj_consume_token(COMMENT);
683                 {if (true) return unescape(_comment.toString());}
684       break;
685     default:
686       jj_la1[19] = jj_gen;
687      {if (true) return "";}
688     }
689     throw new Error("Missing return statement in function");
690   }
691 
692 /**
693  * action-pattern ::= AP NAME ( NIL | "(" <time> | ) "(" <action-pattern-elements> <opt-comment> ")"  
694  */
695   final public ActionPattern actionPattern() throws ParseException {
696     ActionPattern ap;
697     Token name;
698     FormalParameters params = new FormalParameters();
699     String comment;
700     jj_consume_token(AP);
701     name = jj_consume_token(NAME);
702     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
703     case VARS:
704       params = parameters();
705       break;
706     default:
707       jj_la1[20] = jj_gen;
708       ;
709     }
710         ap = LapElementsFactory.createActionPattern(name.toString(), params);
711     jj_consume_token(LBRACE);
712     actionPatternElements(ap, params);
713     comment = optComment();
714                                                                 ap.setComment(comment);
715     jj_consume_token(RBRACE);
716      {if (true) return ap;}
717     throw new Error("Missing return statement in function");
718   }
719 
720 /**
721  * adapt ::= AD NAME [ <parameters> ] "(" "(" <goal> "(" <actionCall> ")"  
722  */
723   final public Adopt adapt() throws ParseException {
724     Token name;
725     FormalParameters params = new FormalParameters();
726     List<Sense> exitCondition = Collections.<Sense>emptyList();
727     PrimitiveCall actionCall = null;
728     jj_consume_token(AD);
729     name = jj_consume_token(NAME);
730     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
731     case VARS:
732       params = parameters();
733       break;
734     default:
735       jj_la1[21] = jj_gen;
736       ;
737     }
738     jj_consume_token(LBRACE);
739     jj_consume_token(LBRACE);
740     exitCondition = goal(params);
741     jj_consume_token(LBRACE);
742     actionCall = senseCall(params);
743     jj_consume_token(RBRACE);
744     jj_consume_token(RBRACE);
745     jj_consume_token(RBRACE);
746         {if (true) return new Adopt(name.toString(), params, exitCondition, actionCall);}
747     throw new Error("Missing return statement in function");
748   }
749 
750 /**
751  * process actions of the AP and add them into the passed ap.
752  * action-pattern-elements ::= ( <full-sense> | NAME )+ ")"
753  * FIXME: full-sense is not implemented here, because I have no idea what to do with it.
754  *        I have't even seen it used anywhere so it is doable for now.
755  */
756   final public void actionPatternElements(ActionPattern ap, FormalParameters formalParameters) throws ParseException {
757     List<TriggeredAction> _actList = new LinkedList<TriggeredAction>();
758     PrimitiveCall actionCall;
759     label_6:
760     while (true) {
761       actionCall = senseCall(formalParameters);
762             ap.addAction(LapElementsFactory.createAction(actionCall));
763       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
764       case NAME:
765         ;
766         break;
767       default:
768         jj_la1[22] = jj_gen;
769         break label_6;
770       }
771     }
772     jj_consume_token(RBRACE);
773   }
774 
775 /**
776  * time ::= <time-unit> <numfloat> ")" 
777  */
778   final public SolTime time() throws ParseException {
779      SolTime.TimeUnits _units;
780         Token _ammount;
781     _units = timeUnit();
782     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
783     case NUMFLOAT:
784       _ammount = jj_consume_token(NUMFLOAT);
785       break;
786     case NUMINT:
787       _ammount = jj_consume_token(NUMINT);
788       break;
789     default:
790       jj_la1[23] = jj_gen;
791       jj_consume_token(-1);
792       throw new ParseException();
793     }
794     jj_consume_token(RBRACE);
795                 {if (true) return new SolTime( _units, _ammount.toString() );}
796     throw new Error("Missing return statement in function");
797   }
798 
799 /**
800  * time-unit ::= HOURS | MINUTES | SECONDS | NONE
801  */
802   final public SolTime.TimeUnits timeUnit() throws ParseException {
803     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
804     case HOURS:
805       jj_consume_token(HOURS);
806                {if (true) return SolTime.TimeUnits.HOURS;}
807       break;
808     case MINUTES:
809       jj_consume_token(MINUTES);
810                {if (true) return SolTime.TimeUnits.MINUTES;}
811       break;
812     case SECONDS:
813       jj_consume_token(SECONDS);
814                {if (true) return SolTime.TimeUnits.SECONDS;}
815       break;
816     case NONE:
817       jj_consume_token(NONE);
818                {if (true) return SolTime.TimeUnits.NONE;}
819       break;
820     default:
821       jj_la1[24] = jj_gen;
822       jj_consume_token(-1);
823       throw new ParseException();
824     }
825     throw new Error("Missing return statement in function");
826   }
827 
828 /**
829  * freq ::= <freq-unit> <numfloat> ")"
830  */
831   final public Freq freq() throws ParseException {
832     Freq.FreqUnits _units;
833         Token _count;
834     _units = freqUnit();
835     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
836     case NUMFLOAT:
837       _count = jj_consume_token(NUMFLOAT);
838       break;
839     case NUMINT:
840       _count = jj_consume_token(NUMINT);
841       break;
842     default:
843       jj_la1[25] = jj_gen;
844       jj_consume_token(-1);
845       throw new ParseException();
846     }
847     jj_consume_token(RBRACE);
848                 {if (true) return new Freq(Double.parseDouble(_count.toString()), _units);}
849     throw new Error("Missing return statement in function");
850   }
851 
852 /**
853  *  freq-unit ::= HOURS | MINUTES | SECONDS | HZ | PM | NONE
854  */
855   final public Freq.FreqUnits freqUnit() throws ParseException {
856     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
857     case HOURS:
858       jj_consume_token(HOURS);
859               {if (true) return Freq.FreqUnits.HOURS;}
860       break;
861     case MINUTES:
862       jj_consume_token(MINUTES);
863                 {if (true) return Freq.FreqUnits.MINUTES;}
864       break;
865     case SECONDS:
866       jj_consume_token(SECONDS);
867                 {if (true) return Freq.FreqUnits.SECONDS;}
868       break;
869     case HZ:
870       jj_consume_token(HZ);
871            {if (true) return Freq.FreqUnits.HZ;}
872       break;
873     case PM:
874       jj_consume_token(PM);
875            {if (true) return Freq.FreqUnits.PM;}
876       break;
877     case NONE:
878       jj_consume_token(NONE);
879              {if (true) return Freq.FreqUnits.NONE;}
880       break;
881     default:
882       jj_la1[26] = jj_gen;
883       jj_consume_token(-1);
884       throw new ParseException();
885     }
886     throw new Error("Missing return statement in function");
887   }
888 
889 /**
890  * Get list of senses of a trigger.
891  * trigger ::= <TRIGGER> <senses> ")" 
892  */
893   final public List<Sense> trigger(FormalParameters formalParameters) throws ParseException {
894         List<Sense> triggerSenses;
895     jj_consume_token(TRIGGER);
896     triggerSenses = senses(formalParameters);
897     jj_consume_token(RBRACE);
898           {if (true) return triggerSenses;}
899     throw new Error("Missing return statement in function");
900   }
901 
902 /**
903  * Get senses (if there are any) and return them.
904  * senses ::= ( NIL | "(" ( NAME | <full-sense> )+ ")" )
905  */
906   final public List<Sense> senses(FormalParameters formalParameters) throws ParseException {
907     List<Sense> senses;
908     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
909     case NIL:
910       jj_consume_token(NIL);
911      {if (true) return Collections.<Sense>emptyList();}
912       break;
913     case LBRACE:
914       jj_consume_token(LBRACE);
915       senses = sensesList(formalParameters);
916       jj_consume_token(RBRACE);
917      {if (true) return senses;}
918       break;
919     default:
920       jj_la1[27] = jj_gen;
921       jj_consume_token(-1);
922       throw new ParseException();
923     }
924     throw new Error("Missing return statement in function");
925   }
926 
927 /**
928  * ( NAME | <full-sense> )+ 
929  * Part of sense() rule.
930  */
931   final public List<Sense> sensesList(FormalParameters formalParameters) throws ParseException {
932     Sense sense;
933     List<Sense> sensesList = new LinkedList<Sense>();
934     label_7:
935     while (true) {
936       sense = sensesListElement(formalParameters);
937           sensesList.add(sense);
938       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
939       case LBRACE:
940       case NAME:
941         ;
942         break;
943       default:
944         jj_la1[28] = jj_gen;
945         break label_7;
946       }
947     }
948       {if (true) return sensesList;}
949     throw new Error("Missing return statement in function");
950   }
951 
952 /**
953  * NAME | <full-sense>
954  * Part of sensesList() rule
955  * XXX: I am using <NAME> in first case, so it is impossible to have (sense1(sense2)) vs (sense1(simple_string))
956  *      because the calling vars are not compulsory, yet   
957  */
958   final public Sense sensesListElement(FormalParameters formalParameters) throws ParseException {
959     Token senseName;
960     Sense _sense;
961     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
962     case NAME:
963       senseName = jj_consume_token(NAME);
964       {if (true) return new Sense(senseName.toString());}
965       break;
966     case LBRACE:
967       _sense = fullSense(formalParameters);
968       {if (true) return _sense;}
969       break;
970     default:
971       jj_la1[29] = jj_gen;
972       jj_consume_token(-1);
973       throw new ParseException();
974     }
975     throw new Error("Missing return statement in function");
976   }
977 
978 /**
979  * full-sense ::= "(" NAME [<value> [<predicate>]] ")"
980  */
981   final public Sense fullSense(FormalParameters formalParameters) throws ParseException {
982     SenseComponents _senseComp = new SenseComponents();
983     PrimitiveCall senseCall;
984     jj_consume_token(LBRACE);
985     senseCall = senseCall(formalParameters);
986     senseValue(_senseComp);
987         // didn't assign
988         if (_senseComp.value == null) {
989             {if (true) return new Sense(senseCall);}
990         } else {
991             {if (true) return new Sense( senseCall, _senseComp.value, _senseComp.predicate == null
992                 ? cz.cuni.amis.pogamut.sposh.elements.Sense.Predicate.DEFAULT
993                 : _senseComp.predicate);}
994         }
995     throw new Error("Missing return statement in function");
996   }
997 
998 /**
999  * SenseCall
1000  * sense:: <NAME> [ "(" callParametersList() ]
1001  */
1002   final public PrimitiveCall senseCall(FormalParameters formalParameters) throws ParseException {
1003     Arguments arguments = new Arguments();
1004     Token name;
1005     name = jj_consume_token(NAME);
1006     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
1007     case LBRACE:
1008       jj_consume_token(LBRACE);
1009       arguments = callParametersList(formalParameters);
1010       break;
1011     default:
1012       jj_la1[30] = jj_gen;
1013       ;
1014     }
1015             {if (true) return new PrimitiveCall(name.toString(), arguments);}
1016     throw new Error("Missing return statement in function");
1017   }
1018 
1019 /**
1020  * callParametersList:: callParameter() ( "," callParameter() )* ")" 
1021  */
1022   final public Arguments callParametersList(FormalParameters formalParameters) throws ParseException {
1023     int index=0;
1024     Arguments arguments = new Arguments();
1025     Packet<Arguments.Argument, Boolean> packet = null;
1026     boolean named = false;
1027     packet = callParameter(index, named);
1028             named = packet.second;
1029             try {
1030                 arguments.addFormal(packet.first, formalParameters);
1031                 ++index;
1032             } catch (IllegalArgumentException ex) {
1033                 ParseException parseEx = new ParseException(ex.getMessage());
1034                 parseEx.currentToken = token;
1035                 {if (true) throw parseEx;}
1036             }
1037     label_8:
1038     while (true) {
1039       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
1040       case COMMA:
1041         ;
1042         break;
1043       default:
1044         jj_la1[31] = jj_gen;
1045         break label_8;
1046       }
1047       jj_consume_token(COMMA);
1048       packet = callParameter(index, named);
1049             named = packet.second;
1050             try {
1051                 arguments.addFormal(packet.first, formalParameters);
1052                 ++index;
1053             } catch (IllegalArgumentException ex) {
1054                 ParseException parseEx = new ParseException(ex.getMessage());
1055                 parseEx.currentToken = token;
1056                 {if (true) throw parseEx;}
1057             }
1058     }
1059     jj_consume_token(RBRACE);
1060         {if (true) return arguments;}
1061     throw new Error("Missing return statement in function");
1062   }
1063 
1064 /**
1065  * Parameter used when calling C/AP/sense/action
1066  * callParameter :: <VARIABLE> [ "=" ( <VARIABLE> | value() ) ]
1067  * @param named was at least one named call parameter specified in the list before this one?     
1068  */
1069   final public Packet<Arguments.Argument, Boolean> callParameter(int index, boolean named) throws ParseException {
1070     Token variable = null;
1071     Object value = null;
1072     Token valueVariable = null;
1073     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
1074     case NIL:
1075     case TRUE:
1076     case FALSE:
1077     case NUMFLOAT:
1078     case NUMINT:
1079     case NAME:
1080     case STRINGVALUE:
1081     case COMMENT:
1082       value = value();
1083             if (named) {
1084                 // sonar is complaining about \" transformed into unicode \ 
1085                 char doubleQuote = '"';
1086                 ParseException ex = new ParseException("Specified unnamed parameter " + doubleQuote + index + doubleQuote + " after at least one named parameter.");
1087                 ex.currentToken = token;
1088                 {if (true) throw ex;}
1089             }
1090             {if (true) return new Packet<Arguments.Argument, Boolean>(new Arguments.ValueArgument(index, value), false);}
1091       break;
1092     case VARIABLE:
1093       variable = jj_consume_token(VARIABLE);
1094       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
1095       case EQUAL_SIGN:
1096         jj_consume_token(EQUAL_SIGN);
1097         switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
1098         case VARIABLE:
1099           valueVariable = jj_consume_token(VARIABLE);
1100                 {if (true) return new Packet<Arguments.Argument, Boolean>(
1101                     new Arguments.VariableArgument(variable.toString(), valueVariable.toString()),
1102                     true);}
1103           break;
1104         case NIL:
1105         case TRUE:
1106         case FALSE:
1107         case NUMFLOAT:
1108         case NUMINT:
1109         case NAME:
1110         case STRINGVALUE:
1111         case COMMENT:
1112           value = value();
1113                 {if (true) return new Packet<Arguments.Argument, Boolean>(new Arguments.ValueArgument(variable.toString(), value), true);}
1114           break;
1115         default:
1116           jj_la1[32] = jj_gen;
1117           jj_consume_token(-1);
1118           throw new ParseException();
1119         }
1120         break;
1121       default:
1122         jj_la1[33] = jj_gen;
1123         ;
1124       }
1125             if (named) {
1126                 // sonar is complaining about \" transformed into unicode \ 
1127                 char doubleQuote = '"';
1128                 ParseException ex = new ParseException("Specified unnamed parameter " + doubleQuote + index + doubleQuote + " after at least one named parameter.");
1129                 ex.currentToken = token;
1130                 {if (true) throw ex;}
1131             }
1132             {if (true) return new Packet<Arguments.Argument, Boolean>(new Arguments.VariableArgument(index, variable.toString()), false);}
1133       break;
1134     default:
1135       jj_la1[34] = jj_gen;
1136       jj_consume_token(-1);
1137       throw new ParseException();
1138     }
1139     throw new Error("Missing return statement in function");
1140   }
1141 
1142 /**
1143  * value ::= NUMINT | NUMFLOAT | NAME | STRINGVALUE | NIL
1144  * Part of full-sense parsing.
1145  */
1146   final public void senseValue(SenseComponents _senseComp) throws ParseException {
1147     Object _value;
1148     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
1149     case RBRACE:
1150       jj_consume_token(RBRACE);
1151       break;
1152     case NIL:
1153     case TRUE:
1154     case FALSE:
1155     case NUMFLOAT:
1156     case NUMINT:
1157     case NAME:
1158     case STRINGVALUE:
1159     case COMMENT:
1160       _value = value();
1161       sensePredicate(_senseComp);
1162         _senseComp.value = _value;
1163       break;
1164     default:
1165       jj_la1[35] = jj_gen;
1166       jj_consume_token(-1);
1167       throw new ParseException();
1168     }
1169   }
1170 
1171 /**
1172  * predicate ::= PREDICATE
1173  */
1174   final public void sensePredicate(SenseComponents _senseComp) throws ParseException {
1175     Token _predicateToken;
1176     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
1177     case RBRACE:
1178       jj_consume_token(RBRACE);
1179       break;
1180     case PREDICATE:
1181       _predicateToken = jj_consume_token(PREDICATE);
1182       jj_consume_token(RBRACE);
1183         _senseComp.predicate = Sense.Predicate.getPredicate(_predicateToken.toString());
1184       break;
1185     default:
1186       jj_la1[36] = jj_gen;
1187       jj_consume_token(-1);
1188       throw new ParseException();
1189     }
1190   }
1191 
1192   final public Object value() throws ParseException {
1193     Token _value;
1194     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
1195     case NUMINT:
1196       _value = jj_consume_token(NUMINT);
1197         {if (true) return Integer.parseInt(_value.toString());}
1198       break;
1199     case NUMFLOAT:
1200       _value = jj_consume_token(NUMFLOAT);
1201         {if (true) return Double.parseDouble(_value.toString());}
1202       break;
1203     case TRUE:
1204       _value = jj_consume_token(TRUE);
1205         {if (true) return Boolean.TRUE;}
1206       break;
1207     case FALSE:
1208       _value = jj_consume_token(FALSE);
1209         {if (true) return Boolean.FALSE;}
1210       break;
1211     case NAME:
1212       _value = jj_consume_token(NAME);
1213         {if (true) return _value.toString();}
1214       break;
1215     case STRINGVALUE:
1216       _value = jj_consume_token(STRINGVALUE);
1217         {if (true) return _value.toString();}
1218       break;
1219     case NIL:
1220       _value = jj_consume_token(NIL);
1221         {if (true) return null;}
1222       break;
1223     case COMMENT:
1224       _value = jj_consume_token(COMMENT);
1225         {if (true) return unescape(_value.toString());}
1226       break;
1227     default:
1228       jj_la1[37] = jj_gen;
1229       jj_consume_token(-1);
1230       throw new ParseException();
1231     }
1232     throw new Error("Missing return statement in function");
1233   }
1234 
1235   /** Generated Token Manager. */
1236   public PoshParserTokenManager token_source;
1237   SimpleCharStream jj_input_stream;
1238   /** Current token. */
1239   public Token token;
1240   /** Next token. */
1241   public Token jj_nt;
1242   private int jj_ntk;
1243   private int jj_gen;
1244   final private int[] jj_la1 = new int[38];
1245   static private int[] jj_la1_0;
1246   static private int[] jj_la1_1;
1247   static {
1248       jj_la1_init_0();
1249       jj_la1_init_1();
1250    }
1251    private static void jj_la1_init_0() {
1252       jj_la1_0 = new int[] {0x800f00,0xf00,0xc0,0xe00,0x40,0x40,0x402000,0xd82fc000,0x200040,0x200040,0x200040,0x1000000,0x2000000,0x3000,0x200040,0x40,0x40,0x40,0x80000000,0x0,0x1000000,0x1000000,0x0,0xc0000000,0x9c000,0xc0000000,0xfc000,0x200040,0x40,0x40,0x40,0x2000000,0xd8200000,0x4000000,0xd8200000,0xd8200080,0x20000080,0xd8200000,};
1253    }
1254    private static void jj_la1_init_1() {
1255       jj_la1_1 = new int[] {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xf,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x4,0x0,0x0,0x1,0x0,0x0,0x0,0x0,0x0,0x1,0x1,0x0,0x0,0xf,0x0,0xf,0x7,0x0,0x7,};
1256    }
1257 
1258   /** Constructor with InputStream. */
1259   public PoshParser(java.io.InputStream stream) {
1260      this(stream, null);
1261   }
1262   /** Constructor with InputStream and supplied encoding */
1263   public PoshParser(java.io.InputStream stream, String encoding) {
1264     try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
1265     token_source = new PoshParserTokenManager(jj_input_stream);
1266     token = new Token();
1267     jj_ntk = -1;
1268     jj_gen = 0;
1269     for (int i = 0; i < 38; i++) jj_la1[i] = -1;
1270   }
1271 
1272   /** Reinitialise. */
1273   public void ReInit(java.io.InputStream stream) {
1274      ReInit(stream, null);
1275   }
1276   /** Reinitialise. */
1277   public void ReInit(java.io.InputStream stream, String encoding) {
1278     try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
1279     token_source.ReInit(jj_input_stream);
1280     token = new Token();
1281     jj_ntk = -1;
1282     jj_gen = 0;
1283     for (int i = 0; i < 38; i++) jj_la1[i] = -1;
1284   }
1285 
1286   /** Constructor. */
1287   public PoshParser(java.io.Reader stream) {
1288     jj_input_stream = new SimpleCharStream(stream, 1, 1);
1289     token_source = new PoshParserTokenManager(jj_input_stream);
1290     token = new Token();
1291     jj_ntk = -1;
1292     jj_gen = 0;
1293     for (int i = 0; i < 38; i++) jj_la1[i] = -1;
1294   }
1295 
1296   /** Reinitialise. */
1297   public void ReInit(java.io.Reader stream) {
1298     jj_input_stream.ReInit(stream, 1, 1);
1299     token_source.ReInit(jj_input_stream);
1300     token = new Token();
1301     jj_ntk = -1;
1302     jj_gen = 0;
1303     for (int i = 0; i < 38; i++) jj_la1[i] = -1;
1304   }
1305 
1306   /** Constructor with generated Token Manager. */
1307   public PoshParser(PoshParserTokenManager tm) {
1308     token_source = tm;
1309     token = new Token();
1310     jj_ntk = -1;
1311     jj_gen = 0;
1312     for (int i = 0; i < 38; i++) jj_la1[i] = -1;
1313   }
1314 
1315   /** Reinitialise. */
1316   public void ReInit(PoshParserTokenManager tm) {
1317     token_source = tm;
1318     token = new Token();
1319     jj_ntk = -1;
1320     jj_gen = 0;
1321     for (int i = 0; i < 38; i++) jj_la1[i] = -1;
1322   }
1323 
1324   private Token jj_consume_token(int kind) throws ParseException {
1325     Token oldToken;
1326     if ((oldToken = token).next != null) token = token.next;
1327     else token = token.next = token_source.getNextToken();
1328     jj_ntk = -1;
1329     if (token.kind == kind) {
1330       jj_gen++;
1331       return token;
1332     }
1333     token = oldToken;
1334     jj_kind = kind;
1335     throw generateParseException();
1336   }
1337 
1338 
1339 /** Get the next Token. */
1340   final public Token getNextToken() {
1341     if (token.next != null) token = token.next;
1342     else token = token.next = token_source.getNextToken();
1343     jj_ntk = -1;
1344     jj_gen++;
1345     return token;
1346   }
1347 
1348 /** Get the specific Token. */
1349   final public Token getToken(int index) {
1350     Token t = token;
1351     for (int i = 0; i < index; i++) {
1352       if (t.next != null) t = t.next;
1353       else t = t.next = token_source.getNextToken();
1354     }
1355     return t;
1356   }
1357 
1358   private int jj_ntk() {
1359     if ((jj_nt=token.next) == null)
1360       return (jj_ntk = (token.next=token_source.getNextToken()).kind);
1361     else
1362       return (jj_ntk = jj_nt.kind);
1363   }
1364 
1365   private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
1366   private int[] jj_expentry;
1367   private int jj_kind = -1;
1368 
1369   /** Generate ParseException. */
1370   public ParseException generateParseException() {
1371     jj_expentries.clear();
1372     boolean[] la1tokens = new boolean[37];
1373     if (jj_kind >= 0) {
1374       la1tokens[jj_kind] = true;
1375       jj_kind = -1;
1376     }
1377     for (int i = 0; i < 38; i++) {
1378       if (jj_la1[i] == jj_gen) {
1379         for (int j = 0; j < 32; j++) {
1380           if ((jj_la1_0[i] & (1<<j)) != 0) {
1381             la1tokens[j] = true;
1382           }
1383           if ((jj_la1_1[i] & (1<<j)) != 0) {
1384             la1tokens[32+j] = true;
1385           }
1386         }
1387       }
1388     }
1389     for (int i = 0; i < 37; i++) {
1390       if (la1tokens[i]) {
1391         jj_expentry = new int[1];
1392         jj_expentry[0] = i;
1393         jj_expentries.add(jj_expentry);
1394       }
1395     }
1396     int[][] exptokseq = new int[jj_expentries.size()][];
1397     for (int i = 0; i < jj_expentries.size(); i++) {
1398       exptokseq[i] = jj_expentries.get(i);
1399     }
1400     return new ParseException(token, exptokseq, tokenImage);
1401   }
1402 
1403   /** Enable tracing. */
1404   final public void enable_tracing() {
1405   }
1406 
1407   /** Disable tracing. */
1408   final public void disable_tracing() {
1409   }
1410 
1411 }
1412 
1413 final class SenseComponents {
1414     public String name;
1415     public Object value;
1416     public Sense.Predicate predicate;
1417 }