Hash :
28182485
Author :
Date :
2012-04-24T23:07:34
Drastically simplified Input class. Now most of the heavy lifting is done by in-built yy_scan_string and yy_scan_bytes. Comment handling will be done by the lexer. git-svn-id: https://angleproject.googlecode.com/svn/trunk@1051 736b8ea6-26fd-11df-bfd4-992fa37f6226
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
/*
//
// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
This file contains the Lex specification for GLSL ES preprocessor.
Based on Microsoft Visual Studio 2010 Preprocessor Grammar:
http://msdn.microsoft.com/en-us/library/2scxys89.aspx
IF YOU MODIFY THIS FILE YOU ALSO NEED TO RUN generate_parser.sh.
*/
%top{
//
// Copyright (c) 2011 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// This file is auto-generated by generate_parser.sh. DO NOT EDIT!
}
%{
#include "Input.h"
#include "Lexer.h"
#include "Token.h"
typedef std::string YYSTYPE;
typedef pp::Token::Location YYLTYPE;
#define YY_USER_ACTION \
do { \
yylloc->line = yylineno; \
yylloc->string = 0; \
} while(0);
// Suppress the default implementation of YY_INPUT which generated
// compiler warnings.
#define YY_INPUT
%}
%option nounput never-interactive
%option reentrant bison-bridge bison-locations
%option prefix="pp"
%option extra-type="pp::Input*"
NEWLINE "\n"|"\r"|"\r\n"
IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
PUNCTUATOR [][<>(){}.+-/*%^|&~=!:;,?]
DECIMAL_CONSTANT [1-9][0-9]*
OCTAL_CONSTANT 0[0-7]*
HEXADECIMAL_CONSTANT 0[xX][0-9a-fA-F]+
DIGIT [0-9]
EXPONENT_PART [eE][+-]?{DIGIT}+
FRACTIONAL_CONSTANT ({DIGIT}*"."{DIGIT}+)|({DIGIT}+".")
%%
# { return yytext[0]; }
{IDENTIFIER} {
yylval->assign(yytext, yyleng);
return pp::Token::IDENTIFIER;
}
{DECIMAL_CONSTANT}|{OCTAL_CONSTANT}|{HEXADECIMAL_CONSTANT} {
yylval->assign(yytext, yyleng);
return pp::Token::CONST_INT;
}
({DIGIT}+{EXPONENT_PART})|({FRACTIONAL_CONSTANT}{EXPONENT_PART}?) {
yylval->assign(yytext, yyleng);
return pp::Token::CONST_FLOAT;
}
/* Anything that starts with a {DIGIT} or .{DIGIT} must be a number. */
/* Rule to catch all invalid integers and floats. */
({DIGIT}+[_a-zA-Z0-9.]*)|("."{DIGIT}+[_a-zA-Z0-9.]*) {
yylval->assign(yytext, yyleng);
return pp::Token::INVALID_NUMBER;
}
"++" { return pp::Token::OP_INC; }
"--" { return pp::Token::OP_DEC; }
"<<" { return pp::Token::OP_LEFT; }
">>" { return pp::Token::OP_RIGHT; }
"<=" { return pp::Token::OP_LE; }
">=" { return pp::Token::OP_GE; }
"==" { return pp::Token::OP_EQ; }
"!=" { return pp::Token::OP_NE; }
"&&" { return pp::Token::OP_AND; }
"^^" { return pp::Token::OP_XOR; }
"||" { return pp::Token::OP_OR; }
"+=" { return pp::Token::OP_ADD_ASSIGN; }
"-=" { return pp::Token::OP_SUB_ASSIGN; }
"*=" { return pp::Token::OP_MUL_ASSIGN; }
"/=" { return pp::Token::OP_DIV_ASSIGN; }
"%=" { return pp::Token::OP_MOD_ASSIGN; }
"<<=" { return pp::Token::OP_LEFT_ASSIGN; }
">>=" { return pp::Token::OP_RIGHT_ASSIGN; }
"&=" { return pp::Token::OP_AND_ASSIGN; }
"^=" { return pp::Token::OP_XOR_ASSIGN; }
"|=" { return pp::Token::OP_OR_ASSIGN; }
{PUNCTUATOR} { return yytext[0]; }
[ \t\v\f]+ { return ' '; }
{NEWLINE} {
++yylineno;
return '\n';
}
. {
yylval->assign(yytext, yyleng);
return pp::Token::INVALID_CHARACTER;
}
<*><<EOF>> { yyterminate(); }
%%
int ppwrap(yyscan_t scanner)
{
pp::Input* input = yyget_extra(scanner);
// Delete the current buffer before switching to the next one.
YY_BUFFER_STATE buffer = static_cast<YY_BUFFER_STATE>(input->buffer);
if (buffer != NULL)
{
yy_delete_buffer(buffer, scanner);
input->buffer = NULL;
}
int index = std::min(input->index + 1, input->count);
if (index == input->count)
return 1; // EOF reached.
int length = input->length ? input->length[index] : -1;
if (length < 0) // NULL terminated string.
buffer = yy_scan_string(input->string[index], scanner);
else
buffer = yy_scan_bytes(input->string[index], length, scanner);
// TODO(alokp): Increment token location.
input->index = index;
input->buffer = buffer;
return 0;
}
namespace pp {
int Lexer::lex(Token* token)
{
bool leadingSpace = false;
token->type = yylex(&token->value, &token->location, mHandle);
while (token->type == ' ')
{
leadingSpace = true;
token->type = yylex(&token->value, &token->location, mHandle);
}
token->setHasLeadingSpace(leadingSpace);
return token->type;
}
bool Lexer::initLexer()
{
if ((mHandle == NULL) && yylex_init_extra(&mInput, &mHandle))
return false;
// Setup first scan string.
mInput.index = -1;
ppwrap(mHandle);
return true;
}
void Lexer::destroyLexer()
{
if (mHandle == NULL)
return;
YY_BUFFER_STATE buffer = static_cast<YY_BUFFER_STATE>(mInput.buffer);
if (buffer != NULL)
{
yy_delete_buffer(buffer, mHandle);
mInput.buffer = NULL;
}
yylex_destroy(mHandle);
mHandle = NULL;
}
} // namespace pp