|
| 1 | +// Copyright 2018 Datalust Pty Ltd |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using Seq.Api.Model.Signals; |
| 16 | +using Superpower; |
| 17 | +using Superpower.Parsers; |
| 18 | + |
| 19 | +namespace SeqCli.Signals; |
| 20 | + |
| 21 | +static class SignalExpressionParser |
| 22 | +{ |
| 23 | + // intersect: `,` |
| 24 | + static readonly TokenListParser<SignalExpressionToken, SignalExpressionKind> Intersect = |
| 25 | + Token.EqualTo(SignalExpressionToken.Comma).Value(SignalExpressionKind.Intersection); |
| 26 | + |
| 27 | + // union: `~` |
| 28 | + static readonly TokenListParser<SignalExpressionToken, SignalExpressionKind> Union = |
| 29 | + Token.EqualTo(SignalExpressionToken.Tilde).Value(SignalExpressionKind.Union); |
| 30 | + |
| 31 | + // operator: `intersect` | `union` |
| 32 | + static readonly TokenListParser<SignalExpressionToken, SignalExpressionKind> Operator = Intersect.Or(Union); |
| 33 | + |
| 34 | + // concrete: `signalid` |
| 35 | + static readonly TokenListParser<SignalExpressionToken, SignalExpressionPart> SignalId = |
| 36 | + from id in Token.EqualTo(SignalExpressionToken.Id) |
| 37 | + select SignalExpressionPart.Signal(id.ToStringValue()); |
| 38 | + |
| 39 | + // brackets: `(` => `operation` => `)` |
| 40 | + static readonly TokenListParser<SignalExpressionToken, SignalExpressionPart> Brackets = |
| 41 | + from lparen in Token.EqualTo(SignalExpressionToken.LParen) |
| 42 | + from expr in Parse.Ref(() => Operation!) |
| 43 | + from rparen in Token.EqualTo(SignalExpressionToken.RParen) |
| 44 | + select expr; |
| 45 | + |
| 46 | + // subexpr: `brackets` | `id` |
| 47 | + static readonly TokenListParser<SignalExpressionToken, SignalExpressionPart> SubExpression = |
| 48 | + Brackets.Or(SignalId).Named("expression"); |
| 49 | + |
| 50 | + // operation: ( `subexpr` => `operator` => `subexpr` )* |
| 51 | + static readonly TokenListParser<SignalExpressionToken, SignalExpressionPart> Operation = |
| 52 | + Parse.Chain(Operator, SubExpression, (kind, lhs, rhs) => new SignalExpressionPart |
| 53 | + { |
| 54 | + Kind = kind, |
| 55 | + Left = lhs, |
| 56 | + Right = rhs |
| 57 | + }); |
| 58 | + |
| 59 | + static readonly TokenListParser<SignalExpressionToken, SignalExpressionPart> SignalExpression = Operation.AtEnd(); |
| 60 | + |
| 61 | + static readonly SignalExpressionTokenizer Tokenizer = new(); |
| 62 | + |
| 63 | + public static SignalExpressionPart ParseExpression(string input) |
| 64 | + { |
| 65 | + var tokens = Tokenizer.Tokenize(input); |
| 66 | + return SignalExpression.Parse(tokens); |
| 67 | + } |
| 68 | +} |
0 commit comments