Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgDangl committed Jan 23, 2021
2 parents ad29ca2 + 54c85cf commit 9518ede
Show file tree
Hide file tree
Showing 19 changed files with 1,816 additions and 2,116 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## v2.1.0:
- 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 `;`
- Add support for substitutions in formulas

## v2.0.4:
- The internal check for null or empty formulas was changed for better compatibility with Node

Expand Down
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# antlr-calculator

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

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

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

You can find the .NET version here: https://github.com/GeorgDangl/Dangl.Calculator

## Installation

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

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

## Substitutions

The calculator can be called with an overload that accepts a callback function for substitution values. For example, take the following formula:
`1,2*#Z4+3`
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:

```typescript
const formula = "1,2*#Z4+3";
const result = Calculator.calculate(formula, substitution =>
{
if (substitution === "#Z4")
{
return 3;
}

return null;
});
```

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.

Substitutions must always start with the `#` character and can then have the following characters: `[a-z] | [A-Z] | [äÄöÖüÜ] | [0-9]`

## Trailing comments

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
just want to attach some trailing formation at the end of a formula. For example, the following formula:
`1 + 3; As per our counting`
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.

---

[MIT License](License.md)
39 changes: 37 additions & 2 deletions build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using static Nuke.Common.Tools.Slack.SlackTasks;
using Nuke.Common.Tools.Slack;
using System.IO;
using Nuke.Common.Tools.Teams;

[UnsetVisualStudioEnvironmentVariables]
class Build : NukeBuild
Expand All @@ -31,8 +32,8 @@ class Build : NukeBuild

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

[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;
[Parameter] readonly string Configuration = IsLocalBuild ? "Debug" : "Release";

[GitVersion(Framework = "netcoreapp3.1")] readonly GitVersion GitVersion;
[GitRepository] readonly GitRepository GitRepository;

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

protected override void OnTargetFailed(string target)
{
if (IsServerBuild)
{
SendTeamsMessage("Build Failed", $"Target {target} failed for Dangl.Calculator, " +
$"Branch: {GitRepository.Branch}", true);
}
}

void SendTeamsMessage(string title, string message, bool isError)
{
if (!string.IsNullOrWhiteSpace(DanglCiCdTeamsWebhookUrl))
{
var themeColor = isError ? "f44336" : "00acc1";
TeamsTasks
.SendTeamsMessage(m => m
.SetTitle(title)
.SetText(message)
.SetThemeColor(themeColor),
DanglCiCdTeamsWebhookUrl);
}
}

Target Clean => _ => _
.Executes(() =>
{
Expand All @@ -75,6 +100,8 @@ class Build : NukeBuild

Target Publish => _ => _
.DependsOn(Clean)
.OnlyWhenDynamic(() => Nuke.Common.CI.Jenkins.Jenkins.Instance == null
|| Nuke.Common.CI.Jenkins.Jenkins.Instance.ChangeId == null)
.Executes(() =>
{
Npm("ci", RootDirectory);
Expand All @@ -91,6 +118,12 @@ class Build : NukeBuild
: "next";
Npm($"publish --tag={npmTag}", distDirectory);
if (npmTag == "latest")
{
SendTeamsMessage("New Release", $"New release available for antlr-calculator: {GitVersion.NuGetVersion}", false);
}
});

Target DeployDemo => _ => _
Expand All @@ -99,6 +132,8 @@ class Build : NukeBuild
.Requires(() => WebDeployPassword)
.Requires(() => AppServiceName)
.Requires(() => DanglCiCdSlackWebhookUrl)
.OnlyWhenDynamic(() => Nuke.Common.CI.Jenkins.Jenkins.Instance == null
|| Nuke.Common.CI.Jenkins.Jenkins.Instance.ChangeId == null)
.Executes(async () =>
{
Npm("ci", RootDirectory);
Expand Down
4 changes: 2 additions & 2 deletions build/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nuke.Common" Version="0.24.11" />
<PackageReference Include="Nuke.Common" Version="5.0.2" />
<PackageDownload Include="GitVersion.Tool" Version="[5.1.3]" />
<PackageReference Include="Nuke.GitHub" Version="1.6.2" />
<PackageReference Include="Nuke.GitHub" Version="2.0.0" />
</ItemGroup>

</Project>
Loading

0 comments on commit 9518ede

Please sign in to comment.