📄 jsontokenizer.as
字号:
return token;
}
/**
* Attempts to read a number from the input string. Places
* the character location at the first character after the
* number.
*
* @return The JSONToken with the number value if a number could
* be read. Throws an error otherwise.
*/
private function readNumber():JSONToken {
// the token for the number we'll try to read
var token:JSONToken = new JSONToken();
token.type = JSONTokenType.NUMBER;
// the string to accumulate the number characters
// into that we'll convert to a number at the end
var input:String = "";
// check for a negative number
if ( ch == '-' ) {
input += '-';
nextChar();
}
// the number must start with a digit
if ( !isDigit( ch ) )
{
parseError( "Expecting a digit" );
}
// 0 can only be the first digit if it
// is followed by a decimal point
if ( ch == '0' )
{
input += ch;
nextChar();
// make sure no other digits come after 0
if ( isDigit( ch ) )
{
parseError( "A digit cannot immediately follow 0" );
}
// Commented out - this should only be available when "strict" is false
// // unless we have 0x which starts a hex number\
// else if ( ch == 'x' )
// {
// // include the x in the input
// input += ch;
// nextChar();
//
// // need at least one hex digit after 0x to
// // be valid
// if ( isHexDigit( ch ) )
// {
// input += ch;
// nextChar();
// }
// else
// {
// parseError( "Number in hex format require at least one hex digit after \"0x\"" );
// }
//
// // consume all of the hex values
// while ( isHexDigit( ch ) )
// {
// input += ch;
// nextChar();
// }
// }
}
else
{
// read numbers while we can
while ( isDigit( ch ) ) {
input += ch;
nextChar();
}
}
// check for a decimal value
if ( ch == '.' ) {
input += '.';
nextChar();
// after the decimal there has to be a digit
if ( !isDigit( ch ) )
{
parseError( "Expecting a digit" );
}
// read more numbers to get the decimal value
while ( isDigit( ch ) ) {
input += ch;
nextChar();
}
}
// check for scientific notation
if ( ch == 'e' || ch == 'E' )
{
input += "e"
nextChar();
// check for sign
if ( ch == '+' || ch == '-' )
{
input += ch;
nextChar();
}
// require at least one number for the exponent
// in this case
if ( !isDigit( ch ) )
{
parseError( "Scientific notation number needs exponent value" );
}
// read in the exponent
while ( isDigit( ch ) )
{
input += ch;
nextChar();
}
}
// convert the string to a number value
var num:Number = Number( input );
if ( isFinite( num ) && !isNaN( num ) ) {
token.value = num;
return token;
} else {
parseError( "Number " + num + " is not valid!" );
}
return null;
}
/**
* Reads the next character in the input
* string and advances the character location.
*
* @return The next character in the input string, or
* null if we've read past the end.
*/
private function nextChar():String {
return ch = jsonString.charAt( loc++ );
}
/**
* Advances the character location past any
* sort of white space and comments
*/
private function skipIgnored():void
{
var originalLoc:int;
// keep trying to skip whitespace and comments as long
// as we keep advancing past the original location
do
{
originalLoc = loc;
skipWhite();
skipComments();
}
while ( originalLoc != loc );
}
/**
* Skips comments in the input string, either
* single-line or multi-line. Advances the character
* to the first position after the end of the comment.
*/
private function skipComments():void {
if ( ch == '/' ) {
// Advance past the first / to find out what type of comment
nextChar();
switch ( ch ) {
case '/': // single-line comment, read through end of line
// Loop over the characters until we find
// a newline or until there's no more characters left
do {
nextChar();
} while ( ch != '\n' && ch != '' )
// move past the \n
nextChar();
break;
case '*': // multi-line comment, read until closing */
// move past the opening *
nextChar();
// try to find a trailing */
while ( true ) {
if ( ch == '*' ) {
// check to see if we have a closing /
nextChar();
if ( ch == '/') {
// move past the end of the closing */
nextChar();
break;
}
} else {
// move along, looking if the next character is a *
nextChar();
}
// when we're here we've read past the end of
// the string without finding a closing */, so error
if ( ch == '' ) {
parseError( "Multi-line comment not closed" );
}
}
break;
// Can't match a comment after a /, so it's a parsing error
default:
parseError( "Unexpected " + ch + " encountered (expecting '/' or '*' )" );
}
}
}
/**
* Skip any whitespace in the input string and advances
* the character to the first character after any possible
* whitespace.
*/
private function skipWhite():void {
// As long as there are spaces in the input
// stream, advance the current location pointer
// past them
while ( isWhiteSpace( ch ) ) {
nextChar();
}
}
/**
* Determines if a character is whitespace or not.
*
* @return True if the character passed in is a whitespace
* character
*/
private function isWhiteSpace( ch:String ):Boolean {
return ( ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' );
}
/**
* Determines if a character is a digit [0-9].
*
* @return True if the character passed in is a digit
*/
private function isDigit( ch:String ):Boolean {
return ( ch >= '0' && ch <= '9' );
}
/**
* Determines if a character is a digit [0-9].
*
* @return True if the character passed in is a digit
*/
private function isHexDigit( ch:String ):Boolean {
// get the uppercase value of ch so we only have
// to compare the value between 'A' and 'F'
var uc:String = ch.toUpperCase();
// a hex digit is a digit of A-F, inclusive ( using
// our uppercase constraint )
return ( isDigit( ch ) || ( uc >= 'A' && uc <= 'F' ) );
}
/**
* Raises a parsing error with a specified message, tacking
* on the error location and the original string.
*
* @param message The message indicating why the error occurred
*/
public function parseError( message:String ):void {
throw new JSONParseError( message, loc, jsonString );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -