def exitPlusOp(self, ctx: FoxySheepParser.PlusOpContext): """PlusOp[expr1,expr2] We have to treat PlusOp special, because we have to keep the operators intact, and only plus and minus (not PlusMinus or MinusPlus) are flat. The situation is complicated by the fact that Mathematica parses "a-b" as "Plus[a, Times[-1, b]]". We Rewrite the parse tree, inserting the Times context and changing BINARYMINUS to BINARYPLUS.""" # If the op isn't Plus or Minus, nothing to do. if ctx.BINARYMINUS() is None and ctx.BINARYPLUS() is None: return # Since ANTLR4 parses this operator as left associative, we only # need to check the left hand side expr. rhs = ctx.getChild(2) # If the operator of the PlusOp is BINARYMINUS, we rewrite the tree as # "Plus[lhs, Times[-1, rhs]]". Note that if rhs is TIMES, we have to # keep that TIMES flat. if ctx.BINARYMINUS() is not None: # Construct Times, or obtain it from the rhs. times = None if isinstance(rhs, FoxySheepParser.TimesContext): times = rhs else: # If rhs is already a times, keep it flat. times = FoxySheepParser.TimesContext( None, FoxySheepParser.ExprContext(None)) ctx.children.remove(rhs) adopt(ctx, times) adopt(times, rhs) # Add "-1" as the first child of Times. addChild(times, makeNumber(times, -1), 0) # Finally, we have to change operator to BINARYPLUS. plustoken = CommonToken(type=FoxySheepParser.BINARYPLUS) plustoken.text = '+' plus = TerminalNodeImpl(plustoken) # Replace minus token with plus. ctx.children[1] = plus plus.parentCtx = ctx # Flatten flatten(ctx)
def visitTerminal(self, ctx: TerminalNodeImpl): if self.fn_nest_cnt == 0: #Tokens under FUNCTION calls are not to be handled by this method tok_txt = ctx.getText() if tok_txt.upper() in ['__LOAD__', '__BLOCK__']: tok_txt = tok_txt.strip('_') hdn_tok = self.getLeftHiddenToken(ctx) if tok_txt == '<EOF>': pass else: self.out_sql += hdn_tok + tok_txt self.last_visited_token = tok_txt
def visitTerminal(self, ctx:TerminalNodeImpl): tok_txt = ctx.getText() if tok_txt.upper() in ['__LOAD__','__BLOCK__']: tok_txt = tok_txt.strip('_') hdn_tok = self.getLeftHiddenToken(ctx) self.out_script += (hdn_tok + tok_txt) self.last_token = tok_txt if tok_txt.upper() in ['BEGIN', 'TRY', 'CATCH', 'TRANSACTION'] or (tok_txt.upper() == 'END' and not(isinstance(ctx.parentCtx, FullTSqlAntlrParser.Case_expressionContext))): self.out_script += ';' self.last_token = ';' self.out_script = re.sub(r'(BEGIN|END)\s*;(\s+)(TRY|CATCH)\s*;',r'\1\2\3;', self.out_script, flags=re.S|re.I) self.out_script = re.sub(r'(BEGIN|END|COMMIT|ROLLBACK)\s*;?(\s+TRANSACTION)\s*;(\s+\w+)\s*;?',r'\1\2\3;', self.out_script, flags=re.S|re.I)
def enterTernary(self, ctx): opRefAnd1 = self.getOpRef('and') opRefAnd2 = self.getOpRef('and') opRefOr = self.getOpRef('or') self.env[opRefAnd1] = [] self.env[opRefAnd2] = [] self.env[opRefOr] = [opRefAnd1, opRefAnd2] self.ctxOp = TerminalNodeImpl('->') iAtom = ctx.expression()[0].predicate().CON().getText() tAtom = ctx.expression()[1].predicate().CON().getText() eAtom = ctx.expression()[2].predicate().CON().getText() self.createBinaryStructure((iAtom, tAtom), '->', ('AtomContext', 'AtomContext'), opRefAnd1) self.createBinaryStructure(('~' + iAtom, eAtom), '->', ('AtomContext', 'AtomContext'), opRefAnd2) self.mergeNeg(opRefAnd1) self.mergeNeg(opRefAnd2) self.ctxOp = None
def visitTerminal(self, ctx:TerminalNodeImpl): if self.fn_nest_cnt == 0 and \ self.intv_nest_cnt == 0 and \ self.cnct_nest_cnt == 0 and \ not(self.fld_mode_cast) and \ not(self.case_spf) and \ not(self.qlfy_expr): tok_txt = ctx.getText() hdn_tok = self.getLeftHiddenToken(ctx) if tok_txt.upper() == 'SELECT' and self.qlfy_scope_stat[self.qlfy_scope]: tok_txt = '[' + str(self.qlfy_scope) + '.S1]' + hdn_tok + tok_txt + ' [' + str(self.qlfy_scope) + '.S2]' self.out_sql += hdn_tok + tok_txt elif isinstance(ctx.parentCtx, TDantlrParser.Column_nameContext) and not(self.alias_order_by) and tok_txt in self.alias_scope_txt[self.alias_scope].keys(): self.out_sql += hdn_tok + self.alias_scope_txt[self.alias_scope][tok_txt] else: if tok_txt == '<EOF>': self.out_sql += hdn_tok + ';' else: self.out_sql += hdn_tok + tok_txt
def addTokenNode(self, token: Token): node = TerminalNodeImpl(token) self.addChild(node) node.parentCtx = self return node
def enterBinary(self, ctx): self.lIVars = [] self.rIVars = [] lAtom, rAtom = self.getAtoms(ctx) setParent = False oprands = [False, False, False, False] oprands = self.setOperandsTypes(ctx, oprands) # Simply for readability notAtomLeft = oprands[0] notAtomRight = oprands[1] literalLeft = oprands[2] literalRight = oprands[3] if notAtomLeft or notAtomRight: setParent = True # Basically, if I am not considering quantified formulas if not self.currParent in self.QOpRef: opRef = self.getOpRefinOp(ctx.op) self.setNegOpRef(ctx, opRef) if not self.currParent: self.toplevelnodes.add(opRef) else: self.negateOpRef(opRef) self.negateOpRefSecChild(opRef) if self.prevFormula: self.opRefStack.append((opRef, self.prevFormula)) self.prevFormula = "" self.currParent = opRef self.env[opRef] = [] else: auxOpRefs = [] if not self.env[self.currParent] and self.groundVars: qRng = self.QRange[-1] self.groundVars.pop() for i in range(int(qRng[0]), int(qRng[1]) + 1): opRef = self.getOpRefinOp(ctx.op) auxOpRefs.append(opRef) self.env[self.currParent].append(opRef) self.QCurrParent = auxOpRefs elif self.QCurrParent and self.groundVars: qRng = self.QRange[-1] # Ground the last var in iVar self.groundVars.pop() for opRef in self.QCurrParent: if not self.env[opRef]: self.env[opRef] = [] for i in range(int(qRng[0]), int(qRng[1]) + 1): auxOpRef = self.getOpRefinOp(ctx.op) auxOpRefs.append(auxOpRef) self.env[opRef].append(auxOpRef) # All the variables were already grounded elif self.QCurrParent: for opRef in self.QCurrParent: if not self.env[opRef]: self.env[opRef] = [] auxOpRef = self.getOpRefinOp(ctx.op) auxOpRefs.append(auxOpRef) self.env[opRef].append(auxOpRef) self.setOpRefStack(ctx, auxOpRefs, oprands, lAtom, rAtom) if literalLeft or literalRight: if not setParent: if not self.currParent in self.QOpRef: opRef = self.getOpRefinOp(ctx.op) if not self.currParent: self.toplevelnodes.add(opRef) self.currParent = opRef else: self.negateOpRef(opRef) if self.secChild: self.env[self.currChild].append(opRef) self.env[self.secChild].append(opRef) self.secChild = '' else: self.env[self.currParent].append(opRef) if self.prevFormula: self.opRefStack.append((opRef, self.prevFormula)) self.prevFormula = "" else: auxOpRefs = [] if not self.env[self.currParent] and self.groundVars: qRng = self.QRange[-1] self.groundVars.pop() for i in range(int(qRng[0]), int(qRng[1]) + 1): opRef = self.getOpRefinOp(ctx.op) auxOpRefs.append(opRef) self.env[self.currParent].append(opRef) elif self.QCurrParent and self.groundVars: # Ground the last var in iVar self.groundVars.pop() qRng = self.QRange[-1] for opRef in self.QCurrParent: if not self.env[opRef]: self.env[opRef] = [] for i in range(int(qRng[0]), int(qRng[1]) + 1): auxOpRef = self.getOpRefinOp(ctx.op) auxOpRefs.append(auxOpRef) self.env[opRef].append(auxOpRef) elif self.QCurrParent: for opRef in self.QCurrParent: if not self.env[opRef]: self.env[opRef] = [] auxOpRef = self.getOpRefinOp(ctx.op) auxOpRefs.append(auxOpRef) self.env[opRef].append(auxOpRef) self.setOpRefStack(ctx, auxOpRefs, oprands, lAtom, rAtom) if ctx.BAR(): if self.currParent in self.QOpRef: if self.QCurrParent and (self.lIVars or self.rIVars): self.createBinaryQuantStructure( lAtom, rAtom, ctx.BAR().getText(), ctx, auxOpRefs) elif self.lIVars or self.rIVars: self.QCurrParent = [self.currParent] self.createBinaryQuantStructure( lAtom, rAtom, andSym, ctx, auxOpRefs) elif self.currParent == self.QOpRef[-1]: for opRef in self.env[self.currParent]: self.createBinaryStructure( (lAtom, rAtom), ctx.BAR().getText(), (type(ctx.left).__name__, type( ctx.right).__name__), opRef) else: self.createBinaryStructure( (lAtom, rAtom), ctx.BAR().getText(), (type(ctx.left).__name__, type(ctx.right).__name__), opRef) elif ctx.AND() or ctx.op.text == '^': if ctx.AND(): andSym = ctx.AND().getText() else: andSym = ctx.op.text if self.currParent in self.QOpRef: if self.QCurrParent and (self.lIVars or self.rIVars): self.createBinaryQuantStructure( lAtom, rAtom, andSym, ctx, auxOpRefs) elif self.lIVars or self.rIVars: self.QCurrParent = [self.currParent] self.createBinaryQuantStructure( lAtom, rAtom, andSym, ctx, auxOpRefs) elif self.currParent == self.QOpRef[-1]: for opRef in self.env[self.currParent]: self.createBinaryStructure( (lAtom, rAtom), andSym, (type(ctx.left).__name__, type( ctx.right).__name__), opRef) else: self.createBinaryStructure( (lAtom, rAtom), andSym, (type(ctx.left).__name__, type(ctx.right).__name__), opRef) elif ctx.IF(): if literalRight: self.RHS = True self.ctxOp = TerminalNodeImpl('<-') self.createBinaryStructure( (lAtom, rAtom), ctx.IF().getText(), (type(ctx.left).__name__, type(ctx.right).__name__), opRef) elif ctx.IFF(): opRefOr1 = self.getOpRef('or') opRefOr2 = self.getOpRef('or') self.env[opRef] = [opRefOr1, opRefOr2] self.env[opRefOr1] = [] self.env[opRefOr2] = [] if not rAtom: self.RHS = True if not lAtom: self.LHS = True self.ctxOp = TerminalNodeImpl('->') self.createBinaryStructure( (lAtom, rAtom), ctx.IFF().getText(), (type(ctx.left).__name__, type(ctx.right).__name__), opRefOr1) if (not type(ctx.left).__name__ == 'AtomContext' or not type(ctx.right).__name__ == 'AtomContext'): self.secChild = opRefOr1 self.ctxOp = TerminalNodeImpl('<-') self.createBinaryStructure( (lAtom, rAtom), ctx.IFF().getText(), (type(ctx.left).__name__, type(ctx.right).__name__), opRefOr2) elif ctx.THEN(): if literalLeft: self.LHS = True self.ctxOp = TerminalNodeImpl('->') self.createBinaryStructure( (lAtom, rAtom), ctx.THEN().getText(), (type(ctx.left).__name__, type(ctx.right).__name__), opRef)
def addTokenNode(self, token): node = TerminalNodeImpl(token) self.addChild(node) node.parentCtx = self return node