/*
* Copyright (C) 1999-2000 Harri Porten ([email protected])
* Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All Rights Reserved.
* Copyright (C) 2007 Cameron Zwarich ([email protected])
*
* 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 "config.h"
#include "Lexer.h"
#include "JSFunction.h"
#include "JSGlobalObjectFunctions.h"
#include "NodeInfo.h"
#include "Nodes.h"
#include "dtoa.h"
#include
#include
#include
#include
using namespace WTF;
using namespace Unicode;
// We can't specify the namespace in yacc's C output, so do it here instead.
using namespace JSC;
#include "Grammar.h"
#include "Lookup.h"
#include "Lexer.lut.h"
namespace JSC {
static const UChar byteOrderMark = 0xFEFF;
Lexer::Lexer(JSGlobalData* globalData)
: m_isReparsing(false)
, m_globalData(globalData)
, m_keywordTable(JSC::mainTable)
{
}
Lexer::~Lexer()
{
m_keywordTable.deleteTable();
}
inline const UChar* Lexer::currentCharacter() const
{
return m_code - 4;
}
inline int Lexer::currentOffset() const
{
return currentCharacter() - m_codeStart;
}
ALWAYS_INLINE void Lexer::shift1()
{
m_current = m_next1;
m_next1 = m_next2;
m_next2 = m_next3;
if (LIKELY(m_code < m_codeEnd))
m_next3 = m_code[0];
else
m_next3 = -1;
++m_code;
}
ALWAYS_INLINE void Lexer::shift2()
{
m_current = m_next2;
m_next1 = m_next3;
if (LIKELY(m_code + 1 < m_codeEnd)) {
m_next2 = m_code[0];
m_next3 = m_code[1];
} else {
m_next2 = m_code < m_codeEnd ? m_code[0] : -1;
m_next3 = -1;
}
m_code += 2;
}
ALWAYS_INLINE void Lexer::shift3()
{
m_current = m_next3;
if (LIKELY(m_code + 2 < m_codeEnd)) {
m_next1 = m_code[0];
m_next2 = m_code[1];
m_next3 = m_code[2];
} else {
m_next1 = m_code < m_codeEnd ? m_code[0] : -1;
m_next2 = m_code + 1 < m_codeEnd ? m_code[1] : -1;
m_next3 = -1;
}
m_code += 3;
}
ALWAYS_INLINE void Lexer::shift4()
{
if (LIKELY(m_code + 3 < m_codeEnd)) {
m_current = m_code[0];
m_next1 = m_code[1];
m_next2 = m_code[2];
m_next3 = m_code[3];
} else {
m_current = m_code < m_codeEnd ? m_code[0] : -1;
m_next1 = m_code + 1 < m_codeEnd ? m_code[1] : -1;
m_next2 = m_code + 2 < m_codeEnd ? m_code[2] : -1;
m_next3 = -1;
}
m_code += 4;
}
void Lexer::setCode(const SourceCode& source, ParserArena& arena)
{
m_arena = &arena.identifierArena();
m_lineNumber = source.firstLine();
m_delimited = false;
m_lastToken = -1;
const UChar* data = source.provider()->data();
m_source = &source;
m_codeStart = data;
m_code = data + source.startOffset();
m_codeEnd = data + source.endOffset();
m_error = false;
m_atLineStart = true;
m_buffer8.reserveInitialCapacity(initialReadBufferCapacity);
m_buffer16.reserveInitialCapacity((m_codeEnd - m_code) / 2);
// ECMA-262 calls for stripping all Cf characters, but we only strip BOM characters.
// See for details.
if (source.provider()->hasBOMs()) {
for (const UChar* p = m_codeStart; p < m_codeEnd; ++p) {
if (UNLIKELY(*p == byteOrderMark)) {
copyCodeWithoutBOMs();
break;
}
}
}
// Read the first characters into the 4-character buffer.
shift4();
ASSERT(currentOffset() == source.startOffset());
}
void Lexer::copyCodeWithoutBOMs()
{
// Note: In this case, the character offset data for debugging will be incorrect.
// If it's important to correctly debug code with extraneous BOMs, then the caller
// should strip the BOMs when creating the SourceProvider object and do its own
// mapping of offsets within the stripped text to original text offset.
m_codeWithoutBOMs.reserveCapacity(m_codeEnd - m_code);
for (const UChar* p = m_code; p < m_codeEnd; ++p) {
UChar c = *p;
if (c != byteOrderMark)
m_codeWithoutBOMs.append(c);
}
ptrdiff_t startDelta = m_codeStart - m_code;
m_code = m_codeWithoutBOMs.data();
m_codeStart = m_code + startDelta;
m_codeEnd = m_codeWithoutBOMs.data() + m_codeWithoutBOMs.size();
}
void Lexer::shiftLineTerminator()
{
ASSERT(isLineTerminator(m_current));
// Allow both CRLF and LFCR.
if (m_current + m_next1 == '\n' + '\r')
shift2();
else
shift1();
++m_lineNumber;
}
ALWAYS_INLINE const Identifier* Lexer::makeIdentifier(const UChar* characters, size_t length)
{
return &m_arena->makeIdentifier(m_globalData, characters, length);
}
inline bool Lexer::lastTokenWasRestrKeyword() const
{
return m_lastToken == CONTINUE || m_lastToken == BREAK || m_lastToken == RETURN || m_lastToken == THROW;
}
static NEVER_INLINE bool isNonASCIIIdentStart(int c)
{
return category(c) & (Letter_Uppercase | Letter_Lowercase | Letter_Titlecase | Letter_Modifier | Letter_Other);
}
static inline bool isIdentStart(int c)
{
return isASCII(c) ? isASCIIAlpha(c) || c == '$' || c == '_' : isNonASCIIIdentStart(c);
}
static NEVER_INLINE bool isNonASCIIIdentPart(int c)
{
return category(c) & (Letter_Uppercase | Letter_Lowercase | Letter_Titlecase | Letter_Modifier | Letter_Other
| Mark_NonSpacing | Mark_SpacingCombining | Number_DecimalDigit | Punctuation_Connector);
}
static inline bool isIdentPart(int c)
{
return isASCII(c) ? isASCIIAlphanumeric(c) || c == '$' || c == '_' : isNonASCIIIdentPart(c);
}
static inline int singleEscape(int c)
{
switch (c) {
case 'b':
return 0x08;
case 't':
return 0x09;
case 'n':
return 0x0A;
case 'v':
return 0x0B;
case 'f':
return 0x0C;
case 'r':
return 0x0D;
default:
return c;
}
}
inline void Lexer::record8(int c)
{
ASSERT(c >= 0);
ASSERT(c <= 0xFF);
m_buffer8.append(static_cast(c));
}
inline void Lexer::record16(UChar c)
{
m_buffer16.append(c);
}
inline void Lexer::record16(int c)
{
ASSERT(c >= 0);
ASSERT(c <= USHRT_MAX);
record16(UChar(static_cast(c)));
}
int Lexer::lex(void* p1, void* p2)
{
ASSERT(!m_error);
ASSERT(m_buffer8.isEmpty());
ASSERT(m_buffer16.isEmpty());
YYSTYPE* lvalp = static_cast(p1);
YYLTYPE* llocp = static_cast(p2);
int token = 0;
m_terminator = false;
start:
while (isWhiteSpace(m_current))
shift1();
int startOffset = currentOffset();
if (m_current == -1) {
if (!m_terminator && !m_delimited && !m_isReparsing) {
// automatic semicolon insertion if program incomplete
token = ';';
goto doneSemicolon;
}
return 0;
}
m_delimited = false;
switch (m_current) {
case '>':
if (m_next1 == '>' && m_next2 == '>') {
if (m_next3 == '=') {
shift4();
token = URSHIFTEQUAL;
break;
}
shift3();
token = URSHIFT;
break;
}
if (m_next1 == '>') {
if (m_next2 == '=') {
shift3();
token = RSHIFTEQUAL;
break;
}
shift2();
token = RSHIFT;
break;
}
if (m_next1 == '=') {
shift2();
token = GE;
break;
}
shift1();
token = '>';
break;
case '=':
if (m_next1 == '=') {
if (m_next2 == '=') {
shift3();
token = STREQ;
break;
}
shift2();
token = EQEQ;
break;
}
shift1();
token = '=';
break;
case '!':
if (m_next1 == '=') {
if (m_next2 == '=') {
shift3();
token = STRNEQ;
break;
}
shift2();
token = NE;
break;
}
shift1();
token = '!';
break;
case '<':
if (m_next1 == '!' && m_next2 == '-' && m_next3 == '-') {
//