2012-08-22 18:28:59 +00:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
A JavaScript tokenizer / parser / beautifier / compressor .
2020-05-05 13:07:33 +00:00
https : //github.com/mishoo/UglifyJS
2012-08-22 18:28:59 +00:00
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ( C ) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
Author : Mihai Bazon
< mihai . bazon @ gmail . com >
http : //mihai.bazon.net/blog
Distributed under the BSD license :
2012-08-27 08:01:27 +00:00
Copyright 2012 ( c ) Mihai Bazon < mihai . bazon @ gmail . com >
2012-08-22 18:28:59 +00:00
Parser based on parse - js ( http : //marijn.haverbeke.nl/parse-js/).
Redistribution and use in source and binary forms , with or without
modification , are permitted provided that the following conditions
are met :
* Redistributions of source code must retain the above
copyright notice , this list of conditions and the following
disclaimer .
* Redistributions in binary form must reproduce the above
copyright notice , this list of conditions and the following
disclaimer in the documentation and / or other materials
provided with the distribution .
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “ AS IS ” AND ANY
EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
LIABLE FOR ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY ,
OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT LIMITED TO ,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE , DATA , OR
PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR
TORT ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
2012-10-02 09:45:31 +00:00
"use strict" ;
2020-10-19 00:32:39 +00:00
var KEYWORDS = "break case catch const continue debugger default delete do else finally for function if in instanceof let new return switch throw try typeof var void while with" ;
2020-09-23 15:06:12 +00:00
var KEYWORDS _ATOM = "false null true" ;
var RESERVED _WORDS = [
2020-12-06 21:22:40 +00:00
"await abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized this throws transient volatile yield" ,
2020-09-23 15:06:12 +00:00
KEYWORDS _ATOM ,
KEYWORDS ,
] . join ( " " ) ;
var KEYWORDS _BEFORE _EXPRESSION = "return new delete throw else case" ;
2012-05-27 11:09:01 +00:00
2012-10-11 08:52:05 +00:00
KEYWORDS = makePredicate ( KEYWORDS ) ;
RESERVED _WORDS = makePredicate ( RESERVED _WORDS ) ;
KEYWORDS _BEFORE _EXPRESSION = makePredicate ( KEYWORDS _BEFORE _EXPRESSION ) ;
KEYWORDS _ATOM = makePredicate ( KEYWORDS _ATOM ) ;
2012-05-27 11:09:01 +00:00
2020-10-04 16:05:03 +00:00
var RE _BIN _NUMBER = /^0b([01]+)$/i ;
var RE _HEX _NUMBER = /^0x([0-9a-f]+)$/i ;
var RE _OCT _NUMBER = /^0o?([0-7]+)$/i ;
2012-05-27 11:09:01 +00:00
2012-10-11 08:52:05 +00:00
var OPERATORS = makePredicate ( [
2012-05-27 11:09:01 +00:00
"in" ,
"instanceof" ,
"typeof" ,
"new" ,
"void" ,
"delete" ,
"++" ,
"--" ,
"+" ,
"-" ,
"!" ,
"~" ,
"&" ,
"|" ,
"^" ,
"*" ,
"/" ,
"%" ,
2021-01-24 21:48:51 +00:00
"**" ,
2012-05-27 11:09:01 +00:00
">>" ,
"<<" ,
">>>" ,
"<" ,
">" ,
"<=" ,
">=" ,
"==" ,
"===" ,
"!=" ,
"!==" ,
"?" ,
"=" ,
"+=" ,
"-=" ,
"/=" ,
"*=" ,
"%=" ,
">>=" ,
"<<=" ,
">>>=" ,
"|=" ,
"^=" ,
"&=" ,
"&&" ,
"||"
] ) ;
2020-10-19 01:34:17 +00:00
var NEWLINE _CHARS = "\n\r\u2028\u2029" ;
var OPERATOR _CHARS = "+-*&%=<>!?|~^" ;
var PUNC _BEFORE _EXPRESSION = "[{(,;:" ;
2021-02-01 02:36:45 +00:00
var PUNC _CHARS = PUNC _BEFORE _EXPRESSION + "`)}]" ;
2020-10-19 01:34:17 +00:00
var WHITESPACE _CHARS = NEWLINE _CHARS + " \u00a0\t\f\u000b\u200b\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\uFEFF" ;
var NON _IDENTIFIER _CHARS = makePredicate ( characters ( "./'\"" + OPERATOR _CHARS + PUNC _CHARS + WHITESPACE _CHARS ) ) ;
2012-05-27 11:09:01 +00:00
2020-10-19 01:34:17 +00:00
NEWLINE _CHARS = makePredicate ( characters ( NEWLINE _CHARS ) ) ;
OPERATOR _CHARS = makePredicate ( characters ( OPERATOR _CHARS ) ) ;
PUNC _BEFORE _EXPRESSION = makePredicate ( characters ( PUNC _BEFORE _EXPRESSION ) ) ;
PUNC _CHARS = makePredicate ( characters ( PUNC _CHARS ) ) ;
WHITESPACE _CHARS = makePredicate ( characters ( WHITESPACE _CHARS ) ) ;
2012-05-27 11:09:01 +00:00
/* -----[ Tokenizer ]----- */
2017-12-10 17:15:44 +00:00
function is _surrogate _pair _head ( code ) {
return code >= 0xd800 && code <= 0xdbff ;
}
function is _surrogate _pair _tail ( code ) {
return code >= 0xdc00 && code <= 0xdfff ;
}
2012-10-11 10:00:58 +00:00
function is _digit ( code ) {
2015-01-19 20:35:53 +00:00
return code >= 48 && code <= 57 ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function is _identifier _char ( ch ) {
2020-10-19 01:34:17 +00:00
return ! NON _IDENTIFIER _CHARS [ ch ] ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2018-06-06 09:50:56 +00:00
function is _identifier _string ( str ) {
2014-04-18 07:47:38 +00:00
return /^[a-z_$][a-z0-9_$]*$/i . test ( str ) ;
2018-06-06 09:50:56 +00:00
}
2013-05-05 18:45:41 +00:00
2021-02-01 09:20:13 +00:00
function decode _escape _sequence ( seq ) {
switch ( seq [ 0 ] ) {
case "b" : return "\b" ;
case "f" : return "\f" ;
case "n" : return "\n" ;
case "r" : return "\r" ;
case "t" : return "\t" ;
case "u" :
var code ;
if ( seq . length == 5 ) {
code = seq . slice ( 1 ) ;
} else if ( seq [ 1 ] == "{" && seq . slice ( - 1 ) == "}" ) {
code = seq . slice ( 2 , - 1 ) ;
} else {
return ;
}
var num = parseInt ( code , 16 ) ;
if ( num < 0 || isNaN ( num ) ) return ;
if ( num < 0x10000 ) return String . fromCharCode ( num ) ;
if ( num > 0x10ffff ) return ;
return String . fromCharCode ( ( num >> 10 ) + 0xd7c0 ) + String . fromCharCode ( ( num & 0x03ff ) + 0xdc00 ) ;
case "v" : return "\u000b" ;
case "x" :
if ( seq . length != 3 ) return ;
var num = parseInt ( seq . slice ( 1 ) , 16 ) ;
if ( num < 0 || isNaN ( num ) ) return ;
return String . fromCharCode ( num ) ;
case "\r" :
case "\n" :
return "" ;
default :
if ( seq == "0" ) return "\0" ;
if ( seq [ 0 ] >= "0" && seq [ 0 ] <= "9" ) return ;
return seq ;
}
}
2012-05-27 11:09:01 +00:00
function parse _js _number ( num ) {
2020-10-04 16:05:03 +00:00
var match ;
if ( match = RE _BIN _NUMBER . exec ( num ) ) return parseInt ( match [ 1 ] , 2 ) ;
if ( match = RE _HEX _NUMBER . exec ( num ) ) return parseInt ( match [ 1 ] , 16 ) ;
if ( match = RE _OCT _NUMBER . exec ( num ) ) return parseInt ( match [ 1 ] , 8 ) ;
var val = parseFloat ( num ) ;
if ( val == num ) return val ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2014-08-11 11:40:01 +00:00
function JS _Parse _Error ( message , filename , line , col , pos ) {
2012-05-27 11:09:01 +00:00
this . message = message ;
2014-08-11 11:40:01 +00:00
this . filename = filename ;
2012-10-02 13:39:53 +00:00
this . line = line ;
this . col = col ;
2012-10-11 12:25:38 +00:00
this . pos = pos ;
2018-06-06 09:50:56 +00:00
}
2017-02-26 19:40:54 +00:00
JS _Parse _Error . prototype = Object . create ( Error . prototype ) ;
JS _Parse _Error . prototype . constructor = JS _Parse _Error ;
JS _Parse _Error . prototype . name = "SyntaxError" ;
configure _error _stack ( JS _Parse _Error ) ;
2012-05-27 11:09:01 +00:00
2012-09-21 11:38:52 +00:00
function js _error ( message , filename , line , col , pos ) {
2014-08-11 11:40:01 +00:00
throw new JS _Parse _Error ( message , filename , line , col , pos ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function is _token ( token , type , val ) {
return token . type == type && ( val == null || token . value == val ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
var EX _EOF = { } ;
2015-02-20 15:08:01 +00:00
function tokenizer ( $TEXT , filename , html5 _comments , shebang ) {
2012-05-27 11:09:01 +00:00
var S = {
2015-05-20 13:17:46 +00:00
text : $TEXT ,
2012-09-21 11:38:52 +00:00
filename : filename ,
2013-07-15 08:59:23 +00:00
pos : 0 ,
tokpos : 0 ,
line : 1 ,
tokline : 0 ,
col : 0 ,
tokcol : 0 ,
2012-05-27 11:09:01 +00:00
newline _before : false ,
regex _allowed : false ,
2016-05-20 08:25:35 +00:00
comments _before : [ ] ,
directives : { } ,
2021-02-01 02:36:45 +00:00
directive _stack : [ ] ,
read _template : with _eof _error ( "Unterminated template literal" , function ( strings ) {
var s = "" ;
for ( ; ; ) {
var ch = next ( true , true ) ;
switch ( ch ) {
case "\\" :
ch += next ( true , true ) ;
break ;
case "`" :
strings . push ( s ) ;
return ;
case "$" :
if ( peek ( ) == "{" ) {
next ( ) ;
strings . push ( s ) ;
return true ;
}
}
s += ch ;
}
} ) ,
2012-05-27 11:09:01 +00:00
} ;
2019-10-20 07:19:19 +00:00
var prev _was _dot = false ;
2012-05-27 11:09:01 +00:00
2018-06-06 09:50:56 +00:00
function peek ( ) {
return S . text . charAt ( S . pos ) ;
}
2012-05-27 11:09:01 +00:00
function next ( signal _eof , in _string ) {
var ch = S . text . charAt ( S . pos ++ ) ;
if ( signal _eof && ! ch )
throw EX _EOF ;
2018-04-03 07:15:01 +00:00
if ( NEWLINE _CHARS [ ch ] ) {
2012-05-27 11:09:01 +00:00
S . col = 0 ;
2020-03-20 23:17:41 +00:00
S . line ++ ;
if ( ! in _string ) S . newline _before = true ;
if ( ch == "\r" && peek ( ) == "\n" ) {
// treat `\r\n` as `\n`
S . pos ++ ;
2015-01-05 10:14:42 +00:00
ch = "\n" ;
}
2012-05-27 11:09:01 +00:00
} else {
2020-03-20 23:17:41 +00:00
S . col ++ ;
2012-05-27 11:09:01 +00:00
}
return ch ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2013-09-06 06:54:30 +00:00
function forward ( i ) {
while ( i -- > 0 ) next ( ) ;
2018-06-06 09:50:56 +00:00
}
2013-09-06 06:54:30 +00:00
function looking _at ( str ) {
return S . text . substr ( S . pos , str . length ) == str ;
2018-06-06 09:50:56 +00:00
}
2013-09-06 06:54:30 +00:00
2016-04-25 04:42:18 +00:00
function find _eol ( ) {
var text = S . text ;
2019-04-15 14:23:11 +00:00
for ( var i = S . pos ; i < S . text . length ; ++ i ) {
if ( NEWLINE _CHARS [ text [ i ] ] ) return i ;
2016-04-25 04:42:18 +00:00
}
return - 1 ;
2018-06-06 09:50:56 +00:00
}
2016-04-25 04:42:18 +00:00
2012-05-27 11:09:01 +00:00
function find ( what , signal _eof ) {
var pos = S . text . indexOf ( what , S . pos ) ;
if ( signal _eof && pos == - 1 ) throw EX _EOF ;
return pos ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function start _token ( ) {
S . tokline = S . line ;
S . tokcol = S . col ;
S . tokpos = S . pos ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function token ( type , value , is _comment ) {
2019-10-20 07:19:19 +00:00
S . regex _allowed = type == "operator" && ! UNARY _POSTFIX [ value ]
|| type == "keyword" && KEYWORDS _BEFORE _EXPRESSION [ value ]
|| type == "punc" && PUNC _BEFORE _EXPRESSION [ value ] ;
if ( type == "punc" && value == "." ) prev _was _dot = true ;
else if ( ! is _comment ) prev _was _dot = false ;
2012-05-27 11:09:01 +00:00
var ret = {
2014-08-08 11:15:43 +00:00
type : type ,
value : value ,
line : S . tokline ,
col : S . tokcol ,
pos : S . tokpos ,
endline : S . line ,
endcol : S . col ,
endpos : S . pos ,
nlb : S . newline _before ,
file : filename
2012-05-27 11:09:01 +00:00
} ;
2015-11-11 20:15:25 +00:00
if ( /^(?:num|string|regexp)$/i . test ( type ) ) {
2015-11-12 10:18:25 +00:00
ret . raw = $TEXT . substring ( ret . pos , ret . endpos ) ;
2015-11-11 20:15:25 +00:00
}
2012-05-27 11:09:01 +00:00
if ( ! is _comment ) {
ret . comments _before = S . comments _before ;
2017-12-21 20:59:54 +00:00
ret . comments _after = S . comments _before = [ ] ;
2012-05-27 11:09:01 +00:00
}
S . newline _before = false ;
return new AST _Token ( ret ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function skip _whitespace ( ) {
2018-04-03 07:15:01 +00:00
while ( WHITESPACE _CHARS [ peek ( ) ] )
2012-05-27 11:09:01 +00:00
next ( ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function read _while ( pred ) {
2021-01-24 01:51:18 +00:00
var ret = "" , ch ;
while ( ( ch = peek ( ) ) && pred ( ch ) ) ret += next ( ) ;
2012-05-27 11:09:01 +00:00
return ret ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function parse _error ( err ) {
2012-09-21 11:38:52 +00:00
js _error ( err , filename , S . tokline , S . tokcol , S . tokpos ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function read _num ( prefix ) {
var has _e = false , after _e = false , has _x = false , has _dot = prefix == "." ;
2021-01-24 01:51:18 +00:00
var num = read _while ( function ( ch ) {
2012-10-11 10:00:58 +00:00
var code = ch . charCodeAt ( 0 ) ;
switch ( code ) {
case 120 : case 88 : // xX
return has _x ? false : ( has _x = true ) ;
case 101 : case 69 : // eE
return has _x ? true : has _e ? false : ( has _e = after _e = true ) ;
2021-01-24 01:51:18 +00:00
case 43 : case 45 : // +-
2012-10-11 10:00:58 +00:00
return after _e ;
case ( after _e = false , 46 ) : // .
return ( ! has _dot && ! has _x && ! has _e ) ? ( has _dot = true ) : false ;
2012-05-27 11:09:01 +00:00
}
2020-10-19 01:34:17 +00:00
return is _digit ( code ) || /[_0-9a-fo]/i . test ( ch ) ;
2012-05-27 11:09:01 +00:00
} ) ;
2012-10-11 10:00:58 +00:00
if ( prefix ) num = prefix + num ;
2020-10-04 16:05:03 +00:00
if ( /^0[0-7_]+$/ . test ( num ) ) {
if ( next _token . has _directive ( "use strict" ) ) parse _error ( "Legacy octal literals are not allowed in strict mode" ) ;
} else {
num = num . replace ( has _x ? /([1-9a-f]|.0)_(?=[0-9a-f])/gi : /([1-9]|.0)_(?=[0-9])/gi , "$1" ) ;
2016-07-21 01:19:24 +00:00
}
2012-05-27 11:09:01 +00:00
var valid = parse _js _number ( num ) ;
2021-01-24 01:51:18 +00:00
if ( isNaN ( valid ) ) parse _error ( "Invalid syntax: " + num ) ;
if ( has _dot || has _e || peek ( ) != "n" ) return token ( "num" , valid ) ;
return token ( "bigint" , num . toLowerCase ( ) + next ( ) ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function read _escaped _char ( in _string ) {
2021-02-01 09:20:13 +00:00
var seq = next ( true , in _string ) ;
if ( seq >= "0" && seq <= "7" ) return read _octal _escape _sequence ( seq ) ;
if ( seq == "u" ) {
var ch = next ( true , in _string ) ;
seq += ch ;
if ( ch != "{" ) {
seq += next ( true , in _string ) + next ( true , in _string ) + next ( true , in _string ) ;
} else do {
ch = next ( true , in _string ) ;
seq += ch ;
} while ( ch != "}" ) ;
} else if ( seq == "x" ) {
seq += next ( true , in _string ) + next ( true , in _string ) ;
2012-05-27 11:09:01 +00:00
}
2021-02-01 09:20:13 +00:00
var str = decode _escape _sequence ( seq ) ;
if ( typeof str != "string" ) parse _error ( "Invalid escape sequence: \\" + seq ) ;
return str ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2016-06-23 14:53:48 +00:00
function read _octal _escape _sequence ( ch ) {
// Read
var p = peek ( ) ;
if ( p >= "0" && p <= "7" ) {
ch += next ( true ) ;
if ( ch [ 0 ] <= "3" && ( p = peek ( ) ) >= "0" && p <= "7" )
ch += next ( true ) ;
}
// Parse
if ( ch === "0" ) return "\0" ;
if ( ch . length > 0 && next _token . has _directive ( "use strict" ) )
2017-02-26 19:40:54 +00:00
parse _error ( "Legacy octal escape sequences are not allowed in strict mode" ) ;
2016-06-23 14:53:48 +00:00
return String . fromCharCode ( parseInt ( ch , 8 ) ) ;
}
2018-06-06 09:50:56 +00:00
var read _string = with _eof _error ( "Unterminated string constant" , function ( quote _char ) {
2012-10-11 10:00:58 +00:00
var quote = next ( ) , ret = "" ;
for ( ; ; ) {
2015-04-23 09:08:19 +00:00
var ch = next ( true , true ) ;
2016-06-23 14:53:48 +00:00
if ( ch == "\\" ) ch = read _escaped _char ( true ) ;
2018-04-03 07:15:01 +00:00
else if ( NEWLINE _CHARS [ ch ] ) parse _error ( "Unterminated string constant" ) ;
2012-10-11 10:00:58 +00:00
else if ( ch == quote ) break ;
ret += ch ;
}
2015-01-27 20:26:27 +00:00
var tok = token ( "string" , ret ) ;
tok . quote = quote _char ;
return tok ;
2012-10-11 10:00:58 +00:00
} ) ;
2012-05-27 11:09:01 +00:00
2013-09-06 06:54:30 +00:00
function skip _line _comment ( type ) {
var regex _allowed = S . regex _allowed ;
2016-04-25 04:42:18 +00:00
var i = find _eol ( ) , ret ;
2012-05-27 11:09:01 +00:00
if ( i == - 1 ) {
ret = S . text . substr ( S . pos ) ;
S . pos = S . text . length ;
} else {
ret = S . text . substring ( S . pos , i ) ;
S . pos = i ;
}
2014-08-08 11:15:43 +00:00
S . col = S . tokcol + ( S . pos - S . tokpos ) ;
2013-09-06 06:54:30 +00:00
S . comments _before . push ( token ( type , ret , true ) ) ;
S . regex _allowed = regex _allowed ;
2016-04-15 23:58:46 +00:00
return next _token ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2018-06-06 09:50:56 +00:00
var skip _multiline _comment = with _eof _error ( "Unterminated multiline comment" , function ( ) {
2013-09-06 06:54:30 +00:00
var regex _allowed = S . regex _allowed ;
2012-10-11 10:00:58 +00:00
var i = find ( "*/" , true ) ;
2020-09-23 15:06:12 +00:00
var text = S . text . substring ( S . pos , i ) . replace ( /\r\n|\r|\u2028|\u2029/g , "\n" ) ;
2012-10-11 10:00:58 +00:00
// update stream position
2016-06-13 10:36:47 +00:00
forward ( text . length /* doesn't count \r\n as 2 char while S.pos - i does */ + 2 ) ;
2013-09-06 06:54:30 +00:00
S . comments _before . push ( token ( "comment2" , text , true ) ) ;
S . regex _allowed = regex _allowed ;
2016-04-15 23:58:46 +00:00
return next _token ;
2012-10-11 10:00:58 +00:00
} ) ;
2012-05-27 11:09:01 +00:00
function read _name ( ) {
var backslash = false , name = "" , ch , escaped = false , hex ;
2020-10-19 01:34:17 +00:00
while ( ch = peek ( ) ) {
2012-05-27 11:09:01 +00:00
if ( ! backslash ) {
if ( ch == "\\" ) escaped = backslash = true , next ( ) ;
else if ( is _identifier _char ( ch ) ) name += next ( ) ;
else break ;
2019-10-20 07:19:19 +00:00
} else {
2017-02-26 19:40:54 +00:00
if ( ch != "u" ) parse _error ( "Expecting UnicodeEscapeSequence -- uXXXX" ) ;
2012-05-27 11:09:01 +00:00
ch = read _escaped _char ( ) ;
2017-02-26 19:40:54 +00:00
if ( ! is _identifier _char ( ch ) ) parse _error ( "Unicode char: " + ch . charCodeAt ( 0 ) + " is not valid in identifier" ) ;
2012-05-27 11:09:01 +00:00
name += ch ;
backslash = false ;
}
}
2018-04-03 07:15:01 +00:00
if ( KEYWORDS [ name ] && escaped ) {
2012-05-27 11:09:01 +00:00
hex = name . charCodeAt ( 0 ) . toString ( 16 ) . toUpperCase ( ) ;
name = "\\u" + "0000" . substr ( hex . length ) + hex + name . slice ( 1 ) ;
}
return name ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2017-05-17 12:10:50 +00:00
var read _regexp = with _eof _error ( "Unterminated regular expression" , function ( source ) {
2012-10-11 10:00:58 +00:00
var prev _backslash = false , ch , in _class = false ;
2018-04-03 07:15:01 +00:00
while ( ( ch = next ( true ) ) ) if ( NEWLINE _CHARS [ ch ] ) {
2017-02-26 19:40:54 +00:00
parse _error ( "Unexpected line terminator" ) ;
2016-06-13 10:36:47 +00:00
} else if ( prev _backslash ) {
2017-05-17 12:10:50 +00:00
source += "\\" + ch ;
2012-10-11 10:00:58 +00:00
prev _backslash = false ;
} else if ( ch == "[" ) {
in _class = true ;
2017-05-17 12:10:50 +00:00
source += ch ;
2012-10-11 10:00:58 +00:00
} else if ( ch == "]" && in _class ) {
in _class = false ;
2017-05-17 12:10:50 +00:00
source += ch ;
2012-10-11 10:00:58 +00:00
} else if ( ch == "/" && ! in _class ) {
break ;
} else if ( ch == "\\" ) {
prev _backslash = true ;
} else {
2017-05-17 12:10:50 +00:00
source += ch ;
2012-10-11 10:00:58 +00:00
}
var mods = read _name ( ) ;
2015-05-14 19:03:54 +00:00
try {
2017-05-17 12:10:50 +00:00
var regexp = new RegExp ( source , mods ) ;
regexp . raw _source = source ;
return token ( "regexp" , regexp ) ;
2019-04-29 22:32:24 +00:00
} catch ( e ) {
2017-05-17 12:10:50 +00:00
parse _error ( e . message ) ;
2015-05-14 19:03:54 +00:00
}
2012-10-11 10:00:58 +00:00
} ) ;
2012-05-27 11:09:01 +00:00
function read _operator ( prefix ) {
function grow ( op ) {
if ( ! peek ( ) ) return op ;
var bigger = op + peek ( ) ;
2018-04-03 07:15:01 +00:00
if ( OPERATORS [ bigger ] ) {
2012-05-27 11:09:01 +00:00
next ( ) ;
return grow ( bigger ) ;
} else {
return op ;
}
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
return token ( "operator" , grow ( prefix || next ( ) ) ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function handle _slash ( ) {
next ( ) ;
switch ( peek ( ) ) {
case "/" :
2013-09-06 06:54:30 +00:00
next ( ) ;
return skip _line _comment ( "comment1" ) ;
2012-05-27 11:09:01 +00:00
case "*" :
2013-09-06 06:54:30 +00:00
next ( ) ;
return skip _multiline _comment ( ) ;
2012-05-27 11:09:01 +00:00
}
return S . regex _allowed ? read _regexp ( "" ) : read _operator ( "/" ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function handle _dot ( ) {
next ( ) ;
2020-12-05 21:19:31 +00:00
var ch = peek ( ) ;
if ( ch == "." ) {
var op = "." ;
do {
op += "." ;
next ( ) ;
} while ( peek ( ) == "." ) ;
return token ( "operator" , op ) ;
}
return is _digit ( ch . charCodeAt ( 0 ) ) ? read _num ( "." ) : token ( "punc" , "." ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function read _word ( ) {
var word = read _name ( ) ;
2013-09-02 08:36:48 +00:00
if ( prev _was _dot ) return token ( "name" , word ) ;
2018-04-03 07:15:01 +00:00
return KEYWORDS _ATOM [ word ] ? token ( "atom" , word )
: ! KEYWORDS [ word ] ? token ( "name" , word )
: OPERATORS [ word ] ? token ( "operator" , word )
2012-05-27 11:09:01 +00:00
: token ( "keyword" , word ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function with _eof _error ( eof _error , cont ) {
2012-10-11 10:00:58 +00:00
return function ( x ) {
try {
return cont ( x ) ;
2019-04-29 22:32:24 +00:00
} catch ( ex ) {
2012-10-11 10:00:58 +00:00
if ( ex === EX _EOF ) parse _error ( eof _error ) ;
else throw ex ;
}
} ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function next _token ( force _regexp ) {
if ( force _regexp != null )
return read _regexp ( force _regexp ) ;
2017-03-05 04:16:02 +00:00
if ( shebang && S . pos == 0 && looking _at ( "#!" ) ) {
start _token ( ) ;
forward ( 2 ) ;
skip _line _comment ( "comment5" ) ;
}
2016-04-15 23:58:46 +00:00
for ( ; ; ) {
skip _whitespace ( ) ;
start _token ( ) ;
if ( html5 _comments ) {
if ( looking _at ( "<!--" ) ) {
forward ( 4 ) ;
skip _line _comment ( "comment3" ) ;
continue ;
}
if ( looking _at ( "-->" ) && S . newline _before ) {
forward ( 3 ) ;
skip _line _comment ( "comment4" ) ;
continue ;
}
2013-09-06 06:54:30 +00:00
}
2016-04-15 23:58:46 +00:00
var ch = peek ( ) ;
if ( ! ch ) return token ( "eof" ) ;
var code = ch . charCodeAt ( 0 ) ;
switch ( code ) {
case 34 : case 39 : return read _string ( ch ) ;
case 46 : return handle _dot ( ) ;
2019-10-20 07:19:19 +00:00
case 47 :
var tok = handle _slash ( ) ;
if ( tok === next _token ) continue ;
return tok ;
2013-09-06 06:54:30 +00:00
}
2016-04-15 23:58:46 +00:00
if ( is _digit ( code ) ) return read _num ( ) ;
2018-04-03 07:15:01 +00:00
if ( PUNC _CHARS [ ch ] ) return token ( "punc" , next ( ) ) ;
2020-12-17 10:23:41 +00:00
if ( looking _at ( "=>" ) ) return token ( "punc" , next ( ) + next ( ) ) ;
2018-04-03 07:15:01 +00:00
if ( OPERATOR _CHARS [ ch ] ) return read _operator ( ) ;
2020-10-19 01:34:17 +00:00
if ( code == 92 || ! NON _IDENTIFIER _CHARS [ ch ] ) return read _word ( ) ;
2016-04-15 23:58:46 +00:00
break ;
2015-02-20 15:08:01 +00:00
}
2017-02-26 19:40:54 +00:00
parse _error ( "Unexpected character '" + ch + "'" ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
next _token . context = function ( nc ) {
if ( nc ) S = nc ;
return S ;
} ;
2016-05-20 08:25:35 +00:00
next _token . add _directive = function ( directive ) {
S . directive _stack [ S . directive _stack . length - 1 ] . push ( directive ) ;
2019-10-20 07:19:19 +00:00
if ( S . directives [ directive ] ) S . directives [ directive ] ++ ;
else S . directives [ directive ] = 1 ;
2016-05-20 08:25:35 +00:00
}
next _token . push _directives _stack = function ( ) {
S . directive _stack . push ( [ ] ) ;
}
next _token . pop _directives _stack = function ( ) {
2019-10-20 07:19:19 +00:00
var directives = S . directive _stack . pop ( ) ;
for ( var i = directives . length ; -- i >= 0 ; ) {
2016-05-20 08:25:35 +00:00
S . directives [ directives [ i ] ] -- ;
}
}
next _token . has _directive = function ( directive ) {
2017-04-23 12:05:22 +00:00
return S . directives [ directive ] > 0 ;
2016-05-20 08:25:35 +00:00
}
2012-05-27 11:09:01 +00:00
return next _token ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
/* -----[ Parser (constants) ]----- */
2019-10-20 07:19:19 +00:00
var UNARY _PREFIX = makePredicate ( "typeof void delete -- ++ ! ~ - +" ) ;
2012-05-27 11:09:01 +00:00
2019-10-20 07:19:19 +00:00
var UNARY _POSTFIX = makePredicate ( "-- ++" ) ;
2012-05-27 11:09:01 +00:00
2019-10-20 07:19:19 +00:00
var ASSIGNMENT = makePredicate ( "= += -= /= *= %= >>= <<= >>>= |= ^= &=" ) ;
2012-05-27 11:09:01 +00:00
2018-06-06 09:50:56 +00:00
var PRECEDENCE = function ( a , ret ) {
2019-10-20 07:19:19 +00:00
for ( var i = 0 ; i < a . length ; ) {
var b = a [ i ++ ] ;
for ( var j = 0 ; j < b . length ; j ++ ) {
ret [ b [ j ] ] = i ;
2012-05-27 11:09:01 +00:00
}
}
return ret ;
2018-06-06 09:50:56 +00:00
} ( [
[ "||" ] ,
[ "&&" ] ,
[ "|" ] ,
[ "^" ] ,
[ "&" ] ,
[ "==" , "===" , "!=" , "!==" ] ,
[ "<" , ">" , "<=" , ">=" , "in" , "instanceof" ] ,
[ ">>" , "<<" , ">>>" ] ,
[ "+" , "-" ] ,
2021-01-24 21:48:51 +00:00
[ "*" , "/" , "%" ] ,
[ "**" ] ,
2018-06-06 09:50:56 +00:00
] , { } ) ;
2012-05-27 11:09:01 +00:00
2021-01-24 01:51:18 +00:00
var ATOMIC _START _TOKEN = makePredicate ( "atom bigint num regexp string" ) ;
2012-05-27 11:09:01 +00:00
/* -----[ Parser ]----- */
2012-09-21 11:19:05 +00:00
function parse ( $TEXT , options ) {
options = defaults ( options , {
2017-03-31 08:41:04 +00:00
bare _returns : false ,
2013-09-06 06:54:30 +00:00
expression : false ,
2017-03-31 08:41:04 +00:00
filename : null ,
2013-09-06 06:54:30 +00:00
html5 _comments : true ,
2015-02-20 15:08:01 +00:00
shebang : true ,
2017-03-31 08:41:04 +00:00
strict : false ,
toplevel : null ,
2017-05-28 10:21:44 +00:00
} , true ) ;
2012-05-27 11:09:01 +00:00
var S = {
2019-10-20 07:19:19 +00:00
input : typeof $TEXT == "string"
? tokenizer ( $TEXT , options . filename , options . html5 _comments , options . shebang )
: $TEXT ,
2020-12-06 21:22:40 +00:00
in _async : false ,
2012-05-27 11:09:01 +00:00
in _directives : true ,
2020-12-08 04:59:08 +00:00
in _funarg : - 1 ,
in _function : 0 ,
2012-05-27 11:09:01 +00:00
in _loop : 0 ,
2020-12-08 04:59:08 +00:00
labels : [ ] ,
peeked : null ,
prev : null ,
token : null ,
2012-05-27 11:09:01 +00:00
} ;
S . token = next ( ) ;
function is ( type , value ) {
return is _token ( S . token , type , value ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2018-06-06 09:50:56 +00:00
function peek ( ) {
return S . peeked || ( S . peeked = S . input ( ) ) ;
}
2012-05-27 11:09:01 +00:00
function next ( ) {
S . prev = S . token ;
if ( S . peeked ) {
S . token = S . peeked ;
S . peeked = null ;
} else {
S . token = S . input ( ) ;
}
S . in _directives = S . in _directives && (
S . token . type == "string" || is ( "punc" , ";" )
) ;
return S . token ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function prev ( ) {
return S . prev ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function croak ( msg , line , col , pos ) {
var ctx = S . input . context ( ) ;
js _error ( msg ,
2012-09-21 11:38:52 +00:00
ctx . filename ,
2012-05-27 11:09:01 +00:00
line != null ? line : ctx . tokline ,
col != null ? col : ctx . tokcol ,
pos != null ? pos : ctx . tokpos ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function token _error ( token , msg ) {
croak ( msg , token . line , token . col ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2019-03-14 16:20:20 +00:00
function token _to _string ( type , value ) {
return type + ( value === undefined ? "" : " «" + value + "»" ) ;
}
2012-05-27 11:09:01 +00:00
function unexpected ( token ) {
2019-10-20 07:19:19 +00:00
if ( token == null ) token = S . token ;
2019-03-14 16:20:20 +00:00
token _error ( token , "Unexpected token: " + token _to _string ( token . type , token . value ) ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function expect _token ( type , val ) {
2019-10-20 07:19:19 +00:00
if ( is ( type , val ) ) return next ( ) ;
2019-03-14 16:20:20 +00:00
token _error ( S . token , "Unexpected token: " + token _to _string ( S . token . type , S . token . value ) + ", expected: " + token _to _string ( type , val ) ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2018-06-06 09:50:56 +00:00
function expect ( punc ) {
return expect _token ( "punc" , punc ) ;
}
2012-05-27 11:09:01 +00:00
2017-12-25 17:38:01 +00:00
function has _newline _before ( token ) {
return token . nlb || ! all ( token . comments _before , function ( comment ) {
return ! comment . nlb ;
} ) ;
}
2012-05-27 11:09:01 +00:00
function can _insert _semicolon ( ) {
2017-12-25 17:38:01 +00:00
return ! options . strict
&& ( is ( "eof" ) || is ( "punc" , "}" ) || has _newline _before ( S . token ) ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2015-12-17 22:02:35 +00:00
function semicolon ( optional ) {
2012-05-27 11:09:01 +00:00
if ( is ( "punc" , ";" ) ) next ( ) ;
2020-12-22 18:16:04 +00:00
else if ( ! optional && ! can _insert _semicolon ( ) ) expect ( ";" ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function parenthesised ( ) {
2012-08-17 12:59:42 +00:00
expect ( "(" ) ;
2020-12-17 10:23:41 +00:00
var exp = expression ( ) ;
2012-08-17 12:59:42 +00:00
expect ( ")" ) ;
return exp ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function embed _tokens ( parser ) {
return function ( ) {
var start = S . token ;
2018-01-04 10:45:51 +00:00
var expr = parser . apply ( null , arguments ) ;
2012-05-27 11:09:01 +00:00
var end = prev ( ) ;
expr . start = start ;
expr . end = end ;
return expr ;
} ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2013-09-02 06:56:27 +00:00
function handle _regexp ( ) {
2012-05-27 11:09:01 +00:00
if ( is ( "operator" , "/" ) || is ( "operator" , "/=" ) ) {
S . peeked = null ;
S . token = S . input ( S . token . value . substr ( 1 ) ) ; // force regexp
}
2018-06-06 09:50:56 +00:00
}
2013-09-02 06:56:27 +00:00
2020-11-07 02:00:04 +00:00
var statement = embed _tokens ( function ( ) {
2013-09-02 06:56:27 +00:00
handle _regexp ( ) ;
2012-05-27 11:09:01 +00:00
switch ( S . token . type ) {
case "string" :
2019-11-29 10:57:29 +00:00
var dir = S . in _directives ;
2020-12-17 10:23:41 +00:00
var body = expression ( ) ;
2019-11-29 10:57:29 +00:00
if ( dir ) {
2019-12-25 00:55:39 +00:00
if ( body instanceof AST _String ) {
var value = body . start . raw . slice ( 1 , - 1 ) ;
S . input . add _directive ( value ) ;
body . value = value ;
2016-05-20 08:25:35 +00:00
} else {
2019-11-29 10:57:29 +00:00
S . in _directives = dir = false ;
2016-05-20 08:25:35 +00:00
}
}
2019-11-29 10:57:29 +00:00
semicolon ( ) ;
return dir ? new AST _Directive ( body ) : new AST _SimpleStatement ( { body : body } ) ;
2012-05-27 11:09:01 +00:00
case "num" :
2021-01-24 01:51:18 +00:00
case "bigint" :
2012-05-27 11:09:01 +00:00
case "regexp" :
case "operator" :
case "atom" :
return simple _statement ( ) ;
case "name" :
2020-12-06 21:22:40 +00:00
switch ( S . token . value ) {
case "async" :
if ( is _token ( peek ( ) , "keyword" , "function" ) ) {
next ( ) ;
next ( ) ;
return function _ ( AST _AsyncDefun ) ;
}
2021-01-10 03:34:26 +00:00
break ;
2020-12-06 21:22:40 +00:00
case "await" :
if ( S . in _async ) return simple _statement ( ) ;
2021-01-10 03:34:26 +00:00
break ;
2020-12-06 21:22:40 +00:00
}
2021-01-10 03:34:26 +00:00
return is _token ( peek ( ) , "punc" , ":" )
? labeled _statement ( )
: simple _statement ( ) ;
2012-05-27 11:09:01 +00:00
case "punc" :
switch ( S . token . value ) {
case "{" :
2012-09-05 08:31:02 +00:00
return new AST _BlockStatement ( {
start : S . token ,
body : block _ ( ) ,
end : prev ( )
} ) ;
2012-05-27 11:09:01 +00:00
case "[" :
case "(" :
2021-02-01 02:36:45 +00:00
case "`" :
2012-05-27 11:09:01 +00:00
return simple _statement ( ) ;
case ";" :
2016-06-13 19:11:08 +00:00
S . in _directives = false ;
2012-05-27 11:09:01 +00:00
next ( ) ;
2012-08-15 10:32:37 +00:00
return new AST _EmptyStatement ( ) ;
2012-05-27 11:09:01 +00:00
default :
unexpected ( ) ;
}
case "keyword" :
2017-05-15 15:02:55 +00:00
switch ( S . token . value ) {
2012-05-27 11:09:01 +00:00
case "break" :
2017-05-15 15:02:55 +00:00
next ( ) ;
2012-05-27 11:09:01 +00:00
return break _cont ( AST _Break ) ;
2020-10-11 17:18:57 +00:00
case "const" :
next ( ) ;
var node = const _ ( ) ;
semicolon ( ) ;
return node ;
2012-05-27 11:09:01 +00:00
case "continue" :
2017-05-15 15:02:55 +00:00
next ( ) ;
2012-05-27 11:09:01 +00:00
return break _cont ( AST _Continue ) ;
case "debugger" :
2017-05-15 15:02:55 +00:00
next ( ) ;
2012-05-27 11:09:01 +00:00
semicolon ( ) ;
return new AST _Debugger ( ) ;
case "do" :
2017-05-15 15:02:55 +00:00
next ( ) ;
var body = in _loop ( statement ) ;
expect _token ( "keyword" , "while" ) ;
var condition = parenthesised ( ) ;
semicolon ( true ) ;
2012-05-27 11:09:01 +00:00
return new AST _Do ( {
2017-05-15 15:02:55 +00:00
body : body ,
condition : condition
2012-05-27 11:09:01 +00:00
} ) ;
case "while" :
2017-05-15 15:02:55 +00:00
next ( ) ;
2012-05-27 11:09:01 +00:00
return new AST _While ( {
condition : parenthesised ( ) ,
body : in _loop ( statement )
} ) ;
case "for" :
2017-05-15 15:02:55 +00:00
next ( ) ;
2012-05-27 11:09:01 +00:00
return for _ ( ) ;
case "function" :
2017-05-15 15:02:55 +00:00
next ( ) ;
2013-10-30 09:50:22 +00:00
return function _ ( AST _Defun ) ;
2012-05-27 11:09:01 +00:00
case "if" :
2017-05-15 15:02:55 +00:00
next ( ) ;
2012-05-27 11:09:01 +00:00
return if _ ( ) ;
2020-10-19 00:32:39 +00:00
case "let" :
next ( ) ;
var node = let _ ( ) ;
semicolon ( ) ;
return node ;
2012-05-27 11:09:01 +00:00
case "return" :
2014-10-20 15:12:13 +00:00
if ( S . in _function == 0 && ! options . bare _returns )
2017-02-26 19:40:54 +00:00
croak ( "'return' outside of function" ) ;
2017-05-15 15:02:55 +00:00
next ( ) ;
var value = null ;
if ( is ( "punc" , ";" ) ) {
next ( ) ;
} else if ( ! can _insert _semicolon ( ) ) {
2020-12-17 10:23:41 +00:00
value = expression ( ) ;
2017-05-15 15:02:55 +00:00
semicolon ( ) ;
}
2012-05-27 11:09:01 +00:00
return new AST _Return ( {
2017-05-15 15:02:55 +00:00
value : value
2012-05-27 11:09:01 +00:00
} ) ;
case "switch" :
2017-05-15 15:02:55 +00:00
next ( ) ;
2012-05-27 11:09:01 +00:00
return new AST _Switch ( {
expression : parenthesised ( ) ,
2012-10-11 10:00:58 +00:00
body : in _loop ( switch _body _ )
2012-05-27 11:09:01 +00:00
} ) ;
case "throw" :
2017-05-15 15:02:55 +00:00
next ( ) ;
2017-12-25 17:38:01 +00:00
if ( has _newline _before ( S . token ) )
2017-02-26 19:40:54 +00:00
croak ( "Illegal newline after 'throw'" ) ;
2020-12-17 10:23:41 +00:00
var value = expression ( ) ;
2017-05-15 15:02:55 +00:00
semicolon ( ) ;
2012-05-27 11:09:01 +00:00
return new AST _Throw ( {
2017-05-15 15:02:55 +00:00
value : value
2012-05-27 11:09:01 +00:00
} ) ;
case "try" :
2017-05-15 15:02:55 +00:00
next ( ) ;
2012-05-27 11:09:01 +00:00
return try _ ( ) ;
case "var" :
2017-05-15 15:02:55 +00:00
next ( ) ;
var node = var _ ( ) ;
semicolon ( ) ;
return node ;
2012-05-27 11:09:01 +00:00
case "with" :
2016-06-12 16:58:20 +00:00
if ( S . input . has _directive ( "use strict" ) ) {
2017-02-26 19:40:54 +00:00
croak ( "Strict mode may not include a with statement" ) ;
2016-06-12 16:58:20 +00:00
}
2017-05-15 15:02:55 +00:00
next ( ) ;
2012-05-27 11:09:01 +00:00
return new AST _With ( {
expression : parenthesised ( ) ,
body : statement ( )
} ) ;
}
}
2017-03-10 02:49:41 +00:00
unexpected ( ) ;
2012-05-27 11:09:01 +00:00
} ) ;
function labeled _statement ( ) {
2012-08-19 12:57:50 +00:00
var label = as _symbol ( AST _Label ) ;
2018-03-22 19:43:52 +00:00
if ( ! all ( S . labels , function ( l ) {
return l . name != label . name ;
} ) ) {
2012-08-19 12:57:50 +00:00
// ECMA-262, 12.12: An ECMAScript program is considered
// syntactically incorrect if it contains a
// LabelledStatement that is enclosed by a
// LabelledStatement with the same Identifier as label.
2017-02-26 19:40:54 +00:00
croak ( "Label " + label . name + " defined twice" ) ;
2012-08-19 12:57:50 +00:00
}
2012-05-27 11:09:01 +00:00
expect ( ":" ) ;
S . labels . push ( label ) ;
2012-10-04 05:49:18 +00:00
var stat = statement ( ) ;
2012-05-27 11:09:01 +00:00
S . labels . pop ( ) ;
2013-09-02 16:36:16 +00:00
if ( ! ( stat instanceof AST _IterationStatement ) ) {
// check for `continue` that refers to this label.
// those should be reported as syntax errors.
2020-05-05 13:07:33 +00:00
// https://github.com/mishoo/UglifyJS/issues/287
2018-06-06 09:50:56 +00:00
label . references . forEach ( function ( ref ) {
2013-09-02 16:36:16 +00:00
if ( ref instanceof AST _Continue ) {
2020-12-17 10:23:41 +00:00
token _error ( ref . label . start , "Continue label `" + label . name + "` must refer to IterationStatement" ) ;
2013-09-02 16:36:16 +00:00
}
} ) ;
}
2012-09-03 09:05:10 +00:00
return new AST _LabeledStatement ( { body : stat , label : label } ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2019-11-29 10:57:29 +00:00
function simple _statement ( ) {
2020-12-17 10:23:41 +00:00
var body = expression ( ) ;
2019-11-29 10:57:29 +00:00
semicolon ( ) ;
return new AST _SimpleStatement ( { body : body } ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function break _cont ( type ) {
2013-09-02 16:36:16 +00:00
var label = null , ldef ;
2012-05-27 11:09:01 +00:00
if ( ! can _insert _semicolon ( ) ) {
2012-08-19 12:57:50 +00:00
label = as _symbol ( AST _LabelRef , true ) ;
2012-05-27 11:09:01 +00:00
}
2012-08-19 12:57:50 +00:00
if ( label != null ) {
2018-06-06 09:50:56 +00:00
ldef = find _if ( function ( l ) {
return l . name == label . name ;
} , S . labels ) ;
2020-12-17 10:23:41 +00:00
if ( ! ldef ) token _error ( label . start , "Undefined label " + label . name ) ;
2013-09-02 16:36:16 +00:00
label . thedef = ldef ;
2018-06-06 09:50:56 +00:00
} else if ( S . in _loop == 0 ) croak ( type . TYPE + " not inside a loop or switch" ) ;
2012-05-27 11:09:01 +00:00
semicolon ( ) ;
2013-09-02 16:36:16 +00:00
var stat = new type ( { label : label } ) ;
if ( ldef ) ldef . references . push ( stat ) ;
return stat ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function for _ ( ) {
expect ( "(" ) ;
var init = null ;
if ( ! is ( "punc" , ";" ) ) {
2020-10-11 17:18:57 +00:00
init = is ( "keyword" , "const" )
? ( next ( ) , const _ ( true ) )
2020-11-17 00:01:24 +00:00
: is ( "keyword" , "let" )
? ( next ( ) , let _ ( true ) )
2020-10-11 17:18:57 +00:00
: is ( "keyword" , "var" )
2012-05-27 11:09:01 +00:00
? ( next ( ) , var _ ( true ) )
2020-12-17 10:23:41 +00:00
: expression ( true ) ;
2012-05-27 11:09:01 +00:00
if ( is ( "operator" , "in" ) ) {
2020-11-17 00:01:24 +00:00
if ( init instanceof AST _Definitions ) {
2020-12-17 10:23:41 +00:00
if ( init . definitions . length > 1 ) {
token _error ( init . start , "Only one variable declaration allowed in for..in loop" ) ;
}
2020-11-17 00:01:24 +00:00
} else if ( ! ( is _assignable ( init ) || ( init = to _destructured ( init ) ) instanceof AST _Destructured ) ) {
2020-12-17 10:23:41 +00:00
token _error ( init . start , "Invalid left-hand side in for..in loop" ) ;
2017-06-22 20:14:30 +00:00
}
2012-05-27 11:09:01 +00:00
next ( ) ;
return for _in ( init ) ;
}
}
return regular _for ( init ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function regular _for ( init ) {
expect ( ";" ) ;
2020-12-17 10:23:41 +00:00
var test = is ( "punc" , ";" ) ? null : expression ( ) ;
2012-05-27 11:09:01 +00:00
expect ( ";" ) ;
2020-12-17 10:23:41 +00:00
var step = is ( "punc" , ")" ) ? null : expression ( ) ;
2012-05-27 11:09:01 +00:00
expect ( ")" ) ;
return new AST _For ( {
init : init ,
condition : test ,
step : step ,
body : in _loop ( statement )
} ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function for _in ( init ) {
2020-12-17 10:23:41 +00:00
var obj = expression ( ) ;
2012-05-27 11:09:01 +00:00
expect ( ")" ) ;
return new AST _ForIn ( {
init : init ,
object : obj ,
body : in _loop ( statement )
} ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2020-12-17 10:23:41 +00:00
function to _funarg ( node ) {
2021-01-07 02:04:09 +00:00
if ( node instanceof AST _Array ) {
var rest = null ;
if ( node . elements [ node . elements . length - 1 ] instanceof AST _Spread ) {
rest = to _funarg ( node . elements . pop ( ) . expression ) ;
}
return new AST _DestructuredArray ( {
start : node . start ,
elements : node . elements . map ( to _funarg ) ,
rest : rest ,
end : node . end ,
} ) ;
}
2020-12-23 22:22:55 +00:00
if ( node instanceof AST _Assign ) return new AST _DefaultValue ( {
start : node . start ,
name : to _funarg ( node . left ) ,
value : node . right ,
2020-12-17 10:23:41 +00:00
end : node . end ,
} ) ;
2020-12-23 22:22:55 +00:00
if ( node instanceof AST _DefaultValue ) {
node . name = to _funarg ( node . name ) ;
return node ;
}
if ( node instanceof AST _DestructuredArray ) {
node . elements = node . elements . map ( to _funarg ) ;
2021-01-07 02:04:09 +00:00
if ( node . rest ) node . rest = to _funarg ( node . rest ) ;
2020-12-23 22:22:55 +00:00
return node ;
}
if ( node instanceof AST _DestructuredObject ) {
node . properties . forEach ( function ( prop ) {
prop . value = to _funarg ( prop . value ) ;
} ) ;
2021-01-07 02:04:09 +00:00
if ( node . rest ) node . rest = to _funarg ( node . rest ) ;
2020-12-23 22:22:55 +00:00
return node ;
}
if ( node instanceof AST _Hole ) return node ;
2021-01-07 02:04:09 +00:00
if ( node instanceof AST _Object ) {
var rest = null ;
if ( node . properties [ node . properties . length - 1 ] instanceof AST _Spread ) {
rest = to _funarg ( node . properties . pop ( ) . expression ) ;
}
return new AST _DestructuredObject ( {
start : node . start ,
properties : node . properties . map ( function ( prop ) {
if ( ! ( prop instanceof AST _ObjectKeyVal ) ) token _error ( prop . start , "Invalid destructuring assignment" ) ;
return new AST _DestructuredKeyVal ( {
start : prop . start ,
key : prop . key ,
value : to _funarg ( prop . value ) ,
end : prop . end ,
} ) ;
} ) ,
rest : rest ,
end : node . end ,
} ) ;
}
2021-01-10 03:34:26 +00:00
if ( node instanceof AST _SymbolFunarg ) return node ;
2020-12-17 10:23:41 +00:00
if ( node instanceof AST _SymbolRef ) return new AST _SymbolFunarg ( node ) ;
token _error ( node . start , "Invalid arrow parameter" ) ;
}
2021-01-10 03:34:26 +00:00
function arrow ( exprs , start , async ) {
2020-12-17 10:23:41 +00:00
var was _async = S . in _async ;
2021-01-10 03:34:26 +00:00
S . in _async = async ;
2020-12-17 10:23:41 +00:00
var was _funarg = S . in _funarg ;
S . in _funarg = S . in _function ;
var argnames = exprs . map ( to _funarg ) ;
2021-01-10 03:34:26 +00:00
var rest = exprs . rest || null ;
if ( rest ) rest = to _funarg ( rest ) ;
2020-12-17 10:23:41 +00:00
S . in _funarg = was _funarg ;
expect ( "=>" ) ;
var body , value ;
var loop = S . in _loop ;
var labels = S . labels ;
++ S . in _function ;
S . in _directives = true ;
S . input . push _directives _stack ( ) ;
S . in _loop = 0 ;
S . labels = [ ] ;
if ( is ( "punc" , "{" ) ) {
body = block _ ( ) ;
value = null ;
if ( S . input . has _directive ( "use strict" ) ) {
argnames . forEach ( strict _verify _symbol ) ;
}
} else {
body = [ ] ;
value = maybe _assign ( ) ;
}
S . input . pop _directives _stack ( ) ;
-- S . in _function ;
S . in _loop = loop ;
S . labels = labels ;
S . in _async = was _async ;
2021-01-10 03:34:26 +00:00
return new ( async ? AST _AsyncArrow : AST _Arrow ) ( {
2020-12-17 10:23:41 +00:00
start : start ,
argnames : argnames ,
2021-01-10 03:34:26 +00:00
rest : rest ,
2020-12-17 10:23:41 +00:00
body : body ,
value : value ,
end : prev ( ) ,
} ) ;
}
2013-10-30 09:50:22 +00:00
var function _ = function ( ctor ) {
2020-12-06 21:22:40 +00:00
var was _async = S . in _async ;
var name ;
if ( ctor === AST _AsyncDefun ) {
name = as _symbol ( AST _SymbolDefun ) ;
S . in _async = true ;
} else if ( ctor === AST _Defun ) {
name = as _symbol ( AST _SymbolDefun ) ;
S . in _async = false ;
} else {
S . in _async = ctor === AST _AsyncFunction ;
name = as _symbol ( AST _SymbolLambda , true ) ;
}
2017-09-30 18:10:41 +00:00
if ( name && ctor !== AST _Accessor && ! ( name instanceof AST _SymbolDeclaration ) )
unexpected ( prev ( ) ) ;
2012-05-27 11:09:01 +00:00
expect ( "(" ) ;
2020-12-08 04:59:08 +00:00
var was _funarg = S . in _funarg ;
S . in _funarg = S . in _function ;
2020-12-07 02:07:34 +00:00
var argnames = expr _list ( ")" , ! options . strict , false , function ( ) {
2020-12-23 22:22:55 +00:00
return maybe _default ( AST _SymbolFunarg ) ;
2020-12-07 02:07:34 +00:00
} ) ;
2020-12-08 04:59:08 +00:00
S . in _funarg = was _funarg ;
2017-04-23 12:05:22 +00:00
var loop = S . in _loop ;
var labels = S . labels ;
++ S . in _function ;
S . in _directives = true ;
S . input . push _directives _stack ( ) ;
S . in _loop = 0 ;
S . labels = [ ] ;
2020-11-07 02:00:04 +00:00
var body = block _ ( ) ;
2017-04-23 12:05:22 +00:00
if ( S . input . has _directive ( "use strict" ) ) {
if ( name ) strict _verify _symbol ( name ) ;
argnames . forEach ( strict _verify _symbol ) ;
2021-01-07 02:04:09 +00:00
if ( argnames . rest ) strict _verify _symbol ( argnames . rest ) ;
2017-04-23 12:05:22 +00:00
}
S . input . pop _directives _stack ( ) ;
-- S . in _function ;
S . in _loop = loop ;
S . labels = labels ;
2020-12-06 21:22:40 +00:00
S . in _async = was _async ;
2012-05-27 11:09:01 +00:00
return new ctor ( {
name : name ,
2017-04-23 12:05:22 +00:00
argnames : argnames ,
2021-01-07 02:04:09 +00:00
rest : argnames . rest || null ,
2017-04-23 12:05:22 +00:00
body : body
2012-05-27 11:09:01 +00:00
} ) ;
} ;
function if _ ( ) {
var cond = parenthesised ( ) , body = statement ( ) , belse = null ;
if ( is ( "keyword" , "else" ) ) {
next ( ) ;
belse = statement ( ) ;
}
return new AST _If ( {
condition : cond ,
2012-09-03 09:11:44 +00:00
body : body ,
2012-05-27 11:09:01 +00:00
alternative : belse
} ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2020-11-07 02:00:04 +00:00
function block _ ( ) {
2012-05-27 11:09:01 +00:00
expect ( "{" ) ;
var a = [ ] ;
while ( ! is ( "punc" , "}" ) ) {
2020-12-22 18:16:04 +00:00
if ( is ( "eof" ) ) expect ( "}" ) ;
2020-11-07 02:00:04 +00:00
a . push ( statement ( ) ) ;
2012-05-27 11:09:01 +00:00
}
next ( ) ;
2012-09-05 08:31:02 +00:00
return a ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2012-10-11 10:00:58 +00:00
function switch _body _ ( ) {
2012-05-27 11:09:01 +00:00
expect ( "{" ) ;
2020-08-17 02:09:12 +00:00
var a = [ ] , branch , cur , default _branch , tmp ;
2012-05-27 11:09:01 +00:00
while ( ! is ( "punc" , "}" ) ) {
2020-12-22 18:16:04 +00:00
if ( is ( "eof" ) ) expect ( "}" ) ;
2012-05-27 11:09:01 +00:00
if ( is ( "keyword" , "case" ) ) {
2012-05-27 14:25:31 +00:00
if ( branch ) branch . end = prev ( ) ;
2012-05-27 11:09:01 +00:00
cur = [ ] ;
2012-05-27 14:25:31 +00:00
branch = new AST _Case ( {
2012-10-11 10:00:58 +00:00
start : ( tmp = S . token , next ( ) , tmp ) ,
2020-12-17 10:23:41 +00:00
expression : expression ( ) ,
2012-05-27 14:25:31 +00:00
body : cur
} ) ;
a . push ( branch ) ;
2012-05-27 11:09:01 +00:00
expect ( ":" ) ;
2019-03-14 16:20:20 +00:00
} else if ( is ( "keyword" , "default" ) ) {
2012-05-27 14:25:31 +00:00
if ( branch ) branch . end = prev ( ) ;
2020-08-17 02:09:12 +00:00
if ( default _branch ) croak ( "More than one default clause in switch statement" ) ;
2012-05-27 11:09:01 +00:00
cur = [ ] ;
2012-05-27 14:25:31 +00:00
branch = new AST _Default ( {
2012-10-11 10:00:58 +00:00
start : ( tmp = S . token , next ( ) , expect ( ":" ) , tmp ) ,
2012-05-27 14:25:31 +00:00
body : cur
2012-10-11 10:00:58 +00:00
} ) ;
2012-05-27 14:25:31 +00:00
a . push ( branch ) ;
2020-08-17 02:09:12 +00:00
default _branch = branch ;
2019-03-14 16:20:20 +00:00
} else {
2012-05-27 11:09:01 +00:00
if ( ! cur ) unexpected ( ) ;
cur . push ( statement ( ) ) ;
}
}
2012-05-27 14:25:31 +00:00
if ( branch ) branch . end = prev ( ) ;
2012-05-27 11:09:01 +00:00
next ( ) ;
2012-10-03 12:52:01 +00:00
return a ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function try _ ( ) {
2012-09-05 08:31:02 +00:00
var body = block _ ( ) , bcatch = null , bfinally = null ;
2012-05-27 11:09:01 +00:00
if ( is ( "keyword" , "catch" ) ) {
2012-05-27 14:25:31 +00:00
var start = S . token ;
2012-05-27 11:09:01 +00:00
next ( ) ;
2020-10-04 21:30:14 +00:00
var name = null ;
if ( is ( "punc" , "(" ) ) {
next ( ) ;
2020-12-19 04:28:38 +00:00
name = maybe _destructured ( AST _SymbolCatch ) ;
2020-10-04 21:30:14 +00:00
expect ( ")" ) ;
}
2012-05-27 11:09:01 +00:00
bcatch = new AST _Catch ( {
2012-05-27 14:25:31 +00:00
start : start ,
2012-05-27 11:09:01 +00:00
argname : name ,
2012-09-05 08:31:02 +00:00
body : block _ ( ) ,
2012-05-27 14:25:31 +00:00
end : prev ( )
2012-05-27 11:09:01 +00:00
} ) ;
}
if ( is ( "keyword" , "finally" ) ) {
2012-05-27 14:25:31 +00:00
var start = S . token ;
2012-05-27 11:09:01 +00:00
next ( ) ;
2012-05-27 14:25:31 +00:00
bfinally = new AST _Finally ( {
start : start ,
2012-09-05 08:31:02 +00:00
body : block _ ( ) ,
2012-05-27 14:25:31 +00:00
end : prev ( )
} ) ;
2012-05-27 11:09:01 +00:00
}
if ( ! bcatch && ! bfinally )
2017-02-26 19:40:54 +00:00
croak ( "Missing catch/finally blocks" ) ;
2012-05-27 11:09:01 +00:00
return new AST _Try ( {
2012-09-05 08:31:02 +00:00
body : body ,
2012-05-27 11:09:01 +00:00
bcatch : bcatch ,
bfinally : bfinally
} ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2020-11-17 00:01:24 +00:00
function vardefs ( type , no _in ) {
2012-05-27 11:09:01 +00:00
var a = [ ] ;
for ( ; ; ) {
2020-10-11 17:18:57 +00:00
var start = S . token ;
2020-11-17 00:01:24 +00:00
var name = maybe _destructured ( type ) ;
2020-10-11 17:18:57 +00:00
var value = null ;
if ( is ( "operator" , "=" ) ) {
next ( ) ;
2020-12-17 10:23:41 +00:00
value = maybe _assign ( no _in ) ;
2020-11-17 00:01:24 +00:00
} else if ( ! no _in && ( type === AST _SymbolConst || name instanceof AST _Destructured ) ) {
2020-10-11 17:18:57 +00:00
croak ( "Missing initializer in declaration" ) ;
}
2012-05-27 11:09:01 +00:00
a . push ( new AST _VarDef ( {
2020-10-11 17:18:57 +00:00
start : start ,
name : name ,
value : value ,
2012-05-27 11:09:01 +00:00
end : prev ( )
} ) ) ;
if ( ! is ( "punc" , "," ) )
break ;
next ( ) ;
}
return a ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2020-10-11 17:18:57 +00:00
var const _ = function ( no _in ) {
return new AST _Const ( {
start : prev ( ) ,
2020-11-17 00:01:24 +00:00
definitions : vardefs ( AST _SymbolConst , no _in ) ,
2020-10-11 17:18:57 +00:00
end : prev ( )
} ) ;
} ;
2020-10-19 00:32:39 +00:00
var let _ = function ( no _in ) {
return new AST _Let ( {
start : prev ( ) ,
definitions : vardefs ( AST _SymbolLet , no _in ) ,
end : prev ( )
} ) ;
} ;
2012-05-27 14:25:31 +00:00
var var _ = function ( no _in ) {
2012-05-27 11:09:01 +00:00
return new AST _Var ( {
2012-05-27 14:25:31 +00:00
start : prev ( ) ,
2020-10-11 17:18:57 +00:00
definitions : vardefs ( AST _SymbolVar , no _in ) ,
2012-05-27 14:25:31 +00:00
end : prev ( )
2012-05-27 11:09:01 +00:00
} ) ;
2012-05-27 14:25:31 +00:00
} ;
2012-05-27 11:09:01 +00:00
2015-08-06 19:27:46 +00:00
var new _ = function ( allow _calls ) {
2012-05-27 14:25:31 +00:00
var start = S . token ;
expect _token ( "operator" , "new" ) ;
2012-05-27 11:09:01 +00:00
var newexp = expr _atom ( false ) , args ;
if ( is ( "punc" , "(" ) ) {
next ( ) ;
2020-12-07 02:07:34 +00:00
args = expr _list ( ")" , ! options . strict ) ;
2012-05-27 11:09:01 +00:00
} else {
args = [ ] ;
}
2018-01-02 20:48:07 +00:00
var call = new AST _New ( {
2012-05-27 14:25:31 +00:00
start : start ,
2012-05-27 11:09:01 +00:00
expression : newexp ,
2012-05-27 14:25:31 +00:00
args : args ,
end : prev ( )
2018-01-02 20:48:07 +00:00
} ) ;
mark _pure ( call ) ;
return subscripts ( call , allow _calls ) ;
2012-05-27 14:25:31 +00:00
} ;
2012-05-27 11:09:01 +00:00
function as _atom _node ( ) {
var tok = S . token , ret ;
switch ( tok . type ) {
case "num" :
2015-11-11 20:15:25 +00:00
ret = new AST _Number ( { start : tok , end : tok , value : tok . value } ) ;
2012-05-27 11:09:01 +00:00
break ;
2021-01-24 01:51:18 +00:00
case "bigint" :
ret = new AST _BigInt ( { start : tok , end : tok , value : tok . value } ) ;
break ;
2012-05-27 11:09:01 +00:00
case "string" :
2015-01-27 20:26:27 +00:00
ret = new AST _String ( {
start : tok ,
end : tok ,
value : tok . value ,
quote : tok . quote
} ) ;
2012-05-27 11:09:01 +00:00
break ;
case "regexp" :
2012-10-09 13:25:45 +00:00
ret = new AST _RegExp ( { start : tok , end : tok , value : tok . value } ) ;
2012-05-27 11:09:01 +00:00
break ;
case "atom" :
switch ( tok . value ) {
case "false" :
ret = new AST _False ( { start : tok , end : tok } ) ;
break ;
case "true" :
ret = new AST _True ( { start : tok , end : tok } ) ;
break ;
case "null" :
ret = new AST _Null ( { start : tok , end : tok } ) ;
break ;
}
break ;
}
next ( ) ;
return ret ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
var expr _atom = function ( allow _calls ) {
if ( is ( "operator" , "new" ) ) {
2015-08-06 19:27:46 +00:00
return new _ ( allow _calls ) ;
2012-05-27 11:09:01 +00:00
}
2012-05-27 14:25:31 +00:00
var start = S . token ;
2012-05-27 11:09:01 +00:00
if ( is ( "punc" ) ) {
2012-05-27 14:25:31 +00:00
switch ( start . value ) {
2021-02-01 02:36:45 +00:00
case "`" :
var tmpl = template ( null ) ;
tmpl . start = start ;
tmpl . end = prev ( ) ;
return subscripts ( tmpl , allow _calls ) ;
2012-05-27 11:09:01 +00:00
case "(" :
next ( ) ;
2020-12-17 10:23:41 +00:00
if ( is ( "punc" , ")" ) ) {
next ( ) ;
return arrow ( [ ] , start ) ;
}
var ex = expression ( false , true ) ;
2017-12-24 04:38:45 +00:00
var len = start . comments _before . length ;
[ ] . unshift . apply ( ex . start . comments _before , start . comments _before ) ;
2019-10-29 19:49:39 +00:00
start . comments _before . length = 0 ;
2017-12-24 04:38:45 +00:00
start . comments _before = ex . start . comments _before ;
start . comments _before _length = len ;
if ( len == 0 && start . comments _before . length > 0 ) {
var comment = start . comments _before [ 0 ] ;
if ( ! comment . nlb ) {
comment . nlb = start . nlb ;
start . nlb = false ;
}
}
2017-12-21 20:59:54 +00:00
start . comments _after = ex . start . comments _after ;
2012-05-27 14:25:31 +00:00
ex . start = start ;
expect ( ")" ) ;
2017-12-21 20:59:54 +00:00
var end = prev ( ) ;
end . comments _before = ex . end . comments _before ;
[ ] . push . apply ( ex . end . comments _after , end . comments _after ) ;
2019-10-29 19:49:39 +00:00
end . comments _after . length = 0 ;
2017-12-21 20:59:54 +00:00
end . comments _after = ex . end . comments _after ;
ex . end = end ;
2017-12-24 04:38:45 +00:00
if ( ex instanceof AST _Call ) mark _pure ( ex ) ;
2020-12-17 10:23:41 +00:00
if ( is ( "punc" , "=>" ) ) return arrow ( ex instanceof AST _Sequence ? ex . expressions : [ ex ] , start ) ;
2012-05-27 14:25:31 +00:00
return subscripts ( ex , allow _calls ) ;
2012-05-27 11:09:01 +00:00
case "[" :
return subscripts ( array _ ( ) , allow _calls ) ;
case "{" :
return subscripts ( object _ ( ) , allow _calls ) ;
}
unexpected ( ) ;
}
2021-01-10 03:34:26 +00:00
if ( is ( "keyword" , "function" ) ) {
2012-05-27 11:09:01 +00:00
next ( ) ;
2021-01-10 03:34:26 +00:00
var func = function _ ( AST _Function ) ;
2012-05-27 11:09:01 +00:00
func . start = start ;
func . end = prev ( ) ;
return subscripts ( func , allow _calls ) ;
}
2020-12-17 10:23:41 +00:00
if ( is ( "name" ) ) {
var sym = _make _symbol ( AST _SymbolRef , start ) ;
next ( ) ;
2021-01-10 03:34:26 +00:00
if ( sym . name == "async" ) {
if ( is ( "keyword" , "function" ) ) {
next ( ) ;
var func = function _ ( AST _AsyncFunction ) ;
func . start = start ;
func . end = prev ( ) ;
return subscripts ( func , allow _calls ) ;
}
if ( is ( "name" ) ) {
start = S . token ;
sym = _make _symbol ( AST _SymbolRef , start ) ;
next ( ) ;
return arrow ( [ sym ] , start , true ) ;
}
if ( is ( "punc" , "(" ) ) {
var call = subscripts ( sym , allow _calls ) ;
if ( ! is ( "punc" , "=>" ) ) return call ;
var args = call . args ;
if ( args [ args . length - 1 ] instanceof AST _Spread ) {
args . rest = args . pop ( ) . expression ;
}
return arrow ( args , start , true ) ;
}
}
2020-12-17 10:23:41 +00:00
return is ( "punc" , "=>" ) ? arrow ( [ sym ] , start ) : subscripts ( sym , allow _calls ) ;
}
2018-04-03 07:15:01 +00:00
if ( ATOMIC _START _TOKEN [ S . token . type ] ) {
2012-05-27 11:09:01 +00:00
return subscripts ( as _atom _node ( ) , allow _calls ) ;
}
unexpected ( ) ;
} ;
2020-11-17 00:01:24 +00:00
function expr _list ( closing , allow _trailing _comma , allow _empty , parser ) {
2020-12-17 10:23:41 +00:00
if ( ! parser ) parser = maybe _assign ;
2012-05-27 11:09:01 +00:00
var first = true , a = [ ] ;
while ( ! is ( "punc" , closing ) ) {
if ( first ) first = false ; else expect ( "," ) ;
if ( allow _trailing _comma && is ( "punc" , closing ) ) break ;
2020-12-07 02:07:34 +00:00
if ( allow _empty && is ( "punc" , "," ) ) {
2013-01-16 19:59:19 +00:00
a . push ( new AST _Hole ( { start : S . token , end : S . token } ) ) ;
2021-01-07 02:04:09 +00:00
} else if ( ! is ( "operator" , "..." ) ) {
a . push ( parser ( ) ) ;
} else if ( parser === maybe _assign ) {
2020-12-05 21:19:31 +00:00
a . push ( new AST _Spread ( {
start : S . token ,
expression : ( next ( ) , parser ( ) ) ,
end : prev ( ) ,
} ) ) ;
2012-05-27 11:09:01 +00:00
} else {
2021-01-07 02:04:09 +00:00
next ( ) ;
a . rest = parser ( ) ;
if ( a . rest instanceof AST _DefaultValue ) token _error ( a . rest . start , "Invalid rest parameter" ) ;
break ;
2012-05-27 11:09:01 +00:00
}
}
2021-01-07 02:04:09 +00:00
expect ( closing ) ;
2012-05-27 11:09:01 +00:00
return a ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2012-05-27 14:25:31 +00:00
var array _ = embed _tokens ( function ( ) {
expect ( "[" ) ;
2012-05-27 11:09:01 +00:00
return new AST _Array ( {
2012-09-21 11:19:05 +00:00
elements : expr _list ( "]" , ! options . strict , true )
2012-05-27 11:09:01 +00:00
} ) ;
2012-05-27 14:25:31 +00:00
} ) ;
2012-05-27 11:09:01 +00:00
2017-02-20 09:14:53 +00:00
var create _accessor = embed _tokens ( function ( ) {
return function _ ( AST _Accessor ) ;
} ) ;
2012-05-27 11:09:01 +00:00
var object _ = embed _tokens ( function ( ) {
2012-05-27 14:25:31 +00:00
expect ( "{" ) ;
2012-05-27 11:09:01 +00:00
var first = true , a = [ ] ;
while ( ! is ( "punc" , "}" ) ) {
if ( first ) first = false ; else expect ( "," ) ;
2020-11-08 02:44:44 +00:00
// allow trailing comma
if ( ! options . strict && is ( "punc" , "}" ) ) break ;
2012-05-27 11:09:01 +00:00
var start = S . token ;
2020-12-05 21:19:31 +00:00
if ( is ( "operator" , "..." ) ) {
next ( ) ;
a . push ( new AST _Spread ( {
start : start ,
2020-12-17 10:23:41 +00:00
expression : maybe _assign ( ) ,
2020-12-05 21:19:31 +00:00
end : prev ( ) ,
} ) ) ;
continue ;
}
2020-12-23 22:22:55 +00:00
if ( is _token ( peek ( ) , "operator" , "=" ) ) {
var name = as _symbol ( AST _SymbolRef ) ;
next ( ) ;
a . push ( new AST _ObjectKeyVal ( {
start : start ,
key : start . value ,
value : new AST _Assign ( {
start : start ,
left : name ,
operator : "=" ,
right : maybe _assign ( ) ,
end : prev ( ) ,
} ) ,
end : prev ( ) ,
} ) ) ;
continue ;
}
if ( is _token ( peek ( ) , "punc" , "," ) || is _token ( peek ( ) , "punc" , "}" ) ) {
a . push ( new AST _ObjectKeyVal ( {
start : start ,
key : start . value ,
value : as _symbol ( AST _SymbolRef ) ,
end : prev ( ) ,
} ) ) ;
continue ;
}
2020-11-08 15:38:32 +00:00
var key = as _property _key ( ) ;
2020-11-08 05:17:53 +00:00
if ( is ( "punc" , "(" ) ) {
var func _start = S . token ;
var func = function _ ( AST _Function ) ;
func . start = func _start ;
func . end = prev ( ) ;
a . push ( new AST _ObjectKeyVal ( {
start : start ,
2020-11-08 15:38:32 +00:00
key : key ,
2020-11-08 05:17:53 +00:00
value : func ,
end : prev ( ) ,
} ) ) ;
continue ;
}
2020-12-20 00:19:04 +00:00
if ( is ( "punc" , ":" ) ) {
next ( ) ;
a . push ( new AST _ObjectKeyVal ( {
start : start ,
key : key ,
value : maybe _assign ( ) ,
end : prev ( ) ,
} ) ) ;
continue ;
}
if ( start . type == "name" ) switch ( key ) {
case "async" :
key = as _property _key ( ) ;
var func _start = S . token ;
var func = function _ ( AST _AsyncFunction ) ;
func . start = func _start ;
func . end = prev ( ) ;
a . push ( new AST _ObjectKeyVal ( {
start : start ,
key : key ,
value : func ,
end : prev ( ) ,
} ) ) ;
continue ;
2020-11-08 02:44:44 +00:00
case "get" :
a . push ( new AST _ObjectGetter ( {
start : start ,
2020-11-08 15:38:32 +00:00
key : as _property _key ( ) ,
2020-11-08 02:44:44 +00:00
value : create _accessor ( ) ,
end : prev ( ) ,
} ) ) ;
continue ;
case "set" :
a . push ( new AST _ObjectSetter ( {
start : start ,
2020-11-08 15:38:32 +00:00
key : as _property _key ( ) ,
2020-11-08 02:44:44 +00:00
value : create _accessor ( ) ,
end : prev ( ) ,
} ) ) ;
continue ;
2012-05-27 11:09:01 +00:00
}
2020-12-20 00:19:04 +00:00
unexpected ( ) ;
2012-05-27 11:09:01 +00:00
}
next ( ) ;
return new AST _Object ( { properties : a } ) ;
} ) ;
2020-11-08 15:38:32 +00:00
function as _property _key ( ) {
2012-10-13 09:42:01 +00:00
var tmp = S . token ;
switch ( tmp . type ) {
2017-04-18 20:27:13 +00:00
case "operator" :
2018-04-03 07:15:01 +00:00
if ( ! KEYWORDS [ tmp . value ] ) unexpected ( ) ;
2012-05-27 11:09:01 +00:00
case "num" :
case "string" :
2012-05-27 14:25:31 +00:00
case "name" :
case "keyword" :
case "atom" :
2017-04-18 20:27:13 +00:00
next ( ) ;
2020-11-08 15:38:32 +00:00
return "" + tmp . value ;
case "punc" :
2020-12-20 12:48:51 +00:00
expect ( "[" ) ;
2020-12-17 10:23:41 +00:00
var key = maybe _assign ( ) ;
2020-11-08 15:38:32 +00:00
expect ( "]" ) ;
return key ;
2012-05-27 14:25:31 +00:00
default :
unexpected ( ) ;
2012-05-27 11:09:01 +00:00
}
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
function as _name ( ) {
2019-03-14 16:20:20 +00:00
var name = S . token . value ;
2020-12-22 18:16:04 +00:00
expect _token ( "name" ) ;
2019-03-14 16:20:20 +00:00
return name ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2020-11-08 02:44:44 +00:00
function _make _symbol ( type , token ) {
var name = token . value ;
2020-12-06 21:22:40 +00:00
if ( name === "await" && S . in _async ) unexpected ( token ) ;
2020-11-08 02:44:44 +00:00
return new ( name === "this" ? AST _This : type ) ( {
name : "" + name ,
start : token ,
end : token ,
2013-10-30 09:50:22 +00:00
} ) ;
2018-06-06 09:50:56 +00:00
}
2013-10-30 09:50:22 +00:00
2017-04-23 12:05:22 +00:00
function strict _verify _symbol ( sym ) {
if ( sym . name == "arguments" || sym . name == "eval" )
2020-12-17 10:23:41 +00:00
token _error ( sym . start , "Unexpected " + sym . name + " in strict mode" ) ;
2017-04-23 12:05:22 +00:00
}
2012-08-19 12:57:50 +00:00
function as _symbol ( type , noerror ) {
if ( ! is ( "name" ) ) {
2017-02-26 19:40:54 +00:00
if ( ! noerror ) croak ( "Name expected" ) ;
2012-08-19 12:57:50 +00:00
return null ;
}
2020-11-08 02:44:44 +00:00
var sym = _make _symbol ( type , S . token ) ;
2017-04-23 12:05:22 +00:00
if ( S . input . has _directive ( "use strict" ) && sym instanceof AST _SymbolDeclaration ) {
strict _verify _symbol ( sym ) ;
}
2012-05-27 11:09:01 +00:00
next ( ) ;
return sym ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2020-11-17 00:01:24 +00:00
function maybe _destructured ( type ) {
var start = S . token ;
if ( is ( "punc" , "[" ) ) {
next ( ) ;
2021-01-07 02:04:09 +00:00
var elements = expr _list ( "]" , ! options . strict , true , function ( ) {
return maybe _default ( type ) ;
} ) ;
2020-11-17 00:01:24 +00:00
return new AST _DestructuredArray ( {
start : start ,
2021-01-07 02:04:09 +00:00
elements : elements ,
rest : elements . rest || null ,
2020-11-17 00:01:24 +00:00
end : prev ( ) ,
} ) ;
}
if ( is ( "punc" , "{" ) ) {
next ( ) ;
2021-01-07 02:04:09 +00:00
var first = true , a = [ ] , rest = null ;
2020-11-17 00:01:24 +00:00
while ( ! is ( "punc" , "}" ) ) {
if ( first ) first = false ; else expect ( "," ) ;
// allow trailing comma
if ( ! options . strict && is ( "punc" , "}" ) ) break ;
var key _start = S . token ;
2020-12-20 12:48:51 +00:00
if ( is ( "punc" , "[" ) || is _token ( peek ( ) , "punc" , ":" ) ) {
var key = as _property _key ( ) ;
expect ( ":" ) ;
2020-11-17 00:01:24 +00:00
a . push ( new AST _DestructuredKeyVal ( {
start : key _start ,
key : key ,
2020-12-23 22:22:55 +00:00
value : maybe _default ( type ) ,
2020-11-17 00:01:24 +00:00
end : prev ( ) ,
} ) ) ;
continue ;
}
2021-01-07 02:04:09 +00:00
if ( is ( "operator" , "..." ) ) {
next ( ) ;
rest = maybe _destructured ( type ) ;
break ;
}
2020-12-23 22:22:55 +00:00
var name = as _symbol ( type ) ;
if ( is ( "operator" , "=" ) ) {
next ( ) ;
name = new AST _DefaultValue ( {
start : name . start ,
name : name ,
value : maybe _assign ( ) ,
end : prev ( ) ,
} ) ;
}
2020-11-17 00:01:24 +00:00
a . push ( new AST _DestructuredKeyVal ( {
start : key _start ,
2020-12-20 12:48:51 +00:00
key : key _start . value ,
2020-12-23 22:22:55 +00:00
value : name ,
2020-11-17 00:01:24 +00:00
end : prev ( ) ,
} ) ) ;
}
2021-01-07 02:04:09 +00:00
expect ( "}" ) ;
2020-11-17 00:01:24 +00:00
return new AST _DestructuredObject ( {
start : start ,
properties : a ,
2021-01-07 02:04:09 +00:00
rest : rest ,
2020-11-17 00:01:24 +00:00
end : prev ( ) ,
} ) ;
}
return as _symbol ( type ) ;
}
2020-12-23 22:22:55 +00:00
function maybe _default ( type ) {
var start = S . token ;
var name = maybe _destructured ( type ) ;
if ( ! is ( "operator" , "=" ) ) return name ;
next ( ) ;
return new AST _DefaultValue ( {
start : start ,
name : name ,
value : maybe _assign ( ) ,
end : prev ( ) ,
} ) ;
}
2017-12-24 04:38:45 +00:00
function mark _pure ( call ) {
var start = call . start ;
var comments = start . comments _before ;
var i = HOP ( start , "comments_before_length" ) ? start . comments _before _length : comments . length ;
while ( -- i >= 0 ) {
var comment = comments [ i ] ;
if ( /[@#]__PURE__/ . test ( comment . value ) ) {
call . pure = comment ;
break ;
}
}
}
2021-02-01 02:36:45 +00:00
function template ( tag ) {
var read = S . input . context ( ) . read _template ;
var strings = [ ] ;
var expressions = [ ] ;
while ( read ( strings ) ) {
next ( ) ;
expressions . push ( expression ( ) ) ;
if ( ! is ( "punc" , "}" ) ) unexpected ( ) ;
}
next ( ) ;
return new AST _Template ( {
expressions : expressions ,
strings : strings ,
tag : tag ,
} ) ;
}
2012-05-27 14:25:31 +00:00
var subscripts = function ( expr , allow _calls ) {
var start = expr . start ;
2012-05-27 11:09:01 +00:00
if ( is ( "punc" , "." ) ) {
next ( ) ;
return subscripts ( new AST _Dot ( {
2012-05-27 14:25:31 +00:00
start : start ,
2012-05-27 11:09:01 +00:00
expression : expr ,
2012-05-27 14:25:31 +00:00
property : as _name ( ) ,
end : prev ( )
2012-05-27 11:09:01 +00:00
} ) , allow _calls ) ;
}
if ( is ( "punc" , "[" ) ) {
next ( ) ;
2020-12-17 10:23:41 +00:00
var prop = expression ( ) ;
2012-05-27 14:25:31 +00:00
expect ( "]" ) ;
2012-05-27 11:09:01 +00:00
return subscripts ( new AST _Sub ( {
2012-05-27 14:25:31 +00:00
start : start ,
2012-05-27 11:09:01 +00:00
expression : expr ,
2012-05-27 14:25:31 +00:00
property : prop ,
end : prev ( )
2012-05-27 11:09:01 +00:00
} ) , allow _calls ) ;
}
if ( allow _calls && is ( "punc" , "(" ) ) {
next ( ) ;
2017-12-24 04:38:45 +00:00
var call = new AST _Call ( {
2012-05-27 14:25:31 +00:00
start : start ,
2012-05-27 11:09:01 +00:00
expression : expr ,
2020-12-07 02:07:34 +00:00
args : expr _list ( ")" , ! options . strict ) ,
2012-05-27 14:25:31 +00:00
end : prev ( )
2017-12-24 04:38:45 +00:00
} ) ;
mark _pure ( call ) ;
return subscripts ( call , true ) ;
2012-05-27 11:09:01 +00:00
}
2021-02-01 02:36:45 +00:00
if ( is ( "punc" , "`" ) ) {
var tmpl = template ( expr ) ;
tmpl . start = expr . start ;
tmpl . end = prev ( ) ;
return subscripts ( tmpl , allow _calls ) ;
}
2012-05-27 11:09:01 +00:00
return expr ;
2012-05-27 14:25:31 +00:00
} ;
2012-05-27 11:09:01 +00:00
2020-12-06 21:22:40 +00:00
function maybe _unary ( ) {
2012-10-13 09:42:01 +00:00
var start = S . token ;
2018-04-03 07:15:01 +00:00
if ( is ( "operator" ) && UNARY _PREFIX [ start . value ] ) {
2012-10-13 09:42:01 +00:00
next ( ) ;
2013-09-02 06:56:27 +00:00
handle _regexp ( ) ;
2020-12-06 21:22:40 +00:00
var ex = make _unary ( AST _UnaryPrefix , start , maybe _await ( ) ) ;
2012-05-27 14:25:31 +00:00
ex . start = start ;
ex . end = prev ( ) ;
return ex ;
2012-05-27 11:09:01 +00:00
}
2020-12-06 21:22:40 +00:00
var val = expr _atom ( true ) ;
2018-04-03 07:15:01 +00:00
while ( is ( "operator" ) && UNARY _POSTFIX [ S . token . value ] && ! has _newline _before ( S . token ) ) {
2017-04-18 20:27:13 +00:00
val = make _unary ( AST _UnaryPostfix , S . token , val ) ;
2012-05-27 14:25:31 +00:00
val . start = start ;
val . end = S . token ;
2012-05-27 11:09:01 +00:00
next ( ) ;
}
return val ;
2020-12-06 21:22:40 +00:00
}
2012-05-27 11:09:01 +00:00
2017-04-18 20:27:13 +00:00
function make _unary ( ctor , token , expr ) {
var op = token . value ;
2017-04-23 12:05:22 +00:00
switch ( op ) {
case "++" :
case "--" :
if ( ! is _assignable ( expr ) )
2020-12-17 10:23:41 +00:00
token _error ( token , "Invalid use of " + op + " operator" ) ;
2017-04-23 12:05:22 +00:00
break ;
case "delete" :
if ( expr instanceof AST _SymbolRef && S . input . has _directive ( "use strict" ) )
2020-12-17 10:23:41 +00:00
token _error ( expr . start , "Calling delete on expression not allowed in strict mode" ) ;
2017-04-23 12:05:22 +00:00
break ;
}
2012-05-27 11:09:01 +00:00
return new ctor ( { operator : op , expression : expr } ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2020-12-06 21:22:40 +00:00
function maybe _await ( ) {
var start = S . token ;
if ( ! ( S . in _async && is ( "name" , "await" ) ) ) return maybe _unary ( ) ;
2020-12-08 04:59:08 +00:00
if ( S . in _funarg === S . in _function ) croak ( "Invalid use of await in function argument" ) ;
2020-12-08 03:26:03 +00:00
S . input . context ( ) . regex _allowed = true ;
2020-12-06 21:22:40 +00:00
next ( ) ;
return new AST _Await ( {
start : start ,
expression : maybe _await ( ) ,
end : prev ( ) ,
} ) ;
}
2012-05-27 14:25:31 +00:00
var expr _op = function ( left , min _prec , no _in ) {
2012-05-27 11:09:01 +00:00
var op = is ( "operator" ) ? S . token . value : null ;
if ( op == "in" && no _in ) op = null ;
var prec = op != null ? PRECEDENCE [ op ] : null ;
if ( prec != null && prec > min _prec ) {
next ( ) ;
2021-01-24 21:48:51 +00:00
var right = expr _op ( maybe _await ( ) , op == "**" ? prec - 1 : prec , no _in ) ;
2012-05-27 11:09:01 +00:00
return expr _op ( new AST _Binary ( {
2012-05-27 14:25:31 +00:00
start : left . start ,
2012-05-27 11:09:01 +00:00
left : left ,
operator : op ,
2012-05-27 14:25:31 +00:00
right : right ,
end : right . end
2012-05-27 11:09:01 +00:00
} ) , min _prec , no _in ) ;
}
return left ;
2012-05-27 14:25:31 +00:00
} ;
2012-05-27 11:09:01 +00:00
function expr _ops ( no _in ) {
2020-12-06 21:22:40 +00:00
return expr _op ( maybe _await ( ) , 0 , no _in ) ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2012-05-27 14:25:31 +00:00
var maybe _conditional = function ( no _in ) {
var start = S . token ;
2012-05-27 11:09:01 +00:00
var expr = expr _ops ( no _in ) ;
if ( is ( "operator" , "?" ) ) {
next ( ) ;
2020-12-17 10:23:41 +00:00
var yes = maybe _assign ( ) ;
2012-05-27 11:09:01 +00:00
expect ( ":" ) ;
return new AST _Conditional ( {
2012-05-27 14:25:31 +00:00
start : start ,
condition : expr ,
consequent : yes ,
2020-12-17 10:23:41 +00:00
alternative : maybe _assign ( no _in ) ,
2014-01-21 08:38:59 +00:00
end : prev ( )
2012-05-27 11:09:01 +00:00
} ) ;
}
return expr ;
2012-05-27 14:25:31 +00:00
} ;
2012-05-27 11:09:01 +00:00
function is _assignable ( expr ) {
2017-03-21 06:11:32 +00:00
return expr instanceof AST _PropAccess || expr instanceof AST _SymbolRef ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2020-11-17 00:01:24 +00:00
function to _destructured ( node ) {
if ( node instanceof AST _Array ) {
2021-01-07 02:04:09 +00:00
var rest = null ;
if ( node . elements [ node . elements . length - 1 ] instanceof AST _Spread ) {
rest = to _destructured ( node . elements . pop ( ) . expression ) ;
if ( ! ( rest instanceof AST _Destructured || is _assignable ( rest ) ) ) return node ;
}
2020-11-17 00:01:24 +00:00
var elements = node . elements . map ( to _destructured ) ;
return all ( elements , function ( node ) {
2020-12-23 22:22:55 +00:00
return node instanceof AST _DefaultValue
|| node instanceof AST _Destructured
|| node instanceof AST _Hole
|| is _assignable ( node ) ;
2020-11-17 00:01:24 +00:00
} ) ? new AST _DestructuredArray ( {
start : node . start ,
elements : elements ,
2021-01-07 02:04:09 +00:00
rest : rest ,
2020-11-17 00:01:24 +00:00
end : node . end ,
} ) : node ;
}
2020-12-23 22:22:55 +00:00
if ( node instanceof AST _Assign ) {
var name = to _destructured ( node . left ) ;
return name instanceof AST _Destructured || is _assignable ( name ) ? new AST _DefaultValue ( {
start : node . start ,
name : name ,
value : node . right ,
end : node . end ,
} ) : node ;
}
2020-11-17 00:01:24 +00:00
if ( ! ( node instanceof AST _Object ) ) return node ;
2021-01-07 02:04:09 +00:00
var rest = null ;
if ( node . properties [ node . properties . length - 1 ] instanceof AST _Spread ) {
rest = to _destructured ( node . properties . pop ( ) . expression ) ;
if ( ! ( rest instanceof AST _Destructured || is _assignable ( rest ) ) ) return node ;
}
2020-11-17 00:01:24 +00:00
var props = [ ] ;
for ( var i = 0 ; i < node . properties . length ; i ++ ) {
var prop = node . properties [ i ] ;
if ( ! ( prop instanceof AST _ObjectKeyVal ) ) return node ;
var value = to _destructured ( prop . value ) ;
2020-12-23 22:22:55 +00:00
if ( ! ( value instanceof AST _DefaultValue || value instanceof AST _Destructured || is _assignable ( value ) ) ) {
return node ;
}
2020-11-17 00:01:24 +00:00
props . push ( new AST _DestructuredKeyVal ( {
start : prop . start ,
key : prop . key ,
value : value ,
end : prop . end ,
} ) ) ;
}
return new AST _DestructuredObject ( {
start : node . start ,
properties : props ,
2021-01-07 02:04:09 +00:00
rest : rest ,
2020-11-17 00:01:24 +00:00
end : node . end ,
} ) ;
}
2020-12-17 10:23:41 +00:00
function maybe _assign ( no _in ) {
2012-05-27 14:25:31 +00:00
var start = S . token ;
2012-05-27 11:09:01 +00:00
var left = maybe _conditional ( no _in ) , val = S . token . value ;
2018-04-03 07:15:01 +00:00
if ( is ( "operator" ) && ASSIGNMENT [ val ] ) {
2020-11-17 00:01:24 +00:00
if ( is _assignable ( left ) || val == "=" && ( left = to _destructured ( left ) ) instanceof AST _Destructured ) {
2012-05-27 11:09:01 +00:00
next ( ) ;
return new AST _Assign ( {
2012-05-27 14:25:31 +00:00
start : start ,
2012-05-27 11:09:01 +00:00
left : left ,
2012-10-11 10:00:58 +00:00
operator : val ,
2012-05-27 14:25:31 +00:00
right : maybe _assign ( no _in ) ,
2013-01-26 12:24:54 +00:00
end : prev ( )
2012-05-27 11:09:01 +00:00
} ) ;
}
2017-02-26 19:40:54 +00:00
croak ( "Invalid assignment" ) ;
2012-05-27 11:09:01 +00:00
}
return left ;
2020-12-17 10:23:41 +00:00
}
2012-05-27 11:09:01 +00:00
2020-12-17 10:23:41 +00:00
function expression ( no _in , maybe _arrow ) {
2012-05-27 14:25:31 +00:00
var start = S . token ;
2017-04-12 13:56:27 +00:00
var exprs = [ ] ;
while ( true ) {
2021-01-07 02:04:09 +00:00
if ( maybe _arrow && is ( "operator" , "..." ) ) {
next ( ) ;
exprs . rest = maybe _destructured ( AST _SymbolFunarg ) ;
break ;
}
2017-04-12 13:56:27 +00:00
exprs . push ( maybe _assign ( no _in ) ) ;
2020-12-17 10:23:41 +00:00
if ( ! is ( "punc" , "," ) ) break ;
2012-05-27 11:09:01 +00:00
next ( ) ;
2020-12-17 10:23:41 +00:00
if ( maybe _arrow && is ( "punc" , ")" ) && is _token ( peek ( ) , "punc" , "=>" ) ) break ;
2012-05-27 11:09:01 +00:00
}
2021-01-07 02:04:09 +00:00
return exprs . length == 1 && ! exprs . rest ? exprs [ 0 ] : new AST _Sequence ( {
start : start ,
expressions : exprs ,
end : prev ( ) ,
2017-04-12 13:56:27 +00:00
} ) ;
2020-12-17 10:23:41 +00:00
}
2012-05-27 11:09:01 +00:00
function in _loop ( cont ) {
++ S . in _loop ;
var ret = cont ( ) ;
-- S . in _loop ;
return ret ;
2018-06-06 09:50:56 +00:00
}
2012-05-27 11:09:01 +00:00
2013-05-15 10:27:23 +00:00
if ( options . expression ) {
2019-03-14 16:20:20 +00:00
handle _regexp ( ) ;
2020-12-22 18:16:04 +00:00
var exp = expression ( ) ;
expect _token ( "eof" ) ;
return exp ;
2013-05-15 10:27:23 +00:00
}
2018-06-06 09:50:56 +00:00
return function ( ) {
2012-09-21 11:19:05 +00:00
var start = S . token ;
var body = [ ] ;
2016-05-20 08:25:35 +00:00
S . input . push _directives _stack ( ) ;
2012-09-21 11:19:05 +00:00
while ( ! is ( "eof" ) )
2020-11-07 02:00:04 +00:00
body . push ( statement ( ) ) ;
2016-05-20 08:25:35 +00:00
S . input . pop _directives _stack ( ) ;
2012-09-21 11:19:05 +00:00
var end = prev ( ) ;
var toplevel = options . toplevel ;
if ( toplevel ) {
toplevel . body = toplevel . body . concat ( body ) ;
toplevel . end = end ;
} else {
toplevel = new AST _Toplevel ( { start : start , body : body , end : end } ) ;
}
return toplevel ;
2018-06-06 09:50:56 +00:00
} ( ) ;
}