Doxygen
constexp.l
浏览该文件的文档.
1 /******************************************************************************
2  *
3  *
4  *
5  *
6  * Copyright (C) 1997-2015 by Dimitri van Heesch.
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation under the terms of the GNU General Public License is hereby
10  * granted. No representations are made about the suitability of this software
11  * for any purpose. It is provided "as is" without express or implied warranty.
12  * See the GNU General Public License for more details.
13  *
14  * Documents produced by Doxygen are derivative works derived from the
15  * input used in their production; they are not affected by this license.
16  *
17  */
18 %option never-interactive
19 %option prefix="constexpYY"
20 %option nounput
21 %option reentrant bison-bridge
22 %option extra-type="struct constexpYY_state *"
23 %top{
24 #include <stdint.h>
25 // forward declare yyscan_t to improve typesafety
26 #include "constexp_p.h"
27 }
28 
29 %{
30 
31 #include "constexp.h"
32 #include "cppvalue.h"
33 #include "ce_parse.hpp" // generated header file
34 #include "message.h"
35 
36 #define YY_NO_INPUT 1
37 #define YY_NO_UNISTD_H 1
38 
39 #define USE_STATE2STRING 0
40 
41 #if USE_STATE2STRING
42 static const char *stateToString(int state);
43 #endif
44 
45 static yy_size_t yyread(char *buf,yy_size_t max_size,yyscan_t yyscanner);
46 
47 #undef YY_INPUT
48 #define YY_INPUT(buf,result,max_size) result=yyread(buf,max_size,yyscanner);
49 
50 %}
51 
52 CONSTSUFFIX ([uU][lL]?[lL]?)|([lL][lL]?[uU]?)
53 
54 
55 %%
56 
57 "?" { return TOK_QUESTIONMARK; }
58 ":" { return TOK_COLON; }
59 "||" { return TOK_OR; }
60 "&&" { return TOK_AND; }
61 "|" { return TOK_BITWISEOR; }
62 "^" { return TOK_BITWISEXOR; }
63 "&" { return TOK_AMPERSAND; }
64 "!=" { return TOK_NOTEQUAL; }
65 "==" { return TOK_EQUAL; }
66 "<" { return TOK_LESSTHAN; }
67 ">" { return TOK_GREATERTHAN; }
68 "<=" { return TOK_LESSTHANOREQUALTO; }
69 ">=" { return TOK_GREATERTHANOREQUALTO; }
70 "<<" { return TOK_SHIFTLEFT; }
71 ">>" { return TOK_SHIFTRIGHT; }
72 "+" { return TOK_PLUS; }
73 "-" { return TOK_MINUS; }
74 "*" { return TOK_STAR; }
75 "/" { return TOK_DIVIDE; }
76 "%" { return TOK_MOD; }
77 "~" { return TOK_TILDE; }
78 "!" { return TOK_NOT; }
79 "(" { return TOK_LPAREN; }
80 ")" { return TOK_RPAREN; }
81 "'"(([^\'\n\r\\]+)|(\\(([ntvbrfa\\?'\"])|([0-9]+)|([xX][0-9a-fA-F]+))))"'" {
82  yyextra->strToken=yytext;
83  return TOK_CHARACTER;
84  }
85 0[0-7]*{CONSTSUFFIX}? { yyextra->strToken=yytext;
86  return TOK_OCTALINT;
87  }
88 [1-9][0-9]*{CONSTSUFFIX}? { yyextra->strToken=yytext;
89  return TOK_DECIMALINT;
90  }
91 (0x|0X)[0-9a-fA-F]+{CONSTSUFFIX}? { yyextra->strToken=yytext+2;
92  return TOK_HEXADECIMALINT;
93  }
94 (0b|0B)[01]+{CONSTSUFFIX}? { yyextra->strToken=yytext+2;
95  return TOK_BINARYINT;
96  }
97 (([0-9]+\.[0-9]*)|([0-9]*\.[0-9]+))([eE]([\-\+])?[0-9]+)?([fFlL])? {
98  yyextra->strToken=yytext; return TOK_FLOAT;
99  }
100 ([0-9]+[eE])([\-\+])?[0-9]+([fFlL])? {
101  yyextra->strToken=yytext; return TOK_FLOAT;
102  }
103 .
104 \n
105 
106 %%
107 
108 static yy_size_t yyread(char *buf,yy_size_t max_size,yyscan_t yyscanner)
109 {
110  struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
111  yy_size_t c=0;
112  while( c < max_size && yyextra->inputString[yyextra->inputPosition] )
113  {
114  *buf = yyextra->inputString[yyextra->inputPosition++] ;
115  c++; buf++;
116  }
117  return c;
118 }
119 
120 struct ConstExpressionParser::Private
121 {
122  yyscan_t yyscanner;
123  struct constexpYY_state constexpYY_extra;
124 };
125 
126 
127 ConstExpressionParser::ConstExpressionParser()
128 {
129  p = new Private;
130  constexpYYlex_init_extra(&p->constexpYY_extra, &p->yyscanner);
131 }
132 
133 ConstExpressionParser::~ConstExpressionParser()
134 {
135  constexpYYlex_destroy(p->yyscanner);
136  delete p;
137 }
138 
139 bool ConstExpressionParser::parse(const char *fileName,int lineNr,const std::string &s)
140 {
141  struct yyguts_t *yyg = (struct yyguts_t*)p->yyscanner;
142 
143 #ifdef FLEX_DEBUG
144  constexpYYset_debug(1,p->yyscanner);
145 #endif
146 
147  yyextra->constExpFileName = fileName;
148  yyextra->constExpLineNr = lineNr;
149  yyextra->inputString = s;
150  yyextra->inputPosition = 0;
151  constexpYYrestart( yyin, p->yyscanner );
152 
153  printlex(yy_flex_debug, true, __FILE__, fileName);
154  //printf("Expression: '%s'\n",qPrint(s));
155 
156  constexpYYparse(p->yyscanner);
157 
158  //printf("Result: %ld\n",(long)g_resultValue);
159  printlex(yy_flex_debug, false, __FILE__, fileName);
160  bool result = (long)yyextra->resultValue!=0;
161 
162  return result;
163 }
164 
165 extern "C" {
166  int constexpYYwrap(yyscan_t yyscanner) { return 1; }
167 }
168 
169 #if USE_STATE2STRING
170 #include "constexp.l.h"
171 #endif