1 /*======================================================================*
2 * Copyright (c) 2008, Yahoo! Inc. All rights reserved. *
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 *======================================================================*/
15 DEBUG_LOOKAHEAD=false;
16 DEBUG_TOKEN_MANAGER=false;
20 PARSER_BEGIN(ESFParser)
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("\"","");
60 | "#" : IN_LINE_COMMENT
63 <IN_LINE_COMMENT> SKIP:
70 <IN_LINE_COMMENT> MORE:
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","."])* >
98 event() ( event() )* <EOF>
108 eventName() "{" [ attributeList() ] "}"
112 * The name of an event, should be max 256 chars ([a-zA-Z0-9_]*)
121 if ( getEventTemplateDB().addEvent(t.image))
123 setCurrentEvent(t.image);
127 throw new ParseException("Problem adding event "+t.image);
132 void attributeList() :
135 attribute() ( attribute() )*
138 String stringLiteral() :
145 return removeQuotes(t.image);
149 boolean booleanLiteral() :
163 int integerLiteral() :
168 t = <INTEGER_LITERAL>
171 return Integer.parseInt(t.image);
172 } catch (NumberFormatException e) {
178 double doubleLiteral() :
186 return Double.parseDouble(t.image);
187 } catch (NumberFormatException e) {
193 Object defaultValue() :
198 obj = integerLiteral()
202 | obj = doubleLiteral()
206 | obj = booleanLiteral()
210 | obj = stringLiteral()
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")
258 throw new ParseException("No such type '"+aType+"'");
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);
270 if (anArraySize > 0) {
274 if ( !getEventTemplateDB().addEventAttribute(evt,
281 throw new ParseException("Problem adding attribute "+evt+"("
282 +aType+","+anAttribute+")");
298 String attributeName() :