/* Cycript - Inlining/Optimizing JavaScript Compiler
 * Copyright (C) 2009  Jay Freeman (saurik)
*/

/* Modified BSD License {{{ */
/*
 *        Redistribution and use in source and binary
 * forms, with or without modification, are permitted
 * provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the
 *    above copyright notice, this list of conditions
 *    and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions
 *    and the following disclaimer in the documentation
 *    and/or other materials provided with the
 *    distribution.
 * 3. The name of the author may not be used to endorse
 *    or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* }}} */

/* XXX: supposedly I will be screwed on very very long multi-line comments and need to replace these with a manual lexer. http://websrv.cs.fsu.edu/~engelen/courses/COP5621/Pr2.pdf */

%{
#define YYLTYPE cy::location
#include "Cycript.tab.hh"
typedef cy::parser::token tk;

#define YY_EXTRA_TYPE CYDriver *

#define T yylval->newline_ = yyextra->state_ == CYNewLine; BEGIN(Div);
#define C T yyextra->state_ = CYClear;
#define R T yyextra->state_ = CYRestricted;

#define E(prefix) L C { \
    char *value(reinterpret_cast<char *>(apr_palloc(yyextra->pool_, yyleng + sizeof(prefix)))); \
    memcpy(value, prefix, sizeof(prefix) - 1); \
    memcpy(value + sizeof(prefix) - 1, yytext, yyleng); \
    value[yyleng + sizeof(prefix) - 1] = '\0'; \
    yylval->literal_ = new(yyextra->pool_) CYRegEx(value); \
    return tk::RegularExpressionLiteral; \
}

#define N \
    if (yyextra->state_ != CYNewLine) { \
        bool restricted(yyextra->state_ == CYRestricted); \
        if (restricted) { \
            yyextra->state_ = CYClear; \
            return tk::NewLine; \
        } else \
            yyextra->state_ = CYNewLine; \
    }

#define V(more) { \
    if (const char *nl = reinterpret_cast<const char *>(memchr(yytext, '\n', yyleng))) { \
        unsigned lines(0); \
        size_t left; \
        do { \
            ++lines; \
            left = yyleng - (nl - yytext) - 1; \
            nl = reinterpret_cast<const char *>(memchr(nl + 1, '\n', left)); \
        } while (nl != NULL); \
        yylloc->end.lines(lines); \
        yylloc->end.columns(left); \
        yylloc->step(); \
        more \
    } else L \
}

#define L { \
    yylloc->step(); \
    yylloc->columns(yyleng); \
}

int H(char c) {
    if (c >= '0' && c <= '9')
        return c - '0';
    if (c >= 'a' && c <= 'f')
        return c - 'a' + 10;
    if (c >= 'A' && c <= 'F')
        return c - 'A' + 10;
    return -1;
}

#define YY_INPUT(data, value, size) { \
    if (yyextra->file_ != NULL) { \
        size_t copy(fread(data, 1, size, yyextra->file_)); \
        value = copy == 0 ? YY_NULL : copy; \
    } else if (yyextra->size_ == 0) \
        value = YY_NULL; \
    else { \
        size_t copy(size); \
        copy = (std::min(copy, yyextra->size_)); \
        memcpy(data, yyextra->data_, copy); \
        yyextra->data_ += copy; \
        yyextra->size_ -= copy; \
        value = copy; \
    } \
}

%}

%option prefix="cy"
%option bison-bridge
%option bison-locations
%option noyywrap
%option yylineno
%option nounput
%option interactive
%option reentrant
%option stack

Exponent [eE][+-]?[0-9]+
Escape   \\[\\'"bfnrtv]|\\0|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\\n

IdentifierStart [a-zA-Z$_]
IdentifierPart  [a-zA-Z$_0-9]

NonTerminator [^\n]
BackslashSequence \\{NonTerminator}
RegularExpressionFirstChar [^\n*\\/]|{BackslashSequence}
RegularExpressionChar [^\n\\/]|{BackslashSequence}
RegularExpressionFlags {IdentifierPart}*
RegularExpressionChars {RegularExpressionChar}*
RegularExpressionBody {RegularExpressionFirstChar}{RegularExpressionChars}

@begin E4X
XMLNameStart [a-zA-Z_:]
XMLNamePart [a-zA-Z0-9.-_:]
XMLName {XMLNameStart}{XMLNamePart}*
@end

%s Div
%s RegExp

@begin E4X
%x XMLContent
%x XMLTag
@end

%%

<RegExp>\/{RegularExpressionBody}\/{RegularExpressionFlags} E("")

\/\/[^\n]* L

	/* http://ostermiller.org/findcomment.html */
	/* XXX: unify these two rules using !? */
\/\*!([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/ V() C yylval->comment_ = new(yyextra->pool_) CYComment(apr_pstrmemdup(yyextra->pool_, yytext, yyleng)); return tk::Comment;
\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/ V(N)

@begin E4X
<RegExp>"<>"      L return tk::LeftRight;
<XMLContent>"</>" L return tk::LeftSlashRight;

<RegExp,XMLContent>\<!\[CDATA\[(\n|[^[]|\[[^[]|\[\[[^>])*]]> V() return tk::XMLCDATA;
<RegExp,XMLContent>\<!--(\n|[^-]|-[^-])*--> V() return tk::XMLComment;
<RegExp,XMLContent>\<?(\n|[^?]|\?[^>])*?> V() return tk::XMLPI;

<XMLTag>"="  L return tk::Equal;
<XMLTag>">"  L return tk::Right;
<XMLTag>"/>" L return tk::SlashRight;
<XMLTag>"{"  L return tk::OpenBrace;

<XMLTag>\"(\n|[^"])*\"|'(\n|[^'])*' V() {
    return tk::XMLAttributeValue;
}

<XMLTag>{XMLName} L return tk::XMLName;
<XMLTag>[ \t\r\n] V() return tk::XMLWhitespace;

<XMLContent>"{"  L return tk::OpenBrace;
<XMLContent>"<"  L return tk::Left;
<XMLContent>"</" L return tk::LeftSlash;
@end

@begin E4X
"::"   L C return tk::ColonColon;
".."   L C return tk::PeriodPeriod;
@end

@begin E4X ObjectiveC
"@"    L C return tk::At;
@end

"&"    L C return tk::Ampersand;
"&&"   L C return tk::AmpersandAmpersand;
"&="   L C return tk::AmpersandEqual;
"^"    L C return tk::Carrot;
"^="   L C return tk::CarrotEqual;
"="    L C return tk::Equal;
"=="   L C return tk::EqualEqual;
"==="  L C return tk::EqualEqualEqual;
"!"    L C return tk::Exclamation;
"!="   L C return tk::ExclamationEqual;
"!=="  L C return tk::ExclamationEqualEqual;
"-"    L C return tk::Hyphen;
"-="   L C return tk::HyphenEqual;
"--"   L C return yylval->newline_ ? tk::HyphenHyphen_ : tk::HyphenHyphen;
"->"   L C return tk::HyphenRight;
"<"    L C return tk::Left;
"<="   L C return tk::LeftEqual;
"<<"   L C return tk::LeftLeft;
"<<="  L C return tk::LeftLeftEqual;
"%"    L C return tk::Percent;
"%="   L C return tk::PercentEqual;
"."    L C return tk::Period;
"|"    L C return tk::Pipe;
"|="   L C return tk::PipeEqual;
"||"   L C return tk::PipePipe;
"+"    L C return tk::Plus;
"+="   L C return tk::PlusEqual;
"++"   L C return yylval->newline_ ? tk::PlusPlus_ : tk::PlusPlus;
">"    L C return tk::Right;
">="   L C return tk::RightEqual;
">>"   L C return tk::RightRight;
">>="  L C return tk::RightRightEqual;
">>>"  L C return tk::RightRightRight;
">>>=" L C return tk::RightRightRightEqual;
"*"    L C return tk::Star;
"*="   L C return tk::StarEqual;
"~"    L C return tk::Tilde;

<Div>"/"  L C return tk::Slash;
<Div>"/=" L C return tk::SlashEqual;

":"    L C return tk::Colon;
","    L C return tk::Comma;
"?"    L C return tk::Question;
";"    L C return tk::SemiColon;

"("    L C return tk::OpenParen;
")"    L C return tk::CloseParen;

"{"    L C return tk::OpenBrace;
"}"    L C return tk::CloseBrace;

"["    L C return tk::OpenBracket;
"]"    L C return tk::CloseBracket;

@begin ObjectiveC
"@class"       L C return tk::AtClass;
"@end"         L C return tk::AtEnd;
"@selector"    L C return tk::AtSelector;
@end

"false"        L C yylval->false_ = new(yyextra->pool_) CYFalse(); return tk::False;
"null"         L C yylval->null_ = new(yyextra->pool_) CYNull(); return tk::Null;
"true"         L C yylval->true_ = new(yyextra->pool_) CYTrue(); return tk::True;

"break"        L R yylval->word_ = new(yyextra->pool_) CYWord("break"); return tk::Break;
"case"         L C yylval->word_ = new(yyextra->pool_) CYWord("case"); return tk::Case;
"catch"        L C yylval->word_ = new(yyextra->pool_) CYWord("catch"); return tk::Catch;
"continue"     L R yylval->word_ = new(yyextra->pool_) CYWord("continue"); return tk::Continue;
"default"      L C yylval->word_ = new(yyextra->pool_) CYWord("default"); return tk::Default;
"delete"       L C yylval->word_ = new(yyextra->pool_) CYWord("delete"); return tk::Delete;
"do"           L C yylval->word_ = new(yyextra->pool_) CYWord("do"); return tk::Do;
"else"         L C yylval->word_ = new(yyextra->pool_) CYWord("else"); return tk::Else;
"finally"      L C yylval->word_ = new(yyextra->pool_) CYWord("finally"); return tk::Finally;
"for"          L C yylval->word_ = new(yyextra->pool_) CYWord("for"); return tk::For;
"function"     L C yylval->word_ = new(yyextra->pool_) CYWord("function"); return tk::Function;
"if"           L C yylval->word_ = new(yyextra->pool_) CYWord("if"); return tk::If;
"in"           L C yylval->word_ = new(yyextra->pool_) CYWord("in"); return tk::In;
"instanceof"   L C yylval->word_ = new(yyextra->pool_) CYWord("instanceof"); return tk::InstanceOf;
"new"          L C yylval->word_ = new(yyextra->pool_) CYWord("new"); return tk::New;
"return"       L R yylval->word_ = new(yyextra->pool_) CYWord("return"); return tk::Return;
"switch"       L C yylval->word_ = new(yyextra->pool_) CYWord("switch"); return tk::Switch;
"this"         L C yylval->this_ = new(yyextra->pool_) CYThis(); return tk::This;
"throw"        L R yylval->word_ = new(yyextra->pool_) CYWord("throw"); return tk::Throw;
"try"          L C yylval->word_ = new(yyextra->pool_) CYWord("try"); return tk::Try;
"typeof"       L C yylval->word_ = new(yyextra->pool_) CYWord("typeof"); return tk::TypeOf;
"var"          L C yylval->word_ = new(yyextra->pool_) CYWord("var"); return tk::Var;
"void"         L C yylval->word_ = new(yyextra->pool_) CYWord("void"); return tk::Void;
"while"        L C yylval->word_ = new(yyextra->pool_) CYWord("while"); return tk::While;
"with"         L C yylval->word_ = new(yyextra->pool_) CYWord("with"); return tk::With;

"debugger"     L C yylval->word_ = new(yyextra->pool_) CYWord("debugger"); return tk::Debugger;

"const"        L C yylval->word_ = new(yyextra->pool_) CYWord("const"); return tk::Const;

"class"        L C yylval->word_ = new(yyextra->pool_) CYWord("class"); return tk::Class;
"enum"         L C yylval->word_ = new(yyextra->pool_) CYWord("enum"); return tk::Enum;
"export"       L C yylval->word_ = new(yyextra->pool_) CYWord("export"); return tk::Export;
"extends"      L C yylval->word_ = new(yyextra->pool_) CYWord("extends"); return tk::Extends;
"import"       L C yylval->word_ = new(yyextra->pool_) CYWord("import"); return tk::Import;
"super"        L C yylval->word_ = new(yyextra->pool_) CYWord("super"); return tk::Super;

"implements"   L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("implements"); return tk::Implements;
"interface"    L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("interface"); return tk::Interface;
"package"      L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("package"); return tk::Package;
"private"      L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("private"); return tk::Private;
"protected"    L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("protected"); return tk::Protected;
"public"       L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("public"); return tk::Public;
"static"       L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("static"); return tk::Static;

"abstract"     L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("abstract"); return tk::Abstract;
"boolean"      L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("boolean"); return tk::Boolean;
"byte"         L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("byte"); return tk::Byte;
"char"         L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("char"); return tk::Char;
"double"       L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("double"); return tk::Double;
"final"        L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("final"); return tk::Final;
"float"        L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("float"); return tk::Float;
"goto"         L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("goto"); return tk::Goto;
"int"          L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("int"); return tk::Int;
"long"         L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("long"); return tk::Long;
"native"       L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("native"); return tk::Native;
"short"        L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("short"); return tk::Short;
"synchronized" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("synchronized"); return tk::Synchronized;
"throws"       L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("throws"); return tk::Throws;
"transient"    L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("transient"); return tk::Transient;
"volatile"     L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("volatile"); return tk::Volatile;

"let"          L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("let"); return tk::Let;
"yield"        L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("yield"); return tk::Yield;

"each"         L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("each"); return tk::Each;

@begin E4X
"namespace"    L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("namespace"); return tk::Namespace;
"xml"          L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("xml"); return tk::XML;
@end

{IdentifierStart}{IdentifierPart}* L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier(apr_pstrmemdup(yyextra->pool_, yytext, yyleng)); return tk::Identifier_;

(\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? L C yylval->number_ = new(yyextra->pool_) CYNumber(strtod(yytext, NULL)); return tk::NumericLiteral;

0[xX][0-9a-fA-F]+ L C yylval->number_ = new(yyextra->pool_) CYNumber(strtoull(yytext + 2, NULL, 16)); return tk::NumericLiteral;
0[0-7]+ L C yylval->number_ = new(yyextra->pool_) CYNumber(strtoull(yytext + 1, NULL, 8)); return tk::NumericLiteral;
0[bB][0-1]+ L C yylval->number_ = new(yyextra->pool_) CYNumber(strtoull(yytext + 2, NULL, 2)); return tk::NumericLiteral;

\"([^"\\\n]|{Escape})*\"|'([^'\\\n]|{Escape})*' L C {
    char *value(reinterpret_cast<char *>(apr_palloc(yyextra->pool_, yyleng)));
    char *local(value);

    for (yy_size_t i(1), e(yyleng - 1); i != e; ++i) {
        char next(yytext[i]);

        if (yytext[i] == '\\')
            switch (next = yytext[++i]) {
                case '\n': continue;
                case '\\': next = '\\'; break;
                case '\'': next = '\''; break;
                case '"': next = '"'; break;
                case 'b': next = '\b'; break;
                case 'f': next = '\f'; break;
                case 'n': next = '\n'; break;
                case 'r': next = '\r'; break;
                case 't': next = '\t'; break;
                case 'v': next = '\v'; break;
                case '0': next = '\0'; break;

                case 'x':
                    next = H(yytext[i + 1]) << 4 | H(yytext[i + 2]);
                    i += 2;
                break;
            }

        *local++ = next;
    }

    *local = '\0';
    yylval->string_ = new(yyextra->pool_) CYString(value, local - value);
    return tk::StringLiteral;
}

\r?\n yylloc->end.lines(); yylloc->step(); N

[ \t] L

<<EOF>> if (yyextra->auto_) { yyextra->auto_ = false; return tk::AutoComplete; } L yyterminate();

. L {
    CYDriver::Error error;
    error.location_ = *yylloc;
    error.message_ = "syntax error, unknown token";
    yyextra->errors_.push_back(error);
    yyterminate();
}

%%

void CYDriver::ScannerInit() {
    cylex_init(&scanner_);
    cyset_extra(this, scanner_);
}

void CYDriver::ScannerDestroy() {
    cylex_destroy(scanner_);
}

CYDriver::Condition CYDriver::GetCondition() {
    switch (yy_top_state(scanner_)) {
        case RegExp:
            return RegExpCondition;
@begin E4X
        case XMLContent:
            return XMLContentCondition;
        case XMLTag:
            return XMLTagCondition;
@end
        default:
            _assert(false);
    }
}

void CYDriver::SetCondition(Condition condition) {
    struct yyguts_t *yyg(reinterpret_cast<struct yyguts_t *>(scanner_));

    switch (condition) {
        case RegExpCondition:
            BEGIN(RegExp);
            break;
@begin E4X
        case XMLContentCondition:
            BEGIN(XMLContent);
            break;
        case XMLTagCondition:
            BEGIN(XMLTag);
            break;
@end
        default:
            _assert(false);
    }
}

void CYDriver::PushCondition(Condition condition) {
    switch (condition) {
        case RegExpCondition:
            yy_push_state(RegExp, scanner_);
            break;
@begin E4X
        case XMLContentCondition:
            yy_push_state(XMLContent, scanner_);
            break;
        case XMLTagCondition:
            yy_push_state(XMLTag, scanner_);
            break;
@end
        default:
            _assert(false);
    }
}

void CYDriver::PopCondition() {
    yy_pop_state(scanner_);
}