]> git.saurik.com Git - cycript.git/blobdiff - Scanner.lpp.in
Use macros to simplify semantic actions for lexer.
[cycript.git] / Scanner.lpp.in
index 72011986e4b26010b69b1e62f74cba2b6d4ce192..399feb828e273a44cdb3d45f360a29e4ed4cfa62 100644 (file)
@@ -41,12 +41,12 @@ typedef cy::parser::token tk;
 
 #include "Highlight.hpp"
 
+#include "IdentifierStart.h"
+#include "IdentifierContinue.h"
+
 #define YY_EXTRA_TYPE CYDriver *
 
 #define F(value, highlight) do { \
-    yyextra->newline_ = yyextra->last_; \
-    yyextra->last_ = false; \
-    yyextra->next_ = false; \
     BEGIN(yyextra->template_.top() ? DivOrTemplateTail : Div); \
     yylval->highlight_ = highlight; \
     return value; \
@@ -69,7 +69,7 @@ typedef cy::parser::token tk;
 } while (false)
 
 #define N \
-    if (yyextra->last_ && yyextra->next_) { \
+    if (yyextra->last_) { \
         yyextra->last_ = false; \
         F(tk::NewLine, hi::Nothing); \
     }
@@ -140,7 +140,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]);
@@ -158,6 +158,7 @@ static void U(char *&local, const char *text, yy_size_t &i) {
     }
 
     U(local, point);
+    return point;
 }
 
 #define CYLexBufferPoint(point) do { \
@@ -217,6 +218,7 @@ 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]
@@ -238,12 +240,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}
@@ -278,26 +280,30 @@ XMLName {XMLNameStart}{XMLNamePart}*
     /* RegEx {{{ */
 <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);
 
@@ -305,7 +311,10 @@ XMLName {XMLNameStart}{XMLNamePart}*
     \**\*\/ R yy_pop_state(yyscanner); M N
     \**{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")
 }
     /* }}} */
@@ -354,13 +363,13 @@ XMLName {XMLNameStart}{XMLNamePart}*
 "="    L F(tk::Equal, hi::Operator);
 "=="   L F(tk::EqualEqual, hi::Operator);
 "==="  L F(tk::EqualEqualEqual, hi::Operator);
-"=>"   L F(yyextra->newline_ ? tk::EqualRight_ : tk::EqualRight, hi::Operator);
+"=>"   L F(tk::EqualRight, hi::Operator);
 "!"    L F(tk::Exclamation, hi::Operator);
 "!="   L F(tk::ExclamationEqual, hi::Operator);
 "!=="  L F(tk::ExclamationEqualEqual, hi::Operator);
 "-"    L F(tk::Hyphen, hi::Operator);
 "-="   L F(tk::HyphenEqual, hi::Operator);
-"--"   L F(yyextra->newline_ ? tk::HyphenHyphen_ : tk::HyphenHyphen, hi::Operator);
+"--"   L F(tk::HyphenHyphen, hi::Operator);
 "->"   L F(tk::HyphenRight, hi::Operator);
 "<"    L F(tk::Left, hi::Operator);
 "<="   L F(tk::LeftEqual, hi::Operator);
@@ -374,7 +383,7 @@ XMLName {XMLNameStart}{XMLNamePart}*
 "||"   L F(tk::PipePipe, hi::Operator);
 "+"    L F(tk::Plus, hi::Operator);
 "+="   L F(tk::PlusEqual, hi::Operator);
-"++"   L F(yyextra->newline_ ? tk::PlusPlus_ : tk::PlusPlus, hi::Operator);
+"++"   L F(tk::PlusPlus, hi::Operator);
 ">"    L F(tk::Right, hi::Operator);
 ">="   L F(tk::RightEqual, hi::Operator);
 ">>"   L F(tk::RightRight, hi::Operator);
@@ -396,7 +405,7 @@ XMLName {XMLNameStart}{XMLNamePart}*
 "("    L F(tk::OpenParen, hi::Structure);
 ")"    L F(tk::CloseParen, hi::Structure);
 
-"{"    L yyextra->template_.push(false); F(yyextra->newline_ ? tk::OpenBrace_ : tk::OpenBrace, hi::Structure);
+"{"    L yyextra->template_.push(false); F(tk::OpenBrace, hi::Structure);
 <Div>"}" L S(template_); F(tk::CloseBrace, hi::Structure);
 
 "["    L F(tk::OpenBracket, hi::Structure);
@@ -425,7 +434,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);
@@ -474,7 +483,8 @@ XMLName {XMLNameStart}{XMLNamePart}*
 "if"              L /*KKK*/ F(tk::_if_, hi::Control);
 "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);
+"in"              L /*KKK*/ F(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);
@@ -488,13 +498,14 @@ XMLName {XMLNameStart}{XMLNamePart}*
 "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);
+"return"          L /*KKK*/ F(tk::_return_, hi::Control);
 "set"             L /*III*/ F(tk::_set_, hi::Meta);
 "short"           L /*FII*/ F(tk::_short_, hi::Type);
 "static"          L /*FS?*/ F(tk::_static_, hi::Meta);
-"super"           L /*FFK*/ F(yyextra->super_.top() ? tk::_super__ : tk::_super_, hi::Constant);
+"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);
@@ -507,7 +518,7 @@ XMLName {XMLNameStart}{XMLNamePart}*
 "volatile"        L /*FII*/ F(tk::_volatile_, hi::Meta);
 "while"           L /*KKK*/ F(tk::_while_, hi::Control);
 "with"            L /*KKK*/ F(tk::_with_, hi::Control);
-"yield"           L /*IS?*/ F(yyextra->yield_.top() ? tk::_yield__ : tk::_yield_, hi::Control);
+"yield"           L /*IS?*/ F(tk::_yield_, hi::Control);
 
 "auto"            L F(tk::_auto_, hi::Meta);
 "each"            L F(tk::_each_, hi::Control);
@@ -541,8 +552,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';
@@ -568,16 +583,16 @@ 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 {{{ */
@@ -591,8 +606,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 {{{ */
@@ -627,20 +642,20 @@ XMLName {XMLNameStart}{XMLNamePart}*
     }
 
     \\{LineTerminatorSequence} yylloc->end.Lines();
-    \\(.|{NotLineTerminator}) R CYLexBufferUnits(yytext + 1, yyleng - 1);
+    \\{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
 {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