How to use chevrotain - 10 common examples

To help you get started, we’ve selected a few chevrotain 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 RokuRoad / bright / test / diagram.spec.ts View on Github external
test('Generate AST Productions', () => {
    // extract the serialized parser.
    const serializedGrammar = parserInstance.getSerializedGastProductions()

    // create the HTML Text
    const htmlText = createSyntaxDiagramsCode(serializedGrammar)

    // Write the HTML file to disk
    const outPath = resolve(__dirname, '../diagram')

    writeFileSync(outPath + '/index.html', htmlText)
  })
})
github jhipster / prettier-java / packages / java-parser / src / visitor.js View on Github external
"Only identifier is allowed with colon",
            undefined
          );
        }
        if (ctx.typeArguments || ctx.Dot) {
          throw new MismatchedTokenException(
            "Only identifier is allowed with type arguments or dot",
            undefined
          );
        }
      }

      // identifier statement
      if (expression.type === "IDENTIFIER" && ctx.Colon) {
        if (ctx.classOrInterfaceModifier) {
          throw new MismatchedTokenException(
            "Identifier statement is not allowed to have annotations or modifiers.",
            undefined
          );
        }
        if (ctx.LSquare || ctx.variableDeclarators) {
          throw new MismatchedTokenException(
            "Identifier statement is not allowed to have squares or variable declarators",
            undefined
          );
        }
        const statement = this.visit(ctx.statement);

        return {
          type: "IDENTIFIER_STATEMENT",
          identifier: expression,
          statement: statement
github jhipster / prettier-java / packages / java-parser / src / visitor.js View on Github external
// parExpression

    // if parExpression
    // -> no
    //       - annotations
    //       - typeArguments
    //       - LSquare/RSquare
    // -> only one expression

    if (
      // ctx.annotation ||
      // ctx.typeArguments ||
      // ctx.LSquare ||
      ctx.expression.length !== 1
    ) {
      throw new MismatchedTokenException(
        "Found parenthesis expression with annotations, typeArguments or Squares",
        undefined
      );
    }

    const expression = this.visit(ctx.expression);

    const parExpression = {
      type: "PAR_EXPRESSION",
      expression: expression
    };

    if (ctx.qualifiedExpressionRest) {
      const rest = this.visit(ctx.qualifiedExpressionRest);

      const qualifiedExpression = {
github jhipster / prettier-java / packages / java-parser / src / visitor.js View on Github external
return this.visit(ctx.commentStandalone);
    }

    if (ctx.expression) {
      let expression = this.visit(ctx.expression);

      if (expression.type === "PRIMITIVE_TYPE") {
        // if expression is only a primitiveType nothing else is allowed with it
        if (ctx.Colon) {
          throw new MismatchedTokenException(
            "Primitive type with colon found",
            undefined
          );
        }
        if (ctx.typeArguments || ctx.Dot) {
          throw new MismatchedTokenException(
            "Primitive type with type arguments or dot found",
            undefined
          );
        }
      }

      if (expression.type !== "IDENTIFIER") {
        // if expression is only a primitiveType nothing else is allowed with it
        if (ctx.Colon) {
          throw new MismatchedTokenException(
            "Only identifier is allowed with colon",
            undefined
          );
        }
        if (ctx.typeArguments || ctx.Dot) {
          throw new MismatchedTokenException(
github jhipster / prettier-java / packages / java-parser / src / visitor.js View on Github external
"Only identifier is allowed with type arguments or dot",
            undefined
          );
        }
      }

      // identifier statement
      if (expression.type === "IDENTIFIER" && ctx.Colon) {
        if (ctx.classOrInterfaceModifier) {
          throw new MismatchedTokenException(
            "Identifier statement is not allowed to have annotations or modifiers.",
            undefined
          );
        }
        if (ctx.LSquare || ctx.variableDeclarators) {
          throw new MismatchedTokenException(
            "Identifier statement is not allowed to have squares or variable declarators",
            undefined
          );
        }
        const statement = this.visit(ctx.statement);

        return {
          type: "IDENTIFIER_STATEMENT",
          identifier: expression,
          statement: statement
        };
      }

      const followedEmptyLine = this.visit(ctx.semiColon).followedEmptyLine;
      if (
        expression.type === "IDENTIFIER" ||
github SAP / chevrotain / examples / lexer / multi_mode_lexer / multi_mode_lexer.js View on Github external
const { createToken, Lexer } = require("chevrotain")

// numbers Tokens
const One = createToken({ name: "One", pattern: /1/ })
const Two = createToken({ name: "Two", pattern: /2/ })
const Three = createToken({ name: "Three", pattern: /3/ })

// Letter Tokens
const Alpha = createToken({ name: "Alpha", pattern: /A/ })
const Beta = createToken({ name: "Beta", pattern: /B/ })
const Gamma = createToken({ name: "Gamma", pattern: /G/ })

// signs Tokens
const Hash = createToken({ name: "Hash", pattern: /#/ })
const Caret = createToken({ name: "Caret", pattern: /\^/ })
const Amp = createToken({ name: "Amp", pattern: /&/ })

// Tokens which control entering a new mode.
const EnterNumbers = createToken({
  name: "EnterNumbers",
  pattern: /NUMBERS/,
  push_mode: "numbers_mode"
})

const EnterLetters = createToken({
github SAP / chevrotain / examples / grammars / ecma5 / ecma5_tokens.js View on Github external
const ThisTok = createToken({ name: "ThisTok", categories: AbsKeyword })

const WithTok = createToken({ name: "WithTok", categories: AbsKeyword })

const DefaultTok = createToken({ name: "DefaultTok", categories: AbsKeyword })

const IfTok = createToken({ name: "IfTok", categories: AbsKeyword })

const ThrowTok = createToken({ name: "ThrowTok", categories: AbsKeyword })

const DeleteTok = createToken({ name: "DeleteTok", categories: AbsKeyword })

const InTok = createToken({ name: "InTok", categories: AbsKeyword })

const TryTok = createToken({ name: "TryTok", categories: AbsKeyword })

// An IdentifierName, but not a reservedKeyword
const Identifier = createToken({
  name: "Identifier",
  categories: IdentifierName
})

// Set/Get are not reservedKeywords so they are modeled as a TypeOf Identifier.
const SetTok = createToken({ name: "SetTok", categories: Identifier })
const GetTok = createToken({ name: "SetTok", categories: Identifier })

// TODO: Missing the future reservedKeywords here.

// Link: https://www.ecma-international.org/ecma-262/5.1/#sec-7.7
const AbsPunctuator = createToken({ name: "AbsPunctuator" })
github SAP / chevrotain / examples / grammars / ecma5 / ecma5_tokens.js View on Github external
const Identifier = createToken({
  name: "Identifier",
  categories: IdentifierName
})

// Set/Get are not reservedKeywords so they are modeled as a TypeOf Identifier.
const SetTok = createToken({ name: "SetTok", categories: Identifier })
const GetTok = createToken({ name: "SetTok", categories: Identifier })

// TODO: Missing the future reservedKeywords here.

// Link: https://www.ecma-international.org/ecma-262/5.1/#sec-7.7
const AbsPunctuator = createToken({ name: "AbsPunctuator" })

const LCurly = createToken({ name: "LCurly", categories: AbsPunctuator })
const RCurly = createToken({ name: "RCurly", categories: AbsPunctuator })
const LParen = createToken({ name: "LParen", categories: AbsPunctuator })
const RParen = createToken({ name: "RParen", categories: AbsPunctuator })
const LBracket = createToken({ name: "LBracket", categories: AbsPunctuator })
const RBracket = createToken({ name: "RBracket", categories: AbsPunctuator })
const Dot = createToken({ name: "Dot", categories: AbsPunctuator })

const Semicolon = createToken({ name: "Semicolon", categories: AbsPunctuator })

const Comma = createToken({ name: "Comma", categories: AbsPunctuator })

const PlusPlus = createToken({ name: "PlusPlus", categories: AbsPunctuator })
const MinusMinus = createToken({
  name: "MinusMinus",
  categories: AbsPunctuator
})
github RokuRoad / bright / src / Tokens.ts View on Github external
export const LESS_THAN = createToken({ name: 'LESS_THAN', pattern: '<', categories: RELATIONAL_OPERATOR })
export const LESS_THAN_EQUAL = createToken({ name: 'LESS_THAN_EQUAL', pattern: '<=', categories: RELATIONAL_OPERATOR })
export const NOT_EQUAL = createToken({ name: 'NOT_EQUAL', pattern: '<>', categories: RELATIONAL_OPERATOR })

export const EQUAL = createToken({ name: 'EQUAL', pattern: '=', categories: EQUALITY_OPERATOR })
export const OP_ASSIGNMENT_ADD = createToken({
  categories: EQUALITY_OPERATOR,
  name: 'OP_ASSIGNMENT_ADD',
  pattern: '+='
})
export const OP_ASSIGNMENT_BITSHIFT_LEFT = createToken({
  categories: EQUALITY_OPERATOR,
  name: 'OP_ASSIGNMENT_BITSHIFT_LEFT',
  pattern: '<<='
})
export const OP_ASSIGNMENT_BITSHIFT_RIGHT = createToken({
  categories: EQUALITY_OPERATOR,
  name: 'OP_ASSIGNMENT_BITSHIFT_RIGHT',
  pattern: '>>='
})

export const OP_ASSIGNMENT_DIVISION = createToken({
  categories: EQUALITY_OPERATOR,
  name: 'OP_ASSIGNMENT_DIVISION',
  pattern: '/='
})

// prettier-ignore
export const OP_ASSIGNMENT_INTEGER_DIVISION = createToken({ name: 'OP_ASSIGNMENT_INTEGER_DIVISION', pattern: '\\=',   categories: EQUALITY_OPERATOR })

export const OP_ASSIGNMENT_MULTIPLY = createToken({
  categories: EQUALITY_OPERATOR,
github SAP / chevrotain / examples / parser / content_assist / content_assist_simple.js View on Github external
/*
 * Example Of using Chevrotain's built in syntactic content assist
 * To implement semantic content assist and content assist on partial inputs.
 *
 * Examples:
 * "Public static " --> ["function"]
 * "Public sta" --> ["static"]
 * "call f" --> ["foo"] // assuming foo is in the symbol table.
 */
const _ = require("lodash")
const { createToken, Lexer, CstParser } = require("chevrotain")

const A = createToken({ name: "A", pattern: /A/ })
const B = createToken({ name: "B", pattern: /B/ })
const C = createToken({ name: "C", pattern: /C/ })

const WhiteSpace = createToken({
  name: "WhiteSpace",
  pattern: /\s+/,
  group: Lexer.SKIPPED
})

const allTokens = [WhiteSpace, A, B, C]
const StatementsLexer = new Lexer(allTokens)

// A completely normal Chevrotain Parser, no changes needed to use the content assist capabilities.
class MyParser extends CstParser {
  constructor() {
    super(allTokens)