1   /* Generated By:JavaCC: Do not edit this line. ParseCpsl.java */
2   package gate.jape.parser;
3   
4   import java.io.*;
5   import java.net.*;
6   import java.util.*;
7   import gate.util.*;
8   import gate.*;
9   import gate.jape.*;
10  import gate.event.*;
11  
12  
13  /**
14    * A parser for the CPSL language. Generated using JavaCC.
15    * @author Hamish Cunningham
16    */
17  public class ParseCpsl implements JapeConstants, ParseCpslConstants {
18  
19    /** Construct from a URL and an encoding
20      */
21    public ParseCpsl(URL url, String encoding) throws IOException {
22      this(url, encoding, new HashMap());
23    }
24  
25    /** Construct from a URL and an encoding
26      */
27    public ParseCpsl(URL url, String encoding, HashMap existingMacros) throws IOException {
28      this(new InputStreamReader(
29             new BufferedInputStream(url.openStream()),
30             encoding));
31      macrosMap = existingMacros;
32      baseURL = url;
33      this.encoding = encoding;
34    }
35  
36    //StatusReporter Implementation
37    public void addStatusListener(StatusListener listener){
38      myStatusListeners.add(listener);
39    }
40    public void removeStatusListener(StatusListener listener){
41      myStatusListeners.remove(listener);
42    }
43    protected void fireStatusChangedEvent(String text){
44      java.util.Iterator listenersIter = myStatusListeners.iterator();
45      while(listenersIter.hasNext())
46        ((StatusListener)listenersIter.next()).statusChanged(text);
47    }
48  
49    private transient java.util.List myStatusListeners = new java.util.LinkedList();
50  
51    /** Position of the current rule */
52    private int ruleNumber;
53  
54    /** A list of all the bindings we made this time, for checking
55      * the RHS during parsing.
56      */
57    private HashSet bindingNameSet = null;
58  
59    /** A table of macro definitions. */
60    private HashMap macrosMap;
61  
62    URL baseURL;
63    String encoding;
64  
65  //////////////
66  // the grammar
67  //////////////
68    final public MultiPhaseTransducer MultiPhaseTransducer() throws ParseException {
69    // macrosMap = new HashMap();
70    SinglePhaseTransducer s = null;
71    MultiPhaseTransducer m = new MultiPhaseTransducer();
72    m.setBaseURL(baseURL);
73    Token mptNameTok = null;
74    Token phaseNameTok = null;
75      switch (jj_nt.kind) {
76      case multiphase:
77        jj_consume_token(multiphase);
78        mptNameTok = jj_consume_token(ident);
79        m.setName(mptNameTok.image);
80        break;
81      default:
82        jj_la1[0] = jj_gen;
83        ;
84      }
85      switch (jj_nt.kind) {
86      case phase:
87        label_1:
88        while (true) {
89          s = SinglePhaseTransducer();
90        m.addPhase(s.getName(), s);
91        s.setBaseURL(baseURL);
92          switch (jj_nt.kind) {
93          case phase:
94            ;
95            break;
96          default:
97            jj_la1[1] = jj_gen;
98            break label_1;
99          }
100       }
101       break;
102     case phases:
103       jj_consume_token(phases);
104       label_2:
105       while (true) {
106         phaseNameTok = jj_consume_token(path);
107           ParseCpsl parser = null;
108 
109             // check file exists
110             String sptPath = phaseNameTok.image + ".jape";
111             URL sptURL = null;
112             try{
113               sptURL = new URL(baseURL, sptPath);
114             }catch(MalformedURLException mue){
115               {if (true) throw(new ParseException("Read error " + mue.toString()));}
116             }
117 
118             if(sptURL == null){
119               {if (true) throw(new ParseException(
120                 "Resource not found: base = " + baseURL.toString() +
121                 " path = " + sptPath
122               ));}
123             }
124 
125             // construct a parser and parse it
126             fireStatusChangedEvent("Reading " + phaseNameTok.image + "...");
127             try {
128               parser = new ParseCpsl(sptURL, encoding, macrosMap);
129             } catch (IOException e) {
130               {if (true) throw(
131                 new ParseException(
132                   "Cannot open URL " + sptURL.toExternalForm()
133                 )
134               );}
135             }
136 
137           // adding the resultant spt to m
138           if(parser != null) {
139             s = parser.SinglePhaseTransducer();
140             if(s != null)
141               m.addPhase(s.getName(), s);
142           }
143         switch (jj_nt.kind) {
144         case path:
145           ;
146           break;
147         default:
148           jj_la1[2] = jj_gen;
149           break label_2;
150         }
151       }
152       break;
153     default:
154       jj_la1[3] = jj_gen;
155       jj_consume_token(-1);
156       throw new ParseException();
157     }
158     jj_consume_token(0);
159 //move this out of here so the input file gets closed properly
160 //    m.finish(); // swap the various JGL types for Java arrays
161     {if (true) return m;}
162     throw new Error("Missing return statement in function");
163   }
164 
165   // MultiPhaseTransducer
166   final public SinglePhaseTransducer SinglePhaseTransducer() throws ParseException {
167   ruleNumber = 0;
168   Token phaseNameTok = null;
169   Token inputTok = null;
170   SinglePhaseTransducer t = null;
171   Rule newRule = null;
172   bindingNameSet = new HashSet();
173   Token optionNameTok = null;
174   Token optionValueTok = null;
175   Object newMacro = null;
176     jj_consume_token(phase);
177     phaseNameTok = jj_consume_token(ident);
178     t = new SinglePhaseTransducer(phaseNameTok.image);
179     switch (jj_nt.kind) {
180     case input:
181       jj_consume_token(input);
182       label_3:
183       while (true) {
184         switch (jj_nt.kind) {
185         case ident:
186           ;
187           break;
188         default:
189           jj_la1[4] = jj_gen;
190           break label_3;
191         }
192         inputTok = jj_consume_token(ident);
193                          t.addInput(inputTok.image);
194       }
195       break;
196     default:
197       jj_la1[5] = jj_gen;
198       ;
199     }
200     switch (jj_nt.kind) {
201     case option:
202       jj_consume_token(option);
203       label_4:
204       while (true) {
205         switch (jj_nt.kind) {
206         case ident:
207           ;
208           break;
209         default:
210           jj_la1[6] = jj_gen;
211           break label_4;
212         }
213         optionNameTok = jj_consume_token(ident);
214         jj_consume_token(assign);
215         switch (jj_nt.kind) {
216         case ident:
217           optionValueTok = jj_consume_token(ident);
218           break;
219         case bool:
220           optionValueTok = jj_consume_token(bool);
221           break;
222         default:
223           jj_la1[7] = jj_gen;
224           jj_consume_token(-1);
225           throw new ParseException();
226         }
227         t.setOption(optionNameTok.image, optionValueTok.image);
228 
229         // control
230         if(optionNameTok.image.equalsIgnoreCase("control")) {
231           if(optionValueTok.image.equalsIgnoreCase("appelt"))
232             t.setRuleApplicationStyle(APPELT_STYLE);
233           else if(optionValueTok.image.equalsIgnoreCase("first"))
234             t.setRuleApplicationStyle(FIRST_STYLE);
235           else if(optionValueTok.image.equalsIgnoreCase("brill"))
236             t.setRuleApplicationStyle(BRILL_STYLE);
237           else if(optionValueTok.image.equalsIgnoreCase("once"))
238             t.setRuleApplicationStyle(ONCE_STYLE);
239           else
240             System.err.println(
241               "ignoring unknown control strategy " + option +
242               " (should be brill, appelt or first)"
243             );
244         } // control
245         else if(optionNameTok.image.equalsIgnoreCase("debug")) {
246           if(optionValueTok.image.equalsIgnoreCase("true") ||
247              optionValueTok.image.equalsIgnoreCase("yes") ||
248              optionValueTok.image.equalsIgnoreCase("y"))
249             t.setDebugMode(true);
250           else t.setDebugMode(false);
251         }
252         else if(optionNameTok.image.equalsIgnoreCase("matchGroup")) {
253           if(optionValueTok.image.equalsIgnoreCase("true") ||
254              optionValueTok.image.equalsIgnoreCase("yes") ||
255              optionValueTok.image.equalsIgnoreCase("y"))
256             t.setMatchGroupMode(true);
257           else t.setMatchGroupMode(false);
258         }
259       }
260       break;
261     default:
262       jj_la1[8] = jj_gen;
263       ;
264     }
265     label_5:
266     while (true) {
267       switch (jj_nt.kind) {
268       case rule:
269       case macro:
270         ;
271         break;
272       default:
273         jj_la1[9] = jj_gen;
274         break label_5;
275       }
276       switch (jj_nt.kind) {
277       case rule:
278         newRule = Rule(phaseNameTok.image);
279                                          t.addRule(newRule);
280         break;
281       case macro:
282         MacroDef();
283         break;
284       default:
285         jj_la1[10] = jj_gen;
286         jj_consume_token(-1);
287         throw new ParseException();
288       }
289     }
290     if(ruleNumber == 0)
291       {if (true) throw(new ParseException("no rules defined in transducer "
292                                + t.getName()));}
293     t.finish(); // swap the various JGL types for Java arrays
294     t.setBaseURL(baseURL);
295     {if (true) return t;}
296     throw new Error("Missing return statement in function");
297   }
298 
299   // SinglePhaseTransducer
300   final public Rule Rule(String phaseName) throws ParseException {
301   Token ruleNameTok = null;
302   String ruleName = null;
303   Token priorityTok = null;
304   int rulePriority = 0;
305   LeftHandSide lhs = null;
306   RightHandSide rhs = null;
307   Rule newRule = null;
308     jj_consume_token(rule);
309     ruleNameTok = jj_consume_token(ident);
310                                ruleName=ruleNameTok.image;
311     switch (jj_nt.kind) {
312     case priority:
313       jj_consume_token(priority);
314       priorityTok = jj_consume_token(integer);
315       try { rulePriority=Integer.parseInt(priorityTok.image); }
316       catch(NumberFormatException e) {
317         System.err.println("bad priority spec(" + priorityTok.image +
318                            "), rule(" + ruleName + ") - treating as 0");
319         rulePriority=0;
320       }
321       break;
322     default:
323       jj_la1[11] = jj_gen;
324       ;
325     }
326     lhs = LeftHandSide();
327     jj_consume_token(58);
328     rhs = RightHandSide(phaseName, ruleName, lhs);
329     try { rhs.createActionClass(); } catch(JapeException e)
330     {
331       /*Debug.pr(
332         this, "ParseCpsl.Rule, FAILED rhs: " + rhs.getActionClassString()
333       );*/
334       {if (true) throw new ParseException("couldn't create rule RHS: " + e.toString());}
335     }
336     /*Debug.pr(this, "ParseCpsl.Rule, done rhs: " + rhs.getActionClassString());*/
337     newRule = new Rule(ruleName, ruleNumber, rulePriority, lhs, rhs);
338     ruleNumber++;
339     {if (true) return newRule;}
340     throw new Error("Missing return statement in function");
341   }
342 
343   // Rule
344   final public void MacroDef() throws ParseException {
345   Token macroNameTok = null;
346   Object body = null;
347     jj_consume_token(macro);
348     macroNameTok = jj_consume_token(ident);
349     if (jj_2_1(2)) {
350       // both blocks and PEs may start with "{"
351           body = PatternElement(null);
352     } else {
353       switch (jj_nt.kind) {
354       case ident:
355       case colon:
356       case leftBrace:
357       case colonplus:
358         body = Action();
359         break;
360       default:
361         jj_la1[12] = jj_gen;
362         jj_consume_token(-1);
363         throw new ParseException();
364       }
365     }
366     macrosMap.put(macroNameTok.image, body);
367   }
368 
369   // MacroDef
370   final public LeftHandSide LeftHandSide() throws ParseException {
371   ConstraintGroup cg = new ConstraintGroup();
372   LeftHandSide lhs = new LeftHandSide(cg);
373     ConstraintGroup(lhs, cg);
374     {if (true) return lhs;}
375     throw new Error("Missing return statement in function");
376   }
377 
378   // LeftHandSide
379 
380 
381 // we pass the lhs down so we can add bindings in CPEs, and the cg
382 // so we can add PEs and create disjunctions here
383   final public void ConstraintGroup(LeftHandSide lhs, ConstraintGroup cg) throws ParseException {
384   PatternElement pat = null;
385     label_6:
386     while (true) {
387       pat = PatternElement(lhs);
388                               cg.addPatternElement(pat);
389       switch (jj_nt.kind) {
390       case string:
391       case ident:
392       case leftBrace:
393       case leftBracket:
394         ;
395         break;
396       default:
397         jj_la1[13] = jj_gen;
398         break label_6;
399       }
400     }
401     label_7:
402     while (true) {
403       switch (jj_nt.kind) {
404       case bar:
405         ;
406         break;
407       default:
408         jj_la1[14] = jj_gen;
409         break label_7;
410       }
411       jj_consume_token(bar);
412             cg.createDisjunction();
413       label_8:
414       while (true) {
415         pat = PatternElement(lhs);
416                                 cg.addPatternElement(pat);
417         switch (jj_nt.kind) {
418         case string:
419         case ident:
420         case leftBrace:
421         case leftBracket:
422           ;
423           break;
424         default:
425           jj_la1[15] = jj_gen;
426           break label_8;
427         }
428       }
429     }
430   }
431 
432   // ConstraintGroup
433   final public PatternElement PatternElement(LeftHandSide lhs) throws ParseException {
434   PatternElement pat = null;
435   Token macroRefTok = null;
436   boolean macroRef = false;
437     switch (jj_nt.kind) {
438     case ident:
439       macroRefTok = jj_consume_token(ident);
440       macroRef = true;
441       Object macro = macrosMap.get(macroRefTok.image);
442       if(macro == null)
443         {if (true) throw(new ParseException("unknown macro name " + macroRefTok.image));}
444       else if(macro instanceof String[])
445         {if (true) throw(
446           new ParseException(
447             "macro " + macroRefTok.image +
448             " references an Action, not a PatternElement"
449           )
450         );}
451       else if(! (macro instanceof PatternElement)) // this should never happen
452         {if (true) throw(
453           new ParseException(
454             "macro " + macroRefTok.image +
455             " doesn't reference a PatternElement!"
456           )
457         );}
458       else { // macro is a pattern element
459         pat = (PatternElement) ((PatternElement) macro).clone();
460       }
461       break;
462     case string:
463     case leftBrace:
464       pat = BasicPatternElement();
465       break;
466     case leftBracket:
467       pat = ComplexPatternElement(lhs);
468       break;
469     default:
470       jj_la1[16] = jj_gen;
471       jj_consume_token(-1);
472       throw new ParseException();
473     }
474     // if its a CPE, make binding into the LHS
475     if(pat instanceof ComplexPatternElement) {
476 
477       String bindingName = ((ComplexPatternElement) pat).getBindingName();
478 
479       if(bindingName != null && lhs != null) {
480 
481         try {
482           lhs.addBinding(
483             bindingName, (ComplexPatternElement) pat, bindingNameSet, macroRef
484           );
485         } catch(JapeException e) {
486           System.err.println(
487             "duplicate binding name " + bindingName +
488             " - ignoring this binding! exception was: " + e.toString()
489           );
490         }
491 
492       } // not null binding or lhs
493     } // its a CPE
494 
495     {if (true) return pat;}
496     throw new Error("Missing return statement in function");
497   }
498 
499   // PatternElement
500   final public BasicPatternElement BasicPatternElement() throws ParseException {
501   Token shortTok = null; // string shorthand token
502   ArrayList constraints = new ArrayList();
503   Token constrTok = null;
504   Constraint c = null;
505   BasicPatternElement bpe = new BasicPatternElement();
506     switch (jj_nt.kind) {
507     case leftBrace:
508       jj_consume_token(leftBrace);
509       c = Constraint();
510                                    bpe.addConstraint(c);
511       label_9:
512       while (true) {
513         switch (jj_nt.kind) {
514         case comma:
515           ;
516           break;
517         default:
518           jj_la1[17] = jj_gen;
519           break label_9;
520         }
521         jj_consume_token(comma);
522         c = Constraint();
523                                  bpe.addConstraint(c);
524       }
525       jj_consume_token(rightBrace);
526       break;
527     case string:
528       // string shorthand
529             shortTok = jj_consume_token(string);
530       System.err.println(
531         "string shorthand not supported yet, ignoring: " + shortTok.image
532       );
533       break;
534     default:
535       jj_la1[18] = jj_gen;
536       jj_consume_token(-1);
537       throw new ParseException();
538     }
539     {if (true) return bpe;}
540     throw new Error("Missing return statement in function");
541   }
542 
543   // BasicPatternElement
544   final public ComplexPatternElement ComplexPatternElement(LeftHandSide lhs) throws ParseException {
545   Token kleeneOpTok = null;
546   Token bindingNameTok = null;
547   ConstraintGroup cg = new ConstraintGroup();
548     jj_consume_token(leftBracket);
549     ConstraintGroup(lhs, cg);
550     jj_consume_token(rightBracket);
551     switch (jj_nt.kind) {
552     case kleeneOp:
553       kleeneOpTok = jj_consume_token(kleeneOp);
554       break;
555     default:
556       jj_la1[19] = jj_gen;
557       ;
558     }
559     switch (jj_nt.kind) {
560     case colon:
561       jj_consume_token(colon);
562       switch (jj_nt.kind) {
563       case ident:
564         bindingNameTok = jj_consume_token(ident);
565         break;
566       case integer:
567         bindingNameTok = jj_consume_token(integer);
568         break;
569       default:
570         jj_la1[20] = jj_gen;
571         jj_consume_token(-1);
572         throw new ParseException();
573       }
574       break;
575     default:
576       jj_la1[21] = jj_gen;
577       ;
578     }
579     int kleeneOp = NO_KLEENE_OP;
580     if(kleeneOpTok != null) {
581       String k = kleeneOpTok.image;
582       if(k.equals("*"))         kleeneOp = KLEENE_STAR;
583       else if(k.equals("?"))    kleeneOp = KLEENE_QUERY;
584       else if(k.equals("+"))    kleeneOp = KLEENE_PLUS;
585       else
586         System.err.println("ignoring uninterpretable Kleene op " + k);
587     }
588 
589     String bindingName = null;
590     if(bindingNameTok != null)
591       bindingName = bindingNameTok.image;
592     {if (true) return new ComplexPatternElement(cg, kleeneOp, bindingName);}
593     throw new Error("Missing return statement in function");
594   }
595 
596   // ComplexPatternElement
597   final public Constraint Constraint() throws ParseException {
598   Token annotTypeTok = null;
599   Token attrNameTok = null;
600   Object attrValObj = null;
601   Pair attrValPair = null;
602   boolean negate = false;
603   Constraint c = null;
604     switch (jj_nt.kind) {
605     case pling:
606       jj_consume_token(pling);
607              negate = true;
608       break;
609     default:
610       jj_la1[22] = jj_gen;
611       ;
612     }
613     // the annotation type
614       annotTypeTok = jj_consume_token(ident);
615     c = new Constraint(annotTypeTok.image);
616     if(negate) c.negate();
617     switch (jj_nt.kind) {
618     case period:
619       jj_consume_token(period);
620       attrNameTok = jj_consume_token(ident);
621       jj_consume_token(equals);
622       attrValPair = AttrVal();
623       attrValObj = attrValPair.second;
624       c.addAttribute(
625         new JdmAttribute(attrNameTok.image, attrValObj)
626       );
627       break;
628     default:
629       jj_la1[23] = jj_gen;
630       ;
631     }
632     {if (true) return c;}
633     throw new Error("Missing return statement in function");
634   }
635 
636   // Constraint
637 
638 
639 // attribute values: strings, identifers (=strings), integers, floats,
640 //                   booleans
641   final public Pair AttrVal() throws ParseException {
642   Token attrValTok = null;
643   Pair val = new Pair();
644     switch (jj_nt.kind) {
645     case string:
646       attrValTok = jj_consume_token(string);
647       break;
648     case ident:
649       attrValTok = jj_consume_token(ident);
650       break;
651     case integer:
652       attrValTok = jj_consume_token(integer);
653       break;
654     case floatingPoint:
655       attrValTok = jj_consume_token(floatingPoint);
656       break;
657     case bool:
658       attrValTok = jj_consume_token(bool);
659       break;
660     default:
661       jj_la1[24] = jj_gen;
662       jj_consume_token(-1);
663       throw new ParseException();
664     }
665     val.first = new Integer(attrValTok.kind);
666 
667     switch(attrValTok.kind) {
668       case string:
669         // strip the quotes
670         val.second
671           = attrValTok.image.substring(1, attrValTok.image.length() - 1);
672         break;
673       case integer:
674         try {
675           val.second = Long.valueOf(attrValTok.image);
676         } catch(NumberFormatException e) {
677           System.err.println("couldn't parse integer " +
678                              attrValTok.image + " - treating as 0");
679           val.second = new Long(0);
680         }
681         break;
682       case ident:
683         val.second = new String(attrValTok.image);
684         break;
685       case bool:
686         val.second = Boolean.valueOf(attrValTok.image);
687         break;
688       case floatingPoint:
689         try {
690           val.second = Double.valueOf(attrValTok.image);
691         } catch(NumberFormatException e) {
692           System.err.println("couldn't parse float " +
693                              attrValTok.image + " - treating as 0.0");
694           val.second = new Double(0.0);
695         }
696         break;
697       default:
698         System.err.println(
699           "didn't understand type of " + attrValTok.image + ": ignoring"
700         );
701         val.second = new String("");
702         break;
703     } // switch
704 
705     {if (true) return val;}
706     throw new Error("Missing return statement in function");
707   }
708 
709   final public RightHandSide RightHandSide(String phaseName, String ruleName, LeftHandSide lhs) throws ParseException {
710   String[] block = new String[2];
711   RightHandSide rhs = new RightHandSide(phaseName, ruleName, lhs);
712     block = Action();
713     // did we get a non-existent block name?
714     if(block[0] != null)
715       if(! bindingNameSet.contains(block[0])) {
716         {if (true) throw(new ParseException("unknown label in RHS action: " + block[0]));}
717       }
718     rhs.addBlock(block[0], block[1]);
719     label_10:
720     while (true) {
721       switch (jj_nt.kind) {
722       case comma:
723         ;
724         break;
725       default:
726         jj_la1[25] = jj_gen;
727         break label_10;
728       }
729       jj_consume_token(comma);
730       block = Action();
731       // did we get a non-existent block name?
732       if(block[0] != null)
733         if(! bindingNameSet.contains(block[0])) {
734           {if (true) throw(new ParseException("unknown label in RHS action: " + block[0]));}
735         }
736       rhs.addBlock(block[0], block[1]);
737     }
738     {if (true) return rhs;} /* action class not created yet */
739 
740     throw new Error("Missing return statement in function");
741   }
742 
743   // RightHandSide
744 
745 
746 // actions return 2 strings, one for the name of the block, and
747 // one for the block itself. if the name is null, it is an anonymous block
748   final public String[] Action() throws ParseException {
749   String[] block = new String[2];
750   Token macroRefTok = null;
751     if (jj_2_2(3)) {
752       block = NamedJavaBlock();
753     } else {
754       switch (jj_nt.kind) {
755       case leftBrace:
756         block = AnonymousJavaBlock();
757         break;
758       case colon:
759       case colonplus:
760         block = AssignmentExpression();
761         break;
762       case ident:
763         macroRefTok = jj_consume_token(ident);
764       Object macro = macrosMap.get(macroRefTok.image);
765       if(macro == null)
766         {if (true) throw(new ParseException("unknown macro name " + macroRefTok.image));}
767       else if(macro instanceof PatternElement)
768         {if (true) throw(
769           new ParseException(
770             "macro " + macroRefTok.image +
771             " references a PatternElement, not an Action"
772           )
773         );}
774       else if(! (macro instanceof String[])) // this should never happen
775         {if (true) throw(
776           new ParseException(
777             "macro " + macroRefTok.image + " doesn't reference an Action!"
778           )
779         );}
780       else { // macro is an action
781         block = (String[]) macro;
782       }
783         break;
784       default:
785         jj_la1[26] = jj_gen;
786         jj_consume_token(-1);
787         throw new ParseException();
788       }
789     }
790     {if (true) return block;}
791     throw new Error("Missing return statement in function");
792   }
793 
794   // Action
795   final public String[] NamedJavaBlock() throws ParseException {
796   String[] block = new String[2];
797   Token nameTok = null;
798     jj_consume_token(colon);
799     nameTok = jj_consume_token(ident);
800                             block[0] = nameTok.image;
801     jj_consume_token(leftBrace);
802     block[1] = ConsumeBlock();
803     {if (true) return block;}
804     throw new Error("Missing return statement in function");
805   }
806 
807   // NamedJavaBlock
808   final public String[] AnonymousJavaBlock() throws ParseException {
809   String[] block = new String[2];
810   block[0] = null;
811     jj_consume_token(leftBrace);
812     block[1] = ConsumeBlock();
813     {if (true) return block;}
814     throw new Error("Missing return statement in function");
815   }
816 
817   // AnonymousJavaBlock
818   final public String[] AssignmentExpression() throws ParseException {
819   String[] block = new String[2];
820   StringBuffer blockBuffer = new StringBuffer();
821   boolean simpleSpan = true;
822   Token nameTok = null;
823   String newAnnotType = null;
824   String newAttrName = null;
825   String nl = Strings.getNl();
826   String annotSetName = null;
827   Pair attrVal = null;
828   String existingAnnotSetName = null;
829   String existingAnnotType = null;
830   String existingAttrName = null;
831 
832   blockBuffer.append("// RHS assignment block" + nl);
833   blockBuffer.append(
834     "      FeatureMap features = Factory.newFeatureMap();" + nl
835   );
836     switch (jj_nt.kind) {
837     case colon:
838       jj_consume_token(colon);
839               simpleSpan = true;
840       break;
841     case colonplus:
842       jj_consume_token(colonplus);
843       simpleSpan = false;
844       {if (true) throw new
845         ParseException(":+ not a legal operator (no multi-span annots)");}
846       break;
847     default:
848       jj_la1[27] = jj_gen;
849       jj_consume_token(-1);
850       throw new ParseException();
851     }
852     // the name of the bound annotation set we're referencing
853       nameTok = jj_consume_token(ident);
854     block[0] = nameTok.image;
855     annotSetName = block[0] + "Annots";
856     jj_consume_token(period);
857     nameTok = jj_consume_token(ident);
858     newAnnotType = nameTok.image;
859     blockBuffer.append(
860       "      String newAnnotType = \"" + newAnnotType + "\";" + nl
861     );
862 
863     // start of the attribute stuff
864     blockBuffer.append("      Object val = null;" + nl);
865     jj_consume_token(assign);
866     jj_consume_token(leftBrace);
867     label_11:
868     while (true) {
869       switch (jj_nt.kind) {
870       case ident:
871         ;
872         break;
873       default:
874         jj_la1[28] = jj_gen;
875         break label_11;
876       }
877       // the name of the attribute, and equals sign
878           nameTok = jj_consume_token(ident);
879       jj_consume_token(assign);
880                                newAttrName = nameTok.image;
881       switch (jj_nt.kind) {
882       case integer:
883       case string:
884       case bool:
885       case ident:
886       case floatingPoint:
887         // a static attribute value
888               attrVal = AttrVal();
889         switch(((Integer) attrVal.first).intValue()) {
890           case string:
891             blockBuffer.append(
892               "      val = new String(\"" + attrVal.second.toString() +
893               "\");" + nl
894             );
895             break;
896           case integer:
897             blockBuffer.append("      try { " +
898               "val = new Long(" + attrVal.second.toString() + "); }" +
899               nl + "      catch(NumberFormatException e) { }" + nl
900             );
901             break;
902           case ident:
903             blockBuffer.append(
904               "      val = new String(\"" + attrVal.second.toString() +
905               "\");" + nl
906             );
907             break;
908           case bool:
909             blockBuffer.append(
910               "      val = new Boolean(\"" +
911               attrVal.second.toString() + "\");" + nl
912             );
913             break;
914           case floatingPoint:
915             blockBuffer.append("      try { " +
916               "val = new Double(" + attrVal.second.toString() + "); }" + nl +
917               "      catch(NumberFormatException e) { }" + nl
918             );
919             break;
920           default:
921             blockBuffer.append(
922               "      val = new String(\"\");" + nl
923             );
924             break;
925         } // switch
926 
927         blockBuffer.append("      features.put(\"" + newAttrName + "\", val);");
928         blockBuffer.append(nl);
929         break;
930       case colon:
931         jj_consume_token(colon);
932         nameTok = jj_consume_token(ident);
933           existingAnnotSetName = nameTok.image + "ExistingAnnots";
934           if(! bindingNameSet.contains(nameTok.image))
935             {if (true) throw(
936               new ParseException(
937                 "unknown label in RHS action(2): " + nameTok.image
938               )
939             );}
940 
941           blockBuffer.append(
942             "      { // need a block for the existing annot set" + nl +
943             "        AnnotationSet " + existingAnnotSetName +
944             " = (AnnotationSet)bindings.get(\"" + nameTok.image + "\"); " + nl
945           );
946         jj_consume_token(period);
947         nameTok = jj_consume_token(ident);
948                                    existingAnnotType = nameTok.image;
949         jj_consume_token(period);
950         nameTok = jj_consume_token(ident);
951                                    existingAttrName = nameTok.image;
952           blockBuffer.append(
953 "        AnnotationSet existingAnnots = " + nl +
954 "        " + existingAnnotSetName + ".get(\"" + existingAnnotType + "\");" + nl +
955 "        Iterator iter = existingAnnots.iterator();" + nl +
956 "        while(iter.hasNext()) {" + nl +
957 "          Annotation existingA = (Annotation) iter.next();" + nl +
958 "          Object existingFeatureValue = existingA.getFeatures().get(\"" +
959 existingAttrName + "\");" + nl +
960 "          if(existingFeatureValue != null) {" + nl +
961 "            features.put(\"" + existingAttrName + "\", existingFeatureValue);" + nl +
962 "            break;" + nl +
963 "          }" + nl + "        } // while" + nl +
964 "      } // block for existing annots" + nl
965           );
966         break;
967       default:
968         jj_la1[29] = jj_gen;
969         jj_consume_token(-1);
970         throw new ParseException();
971       }
972       switch (jj_nt.kind) {
973       case comma:
974         jj_consume_token(comma);
975         break;
976       default:
977         jj_la1[30] = jj_gen;
978         ;
979       }
980     }
981     jj_consume_token(rightBrace);
982     blockBuffer.append("      annotations.add(" + nl);
983     blockBuffer.append("        " + annotSetName + ".firstNode(), ");
984     blockBuffer.append(annotSetName + ".lastNode(), " + nl);
985     blockBuffer.append("        \"" + newAnnotType + "\", features" + nl);
986     blockBuffer.append("      );" + nl);
987     blockBuffer.append("      // end of RHS assignment block");
988     block[1] = blockBuffer.toString();
989     {if (true) return block;}
990     throw new Error("Missing return statement in function");
991   }
992 
993   String ConsumeBlock() throws ParseException {
994   StringBuffer block = new StringBuffer(); // to collect the block in
995   int nesting = 1; // the first "{" was consumed before we were called
996 
997   // step through the code until the final brace
998   while(nesting != 0) {
999     Token nextTok = getNextToken();
1000
1001    // add in any preceding spaces and comments
1002    // for some bizzare reason, this misses the comments...
1003    if(nextTok.specialToken != null) {
1004      Token special = nextTok.specialToken;
1005      while(special != null) {
1006        /*Debug.pr(
1007          this, "ParseCpsl.ConsumeBlock: special.image = " + special.image
1008        );*/
1009        block.append(special.image);
1010        special = special.next;
1011      }
1012    }
1013
1014    // adjust nesting
1015    if(nextTok.image.equals("{")) {
1016      nesting++;
1017      /*Debug.pr(this, "ParseCpsl.ConsumeBlock: nesting = " + nesting);*/
1018    } else if(nextTok.image.equals("}")) {
1019      nesting--;
1020      /*Debug.pr(this, "ParseCpsl.ConsumeBlock: nesting = " + nesting);*/
1021    }
1022
1023    // add the image to the block string (but not the final "}")
1024    if(nesting > 0)
1025      block.append(nextTok.image);
1026    /*Debug.pr(this, "ParseCpsl.ConsumeBlock: nextTok.image = ^" +
1027             nextTok.image + "^");*/
1028
1029  } // while
1030
1031  /*Debug.pr(this, "ParseCpsl.ConsumeBlock: block = " + block.toString());*/
1032  return block.toString();
1033  }
1034
1035  final private boolean jj_2_1(int xla) {
1036    jj_la = xla; jj_lastpos = jj_scanpos = token;
1037    boolean retval = !jj_3_1();
1038    jj_save(0, xla);
1039    return retval;
1040  }
1041
1042  final private boolean jj_2_2(int xla) {
1043    jj_la = xla; jj_lastpos = jj_scanpos = token;
1044    boolean retval = !jj_3_2();
1045    jj_save(1, xla);
1046    return retval;
1047  }
1048
1049  final private boolean jj_3R_19() {
1050    if (jj_scan_token(leftBrace)) return true;
1051    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1052    if (jj_3R_22()) return true;
1053    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1054    return false;
1055  }
1056
1057  final private boolean jj_3R_14() {
1058    if (jj_scan_token(ident)) return true;
1059    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1060    return false;
1061  }
1062
1063  final private boolean jj_3_1() {
1064    if (jj_3R_12()) return true;
1065    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1066    return false;
1067  }
1068
1069  final private boolean jj_3R_12() {
1070    Token xsp;
1071    xsp = jj_scanpos;
1072    if (jj_3R_14()) {
1073    jj_scanpos = xsp;
1074    if (jj_3R_15()) {
1075    jj_scanpos = xsp;
1076    if (jj_3R_16()) return true;
1077    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1078    } else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1079    } else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1080    return false;
1081  }
1082
1083  final private boolean jj_3R_17() {
1084    Token xsp;
1085    xsp = jj_scanpos;
1086    if (jj_3R_19()) {
1087    jj_scanpos = xsp;
1088    if (jj_3R_20()) return true;
1089    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1090    } else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1091    return false;
1092  }
1093
1094  final private boolean jj_3R_24() {
1095    if (jj_scan_token(pling)) return true;
1096    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1097    return false;
1098  }
1099
1100  final private boolean jj_3R_22() {
1101    Token xsp;
1102    xsp = jj_scanpos;
1103    if (jj_3R_24()) jj_scanpos = xsp;
1104    else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1105    if (jj_scan_token(ident)) return true;
1106    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1107    return false;
1108  }
1109
1110  final private boolean jj_3_2() {
1111    if (jj_3R_13()) return true;
1112    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1113    return false;
1114  }
1115
1116  final private boolean jj_3R_20() {
1117    if (jj_scan_token(string)) return true;
1118    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1119    return false;
1120  }
1121
1122  final private boolean jj_3R_23() {
1123    if (jj_3R_12()) return true;
1124    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1125    return false;
1126  }
1127
1128  final private boolean jj_3R_18() {
1129    if (jj_scan_token(leftBracket)) return true;
1130    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1131    if (jj_3R_21()) return true;
1132    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1133    return false;
1134  }
1135
1136  final private boolean jj_3R_21() {
1137    Token xsp;
1138    if (jj_3R_23()) return true;
1139    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1140    while (true) {
1141      xsp = jj_scanpos;
1142      if (jj_3R_23()) { jj_scanpos = xsp; break; }
1143      if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1144    }
1145    return false;
1146  }
1147
1148  final private boolean jj_3R_16() {
1149    if (jj_3R_18()) return true;
1150    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1151    return false;
1152  }
1153
1154  final private boolean jj_3R_15() {
1155    if (jj_3R_17()) return true;
1156    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1157    return false;
1158  }
1159
1160  final private boolean jj_3R_13() {
1161    if (jj_scan_token(colon)) return true;
1162    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1163    if (jj_scan_token(ident)) return true;
1164    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1165    if (jj_scan_token(leftBrace)) return true;
1166    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
1167    return false;
1168  }
1169
1170  public ParseCpslTokenManager token_source;
1171  ASCII_CharStream jj_input_stream;
1172  public Token token, jj_nt;
1173  private Token jj_scanpos, jj_lastpos;
1174  private int jj_la;
1175  public boolean lookingAhead = false;
1176  private boolean jj_semLA;
1177  private int jj_gen;
1178  final private int[] jj_la1 = new int[31];
1179  final private int[] jj_la1_0 = {0x400,0x80000,0x1000,0x80800,0x0,0x100000,0x0,0x0,0x200000,0xc00000,0xc00000,0x1000000,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x4000000,0x8000000,0x0,0x2000000,0x0,0x8000000,0x0,0x0,0x0,0x0,0x8000000,0x0,};
1180  final private int[] jj_la1_1 = {0x0,0x0,0x0,0x0,0x10,0x0,0x10,0x18,0x0,0x0,0x0,0x0,0x41090,0x5014,0x400,0x5014,0x5014,0x800,0x1004,0x0,0x10,0x80,0x0,0x200,0x3c,0x800,0x41090,0x40080,0x10,0xbc,0x800,};
1181  final private JJCalls[] jj_2_rtns = new JJCalls[2];
1182  private boolean jj_rescan = false;
1183  private int jj_gc = 0;
1184
1185  public ParseCpsl(java.io.InputStream stream) {
1186    jj_input_stream = new ASCII_CharStream(stream, 1, 1);
1187    token_source = new ParseCpslTokenManager(jj_input_stream);
1188    token = new Token();
1189    token.next = jj_nt = token_source.getNextToken();
1190    jj_gen = 0;
1191    for (int i = 0; i < 31; i++) jj_la1[i] = -1;
1192    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
1193  }
1194
1195  public void ReInit(java.io.InputStream stream) {
1196    jj_input_stream.ReInit(stream, 1, 1);
1197    token_source.ReInit(jj_input_stream);
1198    token = new Token();
1199    token.next = jj_nt = token_source.getNextToken();
1200    jj_gen = 0;
1201    for (int i = 0; i < 31; i++) jj_la1[i] = -1;
1202    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
1203  }
1204
1205  public ParseCpsl(java.io.Reader stream) {
1206    jj_input_stream = new ASCII_CharStream(stream, 1, 1);
1207    token_source = new ParseCpslTokenManager(jj_input_stream);
1208    token = new Token();
1209    token.next = jj_nt = token_source.getNextToken();
1210    jj_gen = 0;
1211    for (int i = 0; i < 31; i++) jj_la1[i] = -1;
1212    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
1213  }
1214
1215  public void ReInit(java.io.Reader stream) {
1216    jj_input_stream.ReInit(stream, 1, 1);
1217    token_source.ReInit(jj_input_stream);
1218    token = new Token();
1219    token.next = jj_nt = token_source.getNextToken();
1220    jj_gen = 0;
1221    for (int i = 0; i < 31; i++) jj_la1[i] = -1;
1222    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
1223  }
1224
1225  public ParseCpsl(ParseCpslTokenManager tm) {
1226    token_source = tm;
1227    token = new Token();
1228    token.next = jj_nt = token_source.getNextToken();
1229    jj_gen = 0;
1230    for (int i = 0; i < 31; i++) jj_la1[i] = -1;
1231    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
1232  }
1233
1234  public void ReInit(ParseCpslTokenManager tm) {
1235    token_source = tm;
1236    token = new Token();
1237    token.next = jj_nt = token_source.getNextToken();
1238    jj_gen = 0;
1239    for (int i = 0; i < 31; i++) jj_la1[i] = -1;
1240    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
1241  }
1242
1243  final private Token jj_consume_token(int kind) throws ParseException {
1244    Token oldToken = token;
1245    if ((token = jj_nt).next != null) jj_nt = jj_nt.next;
1246    else jj_nt = jj_nt.next = token_source.getNextToken();
1247    if (token.kind == kind) {
1248      jj_gen++;
1249      if (++jj_gc > 100) {
1250        jj_gc = 0;
1251        for (int i = 0; i < jj_2_rtns.length; i++) {
1252          JJCalls c = jj_2_rtns[i];
1253          while (c != null) {
1254            if (c.gen < jj_gen) c.first = null;
1255            c = c.next;
1256          }
1257        }
1258      }
1259      return token;
1260    }
1261    jj_nt = token;
1262    token = oldToken;
1263    jj_kind = kind;
1264    throw generateParseException();
1265  }
1266
1267  final private boolean jj_scan_token(int kind) {
1268    if (jj_scanpos == jj_lastpos) {
1269      jj_la--;
1270      if (jj_scanpos.next == null) {
1271        jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
1272      } else {
1273        jj_lastpos = jj_scanpos = jj_scanpos.next;
1274      }
1275    } else {
1276      jj_scanpos = jj_scanpos.next;
1277    }
1278    if (jj_rescan) {
1279      int i = 0; Token tok = token;
1280      while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
1281      if (tok != null) jj_add_error_token(kind, i);
1282    }
1283    return (jj_scanpos.kind != kind);
1284  }
1285
1286  final public Token getNextToken() {
1287    if ((token = jj_nt).next != null) jj_nt = jj_nt.next;
1288    else jj_nt = jj_nt.next = token_source.getNextToken();
1289    jj_gen++;
1290    return token;
1291  }
1292
1293  final public Token getToken(int index) {
1294    Token t = lookingAhead ? jj_scanpos : token;
1295    for (int i = 0; i < index; i++) {
1296      if (t.next != null) t = t.next;
1297      else t = t.next = token_source.getNextToken();
1298    }
1299    return t;
1300  }
1301
1302  private java.util.Vector jj_expentries = new java.util.Vector();
1303  private int[] jj_expentry;
1304  private int jj_kind = -1;
1305  private int[] jj_lasttokens = new int[100];
1306  private int jj_endpos;
1307
1308  private void jj_add_error_token(int kind, int pos) {
1309    if (pos >= 100) return;
1310    if (pos == jj_endpos + 1) {
1311      jj_lasttokens[jj_endpos++] = kind;
1312    } else if (jj_endpos != 0) {
1313      jj_expentry = new int[jj_endpos];
1314      for (int i = 0; i < jj_endpos; i++) {
1315        jj_expentry[i] = jj_lasttokens[i];
1316      }
1317      boolean exists = false;
1318      for (java.util.Enumeration enum = jj_expentries.elements(); enum.hasMoreElements();) {
1319        int[] oldentry = (int[])(enum.nextElement());
1320        if (oldentry.length == jj_expentry.length) {
1321          exists = true;
1322          for (int i = 0; i < jj_expentry.length; i++) {
1323            if (oldentry[i] != jj_expentry[i]) {
1324              exists = false;
1325              break;
1326            }
1327          }
1328          if (exists) break;
1329        }
1330      }
1331      if (!exists) jj_expentries.addElement(jj_expentry);
1332      if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
1333    }
1334  }
1335
1336  final public ParseException generateParseException() {
1337    jj_expentries.removeAllElements();
1338    boolean[] la1tokens = new boolean[59];
1339    for (int i = 0; i < 59; i++) {
1340      la1tokens[i] = false;
1341    }
1342    if (jj_kind >= 0) {
1343      la1tokens[jj_kind] = true;
1344      jj_kind = -1;
1345    }
1346    for (int i = 0; i < 31; i++) {
1347      if (jj_la1[i] == jj_gen) {
1348        for (int j = 0; j < 32; j++) {
1349          if ((jj_la1_0[i] & (1<<j)) != 0) {
1350            la1tokens[j] = true;
1351          }
1352          if ((jj_la1_1[i] & (1<<j)) != 0) {
1353            la1tokens[32+j] = true;
1354          }
1355        }
1356      }
1357    }
1358    for (int i = 0; i < 59; i++) {
1359      if (la1tokens[i]) {
1360        jj_expentry = new int[1];
1361        jj_expentry[0] = i;
1362        jj_expentries.addElement(jj_expentry);
1363      }
1364    }
1365    jj_endpos = 0;
1366    jj_rescan_token();
1367    jj_add_error_token(0, 0);
1368    int[][] exptokseq = new int[jj_expentries.size()][];
1369    for (int i = 0; i < jj_expentries.size(); i++) {
1370      exptokseq[i] = (int[])jj_expentries.elementAt(i);
1371    }
1372    return new ParseException(token, exptokseq, tokenImage);
1373  }
1374
1375  final public void enable_tracing() {
1376  }
1377
1378  final public void disable_tracing() {
1379  }
1380
1381  final private void jj_rescan_token() {
1382    jj_rescan = true;
1383    for (int i = 0; i < 2; i++) {
1384      JJCalls p = jj_2_rtns[i];
1385      do {
1386        if (p.gen > jj_gen) {
1387          jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
1388          switch (i) {
1389            case 0: jj_3_1(); break;
1390            case 1: jj_3_2(); break;
1391          }
1392        }
1393        p = p.next;
1394      } while (p != null);
1395    }
1396    jj_rescan = false;
1397  }
1398
1399  final private void jj_save(int index, int xla) {
1400    JJCalls p = jj_2_rtns[index];
1401    while (p.gen > jj_gen) {
1402      if (p.next == null) { p = p.next = new JJCalls(); break; }
1403      p = p.next;
1404    }
1405    p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
1406  }
1407
1408  static final class JJCalls {
1409    int gen;
1410    Token first;
1411    int arg;
1412    JJCalls next;
1413  }
1414
1415}
1416