Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #34 from promlabs/bugfix/all
Browse files Browse the repository at this point in the history
fix parsing metric beginning by inf/nan
  • Loading branch information
juliusv authored May 3, 2021
2 parents 57d8bb1 + afdf29e commit 6f53dba
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 23 deletions.
31 changes: 11 additions & 20 deletions src/promql.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ SubqueryExpr {
}

UnaryExpr {
!mul UnaryOp Expr
!mul UnaryOp~signed Expr
}

UnaryOp {
Expand Down Expand Up @@ -239,15 +239,15 @@ MetricIdentifier {
}

StepInvariantExpr {
Expr At ( NumberLiteral | SignedNumber | AtModifierPreprocessors "(" ")" )
Expr At ( NumberLiteral | AtModifierPreprocessors "(" ")" )
}

AtModifierPreprocessors {
Start | End
}

SignedNumber{
("+" | "-" ) NumberLiteral
NumberLiteral {
("-"|"+")?~signed (number | inf | nan)
}

@skip { whitespace | LineComment }
Expand All @@ -256,13 +256,9 @@ SignedNumber{
whitespace { std.whitespace+ }
LineComment { "#" ![\n]* }

NumberLiteral {
("+" | "-")? (
number {
(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"] | "\\" _)* '"'? |
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -344,7 +333,9 @@ SignedNumber{
Without,
And,
Or,
Unless
Unless,
Start,
End
}

// FunctionIdentifier definitions
Expand Down
34 changes: 33 additions & 1 deletion src/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -45,6 +75,8 @@ const contextualKeywordTokens = {
and: And,
or: Or,
unless: Unless,
start: Start,
end: End,
}

export const extendIdentifier = (value, stack) => {
Expand Down
39 changes: 37 additions & 2 deletions test/expression.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -682,7 +696,7 @@ PromQL(
)
)
At,
SignedNumber(NumberLiteral)
NumberLiteral
)
)
)
Expand All @@ -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
Expand Down Expand Up @@ -736,6 +764,13 @@ PromQL(Expr(NumberLiteral))
==>
PromQL(Expr(NumberLiteral))

# Positive Inf.

+Inf

==>
PromQL(Expr(NumberLiteral))

# Lower-cased Inf.

inf
Expand Down

0 comments on commit 6f53dba

Please sign in to comment.