ATbool PT_isTreeFlatLexical(PT_Tree tree) { static PT_Symbol allCharsSymbol; allCharsSymbol = makeSymbolAllChars(); if (PT_isTreeAppl(tree)) { PT_Production listProd = PT_getTreeProd(tree); PT_Symbol listSymbol = PT_getProductionRhs(listProd); return PT_isEqualSymbol(listSymbol, allCharsSymbol); } return ATfalse; }
static PT_Args flattenListAgain(PT_Production prod, PT_Args elems) { PT_Args newElems = PT_makeArgsEmpty(); for ( ; !PT_isArgsEmpty(elems); elems = PT_getArgsTail(elems)) { PT_Tree arg = PT_getArgsHead(elems); if (PT_hasTreeProd(arg)) { ATbool nested = ATfalse; PT_Production argProd = PT_getTreeProd(arg); if (PT_isEqualProduction(prod, argProd)) { nested = ATtrue; } else if (PT_isProductionList(argProd)) { PT_Symbol rhs = PT_getProductionRhs(prod); PT_Symbol rhsArg = PT_getProductionRhs(argProd); if (PT_isSymbolCf(rhs) || PT_isSymbolLex(rhs)) { rhs = PT_getSymbolSymbol(rhs); } if (PT_isSymbolCf(rhsArg) || PT_isSymbolLex(rhsArg)) { rhsArg = PT_getSymbolSymbol(rhsArg); } if (PT_isSymbolIterStar(rhs) && PT_isSymbolIterPlus(rhsArg) && PT_isEqualSymbol(PT_getSymbolSymbol(rhs), PT_getSymbolSymbol(rhsArg))) { nested = ATtrue; } } if (nested) { PT_Args nestedElems = PT_getTreeArgs(arg); newElems = PT_concatArgs(PT_reverseArgs(nestedElems), newElems); continue; } } newElems = PT_makeArgsMany(arg, newElems); } return PT_reverseArgs(newElems); }
ATbool PT_isLexicalInjectionProd(PT_Production prod) { /* This implements: "prod([lex(<term>)],cf(<term>),<term>)" */ if (PT_isProductionDefault(prod)) { PT_Symbols lhs = PT_getProductionLhs(prod); PT_Symbol rhs = PT_getProductionRhs(prod); if (PT_isSymbolCf(rhs) && PT_hasSymbolsHead(lhs)) { PT_Symbol rhsNestedSymbol = PT_getSymbolSymbol(rhs); PT_Symbol lhssym = PT_getSymbolsHead(lhs); PT_Symbols tail = PT_getSymbolsTail(lhs); if (PT_isSymbolsEmpty(tail) && PT_isSymbolLex(lhssym)) { PT_Symbol lhsNestedSymbol = PT_getSymbolSymbol(lhssym); return PT_isEqualSymbol(rhsNestedSymbol, lhsNestedSymbol); } } return ATfalse; } return ATfalse; }
static PT_Symbol renameInSymbol(PT_Symbol symbol, PT_Symbol formalParam, PT_Symbol actualParam) { if (PT_isEqualSymbol(symbol, formalParam)) { return actualParam; } if (PT_hasSymbolSymbols(symbol)) { PT_Symbols args = PT_getSymbolSymbols(symbol); PT_Symbols newArgs = renameInSymbols(args, formalParam, actualParam); symbol = PT_setSymbolSymbols(symbol, newArgs); } if (PT_hasSymbolParameters(symbol)) { PT_Symbols args = PT_getSymbolParameters(symbol); PT_Symbols newArgs = renameInSymbols(args, formalParam, actualParam); symbol = PT_setSymbolParameters(symbol, newArgs); } if (PT_hasSymbolSymbol(symbol)) { PT_Symbol arg = PT_getSymbolSymbol(symbol); PT_Symbol newArg = renameInSymbol(arg, formalParam, actualParam); symbol = PT_setSymbolSymbol(symbol, newArg); } if (PT_hasSymbolLhs(symbol)) { PT_Symbol arg = PT_getSymbolLhs(symbol); PT_Symbol newArg = renameInSymbol(arg, formalParam, actualParam); symbol = PT_setSymbolLhs(symbol, newArg); } if (PT_hasSymbolRhs(symbol)) { PT_Symbol arg = PT_getSymbolRhs(symbol); PT_Symbol newArg = renameInSymbol(arg, formalParam, actualParam); symbol = PT_setSymbolRhs(symbol, newArg); } if (PT_hasSymbolHead(symbol)) { PT_Symbol arg = PT_getSymbolHead(symbol); PT_Symbol newArg = renameInSymbol(arg, formalParam, actualParam); symbol = PT_setSymbolHead(symbol, newArg); } if (PT_hasSymbolRest(symbol)) { PT_Symbols args = PT_getSymbolRest(symbol); PT_Symbols newArgs = renameInSymbols(args, formalParam, actualParam); symbol = PT_setSymbolRest(symbol, newArgs); } if (PT_hasSymbolSeparator(symbol)) { PT_Symbol arg = PT_getSymbolSeparator(symbol); PT_Symbol newArg = renameInSymbol(arg, formalParam, actualParam); symbol = PT_setSymbolSeparator(symbol, newArg); } return symbol; }