Skip to content

Remove Javascript code from a PEG.js grammer file.

License

Notifications You must be signed in to change notification settings

okaxaki/pegjs-strip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pegjs-strip npm version

pegjs-strip is a utility for removing Javascript code fragments from the specified PEG.js grammar file.

The utility removes all code-related statements such as the initializer block, actions and labels. The semantic predicate &{<code>} and !{<code>} are replaced with &{return true;} or !{return false;} respectively.

By default, the utility does not strip comment blocks. To remove comments, --strip-comment option can be used.

Usage

Usage: pegjs-strip [options] file


Options:
 -h, --help                     show this help.
     --strip-comment            Strip comments.
     --keep-initializer         Keep the initializer block.
     --keep-action              Keep actions.
     --keep-label               Keep labels.
     --keep-semantic-predicate  Keep semantic predicates.

Example

The following grammar is from the PEG.js documentation.

start
  = additive

additive
  = left:multiplicative "+" right:additive { return left + right; }
  / multiplicative

multiplicative
  = left:primary "*" right:multiplicative { return left * right; }
  / primary

primary
  = integer
  / "(" additive:additive ")" { return additive; }

integer "integer"
  = digits:[0-9]+ { return parseInt(digits.join(""), 10); }

To remove all the code in the grammar, just run the utility with the grammar file as the first argument. The result is then written to the standard output as follows.

$ pegjs-strip example.pegjs

start
  = additive

additive
  = multiplicative "+" additive
  / multiplicative

multiplicative
  = primary "*" multiplicative
  / primary

primary
  = integer
  / "(" additive ")"

integer "integer"
  = [0-9]+

--keep-label option is available to leave all labels in the resulting output.

$ pegjs-strip --keep-label example.pegjs 

start
  = additive

additive
  = left:multiplicative "+" right:additive
  / multiplicative

multiplicative
  = left:primary "*" right:multiplicative
  / primary

primary
  = integer
  / "(" additive:additive ")"

integer "integer"
  = digits:[0-9]+

About

Remove Javascript code from a PEG.js grammer file.

Resources

License

Stars

Watchers

Forks

Packages

No packages published