Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,25 @@ export namespace PromQLBuilder {
};
};

/**
* Create a string literal.
*
* @param valueUnquoted - The unquoted string value (e.g., 'hello' not '"hello"')
* @param rawValue - Optional raw value as it appeared in source (used by parser).
* If not provided, the printer will handle quoting/escaping.
* @param fromParser - Optional parser fields
*/
export const string = (
value: string,
valueUnquoted: string,
rawValue?: string,
fromParser?: Partial<AstNodeParserFields>
): PromQLStringLiteral => {
return {
dialect: 'promql',
type: 'literal',
literalType: 'string',
name: value,
value,
name: rawValue ?? valueUnquoted,
value: rawValue ?? valueUnquoted,
valueUnquoted,
...PromQLBuilder.parserFields(fromParser),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,16 +596,17 @@ export class PromQLCstToAstConverter {
return this.fromStringToken(token.symbol);
}

return PromQLBuilder.expression.literal.string(ctx.getText(), '', this.getParserFields(ctx));
const text = ctx.getText();
return PromQLBuilder.expression.literal.string('', text, this.getParserFields(ctx));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for the value unquoted to be empty string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because PromQLBuilder can construct the AST node from two patterns: (1) parsed from source text; (2) created programmatically without the source text. In the first case the second argument text is passed and the "unquoted" version is figured out by the builder. In the second case, the actual string (the unquoted version) is passed and the pretty-printer later figures out how to quote it, if at all.

}

private fromStringToken(token: antlr.Token): ast.PromQLStringLiteral {
const text = token.text ?? '';
const valueUnquoted = this.unquoteString(text);

return PromQLBuilder.expression.literal.string(
text,
valueUnquoted,
text,
this.createParserFieldsFromToken(token)
);
}
Expand Down
Loading