How to use the pegjs/lib/compiler/js.stringEscape function in pegjs

To help you get started, we’ve selected a few pegjs 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 metadevpro / ts-pegjs / src / passes / generate-bytecode-ts.js View on Github external
literal(node) {
      if (node.value.length > 0) {
        let stringIndex = addConst("\""
          + js.stringEscape(
              node.ignoreCase ? node.value.toLowerCase() : node.value
            )
          + "\""
        );
        let expectedIndex = addConst(
          "peg$literalExpectation("
            + "\"" + js.stringEscape(node.value) + "\", "
            + node.ignoreCase
            + ")"
        );

        // For case-sensitive strings the value must match the beginning of the
        // remaining input exactly. As a result, we can use |ACCEPT_STRING| and
        // save one |substr| call that would be needed if we used |ACCEPT_N|.
        return buildCondition(
          node.ignoreCase
            ? [op.MATCH_STRING_IC, stringIndex]
            : [op.MATCH_STRING, stringIndex],
          node.ignoreCase
            ? [op.ACCEPT_N, node.value.length]
            : [op.ACCEPT_STRING, stringIndex],
          [op.FAIL, expectedIndex]
        );
github metadevpro / ts-pegjs / src / passes / generate-bytecode-ts.js View on Github external
literal(node) {
      if (node.value.length > 0) {
        let stringIndex = addConst("\""
          + js.stringEscape(
              node.ignoreCase ? node.value.toLowerCase() : node.value
            )
          + "\""
        );
        let expectedIndex = addConst(
          "peg$literalExpectation("
            + "\"" + js.stringEscape(node.value) + "\", "
            + node.ignoreCase
            + ")"
        );

        // For case-sensitive strings the value must match the beginning of the
        // remaining input exactly. As a result, we can use |ACCEPT_STRING| and
        // save one |substr| call that would be needed if we used |ACCEPT_N|.
        return buildCondition(
          node.ignoreCase
github metadevpro / ts-pegjs / src / passes / generate-bytecode-ts.js View on Github external
named(node, context) {
      let nameIndex = addConst(
        "peg$otherExpectation(\"" + js.stringEscape(node.name) + "\")"
      );

      // The code generated below is slightly suboptimal because |FAIL| pushes
      // to the stack, so we need to stick a |POP| in front of it. We lack a
      // dedicated instruction that would just report the failure and not touch
      // the stack.
      return buildSequence(
        [op.SILENT_FAILS_ON],
        generate(node.expression, context),
        [op.SILENT_FAILS_OFF],
        buildCondition([op.IF_ERROR], [op.FAIL, nameIndex], [])
      );
    },
github metadevpro / ts-pegjs / src / passes / generate-ts.js View on Github external
            r => "\"" + js.stringEscape(r.name) + "\""
          ).join(", ") +
github metadevpro / ts-pegjs / src / passes / generate-ts.js View on Github external
          id => "require(\"" + js.stringEscape(id) + "\")"
        ).join(", ");
github metadevpro / ts-pegjs / src / passes / generate-bytecode-ts.js View on Github external
+ node.parts.map(part =>
            Array.isArray(part)
              ? "[\"" + js.stringEscape(part[0]) + "\", \"" + js.stringEscape(part[1]) + "\"]"
              : "\"" + js.stringEscape(part) + "\""
          ).join(", ")
github metadevpro / ts-pegjs / src / passes / generate-ts.js View on Github external
options.returnTypes[rule.name] : "any";
    
    parts.push("function peg$parse" + rule.name + "(): " + outputType +" {");

    if (options.trace) {
      parts.push("  const startPos = peg$currPos;");
    }

    for (let i = 0; i <= stack.maxSp; i++) {
      stackVars[i] = s(i);
    }

    parts.push("  let " + stackVars.join(", ") + ";");

    parts.push(indent2(generateRuleHeader(
      "\"" + js.stringEscape(rule.name) + "\"",
      asts.indexOfRule(ast, rule.name)
    )));
    parts.push(indent2(code));
    parts.push(indent2(generateRuleFooter(
      "\"" + js.stringEscape(rule.name) + "\"",
      s(0)
    )));

    parts.push("}");

    return parts.join("\n");
  }
github metadevpro / ts-pegjs / src / passes / generate-bytecode-ts.js View on Github external
+ node.parts.map(part =>
            Array.isArray(part)
              ? "[\"" + js.stringEscape(part[0]) + "\", \"" + js.stringEscape(part[1]) + "\"]"
              : "\"" + js.stringEscape(part) + "\""
          ).join(", ")
github metadevpro / ts-pegjs / src / passes / generate-ts.js View on Github external
dependencyVars.forEach(variable => {
            parts.push("let " + variable +
              " = require(\"" +
              js.stringEscape(options.dependencies[variable]) +
              "\");"
            );
          });
          parts.push("");
github metadevpro / ts-pegjs / src / passes / generate-ts.js View on Github external
indent2(ast.rules.map(rule =>
          "peg$decode(\"" +
          js.stringEscape(rule.bytecode.map(
            b => String.fromCharCode(b + 32)
          ).join("")) +
          "\")"
        ).join(",\n")),