Skip to content

Commit

Permalink
space-as-terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
drernie committed Nov 21, 2023
1 parent b895abf commit 6ec242f
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/execute/lex-pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export class LexPipe extends Frame implements IFinish, IPerformer {
parser.finish(value)
break
}
case 'bind': {
break
const next_parser = parser.bind()

Check failure on line 50 in src/execute/lex-pipe.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Unreachable code

Check failure on line 50 in src/execute/lex-pipe.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unreachable code
this.set(Frame.kOUT, next_parser)
this.level += 1
break
}
case 'push': {
const next_parser = parser.push(value)
this.set(Frame.kOUT, next_parser)
Expand Down
7 changes: 4 additions & 3 deletions src/execute/lex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ export class Lex extends Frame implements ISourced {
const end = this.isEnd(char)
const terminal = Lex.isTerminal(char)
const not_quote = !this.isQuote()
const not_space = char != ' '

Check failure on line 60 in src/execute/lex.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected '!==' and instead saw '!='

Check failure on line 60 in src/execute/lex.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected '!==' and instead saw '!='

if (end && terminal) { // ends token on a terminal
if (end && terminal && not_space) { // ends token on a terminal
return this.finish(argument, true)
}
if (end) { // ends token, but not on a terminal
Expand All @@ -67,7 +68,7 @@ export class Lex extends Frame implements ISourced {
return result
}

if (terminal && not_quote) { // unquoted terminal implicitly ends token
if (terminal && not_quote && not_space) { // unquoted terminal implicitly ends token
return this.finish(argument, true)
}

Expand Down Expand Up @@ -123,7 +124,7 @@ export class Lex extends Frame implements ISourced {
const output: Token = this.makeFrame()
const out = this.get(Frame.kOUT)
if (output.isSpace()) {
return out
return out // ignore [if not a Terminal]
}
const result = out.call(output)
return result
Expand Down
16 changes: 15 additions & 1 deletion src/execute/parse-pipe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Frame, FrameArray, FrameExpr } from '../frames.js'
import { Context, Frame, FrameArray, FrameBind, FrameExpr } from '../frames.js'
import { IFinish, Terminal } from './terminals.js'

export class ParsePipe extends FrameArray implements IFinish {
Expand All @@ -15,6 +15,7 @@ export class ParsePipe extends FrameArray implements IFinish {
}

public next (statement: boolean = false): Frame {
this.unbind()
if (this.length() === 0) {
return this
}
Expand All @@ -28,6 +29,18 @@ export class ParsePipe extends FrameArray implements IFinish {
return this
}

public bind(_Factory: any = undefined): Frame {
return this.push(FrameBind)
}

public unbind(): boolean {
if (this.Factory === FrameBind) {
this.pop(FrameBind)
return true
}
return false
}

public push (Factory: any): Frame {
const child = new ParsePipe(this, Factory)
return child
Expand All @@ -40,6 +53,7 @@ export class ParsePipe extends FrameArray implements IFinish {
}

public canPop (Factory: any): boolean {
this.unbind()
const match = (this.Factory.name === Factory.name)
if (!match) {
console.error(`ParsePipe.canPop.failed: ${Factory.name} cannot pop ${this.Factory.name}`)
Expand Down
1 change: 1 addition & 0 deletions src/execute/terminals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ terminals[Frame.kEND] = Terminal.end()
addTerminal('\n', 'end')
addTerminal(',', 'next')
addTerminal(';', 'semi-next')
addTerminal(' ', 'bind')
addGroup(FrameArray)
addGroup(FrameGroup)
addGroup(FrameLazy)
Expand Down
2 changes: 1 addition & 1 deletion src/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export { FrameBlob } from './frames/frame-blob.js'
export { FrameBytes } from './frames/frame-bytes.js'
export { FrameComment } from './frames/frame-comment.js'
export { FrameDoc } from './frames/frame-doc.js'
export { FrameExpr } from './frames/frame-expr.js'
export { FrameBind, FrameExpr } from './frames/frame-expr.js'
export { FrameGroup } from './frames/frame-group.js'
export { FrameLazy } from './frames/frame-lazy.js'
export { FrameList, IArrayConstructor } from './frames/frame-list.js'
Expand Down
3 changes: 3 additions & 0 deletions src/frames/frame-expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ export class FrameExpr extends FrameList {
return [array.join(' ') + ',']
}
};

export class FrameBind extends FrameExpr {
}
25 changes: 23 additions & 2 deletions test/execute/lex-pipe-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import { Lex } from '../../src/execute/lex.js'
import { LexBytes } from '../../src/execute/lex-bytes.js'
import { LexPipe } from '../../src/execute/lex-pipe.js'
import * as frame from '../../src/frames.js'
import { ParsePipe } from '../../src/execute/parse-pipe.js'

describe('LexPipe', () => {
const success = new frame.FrameString('success!')
let out: frame.FrameArray
let final: frame.FrameArray
let out: ParsePipe
let pipe: LexPipe

beforeEach(() => {
out = new frame.FrameArray([])
final = new frame.FrameArray([])
out = new ParsePipe(final, frame.FrameArray)
out.set(frame.Frame.kEND, success)
pipe = new LexPipe(out)
})
Expand All @@ -29,4 +32,22 @@ describe('LexPipe', () => {
const result = pipe.finish(frame.Frame.nil)
expect(result).to.equal(pipe)
})

it('changes output on push', () => {
const out = pipe.get(frame.Frame.kOUT)
const result = pipe.perform({ push: frame.FrameExpr })
const out2 = pipe.get(frame.Frame.kOUT)
expect(out2).to.not.equal(out)
expect(result).to.equal(pipe)
expect(pipe.level).to.equal(1)
})

it.skip('changes output on bind', () => {
const out = pipe.get(frame.Frame.kOUT)
const result = pipe.perform({ bind: frame.FrameBind })
const out2 = pipe.get(frame.Frame.kOUT)
expect(out2).to.not.equal(out)
expect(result).to.equal(pipe)
expect(pipe.level).to.equal(1)
})
})
2 changes: 1 addition & 1 deletion test/execute/parse-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('Parse', () => {
expect(group.toString()).to.equal(`((${content}))`)
})

it('joins Grouped strings', () => {
it('joins strings in Grouped', () => {
pipe.call(token)
pipe.call(token)
pipe.call(frame.FrameSymbol.end())
Expand Down

0 comments on commit 6ec242f

Please sign in to comment.