How to use the babylon/lib/parser.prototype function in babylon

To help you get started, we’ve selected a few babylon examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github SuperPaintman / babel-plugin-syntax-pipeline / src / index.js View on Github external
import Parser, { plugins } from 'babylon/lib/parser';
import { TokenType, types as tt } from 'babylon/lib/tokenizer/types';

/** Constants */
const CHAR_CODES = '|>'.split('').map((c) => c.charCodeAt(0));
const PLUGIN_NAME = 'pipeline';

const beforeExpr = true;


/** Types */
tt.pipeline = new TokenType('|>', { beforeExpr, binop: 12 });


/** Parser */
const pp = Parser.prototype;

pp.readToken_pipeline = function readToken_pipeline(code) { // eslint-disable-line camelcase
  return this.finishOp(tt.pipeline, 2);
};


/** Plugin */
function plugin(instance) {
  instance.extend('readToken', (inner) => function readToken(code) {
    const next = this.input.charCodeAt(this.state.pos + 1);

    if (!(code === CHAR_CODES[0] && next === CHAR_CODES[1])) {
      return inner.call(this, code);
    }

    return this.readToken_pipeline(code);
github nikaspran / coffee-to-ts / src / babel / import.js View on Github external
this.semicolon();
};

Printer.prototype.TypeAssertedExpression = function (node) {
	this.print(node.assertion, node);
	this.space();
	this.print(node.expression, node);
};

Printer.prototype.TypeAssertion = function (node) {
	this.token('<');
	this.word(node.value);
	this.token('>');
};

Parser.prototype.tsParseImportRequire = function (node) {
	this.next();
	node.local = this.parseIdentifier();

	this.eat(types.eq);
	this.eatContextual('require');
	this.eat(types.parenL);

	node.source = this.match(types.string) ? this.parseExprAtom() : this.unexpected();
	this.eat(types.parenR);
	this.semicolon();
	return this.finishNode(node, 'ImportRequire');
};

export function parser(instance) {
	instance.extend('parseImport', function (inner) {
		return function (node) {
github forivall / tacoscript / packages / babylon-plugin-cst / src / plugin.js View on Github external
import Parser from "babylon/lib/parser";
import { Token } from "babylon/lib/tokenizer";
import TokenizerState from "babylon/lib/tokenizer/state";
import { types as tt } from "babylon/lib/tokenizer/types";
import { nonASCIIwhitespace } from "babylon/lib/util/whitespace";
import detectIndent from "detect-indent";
import { tokenTypes } from "./types";
import postprocess from "./postprocess";

var pp = Parser.prototype;

pp.startWhitespace = function() {
  this.whitespaceState.start = this.state.pos;
  this.whitespaceState.startLoc = this.state.curPosition();
  this.whitespaceState.value = '';
};

pp.finishWhitespace = function(type = tokenTypes.whitespace) {
  this.whitespaceState.type = type;
  this.whitespaceState.end = this.state.pos;
  this.whitespaceState.endLoc = this.state.curPosition();
  if (this.whitespaceState.end > this.whitespaceState.start) {
    this.state.tokens.push(new Token(this.whitespaceState));
  }
};