import Char from 'typescript-char' ; import * as unicode from './unicode' ; enum CharCategory { NotIdentifierChar = 0 , StartIdentifierChar = 1 , IdentifierChar = 2 , SurrogateChar = 3 } const _identifierCharFastTableSize = 256 ; const _identifierCharFastTable = new Array ( _identifierCharFastTableSize ) ; type CharCategoryMap = { [ code ] : CharCategory ; } ; const _identifierCharMap = { } ; const _surrogateCharMap = { } ; let _identifierCharMapInitialized = false ; export function isIdentifierStartChar ( char , nextChar ? ) { if ( char < _identifierCharFastTableSize ) { return _identifierCharFastTable [ char ] === CharCategory . StartIdentifierChar ; } if ( ! _identifierCharMapInitialized ) { _buildIdentifierLookupTable ( false ) ; _identifierCharMapInitialized = true ; } let charCategory ; if ( nextChar !== undefined ) { charCategory = _lookUpSurrogate ( char , nextChar ) ; } else { charCategory = _identifierCharMap [ char ] ; } return charCategory === CharCategory . StartIdentifierChar ; } export function isIdentifierChar ( char , nextChar ? ) { if ( char < _identifierCharFastTableSize ) { return ( _identifierCharFastTable [ char ] === CharCategory . StartIdentifierChar || _identifierCharFastTable [ char ] === CharCategory . IdentifierChar ) ; } if ( ! _identifierCharMapInitialized ) { _buildIdentifierLookupTable ( false ) ; _identifierCharMapInitialized = true ; } if ( nextChar !== undefined ) { return _lookUpSurrogate ( char , nextChar ) ; } return ( _identifierCharMap [ char ] === CharCategory . StartIdentifierChar || _identifierCharMap [ char ] === CharCategory . IdentifierChar ) ; } export function isSurrogateChar ( char ) { if ( char < _identifierCharFastTableSize ) { return false ; } if ( ! _identifierCharMapInitialized ) { _buildIdentifierLookupTable ( false ) ; _identifierCharMapInitialized = true ; } return _identifierCharMap [ char ] === CharCategory . SurrogateChar ; } export function isWhiteSpace ( ch ) { return ch === Char . Space || ch === Char . Tab || ch === Char . FormFeed ; } export function isLineBreak ( ch ) { return ch === Char . CarriageReturn || ch === Char . LineFeed ; } export function isNumber ( ch ) { return ( ch >= Char . _0 && ch <= Char . _9 ) || ch === Char . Underscore ; } export function isDecimal ( ch ) { return ( ch >= Char . _0 && ch <= Char . _9 ) || ch === Char . Underscore ; } export function isHex ( ch ) { return isDecimal ( ch ) || ( ch >= Char . a && ch <= Char . f ) || ( ch >= Char . A && ch <= Char . F ) || ch === Char . Underscore ; } export function isOctal ( ch ) { return ( ch >= Char . _0 && ch <= Char . _7 ) || ch === Char . Underscore ; } export function isBinary ( ch ) { return ch === Char . _0 || ch === Char . _1 || ch === Char . Underscore ; } function _lookUpSurrogate ( char , nextChar ) { if ( _identifierCharMap [ char ] !== CharCategory . SurrogateChar ) { return CharCategory . NotIdentifierChar ; } const surrogateTable = _surrogateCharMap [ char ] ; if ( ! surrogateTable ) { return CharCategory . NotIdentifierChar ; } return surrogateTable [ nextChar ] ; } const _specialStartIdentifierChars = [ Char . Underscore , 0x1885 , 0x1886 , 0x2118 , 0x212e , 0x309b , 0x309c , ] ; const _startIdentifierCharRanges = [ _specialStartIdentifierChars , unicode . unicodeLu , unicode . unicodeLl , unicode . unicodeLt , unicode . unicodeLo , unicode . unicodeLm , unicode . unicodeNl , ] ; const _startCharSurrogateRanges = [ unicode . unicodeLuSurrogate , unicode . unicodeLlSurrogate , unicode . unicodeLoSurrogate , unicode . unicodeLmSurrogate , unicode . unicodeNlSurrogate , ] ; const _specialIdentifierChars = [ 0x00b7 , 0x0387 , 0x1369 , 0x136a , 0x136b , 0x136c , 0x136d , 0x136e , 0x136f , 0x1370 , 0x1371 , 0x19da , ] ; const _identifierCharRanges = [ _specialIdentifierChars , unicode . unicodeMn , unicode . unicodeMc , unicode . unicodeNd , unicode . unicodePc , ] ; const _identifierCharSurrogateRanges = [ unicode . unicodeMnSurrogate , unicode . unicodeMcSurrogate , unicode . unicodeNdSurrogate , ] ; function _buildIdentifierLookupTableFromUnicodeRangeTable ( table , category , fastTableOnly , fastTable , fullTable ) { for ( let entryIndex = 0 ; entryIndex < table . length ; entryIndex ++ ) { const entry = table [ entryIndex ] ; let rangeStart ; let rangeEnd ; if ( Array . isArray ( entry ) ) { rangeStart = entry [ 0 ] ; rangeEnd = entry [ 1 ] ; } else { rangeStart = rangeEnd = entry ; } for ( let i = rangeStart ; i <= rangeEnd ; i ++ ) { if ( i < _identifierCharFastTableSize ) { fastTable [ i ] = category ; } else { fullTable [ i ] = category ; } } if ( fastTableOnly && rangeStart >= _identifierCharFastTableSize ) { break ; } } } function _buildIdentifierLookupTableFromSurrogateRangeTable ( surrogateTable , category ) { for ( const surrogateChar in surrogateTable ) { if ( ! _surrogateCharMap [ surrogateChar ] ) { _surrogateCharMap [ surrogateChar ] = { } ; _identifierCharMap [ surrogateChar ] = CharCategory . SurrogateChar ; } _buildIdentifierLookupTableFromUnicodeRangeTable ( surrogateTable [ surrogateChar ] , category , false , _surrogateCharMap [ surrogateChar ] , _surrogateCharMap [ surrogateChar ] ) ; } } function _buildIdentifierLookupTable ( fastTableOnly ) { _identifierCharFastTable . fill ( CharCategory . NotIdentifierChar ) ; _identifierCharRanges . forEach ( ( table ) => { _buildIdentifierLookupTableFromUnicodeRangeTable ( table , CharCategory . IdentifierChar , fastTableOnly , _identifierCharFastTable , _identifierCharMap ) ; } ) ; _startIdentifierCharRanges . forEach ( ( table ) => { _buildIdentifierLookupTableFromUnicodeRangeTable ( table , CharCategory . StartIdentifierChar , fastTableOnly , _identifierCharFastTable , _identifierCharMap ) ; } ) ; if ( ! fastTableOnly ) { for ( const surrogateTable of _identifierCharSurrogateRanges ) { _buildIdentifierLookupTableFromSurrogateRangeTable ( surrogateTable , CharCategory . IdentifierChar ) ; } for ( const surrogateTable of _startCharSurrogateRanges ) { _buildIdentifierLookupTableFromSurrogateRangeTable ( surrogateTable , CharCategory . StartIdentifierChar ) ; } } } _buildIdentifierLookupTable ( true ) ;