JsepSharp.Plugins
1.3.8.1
dotnet add package JsepSharp.Plugins --version 1.3.8.1
NuGet\Install-Package JsepSharp.Plugins -Version 1.3.8.1
<PackageReference Include="JsepSharp.Plugins" Version="1.3.8.1" />
paket add JsepSharp.Plugins --version 1.3.8.1
#r "nuget: JsepSharp.Plugins, 1.3.8.1"
// Install JsepSharp.Plugins as a Cake Addin
#addin nuget:?package=JsepSharp.Plugins&version=1.3.8.1
// Install JsepSharp.Plugins as a Cake Tool
#tool nuget:?package=JsepSharp.Plugins&version=1.3.8.1
JsepSharp Plugins
These are the standard optional plugins for the JsepSharp parser. They add support for functionality not available in the core implementation.
Usage
// Create instance to use with plugins:
var j = new Jsep("a ? b : c");
j.RegisterPlugin(typeof(TernaryPlugin));
j.Parse();
// Static parse method with all built-in plugins:
Jsep.Parse("f = (a) => ({ b: 4, c: (d > 4 ? 4 : d), e: /^[a-z]{5}$/ })", BuiltIns.All);
// Static parse method With specific plugins:
Jsep.Parse("a => b ? c : d", new[] { typeof(TernaryPlugin), typeof(ArrowPlugin) });
Note: Plugin inclusion order matters. Ternary should generally be included before other plugins, if it is being used.
Plugins
The plugins work differently from the JavaScript version, due to language differences.
None are included by default, which is a difference from the JavaScript implementation that includes ternary by default.
Included Plugins
Name | Description |
---|---|
Ternary | Adds support for ternary expressions: a ? b : c |
Arrow | Adds support for arrow functions: v => !!v , (x, y) => x + y |
Assignment | Adds support for assignment and update expressions: a = 2 , a++ |
AsyncAwait | Adds support for async and await keywords. |
Comment | Adds support for ignoring comments: a /* ignore this */ > 1 // ignore this too |
New | Adds support for new keyword: new Date() |
Numbers | Adds support for hex, octal, binary literals, and _ literal separators. |
Object | Adds support for object expressions: { a: 1, b: { c }} |
Regex | Adds support for regular expression literals: /[a-z]{2}/ig |
Spread | Adds support for the spread operator, fn(...[1, ...a]) . Works with Object plugin, too. |
TemplateLiteral | Adds support for template and tagged template literals: `hi ${name}` |
How to Add Plugins
Class/Static
Parse
method: Supply the second argument with an enumerable ofSystem.Type
for each plugin.Instance
RegisterPlugin
method: After creating an instance, invokeRegisterPlugin
with theSystem.Type
of the plugin.
Regardlesss of how you register them, the plugin class must implement abstract class Jsep.Plugin
. If it does not, an ArgumentException
will be raised.
List Plugins
Installed plug-ins can be enumerated from a Jsep
instance via the Plugins
property.
Writing Your Own Plugin
Hooks (Events)
The hooks system uses C# events, which differs from the JavaScript implementation. This was done since the C# language already has the necessary feature built-in.
These exist to provide extensibility points that plugins can use to integrate into the parser and add additional functionality to it.
Hook Types
BeforeParsing
: called just before starting all expression parsing. Port ofbefore-all
AfterParsing
: called after parsing all. Read/Writeenv.Node
as required. Port ofafter-all
.BeforeExpression
: called just before attempting to parse an expression. Setenv.Node
as required. Port ofgobble-expression
.AfterExpression
: called just after parsing an expression. Read/Writeenv.Node
as required. Port ofafter-expression
.BeforeToken
: called just before attempting to parse a token. Setenv.Node
as required. Port ofgobble-token
.AfterToken
: called just after parsing a token. Read/Writeenv.Node
as required. Port ofafter-token
.AfterSpaces
: called when gobbling whitespace. Port ofgobble-spaces
.
Examples
- The Spread plugin is the smallest of the bulit-ins.
- The AsyncAwait plugin demonstrates how to declare a plugin depends upon another plugin.
- The Arrow plugin shows how to use
ReplaceNodes()
to alter previously parsed results. - The Comment plugin shows how to instruct the parser to ignore additional sequences.
- The New plugin swaps out a node with a new type of node.
- The Regex plugin shows how to create a new type of literal to the parser.
- The Number plugin shows how to extend the parsing behavior of an existing literal.
License
The JsepSharp Plugins are a derivative work under the MIT license. See LICENSE file.
Thanks
To all the contributors of the original jsep project.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
.NET Framework | net47 is compatible. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.7
- JsepSharp (>= 1.3.8.1)
- Newtonsoft.Json (>= 13.0.3)
-
.NETStandard 2.1
- JsepSharp (>= 1.3.8.1)
- Newtonsoft.Json (>= 13.0.3)
-
net8.0
- JsepSharp (>= 1.3.8.1)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.