adding all of botlist, initial add
[botlist.git] / openbotlist / WEB-INF / tools / antlr / remotedef / RemoteDef.g
blob0665a833de2e8c6cf4cdd587fb217e825b23b822
1 /**
2  * Copyright (c) 2007, Newspiritcompany.com (Berlin Brown)
3  * http://www.opensource.org/licenses/bsd-license.php
4  *
5  * Simple Remote Definition Meta Language
6  * (with parsing, more practical usage)
7  * Grammar Definition for Antlr (3.0+)
8  * Date: 12/27/2007
9  * Author: Berlin Brown
10  *
11  * Description Current Version:
12  *
13  * Parse the Example Remote Def file and 
14  * print the important values.
15  *
16  * In total, we are only building basic data structures
17  * out of the meta language file (lists, maps, etc)
18  *
19  *      All rights reserved.
20  *      
21  *      Redistribution and use in source and binary forms, with or without modification, 
22  *      are permitted provided that the following conditions are met:
23  *      
24  *  Copyright (c) 2007
25  *
26  *          * Redistributions of source code must retain the above copyright notice, 
27  *          this list of conditions and the following disclaimer.
28  *          * Redistributions in binary form must reproduce the above copyright notice, 
29  *          this list of conditions and the following disclaimer in the documentation 
30  *          and/or other materials provided with the distribution.
31  *          * Neither the name of the Newspiritcompany.com (Berlin Brown) nor 
32  *          the names of its contributors may be used to endorse or promote 
33  *          products derived from this software without specific prior written permission.
34  *      
35  *      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36  *      "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37  *      LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38  *      A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
39  *      CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  *      EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
41  *      PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
42  *      PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43  *      LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44  *      NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45  *      SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  */
48 //***********************************************
49 // Begin Grammar Definitions
50 //***********************************************
52 grammar RemoteDef;
54 options {
55     k = 2;
58 scope Symbols {
59         Map types; // JAVA definition
62 @header {
63 package org.spirit.parse.remotedef;
65 import java.util.Set;
66 import java.util.Map;
67 import java.util.HashSet;
68 import java.util.HashMap;
71 // Applies only to the lexer:
72 @lexer::header {
73 package org.spirit.parse.remotedef;
76 @members {
77         private Map rootNamespaceAttributes = new HashMap();
78         private Stack rootOperations = new Stack();
79         private boolean buildRootOperationFlag = false;
80         
81         Map getRootNamespaceAttributes() {
82                 return rootNamespaceAttributes;         
83         }
84         
85         Stack getRootOperations() {
86                 return rootOperations;
87         }
91 root_meta_declarations 
92         scope Symbols; 
93         @init {
94           $Symbols::types = new HashMap();
95         } : 
96         meta_declaration+
97         ;
99 meta_declaration :
100         root_namespace
101         ;
103 /** 
104  * Only one root namespace, allowed.
105  */
106 root_namespace :
107         OPEN_PAREN ( operation_declaration_list|statement_expression_list|end_root_attr_expression )+ CLOSE_PAREN
108         { 
109                 // JAVA COMMENT: name space defined.
110                 System.out.println("INFO: ROOT NAMESPACE FOUND: ");
111         }
112         ;
115  * Root expression list
116  */
117 statement_expression_list :
118         ( attribute_expression )+
119         ;
121 end_root_attr_expression : 
122         '----'  
123         {
124                 System.out.println("END OF ROOT ATTRIBUTES FOUND");
125                 buildRootOperationFlag = true;
126         }
127         ;
128         
129 begin_oper_attr_expression : 
130         IDENTIFIER_ATOM
131         {
132                 System.out.println("BEGIN OPERATIONS FOUND");
133                 // JAVA_COMMENT: create a new operation stack.
134                 rootOperations.push(new HashMap());
135         }
136         ;
137         
139  * Attributes are defined with  @attr: val;
140  * Sub hash data structures are defined by
141  * ID { <DATA> }
143  * Data payloads (String Content) is enclosed
144  * between <<< and >>> tags.
145  */
146 operation_declaration_list :
147         ( begin_oper_attr_expression OPEN_BRACE ( statement_expression_list | DATA_PAYLOAD_VALUE )+
148         CLOSE_BRACE )           
149         {               
150                 // JAVA COMMENT: print the data payload
151                 if ($DATA_PAYLOAD_VALUE != null) {
152                         System.out.println("INFO: data payload: [" + $DATA_PAYLOAD_VALUE.text + "]");
153                         ((Map) (rootOperations.peek())).put("data.payload", $DATA_PAYLOAD_VALUE.text);
154                 } // End if
155         }
156         ;
161  * Process a attribute statement expression, which has the following syntax:
162  * <code>@some_attribute: some_value;</code>
163  */
164 attribute_expression :
165         AT_SIGN_IDENTIFIER attribute_atom_key COLON IDENTIFIER_ATOM END_EXPRESSION
166         {
167                 // JAVA COMMENT: print the attribute key
168                 System.out.println("INFO: define attribute expr: [" + $IDENTIFIER_ATOM.text + "]");
169                 System.out.println("INFO: key: " + $attribute_atom_key.text);
170                 
171                 $Symbols::types.put($attribute_atom_key.text, $IDENTIFIER_ATOM.text);
172                 
173                 // If operations enabled, accumulate that map data
174                 if (buildRootOperationFlag) {                   
175                         ((Map) (rootOperations.peek())).put($attribute_atom_key.text, $IDENTIFIER_ATOM.text);
176                 } else {
177                         getRootNamespaceAttributes().put($attribute_atom_key.text, $IDENTIFIER_ATOM.text);
178                 }
179         }
180         ;
181         
182 attribute_key :
183         AT_SIGN_IDENTIFIER attribute_val COLON
184         { 
185                 // JAVA COMMENT: print the attribute value
186                 System.out.println("INFO: define attribute key: [" + $attribute_val.text + "]");
187         }
188         ;
189         
190 attribute_atom_key :
191         ( IDENTIFIER_ATOM )*
192         ;       
193         
194 attribute_val :
195         ( IDENTIFIER_ATOM )*
196         ;       
198 //***********************************************
199 // Misc Utility Definitions and Tokens
200 //***********************************************
202 IDENTIFIER_ATOM :       
203         ( LETTER | '0'..'9' )*
204         ;
205         
206 fragment LETTER :       
207                 '$'
208         |       'A'..'Z'
209         |       '.'
210         |       'a'..'z'
211         |       '_'
212         |       '-'
213         ;       
215 DATA_PAYLOAD_VALUE : 
216         '<<<' ( . )* '>>>'
217         {
218                 // JAVA COMMENT:
219         }
220         ;
222 //***********************************************
223 // Operators
225 // For example, open and close parens are defined
226 //***********************************************
228 COLON : ':' ;
230 END_EXPRESSION : ';' ;
232 AT_SIGN_IDENTIFIER : '@' ;
234 OPEN_PAREN : '(' ;
236 CLOSE_PAREN : ')' ;
238 OPEN_BRACE : '{' ;
240 CLOSE_BRACE : '}' ;
242 //***********************************************
243 // Ignore (whitespace, comments)
244 //***********************************************
246 WS :
247         (' '|'\r'|'\t'|'\u000C'|'\n') 
248         {channel=99;}
249     ;
251 COMMENT :
252         '/*' ( options {greedy=false;} : . )* '*/' {channel=99;}
253     ;
255 LINE_COMMENT : 
256         '//' ~('\n'|'\r')* '\r'? '\n' 
257         {
258                 // JAVA COMMENT: set channel = 99
259                 channel=99;             
260         }
261     ;
263 // End of Grammar File //