I have the following custom expression structure I am parsing:


I already was able to match the inner structure so that I can just output the value x, like so:

parser[Theorema`Language`VAR$[a_]] := 
    Module[{varName = SymbolName[Unevaluated[a]]},
        "\\VarTM{" <> StringReplace[varName, {"Theorema`Knowledge`VAR$" -> "", "$TM" -> ""}] <> "}"

(Happy to hear if anyone has a more elegant way to achieve this!)

But in the previous parsing example, the structure is different from the predicate structure Theorema`Knowledge`Q$TM[] because there is no outside structure I can reference the inner one in terms of.

I want to achieve something like (this does not work):

parser[Theorema`Knowledge`symbol_ /; StringEndsQ[SymbolName[Unevaluated[Theorema`Knowledge`symbol]], "$TM"][a___]] := "Test2! "

Where my stand-in string take on the value of symbol, so "Q" in the example, meant to be generic predicate of some sort. Does anyone have an idea for how to do this kind of partial/intra-matching, with a wild card for my predicate name (maybe longer than just one letter, also)?


1 Answer 1


You can use Context to match only the symbols in the desired context.


parser[sym_[a___] /; Context[sym] === "Theorema`Knowledge`" && 
  StringStartsQ[SymbolName[Unevaluated[sym]], "Q"]] := "Hit!"
parser[___] := "Miss"

expr = Theorema`Knowledge`Q$TM[x];
(* Hit! *)

expr = Theorema`Knowledge`wrongSymbol[x];
(* Miss *)

expr = WrongContext`Q$TM[x];
(* Miss *)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.