Skip to content

Commit 9518ede

Browse files
committed
Merge branch 'develop'
2 parents ad29ca2 + 54c85cf commit 9518ede

19 files changed

+1816
-2116
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to **antlr-calculator** are documented here.
44

5+
## v2.1.0:
6+
- The calculator now supports trailing comments in a formula, separated by a semicolon `;` at the end of the actual formula input. For example, `1 + 2; Hello World!` now just evaluates `1 + 2` and ignores everything after the semicolon `;`
7+
- Add support for substitutions in formulas
8+
59
## v2.0.4:
610
- The internal check for null or empty formulas was changed for better compatibility with Node
711

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# antlr-calculator
22

3-
[![Build Status](https://jenkins.dangl.me/buildStatus/icon?job=antlr-calculator)](https://jenkins.dangl.me/buildStatus/icon?job=antlr-calculator)
3+
[![Build Status](https://jenkins.dangl.me/buildStatus/icon?job=GeorgDangl%2Fantlr-calculator%2Fdevelop)](https://jenkins.dangl.me/job/GeorgDangl/job/antlr-calculator/job/develop/)
44
[![npm](https://img.shields.io/npm/v/antlr-calculator.svg)](https://www.npmjs.com/package/antlr-calculator)
55

66
This calculator is using the [ANTLR4 TypeScript target](https://github.com/tunnelvisionlabs/antlr4ts)
@@ -17,6 +17,8 @@ Whenever a calculation is performed, a `CalculationResult` is returned with the
1717

1818
[You can check out the live demo here!](https://antlr-calculator.dangl.me)
1919

20+
You can find the .NET version here: https://github.com/GeorgDangl/Dangl.Calculator
21+
2022
## Installation
2123

2224
Clone this repository or just go with `npm install antlr-calculator`.
@@ -118,6 +120,36 @@ Comments in Formulas are supported by encapsulating them either in `/*...*/`, `'
118120

119121
`4"Length"*3"Width"` resolves to `12`
120122

123+
## Substitutions
124+
125+
The calculator can be called with an overload that accepts a callback function for substitution values. For example, take the following formula:
126+
`1,2*#Z4+3`
127+
Here, `#Z4` is a _substitution_, which is a placeholder that can be externally supplied. Let's say you want to resolve `#Z4` to the value three, you could make this simple call:
128+
129+
```typescript
130+
const formula = "1,2*#Z4+3";
131+
const result = Calculator.calculate(formula, substitution =>
132+
{
133+
if (substitution === "#Z4")
134+
{
135+
return 3;
136+
}
137+
138+
return null;
139+
});
140+
```
141+
142+
The callback is in the form of a `(substitution: string) => number`, and it will be called for every substitution found in the formula. Multiple substitutions are supported. If duplicates in substitutions are present, the calculator will request each one individually. If a substitution resolves to `null`, the formula is considered invalid.
143+
144+
Substitutions must always start with the `#` character and can then have the following characters: `[a-z] | [A-Z] | [äÄöÖüÜ] | [0-9]`
145+
146+
## Trailing comments
147+
148+
Formulas may be terminated with a semicolon `;` at the end, followed by extra input that is not evaluated. This is useful when, instead of regular comments, you
149+
just want to attach some trailing formation at the end of a formula. For example, the following formula:
150+
`1 + 3; As per our counting`
151+
Would just evaluate the `1 + 3` portion and return a valid result with the value `4`, ignoring the trailing semicolon and all input that follows.
152+
121153
---
122154

123155
[MIT License](License.md)

build/Build.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using static Nuke.Common.Tools.Slack.SlackTasks;
2020
using Nuke.Common.Tools.Slack;
2121
using System.IO;
22+
using Nuke.Common.Tools.Teams;
2223

2324
[UnsetVisualStudioEnvironmentVariables]
2425
class Build : NukeBuild
@@ -31,8 +32,8 @@ class Build : NukeBuild
3132

3233
public static int Main() => Execute<Build>(x => x.Clean);
3334

34-
[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
35-
readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;
35+
[Parameter] readonly string Configuration = IsLocalBuild ? "Debug" : "Release";
36+
3637
[GitVersion(Framework = "netcoreapp3.1")] readonly GitVersion GitVersion;
3738
[GitRepository] readonly GitRepository GitRepository;
3839

@@ -53,8 +54,32 @@ class Build : NukeBuild
5354
[KeyVaultSecret("AntlrCalculatorDemo-WebDeployUsername")] string WebDeployUsername;
5455
[KeyVaultSecret("AntlrCalculatorDemo-WebDeployPassword")] string WebDeployPassword;
5556
[KeyVaultSecret] string GitHubAuthenticationToken;
57+
[KeyVaultSecret] readonly string DanglCiCdTeamsWebhookUrl;
5658
[Parameter] string AppServiceName = "antlr-calculator-demo";
5759

60+
protected override void OnTargetFailed(string target)
61+
{
62+
if (IsServerBuild)
63+
{
64+
SendTeamsMessage("Build Failed", $"Target {target} failed for Dangl.Calculator, " +
65+
$"Branch: {GitRepository.Branch}", true);
66+
}
67+
}
68+
69+
void SendTeamsMessage(string title, string message, bool isError)
70+
{
71+
if (!string.IsNullOrWhiteSpace(DanglCiCdTeamsWebhookUrl))
72+
{
73+
var themeColor = isError ? "f44336" : "00acc1";
74+
TeamsTasks
75+
.SendTeamsMessage(m => m
76+
.SetTitle(title)
77+
.SetText(message)
78+
.SetThemeColor(themeColor),
79+
DanglCiCdTeamsWebhookUrl);
80+
}
81+
}
82+
5883
Target Clean => _ => _
5984
.Executes(() =>
6085
{
@@ -75,6 +100,8 @@ class Build : NukeBuild
75100

76101
Target Publish => _ => _
77102
.DependsOn(Clean)
103+
.OnlyWhenDynamic(() => Nuke.Common.CI.Jenkins.Jenkins.Instance == null
104+
|| Nuke.Common.CI.Jenkins.Jenkins.Instance.ChangeId == null)
78105
.Executes(() =>
79106
{
80107
Npm("ci", RootDirectory);
@@ -91,6 +118,12 @@ class Build : NukeBuild
91118
: "next";
92119

93120
Npm($"publish --tag={npmTag}", distDirectory);
121+
122+
if (npmTag == "latest")
123+
{
124+
SendTeamsMessage("New Release", $"New release available for antlr-calculator: {GitVersion.NuGetVersion}", false);
125+
}
126+
94127
});
95128

96129
Target DeployDemo => _ => _
@@ -99,6 +132,8 @@ class Build : NukeBuild
99132
.Requires(() => WebDeployPassword)
100133
.Requires(() => AppServiceName)
101134
.Requires(() => DanglCiCdSlackWebhookUrl)
135+
.OnlyWhenDynamic(() => Nuke.Common.CI.Jenkins.Jenkins.Instance == null
136+
|| Nuke.Common.CI.Jenkins.Jenkins.Instance.ChangeId == null)
102137
.Executes(async () =>
103138
{
104139
Npm("ci", RootDirectory);

build/_build.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Nuke.Common" Version="0.24.11" />
13+
<PackageReference Include="Nuke.Common" Version="5.0.2" />
1414
<PackageDownload Include="GitVersion.Tool" Version="[5.1.3]" />
15-
<PackageReference Include="Nuke.GitHub" Version="1.6.2" />
15+
<PackageReference Include="Nuke.GitHub" Version="2.0.0" />
1616
</ItemGroup>
1717

1818
</Project>

0 commit comments

Comments
 (0)