📄 javascriptlexer.lex
字号:
<YYINITIAL> "synchronized" { lastToken = JavaScriptToken.RESERVED_WORD_SYNCHRONIZED; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar+12, nextState)); return (t);}<YYINITIAL> "this" { lastToken = JavaScriptToken.RESERVED_WORD_THIS; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar+4, nextState)); return (t);}<YYINITIAL> "throw" { lastToken = JavaScriptToken.RESERVED_WORD_THROW; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar+5, nextState)); return (t);}<YYINITIAL> "throws" { lastToken = JavaScriptToken.RESERVED_WORD_THROWS; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar+6, nextState)); return (t);}<YYINITIAL> "while" { lastToken = JavaScriptToken.RESERVED_WORD_TRANSIENT; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar+9, nextState)); return (t);}<YYINITIAL> "try" { lastToken = JavaScriptToken.RESERVED_WORD_TRY; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar+3, nextState)); return (t);}<YYINITIAL> "var" { lastToken = JavaScriptToken.RESERVED_WORD_VAR; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar+3, nextState)); return (t);}<YYINITIAL> "void" { lastToken = JavaScriptToken.RESERVED_WORD_VOID; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar+4, nextState)); return (t);}<YYINITIAL> "with" { lastToken = JavaScriptToken.RESERVED_WORD_WITH; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar+4, nextState)); return (t);}<YYINITIAL> "null" { lastToken = JavaScriptToken.LITERAL_NULL; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar+4, nextState)); return (t);}<YYINITIAL> "true" { lastToken = JavaScriptToken.LITERAL_BOOLEAN; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar+4, nextState)); return (t);}<YYINITIAL> "false" { lastToken = JavaScriptToken.LITERAL_BOOLEAN; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar+5, nextState)); return (t);}<YYINITIAL> {Identifier} { lastToken = JavaScriptToken.IDENTIFIER; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {DecimalNum} { /* At this point, the number we found could still be too large. * If it is too large, we need to return an error. * Java has methods built in that will decode from a string * and throw an exception the number is too large */ try { /* bigger negatives are allowed than positives. Thus * we have to be careful to make sure a neg sign is preserved */ if (lastToken == JavaScriptToken.OPERATOR_SUBTRACT){ Integer.decode('-' + yytext()); } else { Integer.decode(yytext()); } lastToken = JavaScriptToken.LITERAL_INTEGER_DECIMAL; } catch (NumberFormatException e){ lastToken = JavaScriptToken.ERROR_INTEGER_DECIMIAL_SIZE; } JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {OctalNum} { /* An Octal number cannot be too big. After removing * initial zeros, It can have 11 digits, the first * of which must be 3 or less. */ lastToken = JavaScriptToken.LITERAL_INTEGER_OCTAL; int i; int length =yytext().length(); for (i=1 ; i<length-11; i++){ //check for initial zeros if (yytext().charAt(i) != '0'){ lastToken = JavaScriptToken.ERROR_INTEGER_OCTAL_SIZE; } } if (length - i > 11){ lastToken = JavaScriptToken.ERROR_INTEGER_OCTAL_SIZE; } else if (length - i == 11){ // if the rest of the number is as big as possible // the first digit can only be 3 or less if (yytext().charAt(i) != '0' && yytext().charAt(i) != '1' && yytext().charAt(i) != '2' && yytext().charAt(i) != '3'){ lastToken = JavaScriptToken.ERROR_INTEGER_OCTAL_SIZE; } } // Otherwise, it should be OK JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {HexNum} { /* A Hex number cannot be too big. After removing * initial zeros, It can have 8 digits */ lastToken = JavaScriptToken.LITERAL_INTEGER_HEXIDECIMAL; int i; int length =yytext().length(); for (i=2 ; i<length-8; i++){ //check for initial zeros if (yytext().charAt(i) != '0'){ lastToken = JavaScriptToken.ERROR_INTEGER_HEXIDECIMAL_SIZE; } } if (length - i > 8){ lastToken = JavaScriptToken.ERROR_INTEGER_HEXIDECIMAL_SIZE; } JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {DecimalLong} { try { if (lastToken == JavaScriptToken.OPERATOR_SUBTRACT){ Long.decode('-' + yytext().substring(0,yytext().length()-1)); } else { Long.decode(yytext().substring(0,yytext().length()-1)); } lastToken = JavaScriptToken.LITERAL_LONG_DECIMAL; } catch (NumberFormatException e){ lastToken = JavaScriptToken.ERROR_LONG_DECIMIAL_SIZE; } JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {OctalLong} { /* An Octal number cannot be too big. After removing * initial zeros, It can have 23 digits, the first * of which must be 1 or less. The last will be the L or l * at the end. */ lastToken = JavaScriptToken.LITERAL_LONG_OCTAL; int i; int length =yytext().length(); for (i=1 ; i<length-23; i++){ //check for initial zeros if (yytext().charAt(i) != '0'){ lastToken = JavaScriptToken.ERROR_LONG_OCTAL_SIZE; } } if (length - i > 23){ lastToken = JavaScriptToken.ERROR_LONG_OCTAL_SIZE; } else if (length - i == 23){ // if the rest of the number is as big as possible // the first digit can only be 3 or less if (yytext().charAt(i) != '0' && yytext().charAt(i) != '1'){ lastToken = JavaScriptToken.ERROR_LONG_OCTAL_SIZE; } } // Otherwise, it should be OK JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {HexLong} { /* A Hex long cannot be too big. After removing * initial zeros, It can have 17 digits, the last of which is * the L or l */ lastToken = JavaScriptToken.LITERAL_LONG_HEXIDECIMAL; int i; int length =yytext().length(); for (i=2 ; i<length-17; i++){ //check for initial zeros if (yytext().charAt(i) != '0'){ lastToken = JavaScriptToken.ERROR_LONG_HEXIDECIMAL_SIZE; } } if (length - i > 17){ lastToken = JavaScriptToken.ERROR_LONG_HEXIDECIMAL_SIZE; } JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {ZeroFloat} { /* catch the case of a zero in parsing, so that we do not incorrectly * give an error that a number was rounded to zero */ lastToken = JavaScriptToken.LITERAL_FLOATING_POINT; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {ZeroDouble} { lastToken = JavaScriptToken.LITERAL_DOUBLE; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {Float} { /* Sun s java has a few bugs here. Their MAX_FLOAT and MIN_FLOAT do not * quite match the spec. Its not far off, so we will deal. If they fix * then we are fixed. So all good. */ Float f; try { f = Float.valueOf(yytext()); if (f.isInfinite() || f.compareTo(new Float(0f)) == 0){ lastToken = JavaScriptToken.ERROR_FLOAT_SIZE; } else { lastToken = JavaScriptToken.LITERAL_FLOATING_POINT; } } catch (NumberFormatException e){ lastToken = JavaScriptToken.ERROR_FLOAT_SIZE; } JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {Double} { Double d; try { d = Double.valueOf(yytext()); if (d.isInfinite() || d.compareTo(new Double(0d)) == 0){ lastToken = JavaScriptToken.ERROR_DOUBLE_SIZE; } else { lastToken = JavaScriptToken.LITERAL_DOUBLE; } } catch (NumberFormatException e){ lastToken = JavaScriptToken.ERROR_DOUBLE_SIZE; } JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {Character} { lastToken = JavaScriptToken.LITERAL_CHARACTER; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {String} { lastToken = JavaScriptToken.LITERAL_STRING; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> ({WhiteSpace}+) { lastToken = JavaScriptToken.WHITE_SPACE; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {Comment} { lastToken = JavaScriptToken.COMMENT_END_OF_LINE; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {HTMLComment} { lastToken = JavaScriptToken.COMMENT_END_OF_LINE; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {TradComment} { lastToken = JavaScriptToken.COMMENT_TRADITIONAL; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {UnclosedString} { /* most of these errors have to be caught down near the end of the file. * This way, previous expressions of the same length have precedence. * This is really useful for catching anything bad by just allowing it * to slip through the cracks. */ lastToken = JavaScriptToken.ERROR_UNCLOSED_STRING; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {MalformedUnclosedString} { lastToken = JavaScriptToken.ERROR_MALFORMED_UNCLOSED_STRING; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {MalformedString} { lastToken = JavaScriptToken.ERROR_MALFORMED_STRING; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {UnclosedCharacter} { lastToken = JavaScriptToken.ERROR_UNCLOSED_CHARACTER; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {MalformedUnclosedCharacter} { lastToken = JavaScriptToken.ERROR_MALFORMED_UNCLOSED_CHARACTER; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {MalformedCharacter} { lastToken = JavaScriptToken.ERROR_MALFORMED_CHARACTER; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {ErrorFloat} { lastToken = JavaScriptToken.ERROR_FLOAT; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {ErrorIdentifier} { lastToken = JavaScriptToken.ERROR_IDENTIFIER; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}<YYINITIAL> {OpenComment} { lastToken = JavaScriptToken.ERROR_UNCLOSED_COMMENT; JavaScriptToken t = (new JavaScriptToken(lastToken, yytext(), yyline, yychar, yychar + yytext().length(), nextState)); return (t);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -