New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

clarity-pattern-parser

Package Overview
Dependencies
Maintainers
1
Versions
183
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

clarity-pattern-parser

``` npm install clarity-pattern-parser ```

  • 1.0.11
  • npm
  • Socket score

Version published
Weekly downloads
5.5K
decreased by-25.2%
Maintainers
1
Weekly downloads
 
Created
Source

Installation

npm install clarity-pattern-parser

How to use

There are two constructs within clarity pattern parser. The first is value based patterns. The second is composite based patterns.

Value patterns can be composed, however, they are reduced to a single value even if they are composed of many parts.

Composite patterns can also be composed, however, they are left with their parts. The best way to understand this is by an example.

We will create a pattern for line ending comments, first with value based patterns, and second with some composite based patterns.

Value based comment

import {
    Cursor,
    Literal,
    AndValue,
    OrValue,
    RepeatValue,
    NotValue
 } from 'clarity-pattern-parser';
 import assert from "assert";

const forwardSlashes = new Literal("forward-slashes", "//");
const newLine = new Literal("new-line", "\n");
const returnCarriage = new Literal("return-carriage", "\r");
const windowsNewLine = new AndValue("windows-new-line", [returnCarriage, newLine]);
const lineEnd = new OrValue("line-end", [newLine, windowNewLine]);
const character = new NotValue("character", lineEnd);
const comment = new RepeatValue("comment", character);

const lineEndingComment = AndValue("line-ending-comment", [
    forwardSlashes,
    comment,
    lineEnd
]);

const string = "// This is a comment\n";
const cursor = new Cursor(string);
const node = lineEndingComment.parse(cursor);

assert.equal(node.name, "line-ending-comment"); // --> true 
assert.equal(node.value, string); // --> true

Composite based comment

import {
    Cursor,
    Literal,
    AndValue,
    OrValue,
    RepeatValue,
    NotValue,
    AndComposite
 } from 'clarity-pattern-parser';
 import assert from "assert";

const forwardSlashes = new Literal("forward-slashes", "//");
const newLine = new Literal("new-line", "\n");
const returnCarriage = new Literal("return-carriage", "\r");
const windowsNewLine = new AndValue("windows-new-line", [returnCarriage, newLine]);
const lineEnd = new OrValue("line-end", [newLine, windowNewLine]);
const character = new NotValue("character", lineEnd);
const comment = new RepeatValue("comment", character);

const lineEndingComment = AndComposite("line-ending-comment", [
    forwardSlashes,
    comment,
    lineEnd
]);

const string = "// This is a comment\n";
const cursor = new Cursor(string);
const node = lineEndingComment.parse(cursor);

assert.equal(node.name, "line-ending-comment");

assert.equal(node.children[0].name, "forward-slashes");
assert.equal(node.children[0].value, "//");

assert.equal(node.children[1].name, "comment");
assert.equal(node.children[1].value, " This is a comment");

assert.equal(node.children[2].name, "line-end");
assert.equal(node.children[2].value, "\n");

Notes

Maybe put the parseError management within the cursor.

FAQs

Package last updated on 09 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc