📄 lexer.js
字号:
/*
* Lexer.
* Copyright (c) 1998-1999 New Generation Software (NGS) Oy
*
* Author: Markku Rossi <mtr@ngs.fi>
*/
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA
*/
/*
* $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/jsc/lexer.js,v $
* $Id: lexer.js 21681 2006-04-21 15:00:24Z peterw $
*/
/*
* Global functions.
*/
function JSC$lexer (stream)
{
var ch, ch2;
JSC$token_value = null;
while ((ch = stream.readByte ()) != -1)
{
if (ch == #'\n')
{
JSC$linenum++;
continue;
}
if (JSC$lexer_is_white_space (ch))
continue;
JSC$token_linenum = JSC$linenum;
if (ch == #'/' && JSC$lexer_peek_char (stream) == #'*')
{
/* Multi line comment. */
stream.readByte ();
while ((ch = stream.readByte ()) != -1
&& (ch != #'*' || JSC$lexer_peek_char (stream) != #'/'))
if (ch == #'\n')
JSC$linenum++;
/* Consume the peeked #'/' character. */
stream.readByte ();
}
else if ((ch == #'/' && JSC$lexer_peek_char (stream) == #'/')
|| (ch == #'#' && JSC$lexer_peek_char (stream) == #'!'))
{
/* Single line comment. */
while ((ch = stream.readByte ()) != -1 && ch != #'\n')
;
if (ch == #'\n')
JSC$linenum++;
}
else if (ch == #'"' || ch == #'\'')
{
/* String constant. */
JSC$token_value = JSC$lexer_read_string (stream, "string", ch);
return JSC$tSTRING;
}
/* Literals. */
else if (ch == #'=' && JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
if (JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
return JSC$tSEQUAL;
}
return JSC$tEQUAL;
}
else if (ch == #'!' && JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
if (JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
return JSC$tSNEQUAL;
}
return JSC$tNEQUAL;
}
else if (ch == #'<' && JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
return JSC$tLE;
}
else if (ch == #'>' && JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
return JSC$tGE;
}
else if (ch == #'&' && JSC$lexer_peek_char (stream) == #'&')
{
stream.readByte ();
return JSC$tAND;
}
else if (ch == #'|' && JSC$lexer_peek_char (stream) == #'|')
{
stream.readByte ();
return JSC$tOR;
}
else if (ch == #'+' && JSC$lexer_peek_char (stream) == #'+')
{
stream.readByte ();
return JSC$tPLUSPLUS;
}
else if (ch == #'-' && JSC$lexer_peek_char (stream) == #'-')
{
stream.readByte ();
return JSC$tMINUSMINUS;
}
else if (ch == #'*' && JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
return JSC$tMULA;
}
else if (ch == #'/' && JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
return JSC$tDIVA;
}
else if (ch == #'%' && JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
return JSC$tMODA;
}
else if (ch == #'+' && JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
return JSC$tADDA;
}
else if (ch == #'-' && JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
return JSC$tSUBA;
}
else if (ch == #'&' && JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
return JSC$tANDA;
}
else if (ch == #'^' && JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
return JSC$tXORA;
}
else if (ch == #'|' && JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
return JSC$tORA;
}
else if (ch == #'<' && JSC$lexer_peek_char (stream) == #'<')
{
stream.readByte ();
if (JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
return JSC$tLSIA;
}
else
return JSC$tLSHIFT;
}
else if (ch == #'>' && JSC$lexer_peek_char (stream) == #'>')
{
stream.readByte ();
ch2 = JSC$lexer_peek_char (stream);
if (ch2 == #'=')
{
stream.readByte ();
return JSC$tRSIA;
}
else if (ch2 == #'>')
{
stream.readByte ();
if (JSC$lexer_peek_char (stream) == #'=')
{
stream.readByte ();
return JSC$tRRSA;
}
else
return JSC$tRRSHIFT;
}
else
return JSC$tRSHIFT;
}
/* Identifiers and keywords. */
else if (JSC$lexer_is_identifier_letter (ch))
{
/* An identifier. */
var id = String.fromCharCode (ch);
while ((ch = stream.readByte ()) != -1
&& (JSC$lexer_is_identifier_letter (ch)
|| JSC$lexer_is_decimal_digit (ch)))
id.append (File.byteToString (ch));
stream.ungetByte (ch);
/* Keywords. */
if (id == "break")
return JSC$tBREAK;
else if (id == "continue")
return JSC$tCONTINUE;
else if (id == "delete")
return JSC$tDELETE;
else if (id == "else")
return JSC$tELSE;
else if (id == "for")
return JSC$tFOR;
else if (id == "function")
return JSC$tFUNCTION;
else if (id == "if")
return JSC$tIF;
else if (id == "in")
return JSC$tIN;
else if (id == "new")
return JSC$tNEW;
else if (id == "return")
return JSC$tRETURN;
else if (id == "this")
return JSC$tTHIS;
else if (id == "typeof")
return JSC$tTYPEOF;
else if (id == "var")
return JSC$tVAR;
else if (id == "void")
return JSC$tVOID;
else if (id == "while")
return JSC$tWHILE;
else if (id == "with")
return JSC$tWITH;
/*
* Future reserved keywords (some of these is already in use
* in this implementation).
*/
else if (id == "case")
return JSC$tCASE;
else if (id == "catch")
return JSC$tCATCH;
else if (id == "class")
return JSC$tCLASS;
else if (id == "const")
return JSC$tCONST;
else if (id == "debugger")
return JSC$tDEBUGGER;
else if (id == "default")
return JSC$tDEFAULT;
else if (id == "do")
return JSC$tDO;
else if (id == "enum")
return JSC$tENUM;
else if (id == "export")
return JSC$tEXPORT;
else if (id == "extends")
return JSC$tEXTENDS;
else if (id == "finally")
return JSC$tFINALLY;
else if (id == "import")
return JSC$tIMPORT;
else if (id == "super")
return JSC$tSUPER;
else if (id == "switch")
return JSC$tSWITCH;
else if (id == "throw")
return JSC$tTHROW;
else if (id == "try")
return JSC$tTRY;
/* Null and boolean literals. */
else if (id == "null")
return JSC$tNULL;
else if (id == "true")
return JSC$tTRUE;
else if (id == "false")
return JSC$tFALSE;
else
{
/* It really is an identifier. */
JSC$token_value = id;
return JSC$tIDENTIFIER;
}
}
/* Character constants. */
else if (ch == #'#' && JSC$lexer_peek_char (stream) == #'\'')
{
/* Skip the starting #'\'' and read more. */
stream.readByte ();
ch = stream.readByte ();
if (ch == #'\\')
{
JSC$token_value
= JSC$lexer_read_backslash_escape (stream, 0, "character");
if (stream.readByte () != #'\'')
error (JSC$filename + ":" + JSC$linenum.toString ()
+ ": malformed character constant");
}
else if (JSC$lexer_peek_char (stream) == #'\'')
{
stream.readByte ();
JSC$token_value = ch;
}
else
error (JSC$filename + ":" + JSC$linenum.toString ()
+ ": malformed character constant");
return JSC$tINTEGER;
}
/* Octal and hex numbers. */
else if (ch == #'0'
&& JSC$lexer_peek_char (stream) != #'.'
&& JSC$lexer_peek_char (stream) != #'e'
&& JSC$lexer_peek_char (stream) != #'E')
{
JSC$token_value = 0;
ch = stream.readByte ();
if (ch == #'x' || ch == #'X')
{
ch = stream.readByte ();
while (JSC$lexer_is_hex_digit (ch))
{
JSC$token_value *= 16;
JSC$token_value += JSC$lexer_hex_to_dec (ch);
ch = stream.readByte ();
}
stream.ungetByte (ch);
}
else
{
while (JSC$lexer_is_octal_digit (ch))
{
JSC$token_value *= 8;
JSC$token_value += ch - #'0';
ch = stream.readByte ();
}
stream.ungetByte (ch);
}
return JSC$tINTEGER;
}
/* Decimal numbers. */
else if (JSC$lexer_is_decimal_digit (ch)
|| (ch == #'.'
&& JSC$lexer_is_decimal_digit (
JSC$lexer_peek_char (stream))))
{
var is_float = false;
var buf = new String (File.byteToString (ch));
var accept_dot = true;
if (ch == #'.')
{
/*
* We started with #'.' and we know that the next character
* is a decimal digit (we peeked it).
*/
is_float = true;
ch = stream.readByte ();
while (JSC$lexer_is_decimal_digit (ch))
{
buf.append (File.byteToString (ch));
ch = stream.readByte ();
}
accept_dot = false;
}
else
{
/* We did start with a decimal digit. */
ch = stream.readByte ();
while (JSC$lexer_is_decimal_digit (ch))
{
buf.append (File.byteToString (ch));
ch = stream.readByte ();
}
}
if ((accept_dot && ch == #'.')
|| ch == #'e' || ch == #'E')
{
is_float = true;
if (ch == #'.')
{
buf.append (File.byteToString (ch));
ch = stream.readByte ();
while (JSC$lexer_is_decimal_digit (ch))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -