feat(I18nAst): introduce an intermediate AST

This commit is contained in:
Victor Berchet
2016-07-08 16:46:49 -07:00
parent 2be50bdbb0
commit 48f230a951
12 changed files with 477 additions and 54 deletions

View File

@ -128,10 +128,10 @@ export class Parser {
if (parts.length <= 1) {
return null;
}
var strings: string[] = [];
var expressions: string[] = [];
const strings: string[] = [];
const expressions: string[] = [];
for (var i = 0; i < parts.length; i++) {
for (let i = 0; i < parts.length; i++) {
var part: string = parts[i];
if (i % 2 === 0) {
// fixed string

View File

@ -125,7 +125,7 @@ class TreeBuilder {
// read the final }
if (this.peek.type !== HtmlTokenType.EXPANSION_FORM_END) {
this.errors.push(
HtmlTreeError.create(null, this.peek.sourceSpan, `Invalid expansion form. Missing '}'.`));
HtmlTreeError.create(null, this.peek.sourceSpan, `Invalid ICU message. Missing '}'.`));
return;
}
this._advance();
@ -141,7 +141,7 @@ class TreeBuilder {
// read {
if (this.peek.type !== HtmlTokenType.EXPANSION_CASE_EXP_START) {
this.errors.push(HtmlTreeError.create(
null, this.peek.sourceSpan, `Invalid expansion form. Missing '{'.,`));
null, this.peek.sourceSpan, `Invalid ICU message. Missing '{'.`));
return null;
}
@ -184,7 +184,7 @@ class TreeBuilder {
} else {
this.errors.push(
HtmlTreeError.create(null, start.sourceSpan, `Invalid expansion form. Missing '}'.`));
HtmlTreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
return null;
}
}
@ -194,14 +194,14 @@ class TreeBuilder {
expansionFormStack.pop();
} else {
this.errors.push(
HtmlTreeError.create(null, start.sourceSpan, `Invalid expansion form. Missing '}'.`));
HtmlTreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
return null;
}
}
if (this.peek.type === HtmlTokenType.EOF) {
this.errors.push(
HtmlTreeError.create(null, start.sourceSpan, `Invalid expansion form. Missing '}'.`));
HtmlTreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
return null;
}

View File

@ -243,7 +243,7 @@ class _ExtractVisitor implements HtmlAstVisitor {
if (significantChildren == 1) {
for (let i = startIndex; i < messages.length; i++) {
let ast = messages[i].ast;
let ast = messages[i].nodes;
if (!(ast.length == 1 && ast[0] instanceof HtmlAttrAst)) {
messages.splice(i, 1);
break;
@ -260,5 +260,5 @@ class _ExtractVisitor implements HtmlAstVisitor {
}
export class AstMessage {
constructor(public ast: HtmlAst[], public meaning: string, public description: string) {}
constructor(public nodes: HtmlAst[], public meaning: string, public description: string) {}
}

View File

@ -0,0 +1,74 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {ParseSourceSpan} from "../parse_util";
export interface I18nNode {
visit(visitor: Visitor, context?: any): any;
}
export class Text implements I18nNode {
constructor(public value: string, public sourceSpan: ParseSourceSpan) {}
visit(visitor: Visitor, context?: any): any {
return visitor.visitText(this, context);
}
}
export class Container implements I18nNode {
constructor(public children: I18nNode[], public sourceSpan: ParseSourceSpan) {}
visit(visitor: Visitor, context?: any): any {
return visitor.visitContainer(this, context);
}
}
export class Icu implements I18nNode {
constructor(public expression: string, public type: string, public cases: {[k: string]: I18nNode}, public sourceSpan: ParseSourceSpan) {}
visit(visitor: Visitor, context?: any): any {
return visitor.visitIcu(this, context);
}
}
export class TagPlaceholder {
constructor(public name: string, public attrs: {[k: string]: string}, public children: I18nNode[], public sourceSpan: ParseSourceSpan) {}
visit(visitor: Visitor, context?: any): any {
return visitor.visitTagPlaceholder(this, context);
}
}
export class Placeholder {
constructor(public value: string, public name: string = '', public sourceSpan: ParseSourceSpan) {}
visit(visitor: Visitor, context?: any): any {
return visitor.visitPlaceholder(this, context);
}
}
export class IcuPlaceholder {
constructor(public value: Icu, public name: string = '', public sourceSpan: ParseSourceSpan) {}
visit(visitor: Visitor, context?: any): any {
return visitor.visitIcuPlaceholder(this, context);
}
}
export interface Visitor {
visitText(text: Text, context?: any): any;
visitContainer(container: Container, context?: any): any;
visitIcu(icu: Icu, context?: any): any;
visitTagPlaceholder(ph: TagPlaceholder, context?: any): any;
visitPlaceholder(ph: Placeholder, context?: any): any;
visitIcuPlaceholder(ph: IcuPlaceholder, context?: any): any;
}

View File

@ -0,0 +1,123 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { extractAstMessages} from './extractor';
import * as hAst from '../html_ast';
import * as i18nAst from './i18n_ast';
import {Parser as ExpressionParser} from '../expression_parser/parser';
import {Lexer as ExpressionLexer} from '../expression_parser/lexer';
import {ParseSourceSpan} from "../parse_util";
import {HtmlAst} from "../html_ast";
import {extractPlaceholderName} from "@angular/compiler/src/i18n/shared";
export class Message {
constructor(public nodes: i18nAst.I18nNode[], public meaning: string, public description: string) {}
}
// TODO: should get the interpolation config
export function extractI18nMessages(
sourceAst: HtmlAst[], implicitTags: string[],
implicitAttrs: {[k: string]: string[]}): Message[] {
const extractionResult = extractAstMessages(sourceAst, implicitTags, implicitAttrs);
if (extractionResult.errors.length) {
return[];
}
const visitor = new _I18nVisitor(new ExpressionParser(new ExpressionLexer()));
return extractionResult.messages.map((msg): Message => {
return new Message(visitor.convertToI18nAst(msg.nodes), msg.meaning, msg.description);
});
}
class _I18nVisitor implements hAst.HtmlAstVisitor {
private _isIcu: boolean;
private _icuDepth: number;
constructor(private _expressionParser: ExpressionParser) {}
visitElement(el:hAst.HtmlElementAst, context:any):i18nAst.I18nNode {
const children = hAst.htmlVisitAll(this, el.children);
const attrs: {[k: string]: string} = {};
el.attrs.forEach(attr => {
// Do not visit the attributes, translatable ones are top-level ASTs
attrs[attr.name] = attr.value;
});
return new i18nAst.TagPlaceholder(el.name, attrs, children, el.sourceSpan);
}
visitAttr(attr:hAst.HtmlAttrAst, context:any):i18nAst.I18nNode {
return this._visitTextWithInterpolation(attr.value, attr.sourceSpan);
}
visitText(text:hAst.HtmlTextAst, context:any):i18nAst.I18nNode {
return this._visitTextWithInterpolation(text.value, text.sourceSpan);
}
visitComment(comment:hAst.HtmlCommentAst, context:any):i18nAst.I18nNode {
return null;
}
visitExpansion(icu:hAst.HtmlExpansionAst, context:any):i18nAst.I18nNode {
this._icuDepth++;
const i18nIcuCases: {[k: string]: i18nAst.I18nNode} = {};
const i18nIcu = new i18nAst.Icu(icu.switchValue, icu.type, i18nIcuCases, icu.sourceSpan);
icu.cases.forEach((caze): void => {
i18nIcuCases[caze.value] = new i18nAst.Container(caze.expression.map((hAst) => hAst.visit(this, {})), caze.expSourceSpan);
});
this._icuDepth--;
if (this._isIcu || this._icuDepth > 0) {
// If the message (vs a part of the message) is an ICU message return its
return i18nIcu;
}
// else returns a placeholder
return new i18nAst.IcuPlaceholder(i18nIcu, 'icu', icu.sourceSpan);
}
visitExpansionCase(icuCase:hAst.HtmlExpansionCaseAst, context:any):i18nAst.I18nNode {
throw new Error('Unreachable code');
}
public convertToI18nAst(htmlAsts: hAst.HtmlAst[]): i18nAst.I18nNode[] {
this._isIcu = htmlAsts.length == 1 && htmlAsts[0] instanceof hAst.HtmlExpansionAst;
this._icuDepth = 0;
return hAst.htmlVisitAll(this, htmlAsts, {});
}
private _visitTextWithInterpolation(text: string, sourceSpan: ParseSourceSpan): i18nAst.I18nNode {
const splitInterpolation = this._expressionParser.splitInterpolation(text, sourceSpan.start.toString());
if (!splitInterpolation) {
// No expression, return a single text
return new i18nAst.Text(text, sourceSpan);
}
// Return a group of text + expressions
const nodes: i18nAst.I18nNode[] = [];
const container = new i18nAst.Container(nodes, sourceSpan);
for (let i = 0; i < splitInterpolation.strings.length - 1; i++) {
const expression = splitInterpolation.expressions[i];
const phName = extractPlaceholderName(expression);
nodes.push(
new i18nAst.Text(splitInterpolation.strings[i], sourceSpan),
new i18nAst.Placeholder(expression, phName, sourceSpan)
)
}
// The last index contains no expression
const lastStringIdx = splitInterpolation.strings.length - 1;
nodes.push(new i18nAst.Text(splitInterpolation.strings[lastStringIdx], sourceSpan));
return container;
}
}

View File

@ -174,6 +174,12 @@ export function extractPhNameFromInterpolation(input: string, index: number): st
return customPhMatch.length > 1 ? customPhMatch[1] : `INTERPOLATION_${index}`;
}
export function extractPlaceholderName(input: string): string {
const matches = StringWrapper.split(input, _CUSTOM_PH_EXP);
return matches[1] || `interpolation`;
}
/**
* Return a unique placeholder name based on the given name
*/