Tobias-Kohn / TigerPython-Parser

Enhanced error recognition in Python
Mozilla Public License 2.0
39 stars 6 forks source link

Python-Error-Messages

This project provides enhanced error messages for Python. It compiles to a JavaScript-library that can readily be imported in a web page and used together with, e.g. Skulpt, Pyodide or any other Python-interpreter, really (however, note that its main support is for Python until 3.6 and selected features from 3.7+ – see below). The library only checks a Python program for syntax errors and is primarily aimed at novice programmers and students.

It has grown out of a PhD thesis by Tobias Kohn and is part of the TigerJython programming envrionment. There is also an online version of TigerJython, where this JavaScript-library is currently used.

Let us know if you find the library useful and include it in your own project (jython at tobiaskohn.ch).

In order to make the project a self-contained JavaScript-library, additional resources like translations of the error messages are directly inlined into the code.

Usage

NPM Package

You can point to this GitHub repository as an NPM dependency. Add the following line to your "dependencies" in your package.json:

"tigerpython-parser": "git+https://github.com/Tobias-Kohn/TigerPython-Parser.git",

ES Module

To be used in larger projects, the parser can be compiled to a JavaScript ES module. The file can be found in release/tigerpython-parser.mjs, along with a standalone JavaScript file (see below). The accompanying index.html demonstrates how the module could be loaded and used in a simple HTML. However, make sure your server delivers the module with MIME type text/javascript.

Note: when compiling to an ES module, the tests currently fail. It seems node.js is complaining about using export instead of module.export in the generated module.

Standalone JavaScript File

In order to use the parser for error checking in your project, load the respective JavaScript-file, set your preferences in the TPyParser object and then check your code. You can find a simple example in doc/index.html.

TPyParser.rejectDeadCode = true;
TPyParser.setLanguage("en");
var err = TPyParser.checkSyntax(my_code);
if (err !== null) {
  var error_line = err.line;
  var error_msg = err.msg;
  // Display the error...
}

The TPyParser Object

The TPyParser object provides two methods for checking syntax:

A third method returns the AST:

Available options:

Supported Python Versions

The parser was originally written so support Python 2.7 and Python 3.6. With Python 3.9, the grammar has significantly changed (including the AST nodes), as the old LL(1)-parser was replaced by a Pegen-parser. The structure of this parser and the generated AST therefore deviates quite a bit from the ones used in Python 3.9+. Nonetheless, we try to support new syntactic elements or changes to the syntax, but cannot guarantee full compatibility. Concerning newer features, the current state is as follows:

Compilation

The entire project is written in Scala 2.12 / Scala.js and uses sbt.

When sbt is installed, go to the project's root directory and use sbt fastOptJS or sbt fullOptJS to compile the project (scala.js supports two compilation modes: fast compilation during development and optimised compilation for production code). The output (JavaScript-files) can then be found in ./tpParser/js/target/scala-2.12/.

The JS-linker was previsouly configured to output a JavaScript ES module. The line responsible for this is in build.sbt: scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) }. Remove the comments on this line in order to get an ES module instead of classic JavaScript file (however, sbt currently crashes because of an export vs. module.export error).

Contribution

The parser was initially written by Tobias Kohn. Further contributions by:

The authors who contributed translations for the error messages are noted in the respective files.

Please let us know if you would like to add another language for error messages - we are more than happy to include new languages.