##// END OF EJS Templates
Working on text writer
cin -
r81:2ac7406b138e default
parent child
Show More
@@ -1,106 +1,111
1 export interface Constructor<T = {}> {
1 export interface Constructor<T = {}> {
2 new (...args: any[]): T;
2 new(...args: any[]): T;
3 prototype: T;
3 prototype: T;
4 }
4 }
5
5
6 export type Factory<T = {}> = (...args: any[]) => T;
6 export type Factory<T = {}> = (...args: any[]) => T;
7
7
8 export type Predicate<T = any> = (x: T) => boolean;
8 export type Predicate<T = any> = (x: T) => boolean;
9
9
10 export interface MapOf<T> {
10 export interface MapOf<T> {
11 [key: string]: T;
11 [key: string]: T;
12 }
12 }
13
13
14 export interface IDestroyable {
14 export interface IDestroyable {
15 destroy(): void;
15 destroy(): void;
16 }
16 }
17
17
18 export interface IRemovable {
18 export interface IRemovable {
19 remove(): void;
19 remove(): void;
20 }
20 }
21
21
22 export interface ICancellation {
22 export interface ICancellation {
23 throwIfRequested(): void;
23 throwIfRequested(): void;
24 isRequested(): boolean;
24 isRequested(): boolean;
25 isSupported(): boolean;
25 isSupported(): boolean;
26 register(cb: (e: any) => void): IDestroyable;
26 register(cb: (e: any) => void): IDestroyable;
27 }
27 }
28
28
29 /**
29 /**
30 * Интерфейс поддерживающий асинхронную активацию
30 * Интерфейс поддерживающий асинхронную активацию
31 */
31 */
32 export interface IActivatable {
32 export interface IActivatable {
33 /**
33 /**
34 * @returns Boolean indicates the current state
34 * @returns Boolean indicates the current state
35 */
35 */
36 isActive(): boolean;
36 isActive(): boolean;
37
37
38 /**
38 /**
39 * Starts the component activation
39 * Starts the component activation
40 * @param ct cancellation token for this operation
40 * @param ct cancellation token for this operation
41 */
41 */
42 activate(ct?: ICancellation): Promise<void>;
42 activate(ct?: ICancellation): Promise<void>;
43
43
44 /**
44 /**
45 * Starts the component deactivation
45 * Starts the component deactivation
46 * @param ct cancellation token for this operation
46 * @param ct cancellation token for this operation
47 */
47 */
48 deactivate(ct?: ICancellation): Promise<void>;
48 deactivate(ct?: ICancellation): Promise<void>;
49
49
50 /**
50 /**
51 * Sets the activation controller for this component
51 * Sets the activation controller for this component
52 * @param controller The activation controller
52 * @param controller The activation controller
53 *
53 *
54 * Activation controller checks whether this component
54 * Activation controller checks whether this component
55 * can be activated and manages the active state of the
55 * can be activated and manages the active state of the
56 * component
56 * component
57 */
57 */
58 setActivationController(controller: IActivationController);
58 setActivationController(controller: IActivationController);
59
59
60 /**
60 /**
61 * Gets the current activation controller for this component
61 * Gets the current activation controller for this component
62 */
62 */
63 getActivationController(): IActivationController;
63 getActivationController(): IActivationController;
64 }
64 }
65
65
66 export interface IActivationController {
66 export interface IActivationController {
67 activating(component: IActivatable, ct?: ICancellation): Promise<void>;
67 activating(component: IActivatable, ct?: ICancellation): Promise<void>;
68
68
69 activated(component: IActivatable, ct?: ICancellation): Promise<void>;
69 activated(component: IActivatable, ct?: ICancellation): Promise<void>;
70
70
71 deactivating(component: IActivatable, ct?: ICancellation): Promise<void>;
71 deactivating(component: IActivatable, ct?: ICancellation): Promise<void>;
72
72
73 deactivated(component: IActivatable, ct?: ICancellation): Promise<void>;
73 deactivated(component: IActivatable, ct?: ICancellation): Promise<void>;
74
74
75 deactivate(ct?: ICancellation): Promise<void>;
75 deactivate(ct?: ICancellation): Promise<void>;
76
76
77 activate(component: IActivatable, ct?: ICancellation): Promise<void>;
77 activate(component: IActivatable, ct?: ICancellation): Promise<void>;
78
78
79 getActive(): IActivatable;
79 getActive(): IActivatable;
80 }
80 }
81
81
82 export interface IAsyncComponent {
82 export interface IAsyncComponent {
83 getCompletion(): Promise<void>;
83 getCompletion(): Promise<void>;
84 }
84 }
85
85
86 export interface ICancellable {
86 export interface ICancellable {
87 cancel(reason?: any): void;
87 cancel(reason?: any): void;
88 }
88 }
89
89
90 export interface IObservable<T> {
90 export interface IObservable<T> {
91 on(next: (x: T) => void, error?: (e: any) => void, complete?: () => void): IDestroyable;
91 on(next: (x: T) => void, error?: (e: any) => void, complete?: () => void): IDestroyable;
92 next(ct?: ICancellation): Promise<T>;
92 next(ct?: ICancellation): Promise<T>;
93 }
93 }
94
94
95 export interface IObserver<T> {
95 export interface IObserver<T> {
96 next(event: T): void;
96 next(event: T): void;
97
97
98 error(e: any): void;
98 error(e: any): void;
99
99
100 complete(): void;
100 complete(): void;
101 }
101 }
102
102
103 export interface TextWriter {
103 export interface TextWriter {
104 formatter;
104 Write(obj: any): void;
105 Write(data, format?: string);
105 Write(format: string, ...args: any[]): void;
106
107 WriteLine(obj: any): void;
108 WriteLine(format: string, ...args: any[]): void;
109
110 WriteValue(value: any, spec?: string): void;
106 }
111 }
@@ -1,65 +1,101
1 import { FormatScanner, TokeType } from "./FormatScanner";
1 import { FormatScanner, TokeType } from "./FormatScanner";
2 import { isNullOrEmptyString } from "../safe";
3 import { TextWriter } from "../interfaces";
2
4
3 export class FormatCompiler {
5 export class FormatCompiler {
6 _scanner: FormatScanner;
4
7
5 visitText(scanner: FormatScanner) {
8 _parts: [];
6 while (scanner.next()) {
9
7 if (scanner.getTokenType() === TokeType.CurlOpen)
10 compile() {
8 this.visitCurlOpen(scanner);
11 return (writer: TextWriter, args: any) => {
12 this._parts.forEach(x => writer.WriteValue(x))
13 };
14 }
15
16 visitText() {
17 while (this._scanner.next()) {
18 switch (this._scanner.getTokenType()) {
19 case TokeType.CurlOpen:
20 this.visitCurlOpen();
21 break;
22 case TokeType.CurlClose:
23 this.visitCurlClose();
24 break;
25 default:
26 this.pushText(this._scanner.getTokenValue());
27 }
28 if (this._scanner.getTokenType() === TokeType.CurlOpen)
29 this.visitCurlOpen();
9 }
30 }
10 }
31 }
11
32
12 visitCurlOpen(scanner: FormatScanner) {
33 visitCurlClose() {
13 if (scanner.next()) {
34 if (!this._scanner.next())
14 if (scanner.getTokenType() === TokeType.CurlOpen)
35 this.dieUnexpectedEnd("}");
36 if (this._scanner.getTokenType() !== TokeType.CurlClose)
37 this.dieUnexpectedToken("}");
38 this.pushText("}");
39 }
40
41 visitCurlOpen() {
42 if (this._scanner.next()) {
43 if (this._scanner.getTokenType() === TokeType.CurlOpen)
15 this.pushText("{");
44 this.pushText("{");
16 else
45 else
17 this.visitTemplateSubst(scanner);
46 this.visitTemplateSubst();
18
19 }
47 }
20 }
48 }
21
49
22 visitTemplateSubst(scanner: FormatScanner) {
50 visitTemplateSubst() {
23 if (scanner.getTokenType() !== TokeType.Text)
51 if (this._scanner.getTokenType() !== TokeType.Text)
24 this.dieUnexpectedToken(scanner);
52 this.dieUnexpectedToken("TEXT");
25
53
26 const fieldName = scanner.getTokenValue();
54 const fieldName = this._scanner.getTokenValue();
27 let filedFormat: string;
55 const filedFormat = this.readColon() && this.readFieldFormat();
28 if (this.readColon(scanner)) {
29 filedFormat = this.readFieldFormat(scanner);
30 } else {
31 if (scanner.getTokenType() !== TokeType.CurlClose)
32 this.dieUnexpectedToken(scanner);
33 }
34
56
35 this.pushSubst(fieldName, filedFormat);
57 this.pushSubst(fieldName, filedFormat);
36 }
58 }
37
59
38 pushSubst(fieldName: string, filedFormat: string) {
60 readFieldFormat() {
39 throw new Error("Method not implemented.");
61 const parts = new Array<string>();
62 while (this._scanner.next()) {
63 if (this._scanner.getTokenType() === TokeType.CurlClose) {
64 return parts.join("");
65 } else {
66 parts.push(this._scanner.getTokenValue());
67 }
68 }
69
70 this.dieUnexpectedEnd("}");
40 }
71 }
41
72
42 readFieldFormat(scanner: FormatScanner): string {
73 readColon() {
43 throw new Error("Method not implemented.");
74 if (!this._scanner.next())
75 this.dieUnexpectedEnd();
76 if (this._scanner.getTokenType() !== TokeType.Colon)
77 return false;
78 if (!this._scanner.next())
79 this.dieUnexpectedEnd();
80 return true;
44 }
81 }
45
82
46 readColon(scanner: FormatScanner) {
83 pushSubst(fieldName: string, filedFormat: string) {
47 if (!scanner.next())
48 this.dieUnexpectedEnd();
49 if (scanner.getTokenType() !== TokeType.Colon)
50 return false;
51
84
52 }
85 }
53
86
54 pushText(text: string) {
87 pushText(text: string) {
55
88
56 }
89 }
57
90
58 dieUnexpectedToken(scanner: FormatScanner) {
91 dieUnexpectedToken(expected?: string) {
59 throw new Error(`Unexpected token ${scanner.getTokenValue()}`);
92 throw new Error(isNullOrEmptyString(expected) ?
93 `Unexpected token ${this._scanner.getTokenValue()}` :
94 `Unexpected token ${this._scanner.getTokenValue()}, expected ${expected}`
95 );
60 }
96 }
61
97
62 dieUnexpectedEnd() {
98 dieUnexpectedEnd(expected?: string) {
63 throw new Error("Unexpected end of string");
99 throw new Error(isNullOrEmptyString(expected) ? "Unexpected end of the string" : `Unexpected end of the string, expected ${expected}`);
64 }
100 }
65 }
101 }
@@ -1,18 +1,18
1 export class StringBuilder {
1 export class StringBuilder {
2 private _data: string[];
2 private _data: string[];
3 private _newLine = "\n";
3 private _newLine = "\n";
4
4
5 Write(obj: any);
5 Write(obj: any);
6 Write(format: string, ...args: any[]) {
6 Write(format: string, ...args: any[]) {
7
7
8 }
8 }
9
9
10 WriteLine(obj: any);
10 WriteLine(obj: any);
11 WriteLine(format: string, ...args: any[]) {
11 WriteLine(format: string, ...args: any[]) {
12
12
13 }
13 }
14
14
15 WriteValue(value: any, spec?: string) {
15 WriteValue(value: any, spec?: string) {
16
16
17 }
17 }
18 }
18 }
General Comments 0
You need to be logged in to leave comments. Login now