@@ -4,12 +4,13 @@ import { ISimpleToken, IRangeToken } from './IToken';
4
4
import { IModule , IModuleEnums } from './module'
5
5
import { IMatcher , IMatcherState } from './matchers'
6
6
import { ITokenEmitter } from './tokenProducers'
7
+ import { isComment , isContinue } from './helpers'
7
8
8
9
const printer = debug ( 'IChannel' )
9
10
10
11
export interface IChannel < T extends ISimpleToken > {
11
12
mod : IModule ;
12
- name : string ;
13
+ name : string ;
13
14
tokens : T [ ] ;
14
15
process ( ) ;
15
16
}
@@ -34,7 +35,7 @@ export function createChannel<T extends ISimpleToken>(name: string) {
34
35
printer ( `module [${ module . name } ] loaded an empty file` )
35
36
return
36
37
}
37
- this . tokens = [ ] //clear
38
+ this . tokens = [ ] //clear
38
39
for ( let i = 0 ; i < raw . length ; i ++ ) {
39
40
te . forEach ( fn => {
40
41
const token = fn ( raw [ i ] , i )
@@ -51,21 +52,64 @@ export function createChannel<T extends ISimpleToken>(name: string) {
51
52
}
52
53
}
53
54
54
- export function createVirtualEOLChannel < T extends ISimpleToken > ( name : string , ch : IChannel < T > ) : IChannel < T > {
55
-
56
- if ( ch . name !== 'lf' ) {
57
- throw new TypeError ( `channel not the "lf" channel` )
58
- }
59
- if ( ch !== ch . mod . channels . get ( 'lf' ) ) {
55
+ export function createLogicalEOLChannel < T extends ISimpleToken > ( ch : IChannel < T > ) : IChannel < T > {
56
+
57
+ if ( ch !== ch . mod . channels . get ( 'lf' ) ) {
60
58
throw new TypeError ( `source "lf" channel is not registered with a module` )
61
59
}
62
- const vCh :IChannel < T > = {
60
+ const vCh : IChannel < T > = {
63
61
mod : ch . mod ,
64
- tokens :[ ] , //vtokens
65
- name,
66
- process ( ) {
67
-
62
+ tokens : [ ] , //vtokens
63
+ name : 'vlf' ,
64
+ process ( ) {
65
+ const tokens = this . tokens = [ ]
66
+ let prev = 0
67
+ for ( let i = 0 ; i < ch . tokens . length ; i ++ ) {
68
+ const pos = ch . tokens [ i ] . f
69
+ const line = ch . mod . raw . slice ( prev , pos )
70
+ prev = pos + 1
71
+ if ( isContinue ( line ) ) {
72
+ if ( tokens . length === 0 ) {
73
+ const err = `first line cannot be continuation: [${ line } ]`
74
+ printer ( err )
75
+ throw new Error ( err )
76
+ }
77
+ tokens [ tokens . length - 1 ] = ch . tokens [ i ]
78
+ continue
79
+ }
80
+ tokens . push ( ch . tokens [ i ] )
81
+ }
68
82
}
69
83
}
84
+ ch . mod . channels . set ( vCh . name , vCh )
70
85
return vCh
86
+ }
87
+
88
+ export function createCommentsChannel < T extends ISimpleToken > ( ch : IChannel < T > ) : IChannel < T > {
89
+
90
+ if ( ch !== ch . mod . channels . get ( 'vlf' ) && ch !== ch . mod . channels . get ( 'lf' ) ) {
91
+ throw new TypeError ( `source "lf/vlf" channel is not registered with a module` )
92
+ }
93
+ const comm : IChannel < T > = {
94
+ mod : ch . mod ,
95
+ tokens : [ ] , //vtokens
96
+ name : 'comments' ,
97
+ process ( ) {
98
+ const tokens = this . tokens = [ ]
99
+ let prev = 0
100
+ for ( let i = 0 ; i < ch . tokens . length ; i ++ ) {
101
+ const pos = ch . tokens [ i ] . f
102
+ const line = ch . mod . raw . slice ( prev , pos )
103
+
104
+ if ( isComment ( line ) ) {
105
+ tokens . push ( { f : prev , t : pos } )
106
+
107
+ }
108
+ prev = pos + 1
109
+
110
+ }
111
+ }
112
+ }
113
+ ch . mod . channels . set ( comm . name , comm )
114
+ return comm
71
115
}
0 commit comments