def validate_ctx(py_ctx:ParserRuleContext, cpp_ctx:ParserRuleContext): assert type(py_ctx) == type(cpp_ctx) pc = list(py_ctx.getChildren()) cc = list(cpp_ctx.getChildren()) assert len(pc) == len(cc) # Validate children for i in range(len(pc)): if isinstance(pc[i], TerminalNodeImpl): validate_tnode(pc[i], cc[i]) elif isinstance(pc[i], ParserRuleContext): validate_ctx(pc[i], cc[i]) else: raise RuntimeError assert pc[i].parentCtx is py_ctx assert cc[i].parentCtx is cpp_ctx # Validate start/stop markers if (py_ctx.start is not None and py_ctx.stop is not None and py_ctx.start.type != Token.EOF and py_ctx.stop.type != Token.EOF and py_ctx.start.tokenIndex <= py_ctx.stop.tokenIndex): validate_common_token(py_ctx.start, cpp_ctx.start) validate_common_token(py_ctx.stop, cpp_ctx.stop) # Validate labels for label in get_rule_labels(py_ctx): if label.startswith("_"): continue py_label = getattr(py_ctx, label) cpp_label = getattr(cpp_ctx, label) assert type(py_label) == type(cpp_label) if isinstance(py_label, CommonToken): validate_common_token(py_label, cpp_label) elif isinstance(py_label, ParserRuleContext): validate_ctx(py_label, cpp_label) # Validate other ctx members assert py_ctx.invokingState == cpp_ctx.invokingState
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