]> git.saurik.com Git - cycript.git/blobdiff - Scanner.lpp.in
The arguments variable needs to be declared Magic.
[cycript.git] / Scanner.lpp.in
index bcbd0d00d99e80e11c0f2a565099ce4a8466783d..1fb16aa5362c3a521b8662ec9efc3b1c08b584a0 100644 (file)
@@ -41,6 +41,9 @@ typedef cy::parser::token tk;
 
 #include "Highlight.hpp"
 
+#include "IdentifierStart.h"
+#include "IdentifierContinue.h"
+
 #define YY_EXTRA_TYPE CYDriver *
 
 #define F(value, highlight) do { \
@@ -52,12 +55,19 @@ typedef cy::parser::token tk;
     return value; \
 } while (false)
 
+#define S(stack) do { \
+    if (yyextra->stack.size() == 1) \
+        E("invalid brace") \
+    yyextra->stack.pop(); \
+} while (false)
+
 #define P yyextra->pool_
 #define A new(P)
 #define Y P.strmemdup(yytext, yyleng)
 
 #define I(type, Type, value, highlight) do { \
     yylval->semantic_.type ## _ = A CY ## Type; \
+    yylval->semantic_.type ## _->location_ = *yylloc; \
     F(value, highlight); \
 } while (false)
 
@@ -77,13 +87,13 @@ typedef cy::parser::token tk;
             nl = reinterpret_cast<const char *>(memchr(nl + 1, '\n', left)); \
         } while (nl != NULL); \
         yylloc->step(); \
-        yylloc->end.lines(lines); \
-        yylloc->end.columns(left); \
+        yylloc->end.Lines(lines); \
+        yylloc->end.Columns(left); \
         more \
     } else L \
 }
 
-#define R yylloc->end.columns(yyleng);
+#define R yylloc->end.Columns(yyleng);
 #define L yylloc->step(); R
 
 #define H(value, highlight) do { \
@@ -133,7 +143,7 @@ static void U(Type_ &local, unsigned point) {
     } else _assert(false);
 }
 
-static void U(char *&local, const char *text, yy_size_t &i) {
+static unsigned U(char *&local, const char *text, yy_size_t &i) {
     unsigned point;
 
     char next(text[++i]);
@@ -151,6 +161,7 @@ static void U(char *&local, const char *text, yy_size_t &i) {
     }
 
     U(local, point);
+    return point;
 }
 
 #define CYLexBufferPoint(point) do { \
@@ -204,15 +215,13 @@ static void U(char *&local, const char *text, yy_size_t &i) {
 %option reentrant
 %option stack
 
-%option full
-%option ecs
-
 U1 [\x00-\x7f]
 U0 [\x80-\xbf]
 U2 [\xc2-\xdf]
 U3 [\xe0-\xef]
 U4 [\xf0-\xf4]
 UN [\xc0-\xc1\xf5-\xff]
+UE {U1}|{U2}|{U3}|{U4}|{UN}
 
 HexDigit [0-9a-fA-F]
 LineTerminatorSequence \r?\n|\r|\xe2\x80[\xa8\xa9]
@@ -234,12 +243,12 @@ IdentifierMore [$_]
 
 UnicodeStart {IdentifierMore}|{UnicodeIDStart}
 UnicodePart {IdentifierMore}|\xe2\x80[\x8c\x8d]|{UnicodeIDContinue}
-UnicodeFail {U2}|{U3}|{U3}{U0}|{U4}|{U4}{U0}|{U4}{U0}{U0}|{UN}|{U0}
-UnicodeScrap {UnicodePart}*{UnicodeFail}?
+UnicodeScrap {U2}|{U3}{U0}{0,1}|{U4}{U0}{0,2}|{UN}|{U0}
+UnicodeError ({U2}|{U3}{U0}{0,1}|{U4}{U0}{0,2}){UE}|{UN}|{U0}
 
 IdentifierStart {UnicodeStart}|{UnicodeEscape}
 IdentifierPart {UnicodePart}|{UnicodeEscape}
-IdentifierFail {UnicodeFail}|\\(u({HexDigit}{0,3}|\{{HexDigit}*))?
+IdentifierFail {UnicodeError}|\\(u({HexDigit}{0,3}|\{{HexDigit}*))?
 IdentifierScrap {IdentifierPart}*{IdentifierFail}?
 
 RegularExpressionBackslashSequence \\{NoneTerminatorCharacter}
@@ -263,8 +272,6 @@ XMLName {XMLNameStart}{XMLNamePart}*
 
 %s Div
 %s DivOrTemplateTail
-%s RegExp
-%s RegExpOrTemplateTail
 
 @begin E4X
 %x XMLContent
@@ -274,38 +281,43 @@ XMLName {XMLNameStart}{XMLNamePart}*
 %%
 
     /* RegEx {{{ */
-<RegExp,RegExpOrTemplateTail>\/ L CYLexBufferStart(RegularExpression); CYLexBufferUnit('/');
-
 <RegularExpression>{
     \/{UnicodePart}* R CYLexBufferUnits(yytext, yyleng); CYLexBufferEnd(literal, RegEx, tk::RegularExpressionLiteral, hi::Constant);
-    \/{UnicodePart}*{UnicodeFail} R E("invalid flags")
+    \/{UnicodePart}*{UnicodeError} R E("invalid character");
 
     {RegExCharacter}+ R CYLexBufferUnits(yytext, yyleng);
-    {RegExCharacter}*{UnicodeFail} R E("invalid character");
 
     {RegularExpressionBackslashSequence} R CYLexBufferUnits(yytext, yyleng);
-    \\{UnicodeFail}? R E("invalid escape")
+    \\ R E("invalid escape")
+
+    (\\|{RegExCharacter}+)?{LineTerminatorSequence} R E("invalid newline");
+    (\\|{RegExCharacter}+)?{UnicodeScrap} R E("invalid character");
 
     "["{RegularExpressionClassChars}"]" R CYLexBufferUnits(yytext, yyleng);
     "["{RegularExpressionClassChars}\\? R E("invalid class");
-    "["{RegularExpressionClassChars}\\?{UnicodeFail} R E("invalid character");
+
     "["{RegularExpressionClassChars}\\?{LineTerminatorSequence} R E("invalid newline");
+    "["{RegularExpressionClassChars}\\?{UnicodeScrap} R E("invalid character");
 
-    (\\|{RegExCharacter}+)?{LineTerminatorSequence} R E("invalid newline");
     <<EOF>> R E("unterminated regex")
 }
     /* }}} */
     /* Comment {{{ */
-#![^\n]* L M
-\/\/[^\n]* L M
+    /* XXX: maybe fold LineTerminatorSequence into these definitions */
+#!{NoneTerminatorCharacter}* L M
+\/\/{NoneTerminatorCharacter}* L M
+(#!|\/\/){NoneTerminatorCharacter}*{UnicodeError} L E("invalid character");
 
 \/\* L yy_push_state(MultiLine, yyscanner);
 
 <MultiLine>{
     \**\*\/ R yy_pop_state(yyscanner); M N
-    \**{LineTerminatorSequence} yylloc->end.lines(); yyextra->last_ = true;
+    \**{LineTerminatorSequence} yylloc->end.Lines(); yyextra->last_ = true;
     \**{CommentCharacter}|\/ R
-    \**({UnicodeFail}|\*) R E("invalid comment");
+
+    \**{UnicodeScrap} R E("invalid character");
+    \**\* R E("invalid comment");
+
     <<EOF>> R E("invalid comment")
 }
     /* }}} */
@@ -385,8 +397,8 @@ XMLName {XMLNameStart}{XMLNamePart}*
 "*="   L F(tk::StarEqual, hi::Operator);
 "~"    L F(tk::Tilde, hi::Operator);
 
-<Div,DivOrTemplateTail>"/"  L F(tk::Slash, hi::Operator);
-<Div,DivOrTemplateTail>"/=" L F(tk::SlashEqual, hi::Operator);
+"/"  L F(tk::Slash, hi::Operator);
+"/=" L F(tk::SlashEqual, hi::Operator);
 
 ":"    L F(tk::Colon, hi::Structure);
 ","    L F(tk::Comma, hi::Structure);
@@ -397,7 +409,7 @@ XMLName {XMLNameStart}{XMLNamePart}*
 ")"    L F(tk::CloseParen, hi::Structure);
 
 "{"    L yyextra->template_.push(false); F(yyextra->newline_ ? tk::OpenBrace_ : tk::OpenBrace, hi::Structure);
-<Div,RegExp>"}" L yyextra->template_.pop(); F(tk::CloseBrace, hi::Structure);
+<Div>"}" L S(template_); F(tk::CloseBrace, hi::Structure);
 
 "["    L F(tk::OpenBracket, hi::Structure);
 "]"    L F(tk::CloseBracket, hi::Structure);
@@ -425,7 +437,7 @@ XMLName {XMLNameStart}{XMLNamePart}*
 "@YES"            L F(tk::At_YES_, hi::Constant);
 @end
 
-@({UnicodeStart}{UnicodeScrap}|{UnicodeFail}) L E("invalid keyword")
+@({UnicodeStart}{UnicodePart}*{UnicodeError}?|{UnicodeError}) L E("invalid keyword")
     /* }}} */
     /* Highlight {{{ */
 "undefined"       L F(tk::_undefined_, hi::Operator);
@@ -450,6 +462,7 @@ XMLName {XMLNameStart}{XMLNamePart}*
 "char"            L /*FII*/ F(tk::_char_, hi::Type);
 "class"           L /*FFK*/ F(tk::_class_, hi::Meta);
 "const"           L /*FFK*/ F(tk::_const_, hi::Meta);
+"constructor"     L /*III*/ F(tk::_constructor_, hi::Special);
 "continue"        L /*KKK*/ F(tk::_continue_, hi::Control);
 "debugger"        L /*FKK*/ F(tk::_debugger_, hi::Meta);
 "default"         L /*KKK*/ F(tk::_default_, hi::Control);
@@ -460,6 +473,7 @@ XMLName {XMLNameStart}{XMLNamePart}*
 "enum"            L /*FFF*/ F(tk::_enum_, hi::Meta);
 "export"          L /*FFK*/ F(tk::_export_, hi::Meta);
 "extends"         L /*FFK*/ F(tk::_extends_, hi::Meta);
+"eval"            L /*III*/ F(tk::_eval_, hi::Special);
 "false"           L /*LLL*/ F(tk::_false_, hi::Constant);
 "final"           L /*FII*/ F(tk::_final_, hi::Meta);
 "finally"         L /*KKK*/ F(tk::_finally_, hi::Control);
@@ -473,6 +487,7 @@ XMLName {XMLNameStart}{XMLNamePart}*
 "implements"      L /*FSS*/ F(tk::_implements_, hi::Meta);
 "import"          L /*FFK*/ F(tk::_import_, hi::Meta);
 "in"              L /*KKK*/ F(yyextra->in_.top() ? tk::_in__ : tk::_in_, hi::Operator);
+"Infinity"        L /*III*/ F(tk::_Infinity_, hi::Constant);
 "instanceof"      L /*KKK*/ F(tk::_instanceof_, hi::Operator);
 "int"             L /*FII*/ F(tk::_int_, hi::Type);
 "interface"       L /*FSS*/ F(tk::_interface_, hi::Meta);
@@ -484,6 +499,7 @@ XMLName {XMLNameStart}{XMLNamePart}*
 "package"         L /*FSS*/ F(tk::_package_, hi::Meta);
 "private"         L /*FSS*/ F(tk::_private_, hi::Meta);
 "protected"       L /*FSS*/ F(tk::_protected_, hi::Meta);
+"prototype"       L /*III*/ F(tk::_prototype_, hi::Special);
 "public"          L /*FSS*/ F(tk::_public_, hi::Meta);
 "return"          L /*KKK*/ F(yyextra->return_.top() ? tk::_return__ : tk::_return_, hi::Control);
 "set"             L /*III*/ F(tk::_set_, hi::Meta);
@@ -492,6 +508,7 @@ XMLName {XMLNameStart}{XMLNamePart}*
 "super"           L /*FFK*/ F(tk::_super_, hi::Constant);
 "switch"          L /*KKK*/ F(tk::_switch_, hi::Control);
 "synchronized"    L /*FII*/ F(tk::_synchronized_, hi::Meta);
+"target"          L /*III*/ F(tk::_target_, hi::Identifier);
 "this"            L /*KKK*/ F(tk::_this_, hi::Constant);
 "throw"           L /*KKK*/ F(tk::_throw_, hi::Control);
 "throws"          L /*FII*/ F(tk::_throws_, hi::Meta);
@@ -538,8 +555,12 @@ XMLName {XMLNameStart}{XMLNamePart}*
         char next(yytext[i]);
         if (next != '\\')
             *local++ = next;
-        else
-            U(local, yytext, ++i);
+        else {
+            bool (*is)(unsigned) = (i == 0 ? &IsIdentifierStart : &IsIdentifierContinue);
+            unsigned point(U(local, yytext, ++i));
+            if (!is(point))
+                E("invalid character");
+        }
     }
 
     *local = '\0';
@@ -565,21 +586,21 @@ XMLName {XMLNameStart}{XMLNamePart}*
 <LegacySingleString,StrictSingleString>{
     \' R CYLexBufferEnd(string, String, tk::StringLiteral, hi::Constant);
     {SingleCharacter}+ R CYLexBufferUnits(yytext, yyleng);
-    {SingleCharacter}*{UnicodeFail} R E("invalid character");
-    {LineTerminatorSequence} R E("invalid newline");
+    {SingleCharacter}*{LineTerminatorSequence} R E("invalid newline");
+    {SingleCharacter}*{UnicodeScrap} R E("invalid character");
 }
 
 \" L CYLexBufferStart(LegacyDoubleString);
 <LegacyDoubleString,StrictDoubleString>{
     \" R CYLexBufferEnd(string, String, tk::StringLiteral, hi::Constant);
     {DoubleCharacter}+ R CYLexBufferUnits(yytext, yyleng);
-    {DoubleCharacter}*{UnicodeFail} R E("invalid character");
-    {LineTerminatorSequence} R E("invalid newline");
+    {DoubleCharacter}*{LineTerminatorSequence} R E("invalid newline");
+    {DoubleCharacter}*{UnicodeScrap} R E("invalid character");
 }
     /* }}} */
     /* Template {{{ */
 "`" L yyextra->tail_ = false; CYLexBufferStart(StrictAccentString);
-<DivOrTemplateTail,RegExpOrTemplateTail>"}" L yyextra->tail_ = true; yyextra->template_.pop(); CYLexBufferStart(StrictAccentString);
+<DivOrTemplateTail>"}" L yyextra->tail_ = true; S(template_); CYLexBufferStart(StrictAccentString);
 
 <StrictAccentString>{
     "`" R CYLexBufferEnd(string, String, yyextra->tail_ ? tk::TemplateTail : tk::NoSubstitutionTemplate, hi::Constant);
@@ -588,8 +609,8 @@ XMLName {XMLNameStart}{XMLNamePart}*
     "$" R CYLexBufferUnit('$');
 
     {PlateCharacter}+ R CYLexBufferUnits(yytext, yyleng);
-    {PlateCharacter}*{UnicodeFail} R E("invalid character");
-    {LineTerminatorSequence} R E("invalid newline");
+    {PlateCharacter}*{LineTerminatorSequence} R E("invalid newline");
+    {PlateCharacter}*{UnicodeScrap} R E("invalid character");
 }
     /* }}} */
     /* Escapes {{{ */
@@ -623,23 +644,27 @@ XMLName {XMLNameStart}{XMLNamePart}*
         CYLexBufferPoint(point);
     }
 
-    \\{LineTerminatorSequence} yylloc->end.lines();
-    \\(.|{NotLineTerminator}) R CYLexBufferUnits(yytext + 1, yyleng - 1);
+    \\{LineTerminatorSequence} yylloc->end.Lines();
+    \\{NoneTerminatorCharacter} R CYLexBufferUnits(yytext + 1, yyleng - 1);
+    \\{UnicodeScrap} R E("invalid character");
 
-    \\(x{HexDigit}{0,1}|u({HexDigit}{0,3}|\{{HexDigit}*)|{UnicodeFail})? R E("invalid escape");
+    \\(x{HexDigit}{0,1}|u({HexDigit}{0,3}|\{{HexDigit}*))? R E("invalid escape");
     <<EOF>> R E("invalid string");
 }
     /* }}} */
 
-{LineTerminatorSequence} yylloc->step(); yylloc->end.lines(); yyextra->last_ = true; N
+{LineTerminatorSequence} yylloc->step(); yylloc->end.Lines(); yyextra->last_ = true; N
 {WhiteSpace} L
+{U1}|{UnicodeScrap} L E("invalid character");
 
 <<EOF>> if (yyextra->auto_) { yyextra->auto_ = false; F(tk::AutoComplete, hi::Nothing); } L yyterminate();
 
-. L E("invalid character")
-
 %%
 
+#undef yyextra
+#define yyextra this
+#define yyscanner scanner_
+
 void CYDriver::ScannerInit() {
     cylex_init(&scanner_);
     cyset_extra(this, scanner_);
@@ -649,13 +674,17 @@ void CYDriver::ScannerDestroy() {
     cylex_destroy(scanner_);
 }
 
+void CYDriver::SetRegEx(bool equal) {
+    CYLexBufferStart(RegularExpression);
+    CYLexBufferUnit('/');
+    if (equal)
+        CYLexBufferUnit('=');
+}
+
 void CYDriver::SetCondition(Condition condition) {
     struct yyguts_t *yyg(reinterpret_cast<struct yyguts_t *>(scanner_));
 
     switch (condition) {
-        case RegExpCondition:
-            BEGIN(template_.top() ? RegExpOrTemplateTail : RegExp);
-            break;
 @begin E4X
         case XMLContentCondition:
             BEGIN(XMLContent);
@@ -671,9 +700,6 @@ void CYDriver::SetCondition(Condition condition) {
 
 void CYDriver::PushCondition(Condition condition) {
     switch (condition) {
-        case RegExpCondition:
-            yy_push_state(RegExp, scanner_);
-            break;
 @begin E4X
         case XMLContentCondition:
             yy_push_state(XMLContent, scanner_);