psparser

0.2.5 • Public • Published

PSParser

This package shells out to the PSParser utility in Powershell to tokenize powershell input. It currently only works on Windows, though it shouldn't be hard to add support for any platform that supports Powershell.

Usage

When parsing repeatedly, keep an instance of PSParser alive until you are done parsing. PSParser spins up a child process and keeps it alive for performance. After you are done with all parsing work, dispose of PSParser to free the child process.

const PSParser = require('psparser');
const main = async () => {
  const psParser = new PSParser();
  const tokens = await psparser.parse('echo hello');
  psParser.dispose(); // always call dispose when you are done with PSPaser.
  // work with tokens :)
};

General output

This library directly translates the output of PSParser.

Tokens will be an array containing objects of the following type:

enum TokenType {
  Attribute = 9,
  Command = 1,
  CommandArgument = 3,
  CommandParameter = 2,
  Comment = 15,
  GroupEnd = 13,
  GroupStart = 12,
  Keyword = 14,
  LineContinuation = 18,
  LoopLabel = 8,
  Member = 7,
  NewLine = 17	,
  Number = 4,
  Operator = 11,
  Position = 19,
  StatementSeparator = 16,
  String = 5,
  Type = 10,
  Unknown = 0,
  Variable = 6
};

interface Token {
  content: string;
  type: TokenType;
  start: number;
  length: number;
  startLine: number;
  startColumn: number;
  endLine: number;
  endColumn: number;
}

You can access TOKEN_TYPE enum directly off of PSParser.TOKEN_TYPE.

Readme

Keywords

none

Package Sidebar

Install

npm i psparser

Weekly Downloads

258

Version

0.2.5

License

MIT

Unpacked Size

12.4 kB

Total Files

7

Last publish

Collaborators

  • implausibility
  • gk-dev