##// END OF EJS Templates
working on text writer
cin -
r79:51649f0f8acd default
parent child
Show More
@@ -0,0 +1,61
1 import { FormatScanner, TokeType } from "./FormatScanner";
2
3 export class FormatCompiler {
4
5 visitText(scanner: FormatScanner) {
6 while (scanner.next()) {
7 if (scanner.getTokenType() === TokeType.CurlOpen)
8 this.visitCurlOpen(scanner);
9 }
10 }
11
12 visitCurlOpen(scanner: FormatScanner) {
13 if (scanner.next()) {
14 if (scanner.getTokenType() === TokeType.CurlOpen)
15 this.pushText("{");
16 else
17 this.visitTemplateSubst(scanner);
18
19 }
20 }
21
22 visitTemplateSubst(scanner: FormatScanner) {
23 if (scanner.getTokenType() !== TokeType.Text)
24 this.dieUnexpectedToken(scanner);
25
26 const fieldName = scanner.getTokenValue();
27 let filedFormat: string;
28 if (this.readColon(scanner)) {
29 filedFormat = this.readFieldFormat(scanner);
30 } else {
31 if (scanner.getTokenType() !== TokeType.CurlClose)
32 this.dieUnexpectedToken(scanner);
33 }
34
35 this.pushSubst(fieldName, filedFormat);
36 }
37 pushSubst(fieldName: string, filedFormat: string) {
38 throw new Error("Method not implemented.");
39 }
40 readFieldFormat(scanner: FormatScanner): string {
41 throw new Error("Method not implemented.");
42 }
43 readColon(scanner: FormatScanner) {
44 if (!scanner.next())
45 this.dieUnexpectedEnd();
46 if (scanner.getTokenType() !== TokeType.Colon)
47 return false;
48 }
49
50 pushText(text: string) {
51
52 }
53
54 dieUnexpectedToken(scanner: FormatScanner) {
55 throw new Error(`Unexpected token ${scanner.getTokenValue()}`);
56 }
57
58 dieUnexpectedEnd() {
59 throw new Error("Unexpected end of string");
60 }
61 }
@@ -0,0 +1,48
1 import { argumentNotEmptyString } from "../safe";
2 import { MapOf } from "../interfaces";
3
4 export const enum TokeType {
5 CurlOpen,
6 CurlClose,
7 Colon,
8 Text
9 }
10
11 const typeMap = {
12 "{": TokeType.CurlOpen,
13 "}": TokeType.CurlClose,
14 ":": TokeType.Colon
15 } as MapOf<TokeType>;
16
17 export class FormatScanner {
18 private _text: string;
19 private _pos: number;
20 private _tokenType: TokeType;
21 private _tokenValue: string;
22 private _rx = /[^{}:]+|(.)/g;
23
24 constructor(text: string) {
25 argumentNotEmptyString(text, text);
26 this._text = text;
27 }
28
29 next() {
30 if (this._rx.lastIndex >= this._text.length)
31 return false;
32 this._pos = this._rx.lastIndex;
33
34 const match = this._rx.exec(this._text);
35 this._tokenType = typeMap[match[1]] || TokeType.Text;
36 this._tokenValue = match[0];
37
38 return true;
39 }
40
41 getTokenValue() {
42 return this._tokenValue;
43 }
44
45 getTokenType() {
46 return this._tokenType;
47 }
48 }
@@ -0,0 +1,18
1 export class StringBuilder {
2 private _data: string[];
3 private _newLine = "\n";
4
5 Write(obj: any);
6 Write(format: string, ...args: any[]) {
7
8 }
9
10 WriteLine(obj: any);
11 WriteLine(format: string, ...args: any[]) {
12
13 }
14
15 WriteValue(value: any, spec?: string) {
16
17 }
18 }
General Comments 0
You need to be logged in to leave comments. Login now