1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
|
%{
/* This file is part of kdev-pg-qt
Copyright (C) 2006 Jakob Petsovits <jpetso@gmx.at>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <QtCore/QDebug>
#include "kdev-pg.h"
#include "kdev-pg-parser.hh"
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QTextStream>
int inp();
void appendLineBuffer();
void newline();
void countNewlines(const char*, int);
void yyerror(const char* );
extern int yyLine;
extern int currentOffset;
extern bool yymoreFlag;
namespace KDevPG
{
extern QFile file;
extern QFileInfo fileInfo;
extern QTextStream checkOut;
}
#define YYMORE yymoreFlag = true; yymore();
#define YY_INPUT(buf, result, max_size) \
{ \
int c = inp(); \
result = (c == EOF) ? YY_NULL : (buf[0] = (char)c, 1); \
}
#define YY_USER_ACTION appendLineBuffer();
#define COPY_TO_YYLVAL(string, len) \
yylval.str = (char*) calloc(len+1, sizeof(char)); \
strncpy(yylval.str, string, len); \
yylval.str[len] = '\0';
#define COPY_CODE_TO_YYLVAL(string, len) \
if(KDevPG::globalSystem.lineNumberPolicy == KDevPG::World::BeautifulCode) \
{ \
COPY_TO_YYLVAL(string, len) \
} \
else \
{ \
QByteArray tmp("\n\01!ASIgnore\"!!\n#"); \
if(KDevPG::globalSystem.lineNumberPolicy == KDevPG::World::CompatibilityLineNumbers) \
tmp += "line"; \
tmp += " " + QString::number(firstCodeLine).toLocal8Bit(); \
tmp += " \"" + KDevPG::fileInfo.absoluteFilePath().toLocal8Bit() + "\""; \
if(KDevPG::globalSystem.lineNumberPolicy == KDevPG::World::FullLineNumbers) \
tmp += " 1"; \
tmp += "\n"; \
size_t memlen = tmp.size() + firstCodeColumn + len + 16 + 1; \
yylval.str = (char*) calloc(memlen, sizeof(char)); \
strncpy(yylval.str, tmp.data(), tmp.size()); \
memset(yylval.str + tmp.size(), ' ', firstCodeColumn); \
strncpy(yylval.str + tmp.size() + firstCodeColumn, string, len); \
strncpy(yylval.str + memlen - 17, "\n\01!AS/Ignore\"!!\n", 16); \
yylval.str[memlen-1] = '\0'; \
}
#define ESCAPE_CHARACTER(chr) \
yylval.str = (char*) calloc(2, sizeof(char)); yylval.str[0] = chr; yylval.str[1] = '\0'; return T_STRING;
namespace {
enum RulePosition {
RuleBody,
RuleFooter,
RuleLexer
};
RulePosition rulePosition = RuleBody;
int openBrackets; // for rule arguments and regexp usage
int firstCodeLine; // where the current code-block begins
int firstCodeColumn;
}
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
%}
Whitespace [ \f\t]
Newline "\r\n"|\r|\n
String ["]([^\r\n\"]|[\\][^\r\n])*["]
Char [_a-zA-Z0-9]|\\[xXuU][0-9a-fA-F]{1,6}|\\[oO][0-7][0-7]*|\\[dD][0-9]{1,7}|\\[yY][01]{1,21}|\\[\x20-\x7f]
%x CODE
%x PARSERCLASS
%x RULE_ARGUMENTS
%x RULE_PARAMETERS_HEADER
%x RULE_PARAMETERS_VARNAME
%x RULE_LEXER
%%
{Whitespace}* /* skip */ ;
{Newline} newline();
"--"[^\r\n]* /* line comments, skip */ ;
";"+ rulePosition = RuleBody; return ';';
"->" if(rulePosition == RuleLexer) BEGIN(RULE_LEXER); else rulePosition = RuleFooter; return T_ARROW;
".=" return T_INLINE;
"(" return '(';
")" return ')';
"{" return '{';
"}" return '}';
"," return ',';
"0" return '0';
"#" return '#';
"." return '.';
":" return ':';
"=" return '=';
"+" return '+';
"*" return '*';
"?" return '?';
"@" return '@';
"|" return '|';
"&" return '&';
"try/recover" return T_TRY_RECOVER;
"try/rollback" return T_TRY_ROLLBACK;
"catch" return T_CATCH;
"%export_macro" return T_EXPORT_MACRO;
"%export_macro_header" return T_EXPORT_MACRO_HEADER;
"%token" return T_TOKEN_DECLARATION;
"%token_stream" return T_TOKEN_STREAM_DECLARATION;
"%namespace" return T_NAMESPACE_DECLARATION;
"%parserclass" BEGIN(PARSERCLASS); return T_PARSERCLASS_DECLARATION;
"%lexerclass" BEGIN(PARSERCLASS); return T_LEXERCLASS_DECLARATION;
"%input_stream" return T_INPUT_STREAM;
"%ast_extra_members" return T_AST_DECLARATION;
"%parser_declaration_header" return T_PARSER_DECLARATION_HEADER;
"%parser_bits_header" return T_PARSER_BITS_HEADER;
"%ast_header" return T_AST_HEADER;
"%lexer_declaration_header" return T_LEXER_DECLARATION_HEADER;
"%lexer_bits_header" return T_LEXER_BITS_HEADER;
"%input_encoding" return T_INPUT_ENCODING;
"%table_lexer" return T_TABLE_LEXER;
"%sequence_lexer" return T_SEQUENCE_LEXER;
"%ast_base" return T_AST_BASE;
"%parser_base" return T_PARSER_BASE;
"%lexer_base" return T_LEXER_BASE;
"%bin" return T_BIN;
"%pre" return T_PRE;
"%post" return T_POST;
"%tern" return T_TERN;
"%paren" return T_PAREN;
"%priority" return T_PRIORITY;
"%<" rulePosition = RuleBody; return T_LOPR;
"%>" rulePosition = RuleFooter; return T_ROPR;
"%left" return T_LEFT_ASSOC;
"%right" return T_RIGHT_ASSOC;
"%isLeft" return T_IS_LEFT_ASSOC;
"%isRight" return T_IS_RIGHT_ASSOC;
"%lexer" rulePosition = RuleLexer; return T_LEXER;
<PARSERCLASS>{
{Whitespace}* /* skip */ ;
{Newline} newline();
"(" return '(';
"public" return T_PUBLIC;
"private" return T_PRIVATE;
"protected" return T_PROTECTED;
"declaration" return T_DECLARATION;
"constructor" return T_CONSTRUCTOR;
"destructor" return T_DESTRUCTOR;
"bits" return T_BITS;
")" BEGIN(INITIAL); return ')';
. BEGIN(INITIAL); REJECT; /* everything else */
}
"[" {
if (rulePosition == RuleBody) { /* use the arguments in a rule call */
firstCodeLine = yyLine;
openBrackets = 0;
BEGIN(RULE_ARGUMENTS);
}
else if (rulePosition == RuleFooter) { /* declare the arguments */
BEGIN(RULE_PARAMETERS_HEADER); return '[';
}
}
<RULE_LEXER>{
"--"[^\r\n]* /* line comments, skip */ ;
{Newline} newline();
"{"[a-zA-Z_][a-zA-Z_0-9]*"}" ++yytext; COPY_TO_YYLVAL(yytext,yyleng-2); return T_NAMED_REGEXP;
";"+(("--"[^\r\n]*[\r\n])|[ \f\t\r\n])+/";"+ countNewlines(yytext, yyleng); rulePosition = RuleBody; BEGIN(INITIAL); return ';';
";"+ return ';';
"[" ++openBrackets; return '[';
"]" --openBrackets; return ']';
"(" ++openBrackets; return '(';
")" --openBrackets; return ')';
"?" return '?';
"|" return '|';
"^" return '^';
{Char}"-"{Char} COPY_TO_YYLVAL(yytext,yyleng); return T_RANGE;
"&" return '&';
"~" return '~';
"*" return '*';
"+" return '+';
"@" return '@';
"." return '.';
"->" return T_ARROW;
"%continue" return T_CONTINUE;
"%fail" return T_FAIL;
"%enter" return T_ENTER_RULE_SET;
"%leave" return T_LEAVE_RULE_SET;
"%la" return T_LOOKAHEAD;
"%ba" return T_BARRIER;
"[:" firstCodeLine = yyLine; firstCodeColumn = currentOffset + 2; BEGIN(CODE);
[_A-Z]+/[ \f\t\r\n]*";" COPY_TO_YYLVAL(yytext,yyleng); return T_TERMINAL;
[_a-zA-Z0-9]+/[ \f\t\r\n]*";" COPY_TO_YYLVAL(yytext,yyleng); return T_IDENTIFIER;
{Char}+ COPY_TO_YYLVAL(yytext,yyleng); return T_UNQUOTED_STRING;
{Whitespace} /* skip */
{String} yytext++; COPY_TO_YYLVAL(yytext,yyleng-2); return T_STRING;
<<EOF>> {
BEGIN(INITIAL); // is not set automatically by yyrestart()
KDevPG::checkOut << "** ERROR Encountered end of file in an unclosed rule lexer definition..." << endl;
yyerror("");
return 0;
}
}
<RULE_ARGUMENTS>{
{Newline} newline(); YYMORE;
{String} YYMORE; /* this and... */
["] YYMORE; /* ...this prevent brackets inside strings to be counted */
[^\[\]\n\r\"]* YYMORE; /* gather everything that's not a bracket, and append what comes next */
"[" openBrackets++; YYMORE;
"]" {
openBrackets--;
if (openBrackets < 0) {
COPY_CODE_TO_YYLVAL(yytext,(yyleng-1)); /* cut off the trailing bracket */
BEGIN(INITIAL);
return T_RULE_ARGUMENTS;
}
}
<<EOF>> {
BEGIN(INITIAL); // is not set automatically by yyrestart()
KDevPG::checkOut << "** ERROR Encountered end of file in an unclosed rule argument specification..." << endl;
yyerror("");
return 0;
}
}
<RULE_PARAMETERS_HEADER>{
{Whitespace}* /* skip */ ;
{Newline} newline();
"--"[^\r\n]* /* line comments, skip */ ;
":"{Whitespace}* BEGIN(RULE_PARAMETERS_VARNAME); return ':';
"#" return '#';
"member" return T_MEMBER;
"temporary" return T_TEMPORARY;
"argument" return T_ARGUMENT;
"node" return T_NODE;
"token" return T_TOKEN;
"variable" return T_VARIABLE;
";" return ';'; /* only used for "token" types */
[_a-zA-Z]*[_a-zA-Z0-9]+ COPY_TO_YYLVAL(yytext,yyleng); return T_IDENTIFIER;
"]" BEGIN(INITIAL); return ']';
. BEGIN(INITIAL); REJECT; /* everything else */
}
<RULE_PARAMETERS_VARNAME>{
{Newline} newline(); YYMORE;
[^;\r\n]* YYMORE; /* gather everything that's not a semicolon, and append what comes next */
";" {
// strip trailing whitespace
int length = yyleng-1; // and first, the trailing semicolon
for (int i = length-1; i < 1; i--) {
switch(yytext[i-1])
{
case ' ':
case '\f':
case '\t':
continue;
default:
length = i;
break;
}
}
COPY_TO_YYLVAL(yytext,length);
BEGIN(RULE_PARAMETERS_HEADER);
return T_IDENTIFIER;
}
. BEGIN(INITIAL); REJECT; /* everything else */
}
"[:" firstCodeLine = yyLine; firstCodeColumn = currentOffset + 2; BEGIN(CODE);
<CODE>{
{Newline} newline(); YYMORE;
[^:\n\r]* YYMORE; /* gather everything that's not a colon, and append what comes next */
":"+[^:\]\n\r]* YYMORE; /* also gather colons that are not followed by colons or newlines */
":]" {
COPY_CODE_TO_YYLVAL(yytext, (yyleng-2)); /* cut off the trailing stuff */
if(rulePosition == RuleLexer)
BEGIN(RULE_LEXER);
else
BEGIN(INITIAL);
return T_CODE;
}
<<EOF>> {
BEGIN(INITIAL); // is not set automatically by yyrestart()
KDevPG::checkOut << "** ERROR Encountered end of file in an unclosed code segment..." << endl;
yyerror("");
return 0;
}
}
[_A-Z]+ COPY_TO_YYLVAL(yytext,yyleng); return T_TERMINAL;
[_a-zA-Z][_a-zA-Z0-9]* COPY_TO_YYLVAL(yytext,yyleng); return T_IDENTIFIER;
[0-9]+ COPY_TO_YYLVAL(yytext,yyleng); return T_NUMBER;
{String} {
yytext++; /* start inside the quotes */
COPY_TO_YYLVAL(yytext,yyleng-2); /* cut off the trailing quote */
return T_STRING;
}
. {
KDevPG::checkOut << "Unexpected character: ``" << yytext[0] << "''" << endl;
yyerror("");
}
%%
char ch;
int yyLine = 1, currentOffset = 0;
bool endOfLine = false, yymoreFlag = false;
int yyTextLineLeng = 1024;
char *yyTextLine = (char*)malloc(yyTextLineLeng);
int inp()
{
if( KDevPG::file.atEnd() )
return EOF;
KDevPG::file.getChar( &ch );
return ch;
}
void newline()
{
++yyLine;
endOfLine = true;
}
void countNewlines(const char* code, int leng)
{
for(int i = 0; i != leng; ++i)
if(code[i] == '\n')
++yyLine;
}
/* initialize the line buffer */
void clearLineBuffer()
{
yyTextLine[0] = '\0';
currentOffset = 0;
endOfLine = false;
}
/* add the current token to the current line */
void appendLineBuffer()
{
if (endOfLine == true)
clearLineBuffer();
static int lastTextLeng = 0;
currentOffset = strlen(yyTextLine); /* start of current */
int newLeng = currentOffset + strlen(yytext) - (yymoreFlag ? lastTextLeng : 0) + 1;
if(newLeng > yyTextLineLeng)
{
do
{
yyTextLineLeng *= 2;
}
while(newLeng > yyTextLineLeng);
yyTextLine = (char*)realloc(yyTextLine, yyTextLineLeng);
}
strcpy(yyTextLine+currentOffset, yytext + (yymoreFlag ? lastTextLeng : 0)); /* append current */
/* strcpy is faster than strcat */
Q_ASSERT(strlen(yyTextLine) < size_t(yyTextLineLeng));
lastTextLeng = strlen(yytext);
yymoreFlag = false;
}
void yyerror(const char* msg )
{
Q_UNUSED(msg);
KDevPG::checkOut << "** LEXICAL ERROR at line " << yyLine << " column " << currentOffset << endl;
char *current_end = yyTextLine + strlen(yyTextLine);
char *p;
/* get the rest of the line if we are not already at the end */
if(!endOfLine)
{
p = current_end;
int c = ch;
while(c != EOF && c != '\n')
{
*p++ = c;
c = inp();
}
*p++ = '\n';
*p = 0;
}
/* yyTextLine[] now has the whole line, with the current token */
/* at currentOffset */
/* print error message and current line */
KDevPG::checkOut << yyTextLine;
/* print a ^ under the most recent token */
KDevPG::checkOut << QString(currentOffset, ' ').append('^') << endl; /* currentOffset spaces, then ^ */
exit(EXIT_FAILURE);
}
int yywrap() { return 1; }
|