import { ParsedMessagePart , ParsedMessagePartType } from './parsed-message-part' ; import { IICUMessage , INormalizedMessage } from '../api/index' ; import { COMMA , CURLY_BRACE_CLOSE , CURLY_BRACE_OPEN , ICUMessageTokenizer , ICUToken , PLURAL , SELECT , TEXT } from './icu-message-tokenizer' ; import { ICUMessage } from './icu-message' ; import { format } from 'util' ; import { IMessageParser } from './i-message-parser' ; /**
 * Created by martin on 02.06.2017.
 * A message part consisting of an icu message.
 * There can only be one icu message in a parsed message.
 * Syntax of ICU message is '{' <keyname> ',' 'select'|'plural' ',' (<category> '{' text '}')+ '}'
 */ export class ParsedMessagePartICUMessage extends ParsedMessagePart { private _message : ICUMessage ; private _messageText : string ; private _tokenizer : ICUMessageTokenizer ; constructor ( icuMessageText , private _parser ) { super ( ParsedMessagePartType . ICU_MESSAGE ) ; if ( icuMessageText ) { this . parseICUMessage ( icuMessageText ) ; } } param icuMessageText return static looksLikeICUMessage ( icuMessageText ) { const part = new ParsedMessagePartICUMessage ( null , null ) ; return part . looksLikeICUMessage ( icuMessageText ) ; } public asDisplayString ( displayFormat ? ) { return '<ICU-Message/>' ; } return public getICUMessage ( ) { return this . _message ; } param text throws private parseICUMessage ( text ) { this . _messageText = text ; this . _tokenizer = new ICUMessageTokenizer ( ) ; this . _tokenizer . input ( text ) ; this . expectNext ( CURLY_BRACE_OPEN ) ; this . expectNext ( TEXT ) ; this . expectNext ( COMMA ) ; let token = this . _tokenizer . next ( ) ; if ( token . type === PLURAL ) { this . _message = new ICUMessage ( this . _parser , true ) ; } else if ( token . type === SELECT ) { this . _message = new ICUMessage ( this . _parser , false ) ; } this . expectNext ( COMMA ) ; token = this . _tokenizer . peek ( ) ; while ( token . type !== CURLY_BRACE_CLOSE ) { const category = this . expectNext ( TEXT ) . value . trim ( ) ; this . expectNext ( CURLY_BRACE_OPEN ) ; const message = this . expectNext ( TEXT ) . value ; this . _message . addCategory ( category , this . parseNativeSubMessage ( message ) ) ; this . expectNext ( CURLY_BRACE_CLOSE ) ; token = this . _tokenizer . peek ( ) ; } this . expectNext ( CURLY_BRACE_CLOSE ) ; this . expectNext ( 'EOF' ) ; } param text private looksLikeICUMessage ( text ) { this . _tokenizer = new ICUMessageTokenizer ( ) ; this . _tokenizer . input ( text ) ; try { this . expectNext ( CURLY_BRACE_OPEN ) ; this . expectNext ( TEXT ) ; this . expectNext ( COMMA ) ; const token = this . _tokenizer . next ( ) ; if ( token . type !== PLURAL && token . type !== SELECT ) { return false ; } this . expectNext ( COMMA ) ; return true ; } catch ( error ) { return false ; } } param tokentype return throws private expectNext ( tokentype ) { const token = this . _tokenizer . next ( ) ; if ( token . type !== tokentype ) { throw new Error ( format ( 'Error parsing ICU Message: expected %s, found %s (%s) (message %s)' , tokentype , token . type , token . value , this . _messageText ) ) ; } return token ; } param message return private parseNativeSubMessage ( message ) { return this . _parser . createNormalizedMessageFromXMLString ( message , null ) ; } }