Changeset 148167 in webkit


Ignore:
Timestamp:
Apr 10, 2013 8:17:08 PM (11 years ago)
Author:
benjamin@webkit.org
Message:

Unify JSC Parser's error and error message
https://bugs.webkit.org/show_bug.cgi?id=114363

Reviewed by Geoffrey Garen.

The parser kept the error state over two attributes:
error and errorMessage. They were changed in sync,
but had some discrepancy (for example, the error message
was always defined to something).

This patch unifies the two. There is an error if
if the error message is non-null or if the parsing finished
before the end.

This also gets rid of the allocation of the error message
when instantiating a parser.

  • parser/Parser.cpp:

(JSC::::Parser):
(JSC::::parseInner):
(JSC::::parseSourceElements):
(JSC::::parseVarDeclaration):
(JSC::::parseConstDeclaration):
(JSC::::parseForStatement):
(JSC::::parseSwitchStatement):
(JSC::::parsePrimaryExpression):

  • parser/Parser.h:

(JSC::Parser::updateErrorMessage):
(JSC::Parser::updateErrorWithNameAndMessage):
(JSC::Parser::hasError):
(Parser):

Location:
trunk/Source/JavaScriptCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r148162 r148167  
     12013-04-10  Benjamin Poulain  <benjamin@webkit.org>
     2
     3        Unify JSC Parser's error and error message
     4        https://bugs.webkit.org/show_bug.cgi?id=114363
     5
     6        Reviewed by Geoffrey Garen.
     7
     8        The parser kept the error state over two attributes:
     9        error and errorMessage. They were changed in sync,
     10        but had some discrepancy (for example, the error message
     11        was always defined to something).
     12
     13        This patch unifies the two. There is an error if
     14        if the error message is non-null or if the parsing finished
     15        before the end.
     16
     17        This also gets rid of the allocation of the error message
     18        when instantiating a parser.
     19
     20        * parser/Parser.cpp:
     21        (JSC::::Parser):
     22        (JSC::::parseInner):
     23        (JSC::::parseSourceElements):
     24        (JSC::::parseVarDeclaration):
     25        (JSC::::parseConstDeclaration):
     26        (JSC::::parseForStatement):
     27        (JSC::::parseSwitchStatement):
     28        (JSC::::parsePrimaryExpression):
     29        * parser/Parser.h:
     30        (JSC::Parser::updateErrorMessage):
     31        (JSC::Parser::updateErrorWithNameAndMessage):
     32        (JSC::Parser::hasError):
     33        (Parser):
     34
    1352013-04-10  Oliver Hunt  <oliver@apple.com>
    236
  • trunk/Source/JavaScriptCore/parser/Parser.cpp

    r146318 r148167  
    3636#include <wtf/WTFThreadData.h>
    3737
    38 #define fail() do { if (!m_error) updateErrorMessage(); return 0; } while (0)
    39 #define failWithToken(tok) do { if (!m_error) updateErrorMessage(tok); return 0; } while (0)
    40 #define failWithMessage(msg) do { if (!m_error) updateErrorMessage(msg); return 0; } while (0)
    41 #define failWithNameAndMessage(before, name, after) do { if (!m_error) updateErrorWithNameAndMessage(before, name, after); return 0; } while (0)
    42 #define failWithStackOverflow() do { m_error = true; m_hasStackOverflow = true; return 0; } while (0)
     38#define fail() do { if (!hasError()) updateErrorMessage(); return 0; } while (0)
     39#define failWithToken(tok) do { if (!hasError()) updateErrorMessage(tok); return 0; } while (0)
     40#define failWithMessage(msg) do { if (!hasError()) updateErrorMessage(msg); return 0; } while (0)
     41#define failWithNameAndMessage(before, name, after) do { if (!hasError()) updateErrorWithNameAndMessage(before, name, after); return 0; } while (0)
     42#define failWithStackOverflow() do { updateErrorMessage("Stack exhausted"); m_hasStackOverflow = true; return 0; } while (0)
    4343#define failIfFalse(cond) do { if (!(cond)) fail(); } while (0)
    4444#define failIfFalseWithMessage(cond, msg) do { if (!(cond)) failWithMessage(msg); } while (0)
     
    6868    , m_stack(wtfThreadData().stack())
    6969    , m_hasStackOverflow(false)
    70     , m_error(false)
    71     , m_errorMessage("Parse error")
    7270    , m_allowsIn(true)
    7371    , m_lastLine(0)
     
    115113    ScopeRef scope = currentScope();
    116114    SourceElements* sourceElements = parseSourceElements<CheckForStrictMode>(context);
    117     if (!sourceElements || !consume(EOFTOK))
    118         parseError = m_errorMessage;
     115    if (!sourceElements || !consume(EOFTOK)) {
     116        if (hasError())
     117            parseError = m_errorMessage;
     118        else
     119            parseError = ASCIILiteral("Parser error");
     120    }
    119121
    120122    IdentifierSet capturedVariables;
     
    175177                    m_lexer->setLastLineNumber(oldLastLineNumber);
    176178                    m_lexer->setLineNumber(oldLineNumber);
    177                     failIfTrue(m_error);
     179                    failIfTrue(hasError());
    178180                    continue;
    179181                }
     
    183185        context.appendStatement(sourceElements, statement);
    184186    }
    185    
    186     if (m_error)
    187         fail();
     187
     188    failIfTrue(hasError());
    188189    return sourceElements;
    189190}
     
    201202    int scratch3 = 0;
    202203    TreeExpression varDecls = parseVarDeclarationList(context, scratch, scratch1, scratch2, scratch3, scratch3, scratch3);
    203     failIfTrue(m_error);
     204    failIfTrue(hasError());
    204205    failIfFalse(autoSemiColon());
    205206   
     
    215216    int end = 0;
    216217    TreeConstDeclList constDecls = parseConstDeclarationList(context);
    217     failIfTrue(m_error);
     218    failIfTrue(hasError());
    218219    failIfFalse(autoSemiColon());
    219220   
     
    354355        decls = parseVarDeclarationList(context, declarations, forInTarget, forInInitializer, declsStart, initStart, initEnd);
    355356        m_allowsIn = true;
    356         if (m_error)
    357             fail();
    358        
     357        failIfTrue(hasError());
     358
    359359        // Remainder of a standard for loop is handled identically
    360360        if (match(SEMICOLON))
     
    574574    startSwitch();
    575575    TreeClauseList firstClauses = parseSwitchClauses(context);
    576     failIfTrue(m_error);
     576    failIfTrue(hasError());
    577577   
    578578    TreeClause defaultClause = parseSwitchDefaultClause(context);
    579     failIfTrue(m_error);
     579    failIfTrue(hasError());
    580580   
    581581    TreeClauseList secondClauses = parseSwitchClauses(context);
    582     failIfTrue(m_error);
     582    failIfTrue(hasError());
    583583    endSwitch();
    584584    consumeOrFail(CLOSEBRACE);
     
    15381538        if (!re) {
    15391539            const char* yarrErrorMsg = Yarr::checkSyntax(pattern->string());
    1540             ASSERT(!m_errorMessage.isNull());
    15411540            failWithMessage(yarrErrorMsg);
    15421541        }
  • trunk/Source/JavaScriptCore/parser/Parser.h

    r146318 r148167  
    753753    NEVER_INLINE void updateErrorMessage()
    754754    {
    755         m_error = true;
    756755        const char* name = getTokenName(m_token.m_type);
    757756        if (!name)
     
    759758        else
    760759            m_errorMessage = String::format("Unexpected token '%s'", name);
     760        ASSERT(!m_errorMessage.isNull());
    761761    }
    762762   
    763763    NEVER_INLINE void updateErrorMessage(JSTokenType expectedToken)
    764764    {
    765         m_error = true;
    766765        const char* name = getTokenName(expectedToken);
    767766        if (name)
     
    773772                updateErrorMessageSpecialCase(expectedToken);
    774773        }
     774        ASSERT(!m_errorMessage.isNull());
    775775    }
    776776   
    777777    NEVER_INLINE void updateErrorWithNameAndMessage(const char* beforeMsg, String name, const char* afterMsg)
    778778    {
    779         m_error = true;
    780779        m_errorMessage = makeString(beforeMsg, " '", name, "' ", afterMsg);
    781780    }
    782781   
    783782    NEVER_INLINE void updateErrorMessage(const char* msg)
    784     {   
    785         m_error = true;
     783    {
     784        ASSERT(msg);
    786785        m_errorMessage = String(msg);
     786        ASSERT(!m_errorMessage.isNull());
    787787    }
    788788   
     
    890890    }
    891891
     892    bool hasError() const
     893    {
     894        return !m_errorMessage.isNull();
     895    }
     896
    892897    JSGlobalData* m_globalData;
    893898    const SourceCode* m_source;
     
    897902    StackBounds m_stack;
    898903    bool m_hasStackOverflow;
    899     bool m_error;
    900904    String m_errorMessage;
    901905    JSToken m_token;
Note: See TracChangeset for help on using the changeset viewer.