Skip to content

Commit 71cde66

Browse files
committed
comments channel created
1 parent d771b6f commit 71cde66

File tree

5 files changed

+81
-29
lines changed

5 files changed

+81
-29
lines changed

src/channel.ts

+57-13
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { ISimpleToken, IRangeToken } from './IToken';
44
import { IModule, IModuleEnums } from './module'
55
import { IMatcher, IMatcherState } from './matchers'
66
import { ITokenEmitter } from './tokenProducers'
7+
import { isComment, isContinue } from './helpers'
78

89
const printer = debug('IChannel')
910

1011
export interface IChannel<T extends ISimpleToken> {
1112
mod: IModule;
12-
name: string;
13+
name: string;
1314
tokens: T[];
1415
process();
1516
}
@@ -34,7 +35,7 @@ export function createChannel<T extends ISimpleToken>(name: string) {
3435
printer(`module [${module.name}] loaded an empty file`)
3536
return
3637
}
37-
this.tokens =[] //clear
38+
this.tokens = [] //clear
3839
for (let i = 0; i < raw.length; i++) {
3940
te.forEach(fn => {
4041
const token = fn(raw[i], i)
@@ -51,21 +52,64 @@ export function createChannel<T extends ISimpleToken>(name: string) {
5152
}
5253
}
5354

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')) {
6058
throw new TypeError(`source "lf" channel is not registered with a module`)
6159
}
62-
const vCh:IChannel<T> = {
60+
const vCh: IChannel<T> = {
6361
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+
}
6882
}
6983
}
84+
ch.mod.channels.set(vCh.name, vCh)
7085
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
71115
}

src/helpers.ts

+4
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ export const isString = s => typeof s === 'string'
33
export const cc = '\u0020'
44
export const _ = undefined
55

6+
export const isComment = c => '*cC'.includes(c[0])
7+
export const isContinue = function (c: string) {
8+
return c.startsWith(' ') && c[5] !== ' '
9+
}

src/index.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createModule } from './module'
22
import { createClassMatcher, createLiteralMatcher } from './matchers'
33
import { simpleProducer, rangeProducer, createTokenEmitter } from './tokenProducers'
44
import { lf, ws } from './classes'
5-
import { createChannel } from './channel'
5+
import { createChannel, createLogicalEOLChannel, createCommentsChannel } from './channel'
66
import { aLoad } from './fsutils'
77

88
export {
@@ -14,7 +14,9 @@ export {
1414
createChannel,
1515
lf,
1616
ws,
17-
createModule
17+
createModule,
18+
createLogicalEOLChannel,
19+
createCommentsChannel
1820
}
1921

2022
export default {
@@ -26,6 +28,8 @@ export default {
2628
createChannel,
2729
lf,
2830
ws,
29-
createModule
31+
createModule,
32+
createLogicalEOLChannel,
33+
createCommentsChannel
3034
}
3135

test.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ const {
99
simpleProducer, rangeProducer,
1010
createTokenEmitter,
1111
createChannel,
12-
aLoad
12+
aLoad,
13+
createLogicalEOLChannel,
14+
createCommentsChannel
1315
} = f77
1416

1517
const mod77 = f77.createModule(resolve('test/dhgeqz.f'))
@@ -26,17 +28,15 @@ const wsChannel = createChannel( 'ws' )( wsEmitter )( mod77 )
2628
mod77.load().then(mod=>{
2729
lfChannel.process()
2830
wsChannel.process()
29-
channel = mod.channels.get('lf')
30-
const hist = new Map()
31-
channel.tokens.reduce( (map, v, i, arr) => {
32-
const delta = (v.t && ( v.t - v.f + 1 )) || 1
33-
let len = map.get(delta) || 0
34-
len += delta
35-
map.set(delta, len)
36-
return map
37-
}, hist)
38-
const e = Array.from(hist.entries()).sort((a,b)=>a[0]-b[0])
39-
e.forEach(eb=>console.log('\n>',eb[0],eb[1]))
31+
const vlfChannel = createLogicalEOLChannel(lfChannel)
32+
vlfChannel.process()
33+
const commChannel = createCommentsChannel(vlfChannel)
34+
commChannel.process()
35+
//channel = mod.channels.get('lf')
36+
commChannel.tokens.forEach((v, i, arr) => {
37+
console.log(`${`${i}`.padStart(4,0)}: ${commChannel.mod.raw.slice(v.f,v.t)}`)
38+
})
39+
4040
})
4141

4242

test/dhgeqz.f

+1-1
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ SUBROUTINE DHGEQZ( JOB, COMPQ, COMPZ, N, ILO, IHI, H, LDH, T, LDT,
12071207
IF( ABS( W22 ).LT.ABS( U2 ) )
12081208
$ SCALE = ABS( W22 / U2 )
12091209
IF( ABS( W11 ).LT.ABS( U1 ) )
1210-
$ SCALE = MIN( SCALE, ABS( W11 / U1 ) )
1210+
12345$ SCALE = MIN( SCALE, ABS( W11 / U1 ) )
12111211
*
12121212
* Solve
12131213
*

0 commit comments

Comments
 (0)