Commit b81c401d by alokp@chromium.org

Modified Token class to store various types of data. Added debug code to dump…

Modified Token class to store various types of data. Added debug code to dump token to an output stream. Review URL: http://codereview.appspot.com/4920041 git-svn-id: https://angleproject.googlecode.com/svn/trunk@737 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent ebbec2e7
...@@ -6,34 +6,53 @@ ...@@ -6,34 +6,53 @@
#include "Token.h" #include "Token.h"
#include "token_type.h"
static const int kLocationLineSize = 16; // in bits. static const int kLocationLineSize = 16; // in bits.
static const int kLocationLineMask = (1 << kLocationLineSize) - 1; static const int kLocationLineMask = (1 << kLocationLineSize) - 1;
namespace pp namespace pp
{ {
Token::Token() : mLocation(-1), mType(-1), mValue(0) Token::Location Token::encodeLocation(int line, int file)
{ {
return (file << kLocationLineSize) | (line & kLocationLineMask);
} }
Token::Token(Location l, int t) : mLocation(l), mType(t) void Token::decodeLocation(Location loc, int* line, int* file)
{ {
if (file) *file = loc >> kLocationLineSize;
if (line) *line = loc & kLocationLineMask;
} }
Token::Token(Location l, int t, const std::string& s) : mLocation(l), mType(t), mValue(s) Token::Token(Location location, int type, std::string* value)
: mLocation(location),
mType(type),
mValue(value)
{ {
} }
Token::Location Token::encodeLocation(int line, int file) Token::~Token() {
{ delete mValue;
return (file << kLocationLineSize) | (line & kLocationLineMask);
} }
void Token::decodeLocation(Location loc, int* line, int* file) std::ostream& operator<<(std::ostream& out, const Token& token)
{ {
if (file) *file = loc >> kLocationLineSize; switch (token.type())
if (line) *line = loc & kLocationLineMask; {
case SPACE:
out << " ";
break;
case INT_CONSTANT:
case FLOAT_CONSTANT:
case IDENTIFIER:
out << *(token.value());
break;
default:
out << static_cast<char>(token.type());
break;
}
return out;
} }
} // namespace pp } // namespace pp
...@@ -17,27 +17,28 @@ class Token ...@@ -17,27 +17,28 @@ class Token
{ {
public: public:
typedef int Location; typedef int Location;
Token();
Token(Location l, int t);
Token(Location l, int t, const std::string& s);
static Location encodeLocation(int line, int file); static Location encodeLocation(int line, int file);
static void decodeLocation(Location loc, int* line, int* file); static void decodeLocation(Location loc, int* line, int* file);
// Takes ownership of string.
Token(Location location, int type, std::string* value);
~Token();
Location location() const { return mLocation; } Location location() const { return mLocation; }
int type() const { return mType; } int type() const { return mType; }
const std::string& value() const { return mValue; } const std::string* value() const { return mValue; }
private: private:
Location mLocation; Location mLocation;
int mType; int mType;
std::string mValue; std::string* mValue;
}; };
typedef std::vector<Token*> TokenVector; typedef std::vector<Token*> TokenVector;
typedef TokenVector::const_iterator TokenIterator; typedef TokenVector::const_iterator TokenIterator;
extern std::ostream& operator<<(std::ostream& out, const Token& token);
} // namepsace pp } // namepsace pp
#endif // COMPILER_PREPROCESSOR_TOKEN_H_ #endif // COMPILER_PREPROCESSOR_TOKEN_H_
...@@ -56,6 +56,10 @@ DECIMAL_CONSTANT [1-9][0-9]* ...@@ -56,6 +56,10 @@ DECIMAL_CONSTANT [1-9][0-9]*
OCTAL_CONSTANT 0[0-7]* OCTAL_CONSTANT 0[0-7]*
HEXADECIMAL_CONSTANT 0[xX][0-9a-fA-F]+ HEXADECIMAL_CONSTANT 0[xX][0-9a-fA-F]+
DIGIT [0-9]
EXPONENT_PART [eE][+-]?{DIGIT}+
FRACTIONAL_CONSTANT ({DIGIT}*"."{DIGIT}+)|({DIGIT}+".")
%% %%
{HASH} { return HASH; } {HASH} { return HASH; }
...@@ -79,13 +83,18 @@ HEXADECIMAL_CONSTANT 0[xX][0-9a-fA-F]+ ...@@ -79,13 +83,18 @@ HEXADECIMAL_CONSTANT 0[xX][0-9a-fA-F]+
{HASH}line { return HASH_LINE; } {HASH}line { return HASH_LINE; }
{IDENTIFIER} { {IDENTIFIER} {
yylval->tval = new pp::Token(yylineno, IDENTIFIER, std::string(yytext, yyleng)); yylval->sval = new std::string(yytext, yyleng);
return IDENTIFIER; return IDENTIFIER;
} }
{DECIMAL_CONSTANT}|{OCTAL_CONSTANT}|{HEXADECIMAL_CONSTANT} { {DECIMAL_CONSTANT}|{OCTAL_CONSTANT}|{HEXADECIMAL_CONSTANT} {
yylval->tval = new pp::Token(yylineno, INTEGER_CONSTANT, std::string(yytext, yyleng)); yylval->sval = new std::string(yytext, yyleng);
return INTEGER_CONSTANT; return INT_CONSTANT;
}
({DIGIT}+{EXPONENT_PART})|({FRACTIONAL_CONSTANT}{EXPONENT_PART}?) {
yylval->sval = new std::string(yytext, yyleng);
return FLOAT_CONSTANT;
} }
{PUNCTUATOR} { return yytext[0]; } {PUNCTUATOR} { return yytext[0]; }
......
...@@ -36,6 +36,8 @@ WHICH GENERATES THE GLSL ES PARSER. ...@@ -36,6 +36,8 @@ WHICH GENERATES THE GLSL ES PARSER.
%union { %union {
int ival; int ival;
std::string* sval;
std::vector<std::string*>* slist;
pp::Token* tval; pp::Token* tval;
pp::TokenVector* tlist; pp::TokenVector* tlist;
} }
...@@ -49,11 +51,11 @@ static void yyerror(YYLTYPE* llocp, ...@@ -49,11 +51,11 @@ static void yyerror(YYLTYPE* llocp,
static void defineMacro(pp::Context* context, static void defineMacro(pp::Context* context,
YYLTYPE* llocp, YYLTYPE* llocp,
pp::Macro::Type type, pp::Macro::Type type,
pp::Token* identifier, const std::string* identifier,
pp::TokenVector* parameters, std::vector<std::string*>* parameters,
pp::TokenVector* replacements); pp::TokenVector* replacements);
static void undefineMacro(pp::Context* context, pp::Token* identifier); static void undefineMacro(pp::Context* context, const std::string* identifier);
static bool isMacroDefined(pp::Context* context, pp::Token* identifier); static bool isMacroDefined(pp::Context* context, const std::string* identifier);
static void pushConditionalBlock(pp::Context* context, bool condition); static void pushConditionalBlock(pp::Context* context, bool condition);
static void popConditionalBlock(pp::Context* context); static void popConditionalBlock(pp::Context* context);
%} %}
...@@ -62,10 +64,11 @@ static void popConditionalBlock(pp::Context* context); ...@@ -62,10 +64,11 @@ static void popConditionalBlock(pp::Context* context);
%token HASH_IF HASH_IFDEF HASH_IFNDEF HASH_ELSE HASH_ELIF HASH_ENDIF DEFINED %token HASH_IF HASH_IFDEF HASH_IFNDEF HASH_ELSE HASH_ELIF HASH_ENDIF DEFINED
%token HASH_ERROR HASH_PRAGMA HASH_EXTENSION HASH_VERSION HASH_LINE %token HASH_ERROR HASH_PRAGMA HASH_EXTENSION HASH_VERSION HASH_LINE
%token SPACE %token SPACE
%token <tval> IDENTIFIER INTEGER_CONSTANT FLOAT_CONSTANT %token <sval> INT_CONSTANT FLOAT_CONSTANT IDENTIFIER
%type <ival> operator %type <ival> operator
%type <slist> parameter_list
%type <tval> conditional_token token %type <tval> conditional_token token
%type <tlist> text_line parameter_list replacement_token_list conditional_token_list token_list %type <tlist> text_line replacement_token_list conditional_token_list token_list
%% %%
input input
...@@ -74,7 +77,12 @@ input ...@@ -74,7 +77,12 @@ input
; ;
line line
: text_line : text_line {
// TODO(alokp): Expand macros.
pp::TokenVector* out = context->output;
out->insert(out->end(), $1->begin(), $1->end());
delete $1;
}
| control_line | control_line
; ;
...@@ -144,7 +152,7 @@ conditional_token ...@@ -144,7 +152,7 @@ conditional_token
parameter_list parameter_list
: /* empty */ { $$ = NULL; } : /* empty */ { $$ = NULL; }
| IDENTIFIER { | IDENTIFIER {
$$ = new pp::TokenVector; $$ = new std::vector<std::string*>();
$$->push_back($1); $$->push_back($1);
} }
| parameter_list ',' IDENTIFIER { | parameter_list ',' IDENTIFIER {
...@@ -155,7 +163,6 @@ parameter_list ...@@ -155,7 +163,6 @@ parameter_list
token_list token_list
: token { : token {
//context->ppData.skipWS = false;
$$ = new pp::TokenVector; $$ = new pp::TokenVector;
$$->push_back($1); $$->push_back($1);
} }
...@@ -166,11 +173,21 @@ token_list ...@@ -166,11 +173,21 @@ token_list
; ;
token token
: IDENTIFIER : operator {
| INTEGER_CONSTANT $$ = new pp::Token(@1.first_line, $1, NULL);
| FLOAT_CONSTANT }
| SPACE { $$ = new pp::Token(@1.first_line, SPACE); } | SPACE {
| operator { $$ = new pp::Token(@1.first_line, $1); } $$ = new pp::Token(@1.first_line, SPACE, NULL);
}
| INT_CONSTANT {
$$ = new pp::Token(@1.first_line, INT_CONSTANT, $1);
}
| FLOAT_CONSTANT {
$$ = new pp::Token(@1.first_line, FLOAT_CONSTANT, $1);
}
| IDENTIFIER {
$$ = new pp::Token(@1.first_line, IDENTIFIER, $1);
}
; ;
operator operator
...@@ -209,17 +226,17 @@ void yyerror(YYLTYPE* llocp, pp::Context* context, const char* reason) ...@@ -209,17 +226,17 @@ void yyerror(YYLTYPE* llocp, pp::Context* context, const char* reason)
void defineMacro(pp::Context* context, void defineMacro(pp::Context* context,
YYLTYPE* llocp, YYLTYPE* llocp,
pp::Macro::Type type, pp::Macro::Type type,
pp::Token* identifier, const std::string* identifier,
pp::TokenVector* parameters, std::vector<std::string*>* parameters,
pp::TokenVector* replacements) pp::TokenVector* replacements)
{ {
} }
void undefineMacro(pp::Context* context, pp::Token* identifier) void undefineMacro(pp::Context* context, const std::string* identifier)
{ {
} }
bool isMacroDefined(pp::Context* context, pp::Token* identifier) bool isMacroDefined(pp::Context* context, const std::string* identifier)
{ {
return false; return false;
} }
......
...@@ -56,9 +56,9 @@ ...@@ -56,9 +56,9 @@
HASH_VERSION = 272, HASH_VERSION = 272,
HASH_LINE = 273, HASH_LINE = 273,
SPACE = 274, SPACE = 274,
IDENTIFIER = 275, INT_CONSTANT = 275,
INTEGER_CONSTANT = 276, FLOAT_CONSTANT = 276,
FLOAT_CONSTANT = 277 IDENTIFIER = 277
}; };
#endif #endif
/* Tokens. */ /* Tokens. */
...@@ -79,9 +79,9 @@ ...@@ -79,9 +79,9 @@
#define HASH_VERSION 272 #define HASH_VERSION 272
#define HASH_LINE 273 #define HASH_LINE 273
#define SPACE 274 #define SPACE 274
#define IDENTIFIER 275 #define INT_CONSTANT 275
#define INTEGER_CONSTANT 276 #define FLOAT_CONSTANT 276
#define FLOAT_CONSTANT 277 #define IDENTIFIER 277
...@@ -91,6 +91,8 @@ typedef union YYSTYPE ...@@ -91,6 +91,8 @@ typedef union YYSTYPE
{ {
int ival; int ival;
std::string* sval;
std::vector<std::string*>* slist;
pp::Token* tval; pp::Token* tval;
pp::TokenVector* tlist; pp::TokenVector* tlist;
} }
......
//
// 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.
//
#ifndef COMPILER_PREPROCESSOR_TOKEN_TYPE_H_
#define COMPILER_PREPROCESSOR_TOKEN_TYPE_H_
#include "pp_tab.h"
#endif // COMPILER_PREPROCESSOR_TOKEN_TYPE_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment