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
|
// lexer.cc see license.txt for copyright and terms of use
// code for lexer.h
#include "lexer.h" // this module
#include "java_lang.h" // JavaLang
#include <ctype.h> // isdigit
#include <stdlib.h> // atoi
/*
* Note about nonseparating tokens and the 'checkForNonsep' function:
*
* To diagnose and report erroneous syntax like "0x5g", which would
* naively be parsed as "0x5" and "g" (two legal tokens), I divide
* all tokens into two classes: separating and nonseparating.
*
* Separating tokens are allowed to be adjacent to each other and
* to nonseparating tokens. An example is "(".
*
* Nonseparating tokens are not allowed to be adjacent to each other.
* They must be separated by either whitespace, or at least one
* separating token. The nonseparating tokens are identifiers,
* alphabetic keywords, and literals. The lexer would of course never
* yield two adjacent keywords, due to maximal munch, but classifying
* such an event as an error is harmless.
*
* By keeping track of whether the last token yielded is separating or
* not, we'll see (e.g.) "0x5g" as two consecutive nonseparating tokens,
* and can report that as an error.
*
* The C++ standard is rather vague on this point as far as I can
* tell. I haven't checked the C standard. In the C++ standard,
* section 2.6 paragraph 1 states:
*
* "There are five kinds of tokens: identifiers, keywords, literals,
* operators, and other separators. Blanks, horizontal and
* vertical tabs, newlines, formfeeds, and comments (collectively,
* "whitespace"), as described below, are ignored except as they
* serve to separate tokens. [Note: Some white space is required
* to separate otherwise adjacent identifiers, keywords, numeric
* literals, and alternative tokens containing alphabetic
* characters.]"
*
* The fact that the restriction is stated only in a parenthetical note
* is of course nonideal. I think the qualifier "numeric" on "literals"
* is a mistake, otherwise "a'b'" would be a legal token sequence. I
* do not currently implement the "alternative tokens".
*
* Update: Mozilla includes things like "foo""bar", i.e. directly
* adjacent string literals. Therefore I'm going to interpret (the
* note in) the standard literally, and take char and string literals
* to be separating.
*/
// -------------------- TokenType ---------------------
// these aren't emitted into java_tokens.cc because doing so would
// make that output dependent on smbase/xassert.h
char const *toString(TokenType type)
{
xassert(NUM_TOKEN_TYPES == tokenNameTableSize);
xassert((unsigned)type < (unsigned)NUM_TOKEN_TYPES);
return tokenNameTable[type];
}
TokenFlag tokenFlags(TokenType type)
{
xassert((unsigned)type < (unsigned)NUM_TOKEN_TYPES);
return (TokenFlag)tokenFlagTable[type];
}
// ------------------------ Lexer -------------------
Lexer::Lexer(StringTable &s, JavaLang &L, char const *fname)
: BaseLexer(s, fname),
prevIsNonsep(false),
lang(L)
{
// prime this lexer with the first token
getTokenFunc()(this);
}
Lexer::Lexer(StringTable &s, JavaLang &L, SourceLoc initLoc,
char const *buf, int len)
: BaseLexer(s, initLoc, buf, len),
prevIsNonsep(false),
lang(L)
{
// do *not* prime the lexer; I think it is a mistake above, but
// am leaving it for now
}
Lexer::~Lexer()
{}
void Lexer::whitespace()
{
BaseLexer::whitespace();
// various forms of whitespace can separate nonseparating tokens
prevIsNonsep = false;
}
// this, and 'svalTok', are out of line because I don't want the
// yylex() function to be enormous; I want that to just have a bunch
// of calls into these routines, which themselves can then have
// plenty of things inlined into them
int Lexer::tok(TokenType t)
{
checkForNonsep(t);
updLoc();
sval = NULL_SVAL; // catch mistaken uses of 'sval' for single-spelling tokens
return t;
}
int Lexer::svalTok(TokenType t)
{
checkForNonsep(t);
updLoc();
sval = (SemanticValue)addString(yytext, yyleng);
return t;
}
STATICDEF void Lexer::tokenFunc(LexerInterface *lex)
{
Lexer *ths = static_cast<Lexer*>(lex);
// call into the flex lexer; this updates 'loc' and sets
// 'sval' as appropriate
ths->type = ths->yylex();
}
Lexer::NextTokenFunc Lexer::getTokenFunc() const
{
return &Lexer::tokenFunc;
}
string Lexer::tokenDesc() const
{
if (tokenFlags((TokenType)type) & TF_MULTISPELL) {
// for tokens with multiple spellings, decode 'sval' as a
// StringRef
//return string((StringRef)sval);
return stringc << toString((TokenType)type) << ": " << (StringRef)sval;
}
else {
// for all others, consult the static table
return string(toString((TokenType)type));
}
}
string Lexer::tokenKindDesc(int kind) const
{
// static table only
return toString((TokenType)kind);
}
// EOF
|