1 %{
2 /*
3  * Copyright (C) 1999, 2000, 2001  Lorenzo Bettini
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20 
21 static int lineno = 1 ; /* number of scanned lines */
22 char linebuf[1024] ; /* current code line in the source */
23 int tokenpos = 0 ; // current token position in the current line
24 
25 #include "tags.h"
26 #include "tokens.h"
27 #include "colors.h"
28 
29 #include "genfun.h"
30 
31 %}
32 %option prefix="java_scanner_"
33 %option noyywrap
34 
35 ws [ ]+
36 tabs [\t]+
37 
38 nl \n
39 cr \r
40 IDE [a-zA-Z_]([a-zA-Z0-9_])*
41 wspace [ \t\n\r]
42 
43 STRING \"[^\"\n]*\"
44 
45 not_alpha [^a-zA-Z0-9]
46 
47 %s COMMENT_STATE
48 %s SINGLELINE_COMMENT
49 %s STRING_STATE
50 %s CHAR_STATE
51 
52 keyword (abstract|assert|break|case|catch|class|const|continue|default|do|else|extends|final|finally|for|goto|if|implements|instanceof|interface|native|new|null|package|private|protected|public|return|static|super|switch|synchronized|throw|throws|this|transient|try|volatile|while)
53 basetype (int|byte|boolean|char|long|float|double|short|void)
54 symbol [\~\!\%\^\*\(\)\-\+\=\[\]\|\\\:\;\,\.\/\?\&\<\>]
55 funccall {IDE}/{wspace}*\(
56 
57 %%
58 
59 
60 
61 \r {}
62 
63 <INITIAL>"/*" { BEGIN COMMENT_STATE ;
64  startComment( yytext ) ;
65 }
66 <INITIAL>"/*".*"*/" { generateComment( yytext ) ;  }
67 
68 
69 <COMMENT_STATE>\n {
70    endComment ("");
71    ++lineno;
72    generateNewLine() ;
73    if (true) {
74       startComment ("");
75    } else {
76      for (int i = 0; i < 10; ++i) {
77        ; // do nothing, just to test the { }
78      }
79    }
80    /* if we encounter another // during a comment we simply
81       treat it as a ordinary string */
82  }
83 <COMMENT_STATE>"*/" { endComment(yytext) ;
84                       BEGIN INITIAL ; /* end of the comment */ }
85 
86 <INITIAL>"//" { BEGIN SINGLELINE_COMMENT ; startComment( yytext ) ; }
87 <SINGLELINE_COMMENT>\n {
88    BEGIN INITIAL ;
89    yyless (0); // put the \n back
90    endComment( yytext ) ;
91    /* if we encounter another // during a comment we simply
92       treat it as a ordinary string */
93  }
94 
95 <INITIAL>\" { BEGIN STRING_STATE ; startString( yytext );  }
96 <STRING_STATE>\\\\ {  generate_preproc( yytext ) ; }
97 <STRING_STATE>"\\\"" {  generate_preproc( yytext ) ; }
98 <STRING_STATE>\n {
99    endString ("");
100    ++lineno;
101    generateNewLine() ;
102    startString ("");
103 }
104 <STRING_STATE>\" { BEGIN INITIAL ; endString( yytext ) ; }
105 
106 <INITIAL>\' { BEGIN CHAR_STATE ; startString( yytext );  }
107 <CHAR_STATE>\\\\ {  generate_preproc( yytext ) ; }
108 <CHAR_STATE>"\\\'" {  generate_preproc( yytext ) ; }
109 <CHAR_STATE>\' { BEGIN INITIAL ; endString( yytext ) ; }
110 
111 <INITIAL>{keyword}  { generateKeyWord( yytext ) ; }
112 <INITIAL>{basetype} { generateBaseType( yytext ) ; }
113 <INITIAL>{symbol} { generateSymbol( yytext ); }
114 <INITIAL>[\{\}] { generateCBracket ( yytext ); }
115 
116 <INITIAL>0[xX][0-9a-fA-F]* { generateNumber( yytext ) ; }
117 <INITIAL>[0-9][0-9]*(\.[0-9]*[eE]?[-+]?[0-9]*)? { generateNumber( yytext ) ; }
118 
119 <INITIAL>{keyword}/{wspace}*\( { generateKeyWord( yytext ) ; }
120 <INITIAL>{basetype}/{wspace}*\( { generateBaseType( yytext ) ; }
121 <INITIAL>{funccall} { generateFunction ( yytext ); }
122 
123 <INITIAL>import { generatePreProc( yytext) ; }
124 
125 <INITIAL>[a-zA-Z_]([a-zA-Z0-9_])* { generate_normal( yytext ) ; }
126 
127 \t {
128         generateTab() ;
129 }
130 
131 . { generate_preproc( yytext ) ; /* anything else */ }
132 
133 \n {
134        ++lineno;
135        generateNewLine() ;
136 }
137 
138 %%
139 
140 void yyerror( char *s ) ;
141 
142 void yyerror( char *s )
143 {
144   fprintf( stderr, "%d: %s: %s\n%s\n", lineno, s, yytext, linebuf ) ;
145   fprintf( stderr, "%*s\n", tokenpos, "^" ) ;
146 }
147 
148 void yyerror( const std::string &s )
149 {
150   yyerror(s.c_str());
151 }
152 
153 /* vim:set ft=flex expandtab cindent tabstop=4 softtabstop=4 shiftwidth=4 textwidth=0: */
154