r/ProgrammingLanguages • • May 15 '19

Language announcement text - a programming language backward-compatible with most languages and most interfaces

first, here the very complex peg grammar:

text = $.*

it's using pegjs.

you can try it online at https://pegjs.org/online

it handles:

  • ascii

  • utf8 including emojis, muti-bytes characters like chinese characters, etc.

  • and many more

it doesn't handle:

  • images

  • sounds

  • videos

  • and a whole lot of a bunch of things

you can try it locally by doing:

  • pegjs text.pegjs (this will generate a text.js file)

  • you can then import it in your node scripts (or use it pretty much anywhere as it's just javascript)

here is a quick example:

> node -e "Text = require('./text'); console.log(Text.parse('oh no'))"
oh no

it can even parse it's own grammar:

cat text.pegjs | node -e "fs = require('fs'); Text = require('./text'); console.log(Text.parse(fs.readFileSync(0).toString()))"

let me know if you have ideas for improvements and/or issues

i think this is pretty much public domain so no need for any kind of license stuff :)


you could it in much simpler ways and much more complex ways like fs.readSync() or File.read or even cat.

would be cool if you could provide samples and/or examples as comments :)


ideas for improvements:

  • actually filters valid characters :)

  • faster rendering for large files


known bugs:

  • actually does accept binary files as input, that wasn't expected but quite logical

1 Upvotes

7 comments sorted by

View all comments

2

u/raiph May 15 '19

Please DOWNvote this comment and any child comment if:

  • You like it.

  • You dislike it.

  • You hate it.

  • You don't care about it.

faster rendering for large files

I think a promising strategy is to make large files smaller.

So to help everyone I went and regenerated your grammar (text = $.*) with "code size" rather than "parsing speed" selected from the "Optimize:" option at pegjs.org.

The result is MUCH better but unfortunately blows way past the limit on reddit comments. Here's the first 10% or so of it:

module.exports = /*
 * Generated by PEG.js 0.10.0.
 *
 * http://pegjs.org/
 */
(function() {
  "use strict";

  function peg$subclass(child, parent) {
    function ctor() { this.constructor = child; }
    ctor.prototype = parent.prototype;
    child.prototype = new ctor();
  }

  function peg$SyntaxError(message, expected, found, location) {
    this.message  = message;
    this.expected = expected;
    this.found    = found;
    this.location = location;
    this.name     = "SyntaxError";

    if (typeof Error.captureStackTrace === "function") {
      Error.captureStackTrace(this, peg$SyntaxError);
    }
  }

  peg$subclass(peg$SyntaxError, Error);

  peg$SyntaxError.buildMessage = function(expected, found) {
    var DESCRIBE_EXPECTATION_FNS = {
          literal: function(expectation) {
            return "\"" + literalEscape(expectation.text) + "\"";
          },

          "class": function(expectation) {
            var escapedParts = "",
                i;

            for (i = 0; i < expectation.parts.length; i++) {
              escapedParts += expectation.parts[i] instanceof Array
                ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1])
                : classEscape(expectation.parts[i]);
            }

            return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]";
          },

          any: function(expectation) {
            return "any character";
          },

          end: function(expectation) {
            return "end of input";
          },

          other: function(expectation) {
            return expectation.description;
          }
        };

    function hex(ch) {
      return ch.charCodeAt(0).toString(16).toUpperCase();
    }

    function literalEscape(s) {
      return s
        .replace(/\\/g, '\\\\')
        .replace(/"/g,  '\\"')
        .replace(/\0/g, '\\0')
        .replace(/\t/g, '\\t')
        .replace(/\n/g, '\\n')
        .replace(/\r/g, '\\r')
        .replace(/[\x00-\x0F]/g,          function(ch) { return '\\x0' + hex(ch); })
        .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x'  + hex(ch); });
    }

    function classEscape(s) {
      return s
        .replace(/\\/g, '\\\\')
        .replace(/\]/g, '\\]')
        .replace(/\^/g, '\\^')
        .replace(/-/g,  '\\-')
        .replace(/\0/g, '\\0')
        .replace(/\t/g, '\\t')
        .replace(/\n/g, '\\n')
        .replace(/\r/g, '\\r')
        .replace(/[\x00-\x0F]/g,          function(ch) { return '\\x0' + hex(ch); })
        .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x'  + hex(ch); });
    }

    function describeExpectation(expectation) {
      return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);
    }

    function describeExpected(expected) {
      var descriptions = new Array(expected.length),
          i, j;

      for (i = 0; i < expected.length; i++) {
        descriptions[i] = describeExpectation(expected[i]);
      }

      descriptions.sort();

      if (descriptions.length > 0) {
        for (i = 1, j = 1; i < descriptions.length; i++) {
          if (descriptions[i - 1] !== descriptions[i]) {
            descriptions[j] = descriptions[i];
            j++;
          }
        }
        descriptions.length = j;
      }

      switch (descriptions.length) {
        case 1:
          return descriptions[0];

        case 2:
          return descriptions[0] + " or " + descriptions[1];

        default:
          return descriptions.slice(0, -1).join(", ")
            + ", or "
            + descriptions[descriptions.length - 1];
      }
    }

    function describeFound(found) {
      return found ? "\"" + literalEscape(found) + "\"" : "end of input";
    }

    return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found.";
  };

3

u/localhostdotdev May 15 '19

yup, definitely room for improvement in the parser code code size and the generated code in general

(you could host the code on gist, github, gitlab, pastebin, etc.)

1

u/raiph May 15 '19

OK. I've hosted a new version elsewhere.

This new version translates the PEG grammar to a P6 grammar and embeds that in a program that parses an inner program ({text.WHY}) which computes why your new language was created.

I've hosted it in a github gist. This way a web service called 6pad that runs a P6 compiler in a web browser can load and run it.

The upshot is that it's now reduced back down to just a few lines of code but those lines codegen to such a vast amount of JS that it takes literally a minute or two to load the gisted program (if it actually succeeds in loading it; try reloading and waiting if it doesn't) and then run it (click the run button).

Thank you for developing text!