def get_key_value_pattern(self, ctx: ParserRuleContext): """ returns the key/value pattern of the context It is assumed, that the context has 5 children, that represent the k/v: '0' left parenthesis '1' key '2' equals '3' value '4' right parenthesis :param ctx: the ParserRuleContext :return formatted key value string :rtype str """ _count = ctx.getChildCount() # count==5 indicates Key/Value context if _count == 5: _key = str(ctx.getChild(1)) if not self._ignore_keyword_case: if self._uppercase_keywords: _key = _key.upper() else: _key = _key.lower() _value = str(ctx.getChild(3)) if not self._ignore_value_case: if self._uppercase_values: _value = _value.upper() else: _value = _value.lower() _ret_string = str(ctx.getChild(0)) + _key + str( ctx.getChild(2)) + _value + str(ctx.getChild(4)) return _ret_string raise Exception("Not a key value ctx: " + ctx.getText())
def get_key_value_pattern(self, ctx: ParserRuleContext): """ returns the key/value pattern of the context It is assumed, that the context has 5 children, that represent the k/v: '0' left parenthesis '1' key '2' equals '3' value '4' right parenthesis :param ctx: the ParserRuleContext :return formatted key value string :rtype str """ _count = ctx.getChildCount() # count==5 indicates Key/Value context if _count == 5: _key = str(ctx.getChild(1)) if not self._ignore_keyword_case: if self._uppercase_keywords: _key = _key.upper() else: _key = _key.lower() _value = str(ctx.getChild(3)) if not self._ignore_value_case: if self._uppercase_values: _value = _value.upper() else: _value = _value.lower() _ret_string = str(ctx.getChild(0)) + _key + str(ctx.getChild(2)) + _value + str(ctx.getChild(4)) return _ret_string raise Exception("Not a key value ctx: " + ctx.getText())
def _visitBinaryExpression(self, ctx: ParserRuleContext): """ All binary expressions fit this patten so we refactored the code """ if ctx.getChildCount() == 1: return self.visit(ctx.getChild(0)) else: return BinaryExpression( op=BinaryOperator[ctx.getChild(1).getText()], lhs=self.visit(ctx.getChild(0)), rhs=self.visit(ctx.getChild(2)), )
def get_value_pattern(self, ctx: ParserRuleContext): """ :param ctx: the ParserRuleContext :return formatted key closing string :rtype str """ _count = ctx.getChildCount() if _count != 1: raise Exception("Not a value ctx: " + ctx.getText()) _key = str(ctx.getText()) if not self._ignore_value_case: if self._uppercase_values: _key = _key.upper() else: _key = _key.lower() _ret_string = _key return _ret_string
def get_key_closing_pattern(self, ctx: ParserRuleContext): """ returns the key closing pattern :param ctx: the ParserRuleContext :return formatted key closing string :rtype str """ _count = ctx.getChildCount() if _count < 5: raise Exception("Not a key closing ctx") _key = str(ctx.getChild(_count - 1)) if not self._ignore_keyword_case: if self._uppercase_keywords: _key = _key.upper() else: _key = _key.lower() _ret_string = _key return _ret_string
def get_key_opening_pattern(self, ctx: ParserRuleContext): """ returns the key opening pattern, like (address= :param ctx: the ParserRuleContext :return formatted key opening string :rtype str """ _count = ctx.getChildCount() if _count < 5: raise Exception("Not a key opening ctx: " + ctx.getText()) _key = str(ctx.getChild(1)) if not self._ignore_keyword_case: if self._uppercase_keywords: _key = _key.upper() else: _key = _key.lower() _ret_string = str(ctx.getChild(0)) + _key + str(ctx.getChild(2)) return _ret_string
def _createPrimitive( self, ctx: ParserRuleContext ) -> Optional[Union[QuotedString, int, bool, float, str]]: ret: Optional[Union[int, bool, float, str]] first_idx = 0 last_idx = ctx.getChildCount() # skip first if whitespace if self.is_ws(ctx.getChild(0)): if last_idx == 1: # Only whitespaces => this is not allowed. raise HydraException( "Trying to parse a primitive that is all whitespaces" ) first_idx = 1 if self.is_ws(ctx.getChild(-1)): last_idx = last_idx - 1 num = last_idx - first_idx if num > 1: # Concatenate, while un-escaping as needed. tokens = [] for i, n in enumerate(ctx.getChildren()): if n.symbol.type == OverrideLexer.WS and ( i < first_idx or i >= last_idx ): # Skip leading / trailing whitespaces. continue tokens.append( n.symbol.text[1::2] # un-escape by skipping every other char if n.symbol.type == OverrideLexer.ESC else n.symbol.text ) ret = "".join(tokens) else: node = ctx.getChild(first_idx) if node.symbol.type == OverrideLexer.QUOTED_VALUE: text = node.getText() qc = text[0] text = text[1:-1] if qc == "'": quote = Quote.single text = text.replace("\\'", "'") elif qc == '"': quote = Quote.double text = text.replace('\\"', '"') else: assert False return QuotedString(text=text, quote=quote) elif node.symbol.type in (OverrideLexer.ID, OverrideLexer.INTERPOLATION): ret = node.symbol.text elif node.symbol.type == OverrideLexer.INT: ret = int(node.symbol.text) elif node.symbol.type == OverrideLexer.FLOAT: ret = float(node.symbol.text) elif node.symbol.type == OverrideLexer.NULL: ret = None elif node.symbol.type == OverrideLexer.BOOL: text = node.getText().lower() if text == "true": ret = True elif text == "false": ret = False else: assert False elif node.symbol.type == OverrideLexer.ESC: ret = node.symbol.text[1::2] else: return node.getText() # type: ignore return ret