source: java/FramclipsePlugin/src/main/java/com/framsticks/framclipse/internal/parser/FramclipseNonScriptParser.jjt @ 13

Last change on this file since 13 was 13, checked in by jbochenski, 15 years ago
File size: 7.4 KB
Line 
1options {
2        JDK_VERSION = "1.5";
3        USER_CHAR_STREAM = true;
4        STATIC = false;
5        MULTI = true;
6}
7
8PARSER_BEGIN(FramclipseNonScriptParser)
9
10package com.framsticks.framclipse.internal.parser;
11
12import java.io.StringReader;
13
14public class FramclipseNonScriptParser {
15
16  private CharStreamWithOffset charStream = null;
17 
18  public java.util.Collection<Exception> exceptions = new java.util.ArrayList<Exception>();
19 
20  public FramclipseNonScriptParser(String input)
21  {
22        this(new CharStreamWithOffset(new StringReader(input)));
23  }
24       
25  private FramclipseNonScriptParser(CharStreamWithOffset charStream)
26  {
27        this((CharStream)charStream);
28        this.charStream = charStream;
29  }
30 
31  public static void main(String args[]) {
32    String s = "expdef: \n name: urnohorl \n id: horla \n code:~ function kupa(a,b) { yeah \n } \n~\n\n\n";
33   
34    FramclipseNonScriptParser t = new FramclipseNonScriptParser(s);
35    try {
36      SimpleNode n = t.FramclipseFile();
37      n.dump("");
38      System.out.println("Thank you.");
39    } catch (Exception e) {
40      System.out.println("Oops.");
41      System.out.println(e.getMessage());
42      //e.printStackTrace();
43    }
44  }
45}
46
47PARSER_END(FramclipseNonScriptParser)
48
49TOKEN_MGR_DECLS : {
50
51    int blockNestingDepth ;
52
53}
54
55<CodeSection, Block> SKIP :
56
57< "/*"(~["*"])* "*"(~["/"] (~["*"])* "*")* "/" >
58| <"//"(~["\r","\n"])*["\r"]"\n">
59}
60
61<CodeSection, GlobalInclude> SKIP : /* WHITE SPACE */
62{
63  " "
64| "\t"
65| "\r"
66| "\n"
67| "\f"
68}
69
70<DEFAULT,SingleSect, MultiSect> SKIP : /* WHITE SPACE */
71{
72  " "
73| "\t"
74}
75
76<DEFAULT> TOKEN : /* IDENTIFIERS */
77{
78  < IDENTIFIER: <LETTER> (<LETTER>|<DIGIT>)* >
79|
80  < #LETTER: ["_","a"-"z","A"-"Z"] >
81|
82  < #DIGIT: ["0"-"9"] >
83}
84
85<DEFAULT> TOKEN :
86{
87  < EOL: ("\r")?"\n" >
88}
89
90 
91
92<DEFAULT> TOKEN : { <CODE_SECTION_START : "code:~" > : CodeSection }
93
94<DEFAULT> TOKEN : { < MULTI_SECT_START : ":~" >  : MultiSect }
95
96<DEFAULT> TOKEN : { <SINGLE_SECT_START : ":" > : SingleSect }
97
98<DEFAULT> TOKEN : { <GLOBAL_INCLUDE_KWD : "#include" > : GlobalInclude }
99 
100<DEFAULT> SKIP :
101{
102 <"# "(~["\r","\n"])*["\r"]"\n">
103}
104
105
106<GlobalInclude> TOKEN :
107{
108                < INCLUDE_STRING:
109      "\""
110      (   (~["\"","\\","\n","\r"])
111        | ("\\"
112            ( ["n","t","b","r","f","\\","'","\"","_"]
113            | ["0"-"7"] ( ["0"-"7"] )?
114            | ["0"-"3"] ["0"-"7"] ["0"-"7"]
115            )
116          )
117      )*
118      "\""
119  > : DEFAULT
120}
121
122<MultiSect> TOKEN : { <
123 MULTI_SECT_END : "~" (" ")* ("\r")?"\n" > : DEFAULT }
124
125<MultiSect> TOKEN :
126{
127  <MULTILINE_PROP_VALUE : (~["~"] | "\\~")* >
128}
129
130<CodeSection> TOKEN :
131{
132        < STRING_LITERAL:
133      "\""
134      (   (~["\"","\\","\n","\r"])
135        | ("\\"
136            ( ["n","t","b","r","f","\\","'","\"","_"]
137            | ["0"-"7"] ( ["0"-"7"] )?
138            | ["0"-"3"] ["0"-"7"] ["0"-"7"]
139            )
140          )
141      )*
142      "\""
143  >
144|
145  < UNTERM_STRING_LITERAL:
146      "\""
147      (   (~["\"","\\","\n","\r"])
148        | ("\\"
149            ( ["n","t","b","r","f","\\","'","\"","_"]
150            | ["0"-"7"] ( ["0"-"7"] )?
151            | ["0"-"3"] ["0"-"7"] ["0"-"7"]
152            )
153          )
154      )* ["\r"] "\n"
155  >
156 
157}
158
159<CodeSection> TOKEN : { < CODE_SECTION_END : "~" (" ")* ("\r")?"\n" > : DEFAULT }
160
161<CodeSection> TOKEN :
162{
163  <GLOBAL_KWD : "global" >
164  |
165  <FUNCTION_KWD : "function" >
166  |
167  <INCLUDE_KWD : "@include" > 
168 
169 /*
170  |
171  <FOR_KWD : "for" >
172  |
173  <WHILE_KWD : "while" >
174  |
175  <VAR_KWD : "VAR" >
176  |
177  <TYPEOF_KWD : "typeof" >
178  |
179  <DO_KWD : "do" >
180  |
181  <BREAK_KWD : "break" >
182  |
183  <SWITCH_KWD : "switch" >
184  |
185  <CASE_KWD : "case" >
186  |
187  <GOTO_KWD : "goto" >
188  |
189  <DEFAULT_KWD : "default" >
190  |
191  <RETURN_KWD : "return" >
192  |
193  <ASM_KWD : "asm" >
194  */
195}
196
197<CodeSection> TOKEN : /* SEPARATORS */
198{
199  < LPAREN: "(" >
200| < RPAREN: ")" >
201| < LBRACE : "{" > { blockNestingDepth = 1 ; } : Block 
202| "}"
203| < LBRACKET: "[" >
204| < RBRACKET: "]" >
205| < SEMICOLON: ";" >
206| < COMMA: "," >
207| < DOT: "." >
208}
209
210<CodeSection> TOKEN : /* IDENTIFIERS */
211{
212  < IDENT: <LETTER> (<LETTER>|<DIGIT>)* >
213}
214
215<Block> TOKEN :
216{
217        < NESTED_LBRACE : "{" > { blockNestingDepth += 1 ; }
218        |
219        < RBRACE : "}" > {
220        blockNestingDepth --;
221        SwitchTo( blockNestingDepth==0 ? CodeSection : Block ) ;
222    }
223}
224
225<Block> SKIP :
226{
227    <~[]>
228}
229
230<SingleSect> TOKEN :
231{
232        < SINGLE_SECT_END : ("\r")?"\n" > : DEFAULT
233}
234
235<SingleSect> TOKEN :
236{
237  < PROP_VALUE : (~["\r","\n"])* >
238}
239
240ASTFramclipseFile FramclipseFile() : {}
241{
242   /* FObject() (LOOKAHEAD ((<EOL>)+ FObject()) (<EOL>)+ FObject())* (<EOL>)* */ 
243  (((FObject()|GlobalInclude()) (<EOL>|<EOF>))|<EOL>)* 
244 
245  { return jjtThis; }
246}
247
248void FObject() : { }
249{
250   {jjtThis.setBeginOffset(charStream.getBeginOffset());}
251   
252   <IDENTIFIER> {
253    jjtThis.setClassName(token.image);
254  } 
255   <SINGLE_SECT_START> (<SINGLE_SECT_END>|<EOF>)
256   
257        (Property() | CodeSection() )*
258   
259   { jjtThis.setEndOffset(charStream.getBeginOffset() - 1); }
260}
261
262void CodeSection() : {}
263{
264        { jjtThis.setBeginOffset(charStream.getBeginOffset()); }
265        <CODE_SECTION_START> (GlobalDecl() | Function() | IncludeStmt())* <CODE_SECTION_END>
266       
267        { jjtThis.setEndOffset(charStream.getBeginOffset()); }
268}
269
270void Function() : {}
271{
272       
273        try{
274        <FUNCTION_KWD> { jjtThis.setBeginOffset(charStream.getBeginOffset()); } IdentList()
275        <LPAREN> [ IdentList() ] <RPAREN> <LBRACE> (CodeBlock())* <RBRACE>
276        }
277        catch(ParseException e)
278        {
279                token_source.SwitchTo(DEFAULT);
280                //error_skipto(new int[] { EOF });
281        }
282       
283        { jjtThis.setEndOffset(charStream.getBeginOffset()); }
284}
285
286void GlobalDecl() : {}
287{
288        { jjtThis.setBeginOffset(charStream.getBeginOffset()); }
289        <GLOBAL_KWD> IdentList() <SEMICOLON>
290        { jjtThis.setEndOffset(charStream.getBeginOffset()); }
291}
292
293void IncludeStmt() : {}
294{
295        { jjtThis.setBeginOffset(charStream.getBeginOffset()); }
296        <INCLUDE_KWD>  <STRING_LITERAL> { jjtThis.setFileName(token.image); }
297        { jjtThis.setEndOffset(charStream.getOffset()); }
298}
299
300void GlobalInclude() : {}
301{
302        { jjtThis.setBeginOffset(charStream.getBeginOffset()); }
303        <GLOBAL_INCLUDE_KWD>  <INCLUDE_STRING> { jjtThis.setFileName(token.image); }
304        { jjtThis.setEndOffset(charStream.getOffset()); }
305}
306
307void IdentList() : {}
308{
309        <IDENT> { jjtThis.addIdent(token.image); } (<COMMA> <IDENT> { jjtThis.addIdent(token.image); } )*
310}
311
312void Property() : {}
313{
314        { jjtThis.setBeginOffset(charStream.getBeginOffset()); }
315       
316  <IDENTIFIER> { jjtThis.setName(token.image); }
317   
318  ( SingleLinePropertyValue() { jjtThis.setEndOffset(charStream.getBeginOffset() - 1); } | MultiLinePropertyValue() { jjtThis.setEndOffset(charStream.getBeginOffset()); } )
319 
320}
321
322void SingleLinePropertyValue()   : {}
323{
324        <SINGLE_SECT_START> <PROP_VALUE>{ jjtThis.setValue(token.image); } (<SINGLE_SECT_END>| <EOF>)
325}
326
327void CodeBlock() #void : {}
328{
329        <NESTED_LBRACE> (CodeBlock())* <RBRACE>
330}
331
332void MultiLinePropertyValue()  : {}
333{
334        <MULTI_SECT_START> <MULTILINE_PROP_VALUE> <MULTI_SECT_END>
335}
336
337JAVACODE
338void error_skipto(int[] kinds) #void {
339  //ParseException e = generateParseException();  // generate the exception object.
340  //System.out.println(e.toString());  // print the error message
341  Token t;
342  if(kinds.length > 0)
343  {
344    java.util.Arrays.sort(kinds);
345   
346        do {
347        t = getNextToken();
348        } while (java.util.Arrays.binarySearch(kinds, t.kind) < 0);
349   
350  }
351}
Note: See TracBrowser for help on using the repository browser.