Skip to content

Commit

Permalink
Revert "Merge pull request #383 from wttech/add-macro-support"
Browse files Browse the repository at this point in the history
This reverts commit c9de4d0, reversing
changes made to c71e06b.
  • Loading branch information
dprzybyl committed Sep 23, 2023
1 parent c9de4d0 commit 564b4a9
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 210 deletions.
31 changes: 2 additions & 29 deletions app/aem/core/src/main/antlr/ApmLang.g4
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ path
;

array
: ARRAY_BEGIN arrayValue (COMMA arrayValue)* ARRAY_END
: ARRAY_BEGIN arrayValue (',' arrayValue)* ARRAY_END
;

arrayValue
Expand All @@ -53,7 +53,7 @@ arrayValue
;

structure
: STRUCTURE_BEGIN structureEntry (COMMA structureEntry)* STRUCTURE_END
: STRUCTURE_BEGIN structureEntry (',' structureEntry)* STRUCTURE_END
;

structureEntry
Expand Down Expand Up @@ -116,8 +116,6 @@ command
| FOR_EACH IDENTIFIER IN argument body # ForEach
| DEFINE IDENTIFIER argument # DefineVariable
| REQUIRE IDENTIFIER # RequireVariable
| REGISTER_MACRO identifier variableList? body # RegisterMacro
| RUN_MACRO identifier argumentList? # RunMacro
| (ALLOW | DENY) argument ON? complexArguments # AllowDenyCommand
| commandName complexArguments? body? # GenericCommand
;
Expand Down Expand Up @@ -161,14 +159,6 @@ body
: BLOCK_BEGIN command+ BLOCK_END
;

variableList
: BRACKET_BEGIN IDENTIFIER (COMMA IDENTIFIER)* BRACKET_END
;

argumentList
: BRACKET_BEGIN argument (COMMA argument)* BRACKET_END
;

/*
* Lexer Rules
*/
Expand All @@ -186,15 +176,6 @@ STRUCTURE_BEGIN
STRUCTURE_END
: '}'
;
BRACKET_BEGIN
: '('
;
BRACKET_END
: ')'
;
COMMA
: ','
;
BLOCK_BEGIN
: 'begin'
| 'BEGIN'
Expand Down Expand Up @@ -223,14 +204,6 @@ DEFINE
: 'define'
| 'DEFINE'
;
REGISTER_MACRO
: 'register-macro'
| 'REGISTER-MACRO'
;
RUN_MACRO
: 'run-macro'
| 'RUN-MACRO'
;
REQUIRE
: 'require'
| 'REQUIRE'
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import com.cognifide.apm.core.grammar.argument.Arguments
import com.cognifide.apm.core.grammar.common.getIdentifier
import com.cognifide.apm.core.grammar.common.getPath
import com.cognifide.apm.core.grammar.executioncontext.ExecutionContext
import com.cognifide.apm.core.grammar.macro.Macro
import com.cognifide.apm.core.grammar.parsedscript.InvalidSyntaxException
import com.cognifide.apm.core.grammar.parsedscript.InvalidSyntaxMessageFactory
import com.cognifide.apm.core.grammar.utils.ImportScript
Expand Down Expand Up @@ -224,45 +223,6 @@ class ScriptRunner(
return Status.SUCCESS
}

override fun visitRegisterMacro(ctx: RegisterMacroContext): Status {
val macroName = getIdentifier(ctx.identifier())
val variableList = ctx.variableList()
?.IDENTIFIER()
?.map { it.toString() }
?: listOf()
val body = ctx.body()
executionContext.registerMacro(Macro(macroName, variableList, body))
return Status.SUCCESS
}

override fun visitRunMacro(ctx: RunMacroContext): Status {
val macroName = getIdentifier(ctx.identifier())
val macro = executionContext.fetchMacro(macroName)
if (macro == null) {
progress(ctx, Status.ERROR, "run-macro", "Macro \"$macroName\" is not registered")
} else {
val argumentList = ctx.argumentList()
?.argument()
?.map { executionContext.resolveArgument(it) }
?: listOf()
val values = macro.variableList
.zip(argumentList)
.toMap()
try {
executionContext.createLocalContext()
val valueStr = values.map { it.key + "=" + it.value }
.joinToString()
progress(ctx, Status.SUCCESS, "run-macro", "Begin: $valueStr")
values.forEach { (k, v) -> executionContext.setVariable(k, v) }
visit(macro.body)
progress(ctx, Status.SUCCESS, "run-macro", "End")
} finally {
executionContext.removeLocalContext()
}
}
return Status.SUCCESS
}

private fun readValues(ctx: ForEachContext): List<Map<String, ApmType>> {
val keys = listOf(ctx.IDENTIFIER().toString())
val values = when (val variableValue = executionContext.resolveArgument(ctx.argument())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,20 @@ import com.cognifide.apm.core.grammar.antlr.ApmLangParser.*
import com.cognifide.apm.core.grammar.argument.ArgumentResolver
import com.cognifide.apm.core.grammar.argument.Arguments
import com.cognifide.apm.core.grammar.common.StackWithRoot
import com.cognifide.apm.core.grammar.macro.Macro
import com.cognifide.apm.core.grammar.parsedscript.ParsedScript
import com.cognifide.apm.core.logger.Progress
import org.apache.commons.lang3.StringUtils
import org.apache.jackrabbit.api.security.user.Authorizable
import org.apache.sling.api.resource.ResourceResolver

class ExecutionContext private constructor(
private val scriptFinder: ScriptFinder,
private val resourceResolver: ResourceResolver,
val root: ParsedScript,
override val progress: Progress
) : ExternalExecutionContext {
private val scriptFinder: ScriptFinder,
private val resourceResolver: ResourceResolver,
val root: ParsedScript,
override val progress: Progress) : ExternalExecutionContext {

private val parsedScripts: MutableMap<String, ParsedScript> = mutableMapOf()
private var runScripts: StackWithRoot<RunScript> = StackWithRoot(RunScript(root))
private val registeredMacros: MutableMap<String, Macro> = mutableMapOf()

val currentRunScript: RunScript
get() = runScripts.peek()
Expand All @@ -59,12 +56,7 @@ class ExecutionContext private constructor(

companion object {
@JvmStatic
fun create(
scriptFinder: ScriptFinder,
resourceResolver: ResourceResolver,
script: Script,
progress: Progress
): ExecutionContext {
fun create(scriptFinder: ScriptFinder, resourceResolver: ResourceResolver, script: Script, progress: Progress): ExecutionContext {
return ExecutionContext(scriptFinder, resourceResolver, ParsedScript.create(script), progress)
}
}
Expand Down Expand Up @@ -124,7 +116,7 @@ class ExecutionContext private constructor(

private fun fetchScript(path: String): ParsedScript {
val script = scriptFinder.find(path, resourceResolver)
?: throw ScriptExecutionException("Script not found $path")
?: throw ScriptExecutionException("Script not found $path")
val parsedScript = ParsedScript.create(script)
registerScript(parsedScript)
return parsedScript
Expand All @@ -141,10 +133,4 @@ class ExecutionContext private constructor(
StringUtils.substringBeforeLast(runScripts.peek().path, "/") + "/" + path
}
}

fun registerMacro(macro: Macro) {
registeredMacros[macro.macroName] = macro
}

fun fetchMacro(macroName: String): Macro? = registeredMacros[macroName]
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,6 @@ class ScriptRunnerTest extends Specification {
"Executing command SHOW [[\"a\", \"b\"], [\"c\", \"d\"]]"]
}

def "run macro"() {
given:
Script script = createScript("/macro.apm")

when:
def result = scriptExecutor.execute(script, new ProgressImpl(""))

then:
def commands = result.entries
.collect { it.command }
.findAll { it.startsWith("Executing") }
commands == ["Executing command SHOW \"simpleMacro\"",
"Executing command SHOW \"parameterizedMacro1 param\"",
"Executing command SHOW \"parameterizedMacro2 param1 param2\"",
"Executing command SHOW \"parameterizedMacro1 ab\"",
"Executing command SHOW \"parameterizedMacro2 a b\"",
"Executing command SHOW \"parameterizedMacroOuter param\"",
"Executing command SHOW \"parameterizedMacroInner param 1\"",
"Executing command SHOW \"parameterizedMacroInner param 2\""]
}

def "run import"() {
given:
Script script = createScript("/import.apm")
Expand Down
49 changes: 0 additions & 49 deletions app/aem/core/src/test/resources/macro.apm

This file was deleted.

0 comments on commit 564b4a9

Please sign in to comment.