changing license to BSD, assigning Yahoo copyrights where appropriate
[lwes-java.git] / src / main / javacc / ESFParser.jj
blobf3730eae9d3f121a46e8a85acadb9b3b6482e644
1 /*======================================================================*
2  * Copyright (c) 2008, Yahoo! Inc. All rights reserved.                 *
3  *                                                                      *
4  * Licensed under the New BSD License (the "License"); you may not use  *
5  * this file except in compliance with the License.  Unless required    *
6  * by applicable law or agreed to in writing, software distributed      *
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT        *
8  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.     *
9  * See the License for the specific language governing permissions and  *
10  * limitations under the License. See accompanying LICENSE file.        *
11  *======================================================================*/
13 options {
14   DEBUG_PARSER=false;
15   DEBUG_LOOKAHEAD=false;
16   DEBUG_TOKEN_MANAGER=false;
17   STATIC=false;
20 PARSER_BEGIN(ESFParser)
22 package org.lwes.db;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
27 public class ESFParser
30     private static transient Log log = LogFactory.getLog(ESFParser.class);
32   private String currentEvent;
33   private EventTemplateDB eventTemplateDB;
35   public void setEventTemplateDB(EventTemplateDB DB)
36     { eventTemplateDB = DB; }
38   public EventTemplateDB getEventTemplateDB()
39     { return eventTemplateDB; }
41   public void setCurrentEvent(String evt)
42     { currentEvent = evt; }
44   public String getCurrentEvent()
45     { return currentEvent; }
47   public String removeQuotes(String str) {
48     return str.replace("\"","");
49   }
52 PARSER_END(ESFParser)
54 SKIP :
56   " "
57 | "\t"
58 | "\n"
59 | "\r"
60 | "#" : IN_LINE_COMMENT
63 <IN_LINE_COMMENT> SKIP:
65    "\n" : DEFAULT
66 |  "\r" : DEFAULT
67 |  "\r\n" : DEFAULT
70 <IN_LINE_COMMENT> MORE:
72   < ~[] >
76 TOKEN :
78     < REQ: "required" >
79   | < OPT: "optional" >
80   | < TRUE: "true" >
81   | < FALSE: "false" >
82   | < LBRACKET: "[" >
83   | < RBRACKET: "]" >
84   | < INTEGER_LITERAL: ["1"-"9"] (["0"-"9"])* >
85   | < STRING_LITERAL: "\""(<ID>)"\"" >
86   | < DOUBLE_LITERAL: (["0"-"9"])+ "." (["0"-"9"])* >
87   | < ID: ["a"-"z","A"-"Z","_",":","0"-"9","."] (["a"-"z","A"-"Z","_",":","0"-"9","."])* >
91 /**
92  * A list of events
93  */
94 void eventlist() :
98   event() ( event() )* <EOF>
102  * a single event
103  */
104 void event() :
108   eventName() "{" [ attributeList() ] "}"
112  * The name of an event, should be max 256 chars ([a-zA-Z0-9_]*)
113  */
114 void eventName() :
116   Token t;
119   t=<ID>
120   {
121     if ( getEventTemplateDB().addEvent(t.image))
122     {
123       setCurrentEvent(t.image);
124     }
125     else
126     {
127       throw new ParseException("Problem adding event "+t.image);
128     }
129   }
132 void attributeList() :
135   attribute() ( attribute() )*
138 String stringLiteral() :
140     Token t;
143     t = <STRING_LITERAL>
144     {
145        return removeQuotes(t.image);
146     }
149 boolean booleanLiteral() :
152   <TRUE>
153   {
154     return true;
155   }
156   |
157   <FALSE>
158   {
159     return false;
160   }
163 int integerLiteral() :
165     Token t;
168   t = <INTEGER_LITERAL>
169   {
170     try {
171       return Integer.parseInt(t.image);
172     } catch (NumberFormatException e) {
173       throw new Error();
174     }
175   }
178 double doubleLiteral() :
180     Token t;
183     t = <DOUBLE_LITERAL>
184     {
185        try {
186          return Double.parseDouble(t.image);
187        } catch (NumberFormatException e) {
188          throw new Error();
189        }
190     }
193 Object defaultValue() :
195     Object obj;
198     obj = integerLiteral()
199     {
200       return obj;
201     }
202     | obj = doubleLiteral()
203     {
204       return obj;
205     }
206     | obj = booleanLiteral()
207     {
208       return obj;
209     }
210     | obj = stringLiteral()
211     {
212       return obj;
213     }
216 boolean required() :
220     <REQ>
221     {
222       return true;
223     }
224     | <OPT>
225     {
226       return false;
227     }
230 void attribute() :
232   String aType;
233   String anAttribute;
234   int anArraySize = 0;
235   boolean required = false;
236   Object aDefaultValue = null;
239   (((required = required()) (aType=type() anAttribute=attributeName() [ "[" anArraySize = integerLiteral() "]" ] [ "=" aDefaultValue = defaultValue() ] ";"))
241   (aType=type() anAttribute=attributeName() [ "[" anArraySize = integerLiteral() "]" ] [ "=" aDefaultValue = defaultValue() ] ";")  ) {
242       if ( !( aType.equals("uint16")  ||
243               aType.equals("int16")   ||
244               aType.equals("uint32")  ||
245               aType.equals("int32")   ||
246               aType.equals("string")  ||
247               aType.equals("ip_addr") ||
248               aType.equals("int64")   ||
249               aType.equals("uint64")  ||
250               aType.equals("byte")    ||
251               aType.equals("double")  ||
252               aType.equals("float")   ||
253               aType.equals("ipv4")    ||
254               aType.equals("boolean")
255             )
256          )
257        {
258          throw new ParseException("No such type '"+aType+"'");
259        }
261       String evt = getCurrentEvent();
262       if ( evt == null ) throw new ParseException("Bad Event");
264       if (log.isTraceEnabled()) {
265         log.trace("Type: "+aType+" attr: "+anAttribute+" size: "+anArraySize);
266         log.trace("Required: "+required);
267         log.trace("default value: "+aDefaultValue);
268       }
270       if (anArraySize > 0) {
271         aType = "[L"+aType;
272       }
274       if ( !getEventTemplateDB().addEventAttribute(evt,
275                                                    anAttribute,
276                                                    aType,
277                                                    anArraySize,
278                                                    required,
279                                                    aDefaultValue))
280       {
281         throw new ParseException("Problem adding attribute "+evt+"("
282                                  +aType+","+anAttribute+")");
283       }
284    }
287 String type() :
289   Token t;
292   t=<ID>
293   {
294     return t.image;
295   }
298 String attributeName() :
300   Token t;
303   t=<ID>
304   {
305     return t.image;
306   }