@@ -2,15 +2,14 @@ import {Injectable} from '@angular/core';
22
33import { ListWrapper } from '../facade/collection' ;
44import { BaseException } from '../facade/exceptions' ;
5- import { StringWrapper , isBlank , isPresent } from '../facade/lang' ;
5+ import { RegExpWrapper , StringWrapper , escapeRegExp , isBlank , isPresent } from '../facade/lang' ;
6+ import { DEFAULT_INTERPOLATION_CONFIG , InterpolationConfig } from '../interpolation_config' ;
67
78import { AST , ASTWithSource , AstVisitor , Binary , BindingPipe , Chain , Conditional , EmptyExpr , FunctionCall , ImplicitReceiver , Interpolation , KeyedRead , KeyedWrite , LiteralArray , LiteralMap , LiteralPrimitive , MethodCall , PrefixNot , PropertyRead , PropertyWrite , Quote , SafeMethodCall , SafePropertyRead , TemplateBinding } from './ast' ;
89import { $COLON , $COMMA , $LBRACE , $LBRACKET , $LPAREN , $PERIOD , $RBRACE , $RBRACKET , $RPAREN , $SEMICOLON , $SLASH , EOF , Lexer , Token , isIdentifier , isQuote } from './lexer' ;
910
1011
1112var _implicitReceiver = new ImplicitReceiver ( ) ;
12- // TODO(tbosch): Cannot make this const/final right now because of the transpiler...
13- var INTERPOLATION_REGEXP = / \{ \{ ( [ \s \S ] * ?) \} \} / g;
1413
1514class ParseException extends BaseException {
1615 constructor ( message : string , input : string , errLocation : string , ctxLocation ?: any ) {
@@ -26,24 +25,40 @@ export class TemplateBindingParseResult {
2625 constructor ( public templateBindings : TemplateBinding [ ] , public warnings : string [ ] ) { }
2726}
2827
28+ function _createInterpolateRegExp ( config : InterpolationConfig ) : RegExp {
29+ const regexp = escapeRegExp ( config . start ) + '([\\s\\S]*?)' + escapeRegExp ( config . end ) ;
30+ return RegExpWrapper . create ( regexp , 'g' ) ;
31+ }
32+
2933@Injectable ( )
3034export class Parser {
35+ private _interpolationConfig : InterpolationConfig ;
36+
3137 constructor ( /** @internal */
3238 public _lexer : Lexer ) { }
3339
34- parseAction ( input : string , location : any ) : ASTWithSource {
40+ parseAction (
41+ input : string , location : any ,
42+ interpolationConfig : InterpolationConfig = DEFAULT_INTERPOLATION_CONFIG ) : ASTWithSource {
43+ this . _interpolationConfig = interpolationConfig ;
3544 this . _checkNoInterpolation ( input , location ) ;
3645 var tokens = this . _lexer . tokenize ( this . _stripComments ( input ) ) ;
3746 var ast = new _ParseAST ( input , location , tokens , true ) . parseChain ( ) ;
3847 return new ASTWithSource ( ast , input , location ) ;
3948 }
4049
41- parseBinding ( input : string , location : any ) : ASTWithSource {
50+ parseBinding (
51+ input : string , location : any ,
52+ interpolationConfig : InterpolationConfig = DEFAULT_INTERPOLATION_CONFIG ) : ASTWithSource {
53+ this . _interpolationConfig = interpolationConfig ;
4254 var ast = this . _parseBindingAst ( input , location ) ;
4355 return new ASTWithSource ( ast , input , location ) ;
4456 }
4557
46- parseSimpleBinding ( input : string , location : string ) : ASTWithSource {
58+ parseSimpleBinding (
59+ input : string , location : string ,
60+ interpolationConfig : InterpolationConfig = DEFAULT_INTERPOLATION_CONFIG ) : ASTWithSource {
61+ this . _interpolationConfig = interpolationConfig ;
4762 var ast = this . _parseBindingAst ( input , location ) ;
4863 if ( ! SimpleExpressionChecker . check ( ast ) ) {
4964 throw new ParseException (
@@ -81,8 +96,11 @@ export class Parser {
8196 return new _ParseAST ( input , location , tokens , false ) . parseTemplateBindings ( ) ;
8297 }
8398
84- parseInterpolation ( input : string , location : any ) : ASTWithSource {
85- let split = this . splitInterpolation ( input , location ) ;
99+ parseInterpolation (
100+ input : string , location : any ,
101+ interpolationConfig : InterpolationConfig = DEFAULT_INTERPOLATION_CONFIG ) : ASTWithSource {
102+ this . _interpolationConfig = interpolationConfig ;
103+ let split = this . splitInterpolation ( input , location , this . _interpolationConfig ) ;
86104 if ( split == null ) return null ;
87105
88106 let expressions : AST [ ] = [ ] ;
@@ -96,8 +114,12 @@ export class Parser {
96114 return new ASTWithSource ( new Interpolation ( split . strings , expressions ) , input , location ) ;
97115 }
98116
99- splitInterpolation ( input : string , location : string ) : SplitInterpolation {
100- var parts = StringWrapper . split ( input , INTERPOLATION_REGEXP ) ;
117+ splitInterpolation (
118+ input : string , location : string ,
119+ interpolationConfig : InterpolationConfig = DEFAULT_INTERPOLATION_CONFIG ) : SplitInterpolation {
120+ this . _interpolationConfig = interpolationConfig ;
121+ const regexp = _createInterpolateRegExp ( this . _interpolationConfig ) ;
122+ const parts = StringWrapper . split ( input , regexp ) ;
101123 if ( parts . length <= 1 ) {
102124 return null ;
103125 }
@@ -147,18 +169,21 @@ export class Parser {
147169 }
148170
149171 private _checkNoInterpolation ( input : string , location : any ) : void {
150- var parts = StringWrapper . split ( input , INTERPOLATION_REGEXP ) ;
172+ var regexp = _createInterpolateRegExp ( this . _interpolationConfig ) ;
173+ var parts = StringWrapper . split ( input , regexp ) ;
151174 if ( parts . length > 1 ) {
152175 throw new ParseException (
153- ' Got interpolation ({{}} ) where expression was expected' , input ,
154- `at column ${ this . _findInterpolationErrorColumn ( parts , 1 ) } in` , location ) ;
176+ ` Got interpolation (${ this . _interpolationConfig . start } ${ this . _interpolationConfig . end } ) where expression was expected` ,
177+ input , `at column ${ this . _findInterpolationErrorColumn ( parts , 1 ) } in` , location ) ;
155178 }
156179 }
157180
158181 private _findInterpolationErrorColumn ( parts : string [ ] , partInErrIdx : number ) : number {
159182 var errLocation = '' ;
160183 for ( var j = 0 ; j < partInErrIdx ; j ++ ) {
161- errLocation += j % 2 === 0 ? parts [ j ] : `{{${ parts [ j ] } }}` ;
184+ errLocation += j % 2 === 0 ?
185+ parts [ j ] :
186+ `${ this . _interpolationConfig . start } ${ parts [ j ] } ${ this . _interpolationConfig . end } ` ;
162187 }
163188
164189 return errLocation . length ;
0 commit comments