o 3a@sNdZddlZGdddZdddZGdd d ZGd d d eZd d ZdS)zJsLex: a lexer for JavascriptNc@seZdZdZdZdddZdS)Tokz, A specification for a token class. rNcCs,tj|_tjd7_||_||_||_dS)N)rnumidnameregexnext)selfrrrr 4/usr/lib/python3/dist-packages/django/utils/jslex.py__init__ s  z Tok.__init__N)__name__ __module__ __qualname____doc__rr r r r r rsrcsdfdd|DS)z Create a regex from a space-separated list of literal `choices`. If provided, `prefix` and `suffix` will be attached to each choice individually. |c3s"|] }t|VqdSr )reescape).0cprefixsuffixr r s zliterals..)joinsplit)choicesrrr rr literalssrc@s eZdZdZddZddZdS)Lexerz2 A generic multi-state regex-based lexer. cCs~i|_i|_|D]/\}}g}|D]}d|j}||j|<|d||jfqtd|tj tj B|j|<q ||_ dS)Nzt%dz (?P<%s>%s)r) regexestoksitemsrappendrrcompiler MULTILINEVERBOSEstate)r statesfirstr(rulespartstokgroupidr r r r #s  " zLexer.__init__c cst|}|j}|j}|j}d}||krB||||D]!}|j}||} ||} |t| 7}| j| fV| jr=| j}nq||ks||_dS)zW Lexically analyze `text`. Yield pairs (`name`, `tokentext`). rN)lenr(r!r"finditer lastgrouprr) r textendr(r!r"startmatchrr-toktextr r r lex1s&   z Lexer.lexN)rrrrr r7r r r r r s r cseZdZdZeddeddeddeded d d d d ededd d dd edddd edddd eddedddd ededd d ededdd ededd d edddd edddd gZed d!gZeeded"d d geeed#d$dd ged%Zfd&d'Z Z S)(JsLexerz A Javascript lexer >>> lexer = JsLexer() >>> list(lexer.lex("a = 1")) [('id', 'a'), ('ws', ' '), ('punct', '='), ('ws', ' '), ('dnum', '1')] This doesn't properly handle non-ASCII characters in the Javascript source. commentz/\*(.|\n)*?\*/ linecommentz//.*?$wsz\s+keywordal break case catch class const continue debugger default delete do else enum export extends finally for function if import in instanceof new return super switch this throw try typeof var void while with z\b)rreg)rreservedznull true falsedivrz ([a-zA-Z_$ ]|\\u[0-9a-fA-Z]{4}) # first char ([a-zA-Z_$0-9]|\\u[0-9a-fA-F]{4})* # rest chars hnumz0[xX][0-9a-fA-F]+onumz0[0-7]+dnuma| ( (0|[1-9][0-9]*) # DecimalIntegerLiteral \. # dot [0-9]* # DecimalDigits-opt ([eE][-+]?[0-9]+)? # ExponentPart-opt | \. # dot [0-9]+ # DecimalDigits ([eE][-+]?[0-9]+)? # ExponentPart-opt | (0|[1-9][0-9]*) # DecimalIntegerLiteral ([eE][-+]?[0-9]+)? # ExponentPart-opt ) punctz >>>= === !== >>> <<= >>= <= >= == != << >> && || += -= *= %= &= |= ^= z ++ -- ) ]z){ } ( [ . ; , < > + - * % & | ^ ! ~ ? : =stringz"([^"\\]|(\\(.|\n)))*?"z'([^'\\]|(\\(.|\n)))*?'other.z/= /ra / # opening slash # First character is.. ( [^*\\/[] # anything but * \ / or [ | \\. # or an escape sequence | \[ # or a class, which has ( [^\]\\] # anything but \ or ] | \\. # or an escape sequence )* # many times \] ) # Following characters are same, except for excluding a star ( [^\\/[] # anything but \ / or [ | \\. # or an escape sequence | \[ # or a class, which has ( [^\]\\] # anything but \ or ] | \\. # or an escape sequence )* # many times \] )* # many times / # closing slash [a-zA-Z0-9]* # trailing flags )r?r=cst|jddS)Nr=)superr r))r  __class__r r r szJsLexer.__init__) rrrrrr both_before both_afterr)r __classcell__r r rHr r8LsX     +#r8cCsdd}t}g}||D]5\}}|dkrd}n%|dkr4|dr3td||dd }d |d }n |d kr>|d d }||qd|S)z Convert the Javascript source `js` into something resembling C for xgettext. What actually happens is that all the regex literals are replaced with "REGEX". cSs|d}|dkr dS|S)z1Used in a regex to properly escape double quotes.r"z\"r )msr r r escape_quotessz-prepare_js_for_gettext..escape_quotesrz"REGEX"rD'z\\.|.rrMr\Ur)r8r7 startswithrsubreplacer$r)jsrPlexerrrr-gutsr r r prepare_js_for_gettexts     r[)rr)rrrrr r8r[r r r r s  . m