Skip to content

Commit

Permalink
wip(feat): add pattern for escaping token lookup()
Browse files Browse the repository at this point in the history
Ref: #14
  • Loading branch information
mdeltito committed Jul 6, 2021
1 parent 5fe31e6 commit 44fb66c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const {object} = require('@logdna/stdlib')
const typeOf = require('./lang/type-of.js')
const builtins = require('./actions/index.js')
const {Parser, Lexer, Visitor} = require('./parser/index.js')
const {BANG, HASH} = require('./parser/tokens.js')
const {BANG, HASH, DASH} = require('./parser/tokens.js')
const ESCAPE_TOKENS = new Set([DASH.name])
const PARSE_TOKENS = new Set([BANG.name, HASH.name])

const lookup = Symbol('lookup')
Expand Down Expand Up @@ -113,6 +114,10 @@ module.exports = class SetupChain {
if (!lexed.tokens.length) return this.visitor.wrapLiteral(str)

const firstToken = lexed.tokens[0]
if (ESCAPE_TOKENS.has(firstToken.tokenType.name)) {
return this.visitor.wrapLiteral(str.slice(1))
}

if (!PARSE_TOKENS.has(firstToken.tokenType.name)) {
return this.visitor.wrapLiteral(str)
}
Expand Down Expand Up @@ -155,7 +160,6 @@ module.exports = class SetupChain {
const fn = this.actions[key]
this.state[label || key] = await fn.apply(this, rest)
} catch (err) {
console.error(err)
const error = new Error(`Setup Chain error in ${key}`)
error.stack = err.stack
error.chain_params = task
Expand Down
7 changes: 7 additions & 0 deletions lib/parser/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const BANG = createToken({
, pattern: /!/
})

const DASH = createToken({
name: 'dash'
, pattern: /-/
})

const COMMA = createToken({
name: 'comma'
, pattern: ','
Expand Down Expand Up @@ -54,6 +59,7 @@ const WHITE_SPACE = createToken({
})

BANG.LABEL = '!'
DASH.LABEL = '-'
HASH.LABEL = '#'
COMMA.LABEL = ','
COLON.LABEL = ':'
Expand All @@ -66,6 +72,7 @@ module.exports = {
, DOT
, HASH
, BANG
, DASH
, LPAREN
, RPAREN
, COMMA
Expand Down
21 changes: 21 additions & 0 deletions test/integration/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,27 @@ test('Setup chain', async (t) => {
}
})

t.test('escape control', async (t) => {
class EscapeChain extends Chain {
constructor(state) {
super(state)
}

$test(arg) {
return arg
}
}

const chain = new EscapeChain({f00: 'wrong'})
chain.set('color', '-#f00')
chain.set('rand', '-!random')
chain.set('complex', '!test("-#foo")')
const state = await chain.execute()
t.equal(state.color, '#f00')
t.equal(state.rand, '!random')
t.equal(state.complex, '#foo')
})

t.test('Default SetupChain (no actions, state); Only built-ins', async (t) => {
const chain = new Chain(null, null)
t.same(chain.state, {}, 'Empty state')
Expand Down

0 comments on commit 44fb66c

Please sign in to comment.