Adding handling for primitives, non-primitives, and user-defined types; doesn't handl...
[iot2.git] / others / jflex / iotparser.jflex
1 // JFlex parser specification written by
2 // Rahmadi Trimananda
3 // for Sentinel system
4 // University of California, Irvine
5
6 // Technische Universitaet Muenchen 
7 // Fakultaet fuer Informatik 
8
9 import java_cup.runtime.Symbol;
10 import java_cup.runtime.ComplexSymbolFactory;
11 import java_cup.runtime.ComplexSymbolFactory.Location;
12
13 %%
14
15 %public
16 %class Lexer
17 %cup
18 %implements sym
19 %char
20 %line
21 %column
22
23 %{
24     StringBuffer string = new StringBuffer();
25     public Lexer(java.io.Reader in, ComplexSymbolFactory sf){
26         this(in);
27         symbolFactory = sf;
28     }
29     ComplexSymbolFactory symbolFactory;
30
31   private Symbol symbol(String name, int sym) {
32       return symbolFactory.newSymbol(name, sym, new Location(yyline+1,yycolumn+1,yychar), new Location(yyline+1,yycolumn+yylength(),yychar+yylength()));
33   }
34   
35   private Symbol symbol(String name, int sym, Object val) {
36       Location left = new Location(yyline+1,yycolumn+1,yychar);
37       Location right= new Location(yyline+1,yycolumn+yylength(), yychar+yylength());
38       return symbolFactory.newSymbol(name, sym, left, right,val);
39   } 
40   private Symbol symbol(String name, int sym, Object val,int buflength) {
41       Location left = new Location(yyline+1,yycolumn+yylength()-buflength,yychar+yylength()-buflength);
42       Location right= new Location(yyline+1,yycolumn+yylength(), yychar+yylength());
43       return symbolFactory.newSymbol(name, sym, left, right,val);
44   }       
45   private void error(String message) {
46     System.out.println("Error at line "+(yyline+1)+", column "+(yycolumn+1)+" : "+message);
47   }
48 %} 
49
50 %eofval{
51      return symbolFactory.newSymbol("EOF", EOF, new Location(yyline+1,yycolumn+1,yychar), new Location(yyline+1,yycolumn+1,yychar+1));
52 %eofval}
53
54
55 Ident = [a-zA-Z$_] [a-zA-Z0-9$_]*
56
57 new_line = \r|\n|\r\n;
58
59 white_space = {new_line} | [ \t\f]
60
61 %state STRING
62
63 %%
64
65 <YYINITIAL>{
66 /* keywords */
67 "int"             { return symbol("int",TYPE, "int" ); }
68 "short"           { return symbol("short",TYPE, "short" ); }
69 "byte"            { return symbol("byte",TYPE, "byte" ); }
70 "long"            { return symbol("long",TYPE, "long" ); }
71 "float"           { return symbol("float",TYPE, "float" ); }
72 "double"          { return symbol("double",TYPE, "double" ); }
73 "char"            { return symbol("char",TYPE, "char" ); }
74 "string"          { return symbol("string",TYPE, "String" ); }
75 "String"          { return symbol("String",TYPE, "String" ); }
76 "boolean"         { return symbol("boolean",TYPE, "boolean" ); }
77 "void"            { return symbol("void",TYPE, "void" ); }
78 "public"          { return symbol("public",PUBLIC); }
79 "interface"       { return symbol("interface",INTERFACE); }
80 "capability"      { return symbol("capability",CAPABILITY); }
81 "description"     { return symbol("description",DESCRIPTION); }
82 "method"          { return symbol("method",METHOD); }
83 "requires"        { return symbol("requires",REQUIRES); }
84 "with"            { return symbol("with",WITH); }
85 "as"              { return symbol("as",AS); }
86
87 /* names */
88 {Ident}           { return symbol("Identifier",IDENT, yytext()); }
89
90   
91 /* string literals */
92
93 /* char literal */
94
95 /* bool literal */
96
97 /* literals */
98
99
100
101 /* separators */
102   \"              { string.setLength(0); yybegin(STRING); }
103 ";"               { return symbol("semicolon",SEMICOLON); }
104 ","               { return symbol("comma",COMMA); }
105 "("               { return symbol("(",LPAR); }
106 ")"               { return symbol(")",RPAR); }
107 "<"               { return symbol("<",LANG); }
108 ">"               { return symbol(">",RANG); }
109 "{"               { return symbol("{",BEGIN); }
110 "}"               { return symbol("}",END); }
111 "="               { return symbol("=",ASSIGN); }
112
113 {white_space}     { /* ignore */ }
114
115 }
116
117 <STRING> {
118   \"                             { yybegin(YYINITIAL); 
119       return symbol("StringConst",STRINGCONST,string.toString(),string.length()); }
120   [^\n\r\"\\]+                   { string.append( yytext() ); }
121   \\t                            { string.append('\t'); }
122   \\n                            { string.append('\n'); }
123
124   \\r                            { string.append('\r'); }
125   \\\"                           { string.append('\"'); }
126   \\                             { string.append('\\'); }
127 }
128
129
130 /* error fallback */
131 .|\n              {  /* throw new Error("Illegal character <"+ yytext()+">");*/
132                     error("Illegal character <"+ yytext()+">");
133                   }