Skip to content

Commit

Permalink
check parser.canPop
Browse files Browse the repository at this point in the history
  • Loading branch information
drernie committed Oct 8, 2023
1 parent 12fa3f8 commit ec1cb27
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/execute/lex-pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export class LexPipe extends Frame implements IFinish, IPerformer {
break
}
case 'pop': {
if (this.level === 0) {
console.error('LexPipe.perform.pop.failed: already at top level')
break
}
if (!parser.canPop(value)) {
break
}
const next_parser = parser.pop(value)
this.set(Frame.kOUT, next_parser)
this.level -= 1
Expand Down
12 changes: 8 additions & 4 deletions src/execute/parse-pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ export class ParsePipe extends FrameArray implements IFinish {

public pop (Factory: any): Frame {
const parent = this.get(ParsePipe.kOUT) as ParsePipe
if (parent.Factory !== Factory) {
const msg = [' open:', parent.Factory, ' close:', Factory]
console.error('pop.mismatched-brackets' + msg.join(''))
}
this.finish(Frame.nil)
return parent
}

public canPop (Factory: any): boolean {
const match = (this.Factory.name === Factory.name)
if (!match) {
console.error(`ParsePipe.canPop.failed: ${Factory.name} cannot pop ${this.Factory.name}`)
}
return match
}

public finish (terminal: any): Frame {
this.next()
const out = this.get(Frame.kOUT)
Expand Down
30 changes: 30 additions & 0 deletions test/execute/evaluate-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,35 @@ describe('evaluate', () => {
const output = result.at(0)
expect(output).to.be.instanceof(frame.FrameLazy)
})

it('returns FrameNote for empty ()', () => {
const result = evaluate('()')
const output = result.at(0)
expect(output).to.be.instanceof(frame.FrameNote)
})

it('returns FrameArray for empty [] with spaces', () => {
const result = evaluate(' [ ] ')
const output = result.at(0)
expect(output).to.be.instanceof(frame.FrameArray)
})

it('returns FrameArray for empty [] with comments', () => {
const result = evaluate('[#comment#]')
const output = result.at(0)
expect(output).to.be.instanceof(frame.FrameArray)
})

it('returns FrameNote for mis-matched brackets', () => {
const result = evaluate('[}')
const output = result.at(0)
expect(output).to.be.instanceof(frame.FrameNote)
})

it('returns FrameNote for un-opened close bracket', () => {
const result = evaluate('}')
const output = result.at(0)
expect(output).to.be.instanceof(frame.FrameNote)
})
})
})

0 comments on commit ec1cb27

Please sign in to comment.