From 56609cdcf5723a51dfba0eccccf69a7290a96eaa Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Thu, 29 Apr 2021 02:20:17 +0200 Subject: [PATCH] fix parsing metric beginning by inf/nan Signed-off-by: Augustin Husson --- src/promql.grammar | 31 +++++++++++-------------------- src/tokens.js | 34 +++++++++++++++++++++++++++++++++- test/expression.txt | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 81 insertions(+), 23 deletions(-) diff --git a/src/promql.grammar b/src/promql.grammar index 9f6786f..1857d21 100644 --- a/src/promql.grammar +++ b/src/promql.grammar @@ -198,7 +198,7 @@ SubqueryExpr { } UnaryExpr { - !mul UnaryOp Expr + !mul UnaryOp~statement Expr } UnaryOp { @@ -239,15 +239,15 @@ MetricIdentifier { } StepInvariantExpr { - Expr At ( NumberLiteral | SignedNumber | AtModifierPreprocessors "(" ")" ) + Expr At ( NumberLiteral | AtModifierPreprocessors "(" ")" ) } AtModifierPreprocessors { Start | End } -SignedNumber{ - ("+" | "-" ) NumberLiteral +NumberLiteral { + ("-"|"+")?~statement (digit | inf | nan) } @skip { whitespace | LineComment } @@ -256,13 +256,9 @@ SignedNumber{ whitespace { std.whitespace+ } LineComment { "#" ![\n]* } - NumberLiteral { - ("+" | "-")? ( + digit { (std.digit+ ("." std.digit*)? | "." std.digit+) (("e" | "E") ("+" | "-")? std.digit+)? | - "0x" (std.digit | $[a-fA-F])+ | - $[nN]$[aA]$[nN] | - $[iI]$[nN]$[fF] - ) + "0x" (std.digit | $[a-fA-F])+ } StringLiteral { // TODO: This is for JS, make this work for PromQL. '"' (![\\\n"] | "\\" _)* '"'? | @@ -303,20 +299,13 @@ SignedNumber{ // Special Modifier At { "@" } - - // Modifier Preprocessors - Start { "start" } - End { "end" } - - // To parse signed numbers into a number literal and not a unary expression. - @precedence { NumberLiteral, "+", "-" } - // To parse NaN/Inf as a number literal. - @precedence { NumberLiteral, Identifier } } // Keywords @external specialize {Identifier} specializeIdentifier from "./tokens" { + inf, + nan, Bool, Ignoring, On, @@ -344,7 +333,9 @@ SignedNumber{ Without, And, Or, - Unless + Unless, + Start, + End } // FunctionIdentifier definitions diff --git a/src/tokens.js b/src/tokens.js index 14932af..7ad3e47 100644 --- a/src/tokens.js +++ b/src/tokens.js @@ -12,9 +12,39 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {Bool, Ignoring, On, GroupLeft, GroupRight, Offset, Avg, Bottomk, Count, CountValues, Group, Max, Min, Quantile, Stddev, Stdvar, Sum, Topk, By, Without, And, Or, Unless} from './parser.terms.js'; +import { + And, + Avg, + Bool, + Bottomk, + By, + Count, + CountValues, + End, + Group, + GroupLeft, + GroupRight, + Ignoring, + inf, + Max, + Min, + nan, + Offset, + On, + Or, + Quantile, + Start, + Stddev, + Stdvar, + Sum, + Topk, + Unless, + Without, +} from './parser.terms.js'; const keywordTokens = { + inf: inf, + nan: nan, bool: Bool, ignoring: Ignoring, on: On, @@ -45,6 +75,8 @@ const contextualKeywordTokens = { and: And, or: Or, unless: Unless, + start: Start, + end: End, } export const extendIdentifier = (value, stack) => { diff --git a/test/expression.txt b/test/expression.txt index 0210882..5041bb8 100644 --- a/test/expression.txt +++ b/test/expression.txt @@ -518,6 +518,20 @@ PromQL( ) ) +# Metric start + +start + +==> +PromQL(Expr(VectorSelector(MetricIdentifier(Identifier)))) + +# Metric end + +end + +==> +PromQL(Expr(VectorSelector(MetricIdentifier(Identifier)))) + # Simple At start foo @ start() @@ -682,7 +696,7 @@ PromQL( ) ) At, - SignedNumber(NumberLiteral) + NumberLiteral ) ) ) @@ -703,11 +717,25 @@ PromQL( ) ) At, - SignedNumber(NumberLiteral) + NumberLiteral ) ) ) +# Metric prefixed by Inf + +infra + +==> +PromQL(Expr(VectorSelector(MetricIdentifier(Identifier)))) + +# Metric prefixed by Nan + +nananere + +==> +PromQL(Expr(VectorSelector(MetricIdentifier(Identifier)))) + # Mixed-case NaN. NaN @@ -736,6 +764,13 @@ PromQL(Expr(NumberLiteral)) ==> PromQL(Expr(NumberLiteral)) +# Positive Inf. + ++Inf + +==> +PromQL(Expr(NumberLiteral)) + # Lower-cased Inf. inf