Skip to content

Commit

Permalink
fix: macro string escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
NLKNguyen committed Apr 9, 2023
1 parent 114b529 commit 978395e
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 46 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "code-formation",
"version": "0.7.0",
"version": "0.7.1",
"description": "context-free text manipulator using line-oriented DSL for easy embedding to existing source code",
"main": "./src/index.js",
"bin": "./src/index.js",
Expand Down Expand Up @@ -41,7 +41,7 @@
"parse-pairs": "^1.1.0",
"query-string": "^7.1.1",
"requireg": "^0.2.2",
"s-expression.js": "^0.6.2",
"s-expression.js": "^0.6.4",
"vm2": "^3.9.11",
"wildcard-match": "^5.1.2",
"winston": "^3.6.0"
Expand Down
7 changes: 5 additions & 2 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ invoke = async (snippet, context) => {
// }
const LANGUAGE = _.get(context, "LANGUAGE")
const LINE_BREAK = _.get(context, "LINE_BREAK")

// console.dir({template: snippet.template, LANGUAGE, LINE_BREAK})
const template = snippet.template.join(LINE_BREAK)

let result = ""
Expand Down Expand Up @@ -104,7 +104,10 @@ invoke = async (snippet, context) => {
}
function serializeMacro(macro) {
const S = new SExpr()
return S.serialize(S.parse(macro), { rootBrackets: false })
const ast = S.parse(macro, { includedRootParentheses: false })
console.dir(ast)
process.exit()
return S.serialize(ast, { includingRootParentheses: false })
}
async function parseParams(str) {
const S = new SExpr()
Expand Down
8 changes: 5 additions & 3 deletions src/eval-blobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ module.exports = async function (files, profile, log) {
// let {key, value} = S.ExtractEntry(action)
const key = Object.keys(action)[0]
const value = action[key]
// console.dir({key, value})
// console.dir({APPLY, action, key, value})
// logger.info(colorize(params, {pretty: true}))
// process.exit()

// TODO: check if key starts with @ to expand macro, no need for nested macro

Expand Down Expand Up @@ -351,8 +353,8 @@ module.exports = async function (files, profile, log) {
SECTION_SEPARATOR,
},
// separator: SECTION_SEPARATOR,
})
_.set(profile, outpath, destination)
})
_.set(profile, outpath, destination)
// console.log(destination)

// _.set(profile, ['content', 'a'], output)
Expand Down
46 changes: 19 additions & 27 deletions src/eval-snippet-injections.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,25 @@ async function evalBlock(

// console.log(rest)
injection.params = await common.parseParams(rest)
// process.exit()

// TODO: should just be one "LINE_BREAK" instead of LINE_BREAK/LINE_BREAK
const LINE_BREAK = _.get(injection.params, ["LINE_BREAK"], common.profile.LINE_BREAK)
const LINE_BREAK = _.get(
injection.params,
["LINE_BREAK"],
common.profile.LINE_BREAK
)
const INPUT = injection.blockContent.join(LINE_BREAK)


injection.args = _.merge({}, injection.params, {
INPUT,
_logger: logger,
_common: common
_common: common,
// _ is available by the template library
})
let snippet_name = command
if (snippet_name.startsWith("@")) {
injection.macroExpansion = true
snippet_name = snippet_name.split("@")[1]
// console.dir(snippet_name)

const snippet = _.get(common.profile, ["snippets", snippet_name])
if (_.isUndefined(snippet)) {
Expand All @@ -92,10 +94,14 @@ async function evalBlock(
let context = _.merge({}, params, snippet.params, injection.args)

const macro = await common.invoke(snippet, context)
const expansion = common.serializeMacro(`(${macro})`)
const expansion = common.serializeMacro(macro)

lines[line_number] = line.replace(openRegex, () => {
return macroExpansionCallback(common.profile.MARKER_PREFIX, label, expansion)
return macroExpansionCallback(
common.profile.MARKER_PREFIX,
label,
expansion
)
})
logger.info(
`${chalk.cyan(`expand macro "@${snippet_name}":`)} ${chalk.gray(
Expand All @@ -118,9 +124,7 @@ async function evalSnippetInjection(content, params, profile, log) {
let hasSnippetInjection = false
let LINE_BREAK = _.get(params, ["LINE_BREAK"])



const CURRENT_DIR = _.get(params, ["CURRENT_DIR"])
const CURRENT_DIR = _.get(params, ["CURRENT_DIR"])

// const OUT_DIR = _.get(profile, "OUT_DIR")
// console.log(content)
Expand Down Expand Up @@ -167,7 +171,6 @@ async function evalSnippetInjection(content, params, profile, log) {
`${markerPrefix}$${markerLabel}[:${macroExpansion}`
)


if (injection && injection.macroExpansion) {
continue
}
Expand All @@ -187,8 +190,6 @@ async function evalSnippetInjection(content, params, profile, log) {
)
}



if (injection && injection.macroExpansion) {
continue
}
Expand Down Expand Up @@ -231,10 +232,10 @@ async function evalSnippetInjection(content, params, profile, log) {

if (!_.isUndefined(FILE)) {
// console.dir(params)

// console.log(FILE)
// if (common.isLocalFilePath(FILE)) {

// FILE = path.posix.join(CURRENT_DIR, FILE)

// console.log("is local file")
Expand Down Expand Up @@ -311,11 +312,7 @@ async function evalSnippetInjection(content, params, profile, log) {
let evaluatedContent
let data = context.INPUT
do {
logger.info(
`${chalk.cyan(
`try evaluate nested snippet injections`
)}`
)
logger.info(`${chalk.cyan(`try evaluate nested snippet injections`)}`)
// console.dir({
// context,
// // "context.args.INPUT": context.args.INPUT,
Expand All @@ -329,20 +326,15 @@ async function evalSnippetInjection(content, params, profile, log) {
log
) // TODO: use common.profile and logger
// console.dir({ evaluatedContent })


data = evaluatedContent.data
// console.log(data)
// throw new Error(data)


// process.exit()
// context.INPUT = evaluatedContent.data
} while (evaluatedContent.hasSnippetInjection)




// console.log(context.INPUT)
// process.exit()

Expand Down Expand Up @@ -394,7 +386,7 @@ async function evalSnippetInjection(content, params, profile, log) {
// if (content.includes("February")){
// // console.dir(injection)
// throw new Error(content)
// }
// }

line_number = injection.lineNumber
}
Expand All @@ -407,7 +399,7 @@ async function evalSnippetInjection(content, params, profile, log) {
// if (output.includes("February")){
// // console.dir(injection)
// throw new Error(output)
// }
// }

// console.dir(output)
// process.exit()
Expand Down
6 changes: 5 additions & 1 deletion src/scan-blobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chalk = require("chalk")
const error = require("./error.js")
const common = require("./common.js")
const logger = require("./logger.js")
const colorize = require("json-colorizer")
const source_id = "scan-blobs"

module.exports = async function (files, profile, log) {
Expand Down Expand Up @@ -131,7 +132,7 @@ module.exports = async function (files, profile, log) {
// logger.info(`expansion = ${expansion}`)
lines[line_number] = line.replace(
openRegex,
() => `${profile.MARKER_PREFIX}!${label}<:${expansion}`
() => `${profile.MARKER_PREFIX}!${label}<:${expansion} `
)
logger.info(
`${chalk.cyan(
Expand Down Expand Up @@ -275,5 +276,8 @@ module.exports = async function (files, profile, log) {
)
}
}

// logger.info(colorize(focused_entity, {pretty: true}))
// process.exit()
}
}
8 changes: 2 additions & 6 deletions src/scan-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = async function (files, profile, log) {
// `${chalk.cyan(`define variable`)} ${chalk.green(
// name
// )} ${chalk.cyan(`as`)} ${chalk.green(JSON.stringify(value))} `
// )
// )
_.set(profile, name, value)
return true // not needed
},
Expand Down Expand Up @@ -92,11 +92,6 @@ module.exports = async function (files, profile, log) {
let params = await common.parseParams(rest)

logger.info(`${chalk.cyan(`defined macro`)} ${chalk.green(name)}`)
// console.dir({
// name,
// params,
// expansion,
// })

let LANGUAGE = _.get(params, "LANGUAGE", "ejs")
_.set(params, "LANGUAGE", LANGUAGE)
Expand All @@ -112,6 +107,7 @@ module.exports = async function (files, profile, log) {
custom: {},
}

// logger.info(colorize(new_snippet, { pretty: true }))
_.set(profile, ["snippets", name], new_snippet)

return true // not needed
Expand Down
2 changes: 1 addition & 1 deletion src/scan-snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = async function (files, profile, log) {
)
let rest = _.get(openTag, "[3]", "").trim()

console.dir(rest)
// console.dir(rest)
let params = await common.parseParams(rest)
// console.dir({params})
// process.exit()
Expand Down

0 comments on commit 978395e

Please sign in to comment.