0.0.1-security • Published 2 years ago

pegjs-override-action v0.0.1-security

Weekly downloads
1
License
-
Repository
-
Last release
2 years ago

PEGjs override action

Override action (PEGjs plugin)

Reason

This plugin came to life, as part of a process of referencing basic PEGs and build structure outside of the PEGs. In essence, it gives the possibility for reusable PEGs.

E.g. A collection of core PEGjs grammars (RFC, ISO, etc.)

Usage

npm install pegjs-override-action
PEG = require 'pegjs'
overrideAction = require 'pegjs-override-action'

parser = PEG.buildParser "start = 'a' / 'b' / 'c' / 'd' { return 'd' } / 'e'",
  plugins: [overrideAction]
  overrideActionPlugin:
    initializer: "_ = require('lodash');"
    rules:
      start: [
        () -> "b"
        "return 'a';"
        undefined
        '__skip__' # equivalent to undefined atm
        () -> _.VERSION
      ]

parser.parse 'a' # 'b'
parser.parse 'b' # 'a'
parser.parse 'c' # 'c'
parser.parse 'd' # 'd'
parser.parse 'e' # e.g. 1.3.1

Scopes and require paths

When defining an action as a function, it will be stringified and injected into a different scope, which breaks variable closure. Any data or functions that need to be shared between rule actions should be defined in your initializer block. When using require in your actions or initializer, it is a good idea to resolve an absolute path (e.g. using require.resolve as the code is also executed in a different path than where it is defined.

Convenience

PEG = require 'pegjs'
{makeBuildParser} = require 'pegjs-override-action'

buildParser = makeParser {
  PEG
  grammar: "start = 'a' / 'b' / 'c' / 'd' { return 'd' } / 'e'"
  initializer: "_ = require('lodash');"
  rules:
    start: [
      () -> "b"
      "return 'a';"
      undefined
      '__skip__' # equivalent to undefined atm
      () -> _.VERSION
    ]
  mixins: {} # list of default rules

parser = buildParser {
  startRule: 'start'
  options: {} #
}

parser.parse 'a' # 'b'
parser.parse 'b' # 'a'
parser.parse 'c' # 'c'
parser.parse 'd' # 'd'
parser.parse 'e' # e.g. 1.3.1

License

UNLICENSE

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.3

2 years ago

0.0.1-security

2 years ago

0.3.0

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.5.1

2 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.7

11 years ago

0.0.6

11 years ago

0.0.5

11 years ago

0.0.4

11 years ago

0.0.3

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago